Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Porting latest v5 analytics changes to v6 #2568

Merged
merged 6 commits into from
Feb 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions packages/lib/src/core/Analytics/Analytics.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,7 @@ const Analytics = ({ loadingContext, locale, clientKey, analytics, amount, analy
const checkoutAttemptId = await collectId({ ...initialEvent, ...(payload && { ...payload }) });
capturedCheckoutAttemptId = checkoutAttemptId;
} catch (e) {
// Caught at collectId level. We do not expect this catch block to ever fire, but... just in case...
console.debug(`Fetching checkoutAttemptId failed.${e ? ` Error=${e}` : ''}`);
console.warn(`Fetching checkoutAttemptId failed.${e ? ` Error=${e}` : ''}`);
}
}

Expand Down
2 changes: 1 addition & 1 deletion packages/lib/src/core/Analytics/EventsQueue.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import EventsQueue from './EventsQueue';
import { ANALYTICS_PATH } from './constants';

const task1 = { foo: 'bar', timestamp: '1234', component: 'scheme' };
const task1 = { foo: 'bar', timestamp: '1234', component: 'scheme', id: '678' };

describe('CAEventsQueue', () => {
const queue = EventsQueue({ analyticsContext: 'https://mydomain.com', clientKey: 'fsdjkh', analyticsPath: ANALYTICS_PATH });
Expand Down
2 changes: 2 additions & 0 deletions packages/lib/src/core/Analytics/EventsQueue.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { AnalyticsObject, EventQueueProps } from './types';

interface CAActions {
channel: 'Web';
platform: 'Web';
info: AnalyticsObject[];
errors: AnalyticsObject[];
logs: AnalyticsObject[];
Expand All @@ -17,6 +18,7 @@ export interface EventsQueueModule {
const EventsQueue = ({ analyticsContext, clientKey, analyticsPath }: EventQueueProps): EventsQueueModule => {
const caActions: CAActions = {
channel: 'Web',
platform: 'Web',
info: [],
errors: [],
logs: []
Expand Down
7 changes: 4 additions & 3 deletions packages/lib/src/core/Analytics/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ export type AnalyticsProps = Pick<CoreConfiguration, 'loadingContext' | 'locale'
export interface AnalyticsObject {
timestamp: string;
component: string;
id: string;
code?: string;
errorType?: string;
message?: string;
Expand All @@ -57,7 +58,7 @@ export interface AnalyticsObject {

export type ANALYTICS_EVENT = 'log' | 'error' | 'info';

export type CreateAnalyticsObject = Omit<AnalyticsObject, 'timestamp'> & { event: ANALYTICS_EVENT };
export type CreateAnalyticsObject = Omit<AnalyticsObject, 'timestamp' | 'id'> & { event: ANALYTICS_EVENT };

export type AnalyticsInitialEvent = {
containerWidth: number;
Expand All @@ -75,7 +76,7 @@ export type AnalyticsConfig = {
loadingContext?: string;
};

export type CreateAnalyticsEventData = Omit<AnalyticsObject, 'timestamp'>;
export type CreateAnalyticsEventData = Omit<AnalyticsObject, 'timestamp' | 'id'>;

export type CreateAnalyticsEventObject = {
event: ANALYTICS_EVENT;
Expand All @@ -84,7 +85,7 @@ export type CreateAnalyticsEventObject = {

export type EventQueueProps = Pick<AnalyticsConfig, 'analyticsContext' | 'clientKey'> & { analyticsPath: string };

export type SendAnalyticsObject = Omit<AnalyticsObject, 'timestamp' | 'component'>;
export type SendAnalyticsObject = Omit<AnalyticsObject, 'timestamp' | 'component' | 'id'>;

export type FieldErrorAnalyticsObject = {
fieldType: string;
Expand Down
2 changes: 2 additions & 0 deletions packages/lib/src/core/Analytics/utils.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { AnalyticsObject, CreateAnalyticsObject } from './types';
import { ANALYTICS_ACTION_STR, ANALYTICS_VALIDATION_ERROR_STR } from './constants';
import uuid from '../../utils/uuid';

export const getUTCTimestamp = () => Date.now();

Expand All @@ -24,6 +25,7 @@ export const getUTCTimestamp = () => Date.now();
export const createAnalyticsObject = (aObj: CreateAnalyticsObject): AnalyticsObject => ({
timestamp: String(getUTCTimestamp()),
component: aObj.component,
id: uuid(),
/** ERROR */
...(aObj.event === 'error' && { code: aObj.code, errorType: aObj.errorType, message: aObj.message }), // error event
/** LOG */
Expand Down
3 changes: 1 addition & 2 deletions packages/lib/src/core/Services/analytics/collect-id.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,7 @@ const collectId = ({ analyticsContext, clientKey, locale, analyticsPath }: Colle
return undefined;
})
.catch(() => {
console.debug(FAILURE_MSG);
return FAILURE_MSG;
return Promise.reject(FAILURE_MSG);
});

return promise;
Expand Down
Loading