-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathtrack.ts
155 lines (134 loc) · 3.67 KB
/
track.ts
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
import { getCookie } from 'cookies-next';
import cookies from '@weco/common/data/cookies';
import Router from 'next/router';
import { ParsedUrlQuery } from 'querystring';
import { v4 as uuidv4 } from 'uuid';
import { PageviewName } from '@weco/common/data/segment-values';
declare global {
interface Window {
// Segment.io requires `analytics: any;`
// https://ashokraju.medium.com/using-segment-io-analytics-js-with-single-page-react-typescript-app-a8c12b4816c4
// eslint-disable-next-line
analytics: any;
// eslint-disable-next-line
dataLayer: Record<string, any>[] | undefined;
}
}
type Page = {
path: string;
name: string;
query: ParsedUrlQuery;
};
type ConversionType = 'pageview' | 'event';
type EventGroup = 'conversion' | 'similarity';
interface Conversion {
type: ConversionType;
source?: string;
page: Page;
properties: {
[key: string]: unknown;
};
eventGroup: EventGroup;
}
type Session = {
id: string;
timeout: number;
};
type EventProps = {
name: string;
properties: { [key: string]: unknown };
eventGroup?: EventGroup;
};
export type Pageview = {
name: PageviewName;
properties: Record<string, string[] | number[] | string | number | undefined>;
eventGroup?: EventGroup;
};
const sessionIdLocalStorageKey = 'sessionId';
const lastTrackedLocalStorageKey = 'lastTracked';
const sessionTimeout = 1000 * 60 * 30; // 30 minutes
function getSessionId(): string {
const lsSessionId = localStorage.getItem(sessionIdLocalStorageKey);
const lsLastTracked = localStorage.getItem(lastTrackedLocalStorageKey);
if (lsLastTracked) {
const now = Date.now();
const diff = now - parseInt(lsLastTracked, 10);
if (diff >= sessionTimeout) {
return resetSessionId();
}
}
if (lsSessionId) {
return lsSessionId;
}
return resetSessionId();
}
function resetSessionId(): string {
const sessionId = uuidv4();
localStorage.setItem(sessionIdLocalStorageKey, sessionId);
return sessionId;
}
let pageName: string;
function trackPageview({
name,
properties,
eventGroup = 'conversion',
}: EventProps): void {
// Source is passed in the querystring in the app, but not the client.
// e.g. /common/views/component/WorkLink/WorkLink.tsx
const { source, ...query } = Router.query;
pageName = name;
const conversion: Conversion = {
type: 'pageview',
source: source?.toString() || 'unknown',
eventGroup,
page: {
path: Router.asPath,
name,
query,
},
properties,
};
track(conversion);
}
function trackSegmentEvent({
name,
properties,
eventGroup = 'conversion',
}: EventProps): void {
track({
type: 'event',
eventGroup,
page: {
path: Router.asPath,
name: pageName,
query: Router.query,
},
properties: { event: name, ...properties },
});
}
function track(conversion: Conversion) {
const debug = getCookie(cookies.analyticsDebug) === true;
// We make toggles available of the dataLayer for GTM, so can use them here too
const togglesString = window?.dataLayer?.find(data => data.toggles)?.toggles;
const toggles = togglesString ? togglesString.split(',') : [];
const sessionId = getSessionId();
const session: Session = {
id: sessionId,
timeout: sessionTimeout,
};
const { eventGroup, properties, ...restConversion } = conversion;
localStorage.setItem(lastTrackedLocalStorageKey, Date.now().toString());
if (debug) {
console.info(eventGroup, {
session,
properties: { ...properties, toggles },
...restConversion,
});
}
window.analytics.track(eventGroup, {
session,
properties: { ...properties, toggles },
...restConversion,
});
}
export { trackPageview, trackSegmentEvent };