Skip to content

Commit 4a9a817

Browse files
committed
Cleanup code and include event IDs
1 parent 1faa228 commit 4a9a817

7 files changed

Lines changed: 32 additions & 50 deletions

File tree

src/CONST/index.ts

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8573,20 +8573,25 @@ const CONST = {
85738573

85748574
ANALYTICS: {
85758575
EVENT: {
8576-
SIGN_UP: 'sign_up',
8577-
WORKSPACE_CREATED: 'workspace_created',
8578-
PAID_ADOPTION: 'paid_adoption',
8576+
SIGN_UP: {
8577+
NAME: 'sign_up',
8578+
REDDIT: 'SignUp',
8579+
LINKEDIN: 507587661,
8580+
},
8581+
WORKSPACE_CREATED: {
8582+
NAME: 'workspace_created',
8583+
REDDIT: 'Lead',
8584+
LINKEDIN: 25474804,
8585+
},
8586+
PAID_ADOPTION: {
8587+
NAME: 'paid_adoption',
8588+
REDDIT: 'Purchase',
8589+
LINKEDIN: 25474820,
8590+
},
85798591
PRODUCT_TRAINING_SCAN_TEST_TOOLTIP_SHOWN: 'training_scan_test_tooltip_shown',
85808592
PRODUCT_TRAINING_SCAN_TEST_TOOLTIP_DISMISSED: 'training_scan_test_tooltip_dismissed',
85818593
PRODUCT_TRAINING_SCAN_TEST_TOOLTIP_CONFIRMED: 'training_scan_test_tooltip_confirmed',
85828594
},
8583-
// LinkedIn uses numeric conversion IDs instead of named events
8584-
LINKEDIN_SIGN_UP_CONVERSION_ID: 507587661,
8585-
LINKEDIN_WORKSPACE_CREATED_CONVERSION_ID: 25474804,
8586-
LINKEDIN_PAID_ADOPTION_CONVERSION_ID: 25474820,
8587-
REDDIT_SIGN_UP_CONVERSION_ID: 'SignUp',
8588-
REDDIT_WORKSPACE_CREATED_CONVERSION_ID: 'Lead',
8589-
REDDIT_PAID_ADOPTION_CONVERSION_ID: 'Purchase',
85908595
NVP: {
85918596
META_CLICK_ID: 'expensify_leadFbclid',
85928597
GOOGLE_CLICK_ID: 'expensify_leadGclid',

src/libs/GoogleTagManager/index.ts

Lines changed: 9 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
/* eslint-disable @typescript-eslint/naming-convention */
2-
import Onyx from 'react-native-onyx';
32
import Log from '@libs/Log';
43
import CONST from '@src/CONST';
5-
import ONYXKEYS from '@src/ONYXKEYS';
64
import type {GoogleTagManagerEvent} from './types';
75
import type GoogleTagManagerModule from './types';
86

@@ -23,51 +21,31 @@ type DataLayerPushParams = {
2321
event: GoogleTagManagerEvent;
2422
user_id: number;
2523
user_data: {email: string};
26-
gclid?: string;
2724
};
2825

2926
declare const window: WindowWithPixels;
3027

31-
const PIXEL_EVENTS = new Set<GoogleTagManagerEvent>([CONST.ANALYTICS.EVENT.SIGN_UP, CONST.ANALYTICS.EVENT.WORKSPACE_CREATED, CONST.ANALYTICS.EVENT.PAID_ADOPTION]);
32-
33-
const LINKEDIN_CONVERSION_IDS: Partial<Record<GoogleTagManagerEvent, number>> = {
34-
[CONST.ANALYTICS.EVENT.SIGN_UP]: CONST.ANALYTICS.LINKEDIN_SIGN_UP_CONVERSION_ID,
35-
[CONST.ANALYTICS.EVENT.WORKSPACE_CREATED]: CONST.ANALYTICS.LINKEDIN_WORKSPACE_CREATED_CONVERSION_ID,
36-
[CONST.ANALYTICS.EVENT.PAID_ADOPTION]: CONST.ANALYTICS.LINKEDIN_PAID_ADOPTION_CONVERSION_ID,
37-
};
38-
39-
const REDDIT_CONVERSION_IDS: Partial<Record<GoogleTagManagerEvent, string>> = {
40-
[CONST.ANALYTICS.EVENT.SIGN_UP]: CONST.ANALYTICS.REDDIT_SIGN_UP_CONVERSION_ID,
41-
[CONST.ANALYTICS.EVENT.WORKSPACE_CREATED]: CONST.ANALYTICS.REDDIT_WORKSPACE_CREATED_CONVERSION_ID,
42-
[CONST.ANALYTICS.EVENT.PAID_ADOPTION]: CONST.ANALYTICS.REDDIT_PAID_ADOPTION_CONVERSION_ID,
43-
};
44-
45-
let googleClickId: string | null | undefined;
46-
Onyx.connectWithoutView({
47-
key: ONYXKEYS.NVP_GOOGLE_CLICK_ID,
48-
callback: (value) => {
49-
googleClickId = value;
50-
},
51-
});
28+
const PIXEL_EVENTS = [CONST.ANALYTICS.EVENT.SIGN_UP, CONST.ANALYTICS.EVENT.WORKSPACE_CREATED, CONST.ANALYTICS.EVENT.PAID_ADOPTION] as const;
5229

5330
function publishEvent(event: GoogleTagManagerEvent, accountID: number, email: string) {
5431
if (!window.dataLayer) {
5532
return;
5633
}
5734

58-
const params = {event, user_id: accountID, user_data: {email}, gclid: googleClickId ?? undefined};
35+
const params = {event, user_id: accountID, user_data: {email}};
5936

6037
// Pass a copy of params here since the dataLayer modifies the object
6138
window.dataLayer.push({...params});
6239

6340
Log.info('[GTM] event published', false, params);
6441

65-
if (!PIXEL_EVENTS.has(event)) {
42+
const pixelEvent = PIXEL_EVENTS.find((e) => e.NAME === event);
43+
if (!pixelEvent) {
6644
return;
6745
}
6846

6947
// Events in Meta & Reddit are currently being set in CamelCase from OldDot
70-
const pixelEventName = (event as string)
48+
const pixelEventName = event
7149
.split('_')
7250
.map((w) => w.charAt(0).toUpperCase() + w.slice(1))
7351
.join('');
@@ -80,19 +58,17 @@ function publishEvent(event: GoogleTagManagerEvent, accountID: number, email: st
8058
}
8159

8260
// Reddit
83-
const redditConversionID = REDDIT_CONVERSION_IDS[event];
84-
if (typeof window.rdt === 'function' && redditConversionID) {
85-
window.rdt('track', redditConversionID, {
61+
if (typeof window.rdt === 'function') {
62+
window.rdt('track', pixelEvent.REDDIT, {
8663
conversionId: eventID,
8764
email,
8865
});
8966
}
9067

9168
// LinkedIn (uses numeric conversion IDs instead of named events)
92-
const linkedInConversionID = LINKEDIN_CONVERSION_IDS[event];
93-
if (typeof window.lintrk === 'function' && linkedInConversionID) {
69+
if (typeof window.lintrk === 'function') {
9470
window.lintrk('setUserData', {email});
95-
window.lintrk('track', {conversion_id: linkedInConversionID});
71+
window.lintrk('track', {conversion_id: pixelEvent.LINKEDIN, event_id: eventID});
9672
}
9773
}
9874

src/libs/GoogleTagManager/types.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@ import type CONST from '@src/CONST';
55
* An event that can be published to Google Tag Manager. New events must be configured in GTM before they can be used
66
* in the app.
77
*/
8-
type GoogleTagManagerEvent = ValueOf<typeof CONST.ANALYTICS.EVENT>;
8+
type ExtractEventName<T> = T extends {NAME: infer N extends string} ? N : T extends string ? T : never;
9+
type GoogleTagManagerEvent = ExtractEventName<ValueOf<typeof CONST.ANALYTICS.EVENT>>;
910

1011
type GoogleTagManagerModule = {
1112
publishEvent: (event: GoogleTagManagerEvent, accountID: number, email: string) => void;

src/libs/Navigation/AppNavigator/Navigators/OnboardingModalNavigator.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ function OnboardingModalNavigator() {
7979
}
8080

8181
signUpEventPublishedForAccountID = accountID;
82-
GoogleTagManager.publishEvent(CONST.ANALYTICS.EVENT.SIGN_UP, accountID, email ?? '');
82+
GoogleTagManager.publishEvent(CONST.ANALYTICS.EVENT.SIGN_UP.NAME, accountID, email ?? '');
8383
}, [accountID]);
8484

8585
const handleOuterClick = useCallback(() => {

src/libs/actions/IOU/TrackExpense.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2126,7 +2126,7 @@ function categorizeTrackedExpense(trackedExpenseParams: TrackedExpenseParams) {
21262126
// If a draft policy was used, then the CategorizeTrackedExpense command will create a real one
21272127
// so let's track that conversion here
21282128
if (isDraftPolicy) {
2129-
GoogleTagManager.publishEvent(CONST.ANALYTICS.EVENT.WORKSPACE_CREATED, getUserAccountID(), getCurrentUserEmail());
2129+
GoogleTagManager.publishEvent(CONST.ANALYTICS.EVENT.WORKSPACE_CREATED.NAME, getUserAccountID(), getCurrentUserEmail());
21302130
}
21312131
}
21322132

src/libs/actions/PaymentMethods.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -207,7 +207,7 @@ function addPaymentCard(accountID: number, params: PaymentCardParams) {
207207
failureData,
208208
});
209209

210-
GoogleTagManager.publishEvent(CONST.ANALYTICS.EVENT.PAID_ADOPTION, accountID, getCurrentUserEmail() ?? '');
210+
GoogleTagManager.publishEvent(CONST.ANALYTICS.EVENT.PAID_ADOPTION.NAME, accountID, getCurrentUserEmail() ?? '');
211211
}
212212

213213
/**
@@ -276,9 +276,9 @@ function addSubscriptionPaymentCard(
276276
});
277277
}
278278
if (getCardForSubscriptionBilling(fundList)) {
279-
Log.info(`[GTM] Not logging ${CONST.ANALYTICS.EVENT.PAID_ADOPTION} because a card was already added`);
279+
Log.info(`[GTM] Not logging ${CONST.ANALYTICS.EVENT.PAID_ADOPTION.NAME} because a card was already added`);
280280
} else {
281-
GoogleTagManager.publishEvent(CONST.ANALYTICS.EVENT.PAID_ADOPTION, accountID, getCurrentUserEmail() ?? '');
281+
GoogleTagManager.publishEvent(CONST.ANALYTICS.EVENT.PAID_ADOPTION.NAME, accountID, getCurrentUserEmail() ?? '');
282282
}
283283
}
284284

src/libs/actions/Policy/Policy.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2975,7 +2975,7 @@ function createWorkspace(options: CreateWorkspaceDataOptions): CreateWorkspacePa
29752975

29762976
// Publish a workspace created event if this is their first policy
29772977
if (!options.hasActiveAdminPolicies) {
2978-
GoogleTagManager.publishEvent(CONST.ANALYTICS.EVENT.WORKSPACE_CREATED, options.currentUserAccountIDParam ?? CONST.DEFAULT_NUMBER_ID, options.currentUserEmailParam);
2978+
GoogleTagManager.publishEvent(CONST.ANALYTICS.EVENT.WORKSPACE_CREATED.NAME, options.currentUserAccountIDParam ?? CONST.DEFAULT_NUMBER_ID, options.currentUserEmailParam);
29792979
}
29802980

29812981
return params;

0 commit comments

Comments
 (0)