|
1 | 1 | /* eslint-disable @typescript-eslint/naming-convention */ |
2 | 2 | import Log from '@libs/Log'; |
| 3 | +import CONST from '@src/CONST'; |
3 | 4 | import type {GoogleTagManagerEvent} from './types'; |
4 | 5 | import type GoogleTagManagerModule from './types'; |
5 | 6 |
|
6 | 7 | /** |
7 | 8 | * The dataLayer is added with a js snippet from Google in web/thirdPartyScripts.js. Set USE_THIRD_PARTY_SCRIPTS to true |
8 | 9 | * in your .env to enable this |
9 | 10 | */ |
10 | | -type WindowWithDataLayer = Window & { |
| 11 | +type WindowWithPixels = Window & { |
11 | 12 | dataLayer?: { |
12 | 13 | push: (params: DataLayerPushParams) => void; |
13 | 14 | }; |
| 15 | + fbq?: (method: string, eventName: string, params?: Record<string, unknown>, options?: Record<string, unknown>) => void; |
| 16 | + rdt?: (method: string, eventType: string, params?: Record<string, string>) => void; |
| 17 | + lintrk?: (method: string, params: Record<string, unknown>) => void; |
14 | 18 | }; |
15 | 19 |
|
16 | 20 | type DataLayerPushParams = { |
17 | 21 | event: GoogleTagManagerEvent; |
18 | 22 | user_id: number; |
| 23 | + user_data: {email: string}; |
19 | 24 | }; |
20 | 25 |
|
21 | | -declare const window: WindowWithDataLayer; |
| 26 | +declare const window: WindowWithPixels; |
22 | 27 |
|
23 | | -function publishEvent(event: GoogleTagManagerEvent, accountID: number) { |
| 28 | +const PIXEL_EVENTS = [CONST.ANALYTICS.EVENT.SIGN_UP, CONST.ANALYTICS.EVENT.WORKSPACE_CREATED, CONST.ANALYTICS.EVENT.PAID_ADOPTION] as const; |
| 29 | + |
| 30 | +function publishEvent(event: GoogleTagManagerEvent, accountID: number, email: string) { |
24 | 31 | if (!window.dataLayer) { |
25 | 32 | return; |
26 | 33 | } |
27 | 34 |
|
28 | | - const params = {event, user_id: accountID}; |
| 35 | + const params = {event, user_id: accountID, user_data: {email}}; |
29 | 36 |
|
30 | 37 | // Pass a copy of params here since the dataLayer modifies the object |
31 | 38 | window.dataLayer.push({...params}); |
32 | 39 |
|
33 | 40 | Log.info('[GTM] event published', false, params); |
| 41 | + |
| 42 | + const pixelEvent = PIXEL_EVENTS.find((e) => e.NAME === event); |
| 43 | + if (!pixelEvent) { |
| 44 | + return; |
| 45 | + } |
| 46 | + |
| 47 | + const eventID = `${accountID}-${event}`; |
| 48 | + |
| 49 | + // Meta |
| 50 | + if (typeof window.fbq === 'function') { |
| 51 | + window.fbq('trackCustom', pixelEvent.META, {em: email}, {eventID}); |
| 52 | + } |
| 53 | + |
| 54 | + // Reddit |
| 55 | + if (typeof window.rdt === 'function') { |
| 56 | + window.rdt('track', pixelEvent.REDDIT, { |
| 57 | + conversionId: eventID, |
| 58 | + email, |
| 59 | + }); |
| 60 | + } |
| 61 | + |
| 62 | + // LinkedIn (uses numeric conversion IDs instead of named events) |
| 63 | + if (typeof window.lintrk === 'function') { |
| 64 | + window.lintrk('setUserData', {email}); |
| 65 | + window.lintrk('track', {conversion_id: pixelEvent.LINKEDIN, event_id: eventID}); |
| 66 | + } |
34 | 67 | } |
35 | 68 |
|
36 | 69 | const GoogleTagManager: GoogleTagManagerModule = { |
|
0 commit comments