Skip to content
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
5 changes: 4 additions & 1 deletion cspell.json
Original file line number Diff line number Diff line change
Expand Up @@ -926,7 +926,10 @@
"CARDFROZEN",
"CARDUNFROZEN",
"backgrounded",
"Kolkata"
"Kolkata",
"lintrk",
"Fbclid",
"Gclid"
],
"ignorePaths": [
".gitignore",
Expand Down
21 changes: 18 additions & 3 deletions src/CONST/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8630,9 +8630,24 @@ const CONST = {

ANALYTICS: {
EVENT: {
SIGN_UP: 'sign_up',
WORKSPACE_CREATED: 'workspace_created',
PAID_ADOPTION: 'paid_adoption',
SIGN_UP: {
NAME: 'sign_up',
META: 'SignUp',
REDDIT: 'SignUp',
LINKEDIN: 507587661,
},
WORKSPACE_CREATED: {
NAME: 'workspace_created',
META: 'WorkspaceCreated',
REDDIT: 'Lead',
LINKEDIN: 25474804,
},
PAID_ADOPTION: {
NAME: 'paid_adoption',
META: 'PaidAdoption',
REDDIT: 'Purchase',
LINKEDIN: 25474820,
},
},
},

Expand Down
6 changes: 3 additions & 3 deletions src/libs/GoogleTagManager/index.native.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ import type GoogleTagManagerModule from './types';

const analytics = getAnalytics();

function publishEvent(event: GoogleTagManagerEvent, accountID: number) {
logEvent(analytics, event as string, {user_id: accountID});
Log.info('[GTM] event published', false, {event, user_id: accountID});
function publishEvent(event: GoogleTagManagerEvent, accountID: number, email: string) {
logEvent(analytics, event as string, {user_id: accountID, email});
Log.info('[GTM] event published', false, {event, user_id: accountID, user_data: {email}});
}

const GoogleTagManager: GoogleTagManagerModule = {
Expand Down
41 changes: 37 additions & 4 deletions src/libs/GoogleTagManager/index.ts
Original file line number Diff line number Diff line change
@@ -1,36 +1,69 @@
/* eslint-disable @typescript-eslint/naming-convention */
import Log from '@libs/Log';
import CONST from '@src/CONST';
import type {GoogleTagManagerEvent} from './types';
import type GoogleTagManagerModule from './types';

/**
* The dataLayer is added with a js snippet from Google in web/thirdPartyScripts.js. Set USE_THIRD_PARTY_SCRIPTS to true
* in your .env to enable this
*/
type WindowWithDataLayer = Window & {
type WindowWithPixels = Window & {
dataLayer?: {
push: (params: DataLayerPushParams) => void;
};
fbq?: (method: string, eventName: string, params?: Record<string, unknown>, options?: Record<string, unknown>) => void;
rdt?: (method: string, eventType: string, params?: Record<string, string>) => void;
lintrk?: (method: string, params: Record<string, unknown>) => void;
};

type DataLayerPushParams = {
event: GoogleTagManagerEvent;
user_id: number;
user_data: {email: string};
};

declare const window: WindowWithDataLayer;
declare const window: WindowWithPixels;

function publishEvent(event: GoogleTagManagerEvent, accountID: number) {
const PIXEL_EVENTS = [CONST.ANALYTICS.EVENT.SIGN_UP, CONST.ANALYTICS.EVENT.WORKSPACE_CREATED, CONST.ANALYTICS.EVENT.PAID_ADOPTION] as const;

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

const params = {event, user_id: accountID};
const params = {event, user_id: accountID, user_data: {email}};

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

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

const pixelEvent = PIXEL_EVENTS.find((e) => e.NAME === event);
if (!pixelEvent) {
return;
}

const eventID = `${accountID}-${event}`;

// Meta
if (typeof window.fbq === 'function') {
window.fbq('trackCustom', pixelEvent.META, {em: email}, {eventID});
}

// Reddit
if (typeof window.rdt === 'function') {
window.rdt('track', pixelEvent.REDDIT, {
conversionId: eventID,
email,
});
}

// LinkedIn (uses numeric conversion IDs instead of named events)
if (typeof window.lintrk === 'function') {
window.lintrk('setUserData', {email});
window.lintrk('track', {conversion_id: pixelEvent.LINKEDIN, event_id: eventID});
}
}

const GoogleTagManager: GoogleTagManagerModule = {
Expand Down
5 changes: 3 additions & 2 deletions src/libs/GoogleTagManager/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,11 @@ import type CONST from '@src/CONST';
* An event that can be published to Google Tag Manager. New events must be configured in GTM before they can be used
* in the app.
*/
type GoogleTagManagerEvent = ValueOf<typeof CONST.ANALYTICS.EVENT>;
type ExtractEventName<T> = T extends {NAME: infer N extends string} ? N : T extends string ? T : never;
type GoogleTagManagerEvent = ExtractEventName<ValueOf<typeof CONST.ANALYTICS.EVENT>>;

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

export default GoogleTagManagerModule;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import {CardStyleInterpolators} from '@react-navigation/stack';
import {accountIDSelector} from '@selectors/Session';
import {accountIDSelector, emailSelector} from '@selectors/Session';
import React, {useCallback, useEffect, useMemo} from 'react';
import {View} from 'react-native';
import type {ValueOf} from 'type-fest';
Expand Down Expand Up @@ -68,17 +68,20 @@ function OnboardingModalNavigator() {
const [accountID] = useOnyx(ONYXKEYS.SESSION, {
selector: accountIDSelector,
});
const [email] = useOnyx(ONYXKEYS.SESSION, {
selector: emailSelector,
});

// Publish a sign_up event when we start the onboarding flow. This should track basic sign ups
// as well as Google and Apple SSO.
useEffect(() => {
if (!accountID || signUpEventPublishedForAccountID === accountID) {
if (!accountID || !email || signUpEventPublishedForAccountID === accountID) {
return;
}

signUpEventPublishedForAccountID = accountID;
GoogleTagManager.publishEvent(CONST.ANALYTICS.EVENT.SIGN_UP, accountID);
}, [accountID]);
GoogleTagManager.publishEvent(CONST.ANALYTICS.EVENT.SIGN_UP.NAME, accountID, email);
}, [accountID, email]);

const handleOuterClick = useCallback(() => {
OnboardingRefManager.handleOuterClick();
Expand Down
6 changes: 4 additions & 2 deletions src/libs/actions/IOU/TrackExpense.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2070,7 +2070,7 @@ function convertBulkTrackedExpensesToIOU({
}

function categorizeTrackedExpense(trackedExpenseParams: TrackedExpenseParams) {
const {onyxData, reportInformation, transactionParams, policyParams, createdWorkspaceParams, currentUserAccountID} = trackedExpenseParams;
const {onyxData, reportInformation, transactionParams, policyParams, createdWorkspaceParams, currentUserAccountID, currentUserEmail} = trackedExpenseParams;
const {optimisticData, successData, failureData} = onyxData ?? {};
const {transactionID} = transactionParams;
const {isDraftPolicy} = policyParams;
Expand Down Expand Up @@ -2128,7 +2128,7 @@ function categorizeTrackedExpense(trackedExpenseParams: TrackedExpenseParams) {
// If a draft policy was used, then the CategorizeTrackedExpense command will create a real one
// so let's track that conversion here
if (isDraftPolicy) {
GoogleTagManager.publishEvent(CONST.ANALYTICS.EVENT.WORKSPACE_CREATED, currentUserAccountID);
GoogleTagManager.publishEvent(CONST.ANALYTICS.EVENT.WORKSPACE_CREATED.NAME, currentUserAccountID, currentUserEmail ?? '');
}
}

Expand Down Expand Up @@ -2513,6 +2513,7 @@ function trackExpense(params: CreateTrackExpenseParams) {
policyParams,
createdWorkspaceParams,
currentUserAccountID: currentUserAccountIDParam,
currentUserEmail: currentUserEmailParam,
};

categorizeTrackedExpense(trackedExpenseParams);
Expand Down Expand Up @@ -2565,6 +2566,7 @@ function trackExpense(params: CreateTrackExpenseParams) {
createdWorkspaceParams,
accountantParams,
currentUserAccountID: currentUserAccountIDParam,
currentUserEmail: currentUserEmailParam,
};
shareTrackedExpense(trackedExpenseParams);
break;
Expand Down
1 change: 1 addition & 0 deletions src/libs/actions/IOU/types/TrackedExpenseParams.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ type TrackedExpenseParams = {
createdWorkspaceParams?: CreateWorkspaceParams;
accountantParams?: TrackExpenseAccountantParams;
currentUserAccountID: number;
currentUserEmail?: string;
};

export type {TrackedExpenseParams, TrackedExpensePolicyParams, TrackedExpenseTransactionParams, TrackedExpenseReportInformation, BuildOnyxDataForTrackExpenseKeys};
7 changes: 4 additions & 3 deletions src/libs/actions/PaymentMethods.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import * as CardUtils from '@libs/CardUtils';
import GoogleTagManager from '@libs/GoogleTagManager';
import Log from '@libs/Log';
import Navigation from '@libs/Navigation/Navigation';
import {getCurrentUserEmail} from '@libs/Network/NetworkStore';
import {isPolicyUser} from '@libs/PolicyUtils';
import {getCardForSubscriptionBilling} from '@libs/SubscriptionUtils';
import CONST from '@src/CONST';
Expand Down Expand Up @@ -204,7 +205,7 @@ function addPaymentCard(accountID: number, params: PaymentCardParams) {
failureData,
});

GoogleTagManager.publishEvent(CONST.ANALYTICS.EVENT.PAID_ADOPTION, accountID);
GoogleTagManager.publishEvent(CONST.ANALYTICS.EVENT.PAID_ADOPTION.NAME, accountID, getCurrentUserEmail() ?? '');
}

/**
Expand Down Expand Up @@ -272,9 +273,9 @@ function addSubscriptionPaymentCard(
});
}
if (getCardForSubscriptionBilling(fundList)) {
Log.info(`[GTM] Not logging ${CONST.ANALYTICS.EVENT.PAID_ADOPTION} because a card was already added`);
Log.info(`[GTM] Not logging ${CONST.ANALYTICS.EVENT.PAID_ADOPTION.NAME} because a card was already added`);
} else {
GoogleTagManager.publishEvent(CONST.ANALYTICS.EVENT.PAID_ADOPTION, accountID);
GoogleTagManager.publishEvent(CONST.ANALYTICS.EVENT.PAID_ADOPTION.NAME, accountID, getCurrentUserEmail() ?? '');
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/libs/actions/Policy/Policy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3085,7 +3085,7 @@ function createWorkspace(options: CreateWorkspaceDataOptions): CreateWorkspacePa

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

return params;
Expand Down
2 changes: 1 addition & 1 deletion tests/actions/PolicyTest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1737,7 +1737,7 @@ describe('actions/Policy', () => {
});

expect(GoogleTagManager.publishEvent).toHaveBeenCalledTimes(1);
expect(GoogleTagManager.publishEvent).toHaveBeenCalledWith(CONST.ANALYTICS.EVENT.WORKSPACE_CREATED, ESH_ACCOUNT_ID);
expect(GoogleTagManager.publishEvent).toHaveBeenCalledWith(CONST.ANALYTICS.EVENT.WORKSPACE_CREATED.NAME, ESH_ACCOUNT_ID, ESH_EMAIL);
});

it('should not publish a workspace created event if this is not their first policy', () => {
Expand Down
17 changes: 11 additions & 6 deletions tests/unit/GoogleTagManagerTest.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -121,12 +121,13 @@ const FUND_LIST: FundList = {

describe('GoogleTagManagerTest', () => {
const accountID = 123456;
const email = 'test@test.com';

beforeAll(() => {
Onyx.init({
keys: ONYXKEYS,
initialKeyStates: {
session: {accountID},
session: {accountID, email},
},
});
});
Expand Down Expand Up @@ -156,7 +157,7 @@ describe('GoogleTagManagerTest', () => {

// Then we publish the sign_up event only once
expect(GoogleTagManager.publishEvent).toHaveBeenCalledTimes(1);
expect(GoogleTagManager.publishEvent).toHaveBeenCalledWith(CONST.ANALYTICS.EVENT.SIGN_UP, accountID);
expect(GoogleTagManager.publishEvent).toHaveBeenCalledWith(CONST.ANALYTICS.EVENT.SIGN_UP.NAME, accountID, email);
});

test('workspace_created', async () => {
Expand Down Expand Up @@ -197,10 +198,14 @@ describe('GoogleTagManagerTest', () => {

// Then we publish a workspace_created event only once
expect(GoogleTagManager.publishEvent).toHaveBeenCalledTimes(1);
expect(GoogleTagManager.publishEvent).toHaveBeenCalledWith(CONST.ANALYTICS.EVENT.WORKSPACE_CREATED, 123456);
expect(GoogleTagManager.publishEvent).toHaveBeenCalledWith(CONST.ANALYTICS.EVENT.WORKSPACE_CREATED.NAME, 123456, email);
});

test('workspace_created - categorizeTrackedExpense', async () => {
await act(async () => {
await Onyx.set(ONYXKEYS.SESSION, {accountID, email});
});

const recentWaypoints = (await getOnyxValue(ONYXKEYS.NVP_RECENT_WAYPOINTS)) ?? [];

trackExpense({
Expand Down Expand Up @@ -240,7 +245,7 @@ describe('GoogleTagManagerTest', () => {

// Then we publish a workspace_created event only once
expect(GoogleTagManager.publishEvent).toHaveBeenCalledTimes(1);
expect(GoogleTagManager.publishEvent).toHaveBeenCalledWith('workspace_created', accountID);
expect(GoogleTagManager.publishEvent).toHaveBeenCalledWith(CONST.ANALYTICS.EVENT.WORKSPACE_CREATED.NAME, accountID, email);
});

test('paid_adoption - addPaymentCard', async () => {
Expand All @@ -257,7 +262,7 @@ describe('GoogleTagManagerTest', () => {

// Then we publish a paid_adoption event only once
expect(GoogleTagManager.publishEvent).toHaveBeenCalledTimes(1);
expect(GoogleTagManager.publishEvent).toHaveBeenCalledWith(CONST.ANALYTICS.EVENT.PAID_ADOPTION, accountID);
expect(GoogleTagManager.publishEvent).toHaveBeenCalledWith(CONST.ANALYTICS.EVENT.PAID_ADOPTION.NAME, accountID, email);
});

test('paid_adoption - addSubscriptionPaymentCard', async () => {
Expand All @@ -280,7 +285,7 @@ describe('GoogleTagManagerTest', () => {

// Then we publish a paid_adoption event only once
expect(GoogleTagManager.publishEvent).toHaveBeenCalledTimes(1);
expect(GoogleTagManager.publishEvent).toHaveBeenCalledWith(CONST.ANALYTICS.EVENT.PAID_ADOPTION, accountID);
expect(GoogleTagManager.publishEvent).toHaveBeenCalledWith(CONST.ANALYTICS.EVENT.PAID_ADOPTION.NAME, accountID, email);
});

it('addSubscriptionPaymentCard when changing payment card, will not publish event paid_adoption', async () => {
Expand Down
Loading