forked from snowplow/snowplow-javascript-tracker
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.ts
More file actions
94 lines (83 loc) · 3.64 KB
/
Copy pathindex.ts
File metadata and controls
94 lines (83 loc) · 3.64 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
import { SelfDescribingJson } from '@snowplow/tracker-core';
import { BrowserPlugin, cookie } from '@snowplow/browser-tracker-core';
import { GA4Cookies, GA4SessionCookies, UACookies } from './types';
import { GOOGLE_ANALYTICS_4_COOKIES_SCHEMA, UNIVERSAL_ANALYTICS_COOKIES_SCHEMA } from './schemata';
interface GACookiesPluginOptions {
ua?: boolean;
ga4?: boolean;
ga4MeasurementId?: string | string[];
cookiePrefix?: string | string[];
}
const defaultPluginOptions: GACookiesPluginOptions = {
ua: false,
ga4: true,
ga4MeasurementId: '',
cookiePrefix: [],
};
/**
* Captures the GA cookies on a page and sends as context on each event
* @param pluginOptions.ua - Send Universal Analytics specific cookie values.
* @param pluginOptions.ga4 - Send Google Analytics 4 specific cookie values.
* @param pluginOptions.ga4MeasurementId - Measurement id/ids to search the Google Analytics 4 session cookie. Can be a single measurement id as a string or an array of measurement id strings. The cookie has the form of _ga_<container-id> where <container-id> is the data stream container id.
* @param pluginOptions.cookiePrefix - Cookie prefix set on the Google Analytics 4 cookies using the cookie_prefix option of the gtag.js tracker.
*/
export function GaCookiesPlugin(pluginOptions: GACookiesPluginOptions = defaultPluginOptions): BrowserPlugin {
return {
contexts: () => {
const contexts: SelfDescribingJson<Record<string, unknown>>[] = [];
const { ga4, ga4MeasurementId, ua, cookiePrefix } = { ...defaultPluginOptions, ...pluginOptions };
const GA_USER_COOKIE = '_ga';
const GA4_MEASUREMENT_ID_PREFIX = 'G-';
const GA4_COOKIE_PREFIX = '_ga_';
if (ua) {
const uaCookiesContext: SelfDescribingJson<UACookies> = {
schema: UNIVERSAL_ANALYTICS_COOKIES_SCHEMA,
data: {},
};
['__utma', '__utmb', '__utmc', '__utmv', '__utmz', GA_USER_COOKIE].forEach(function (cookieType) {
var value = cookie(cookieType);
if (value) {
uaCookiesContext.data[cookieType] = value;
}
});
contexts.push(uaCookiesContext);
}
if (ga4) {
const cookiePrefixes = Array.isArray(cookiePrefix) ? [...cookiePrefix] : [cookiePrefix];
/* We also will search for the default (no prefix) in every case. */
cookiePrefixes.unshift('');
cookiePrefixes.forEach((prefix) => {
const userCookie = cookie(prefix + GA_USER_COOKIE);
const sessionCookies: GA4SessionCookies[] = [];
if (ga4MeasurementId) {
const measurementIdentifiers = Array.isArray(ga4MeasurementId) ? [...ga4MeasurementId] : [ga4MeasurementId];
measurementIdentifiers.forEach(function (cookieIdentifier) {
const sessionCookieValue = cookie(
prefix + cookieIdentifier.replace(GA4_MEASUREMENT_ID_PREFIX, GA4_COOKIE_PREFIX)
);
if (sessionCookieValue) {
sessionCookies.push({
measurement_id: cookieIdentifier,
session_cookie: sessionCookieValue,
});
}
});
}
if (!userCookie && !sessionCookies.length) {
return;
}
const ga4CookiesContext: SelfDescribingJson<GA4Cookies> = {
schema: GOOGLE_ANALYTICS_4_COOKIES_SCHEMA,
data: {
_ga: userCookie,
session_cookies: sessionCookies.length ? sessionCookies : undefined,
cookie_prefix: prefix || undefined,
},
};
contexts.push(ga4CookiesContext);
});
}
return contexts;
},
};
}