forked from codler/react-ga4
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ga4.test.js
371 lines (315 loc) · 9.78 KB
/
ga4.test.js
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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
import gtag from "./gtag";
import GA4 from "./ga4";
const newDate = new Date("2020-01-01");
jest.mock("./gtag");
jest.useFakeTimers("modern").setSystemTime(newDate.getTime());
describe("GA4", () => {
// Given
const GA_MEASUREMENT_ID = "GA_MEASUREMENT_ID";
beforeEach(() => {
gtag.mockReset();
GA4.reset();
});
describe("GA4.initialize()", () => {
it("initialize() default", () => {
// When
GA4.initialize(GA_MEASUREMENT_ID);
// Then
expect(gtag).toHaveBeenNthCalledWith(1, "js", newDate);
expect(gtag).toHaveBeenNthCalledWith(2, "config", GA_MEASUREMENT_ID);
expect(gtag).toHaveBeenCalledTimes(2);
});
it("initialize() with options", () => {
// Given
const options = {
gaOptions: {
cookieUpdate: false,
},
};
// When
GA4.initialize(GA_MEASUREMENT_ID, options);
// Then
expect(gtag).toHaveBeenNthCalledWith(1, "js", newDate);
expect(gtag).toHaveBeenNthCalledWith(2, "config", GA_MEASUREMENT_ID, {
cookie_update: false,
});
expect(gtag).toHaveBeenCalledTimes(2);
});
it("initialize() in test mode", () => {
// Given
const options = {
testMode: true,
};
const command = "send";
const object = { hitType: "pageview" };
// When
GA4.initialize(GA_MEASUREMENT_ID, options);
GA4.ga(command, object);
// Then
expect(gtag).toHaveBeenCalledTimes(0);
});
it("initialize() multiple products", () => {
// Given
const GA_MEASUREMENT_ID2 = "GA_MEASUREMENT_ID2";
const config = [
{ trackingId: GA_MEASUREMENT_ID },
{ trackingId: GA_MEASUREMENT_ID2 },
];
// When
GA4.initialize(config);
// Then
expect(gtag).toHaveBeenNthCalledWith(1, "js", newDate);
expect(gtag).toHaveBeenNthCalledWith(2, "config", GA_MEASUREMENT_ID);
expect(gtag).toHaveBeenNthCalledWith(3, "config", GA_MEASUREMENT_ID2);
expect(gtag).toHaveBeenCalledTimes(3);
});
});
describe("GA4.ga()", () => {
it("ga() send pageview", () => {
// Given
const command = "send";
const object = { hitType: "pageview" };
// When
GA4.ga(command, object);
// Then
expect(gtag).toHaveBeenNthCalledWith(1, "event", "page_view");
});
it("ga() send timing", () => {
// Given
const command = "send";
const hitType = "timing";
const timingCategory = "DOM";
const timingVar = "first-contentful-paint";
const timingValue = 120;
// When
GA4.ga(command, hitType, timingCategory, timingVar, timingValue);
// Then
expect(gtag).toHaveBeenNthCalledWith(1, "event", "timing_complete", {
event_category: timingCategory,
name: timingVar,
value: timingValue,
});
});
it("ga() callback", (done) => {
// Given
const clientId = "clientId value";
gtag.mockImplementationOnce((command, target, field_name, cb) =>
cb(clientId)
);
const callback = jest.fn((tracker) => {
const trackerClientId = tracker.get("clientId");
const trackerTrackingId = tracker.get("trackingId");
const trackerApiVersion = tracker.get("apiVersion");
expect(trackerClientId).toEqual(clientId);
expect(trackerTrackingId).toEqual(GA_MEASUREMENT_ID);
expect(trackerApiVersion).toEqual("1");
done();
});
// When
GA4.ga(callback);
// Then
expect(gtag).toHaveBeenNthCalledWith(
1,
"get",
GA_MEASUREMENT_ID,
"client_id",
expect.any(Function)
);
});
it("ga() async callback", (done) => {
// Given
const clientId = "clientId value";
gtag.mockImplementationOnce((command, target, field_name, cb) =>
cb(clientId)
);
const callback = jest.fn(async (tracker) => {
const trackerClientId = tracker.get("clientId");
expect(trackerClientId).toEqual(clientId);
done();
});
// When
GA4.ga(callback);
// Then
expect(gtag).toHaveBeenNthCalledWith(
1,
"get",
GA_MEASUREMENT_ID,
"client_id",
expect.any(Function)
);
});
it("ga() callback queue", (done) => {
// Given
const clientId = "clientId value";
gtag.mockImplementationOnce((command, target, field_name, cb) => {
setImmediate(() => cb(clientId));
});
const callback = jest.fn(() => {
GA4.ga("send", { hitType: "pageview" });
expect(gtag).toHaveBeenNthCalledWith(2, "event", "page_view");
done();
});
// When
GA4.ga(callback);
GA4.ga("send", "event", "category value");
// Then
expect(gtag).toHaveBeenNthCalledWith(
1,
"get",
GA_MEASUREMENT_ID,
"client_id",
expect.any(Function)
);
expect(gtag).toHaveBeenCalledTimes(1);
expect(GA4._isQueuing).toBeTruthy();
expect(GA4._queueGtag).toHaveLength(1);
jest.runAllTimers();
expect(GA4._isQueuing).toBeFalsy();
expect(GA4._queueGtag).toHaveLength(0);
expect(gtag).toHaveBeenNthCalledWith(3, "event", undefined, {
event_category: "category value",
});
});
});
describe("GA4.send()", () => {
it("send() pageview", () => {
// Given
const object = { hitType: "pageview" };
// When
GA4.send(object);
// Then
expect(gtag).toHaveBeenNthCalledWith(1, "event", "page_view");
});
});
describe("GA4.event()", () => {
it("event() custom events", () => {
// Given
const eventName = "screen_view";
const eventParams = {
app_name: "myAppName",
screen_name: "Home",
};
// When
GA4.event(eventName, eventParams);
// Then
expect(gtag).toHaveBeenNthCalledWith(1, "event", eventName, eventParams);
});
it("event() simple", () => {
// Given
const object = {
category: "category value",
action: "action value",
label: "label value",
nonInteraction: true,
};
// When
GA4.event(object);
// Then
expect(gtag).toHaveBeenNthCalledWith(1, "event", "Action Value", {
event_category: "Category Value",
event_label: "Label Value",
non_interaction: true,
});
});
});
describe("GA4.set()", () => {
it("set()", () => {
// Given
const object = {
anonymizeIp: true,
referrer: "/signup",
allowAdFeatures: "allowAdFeatures value",
allowAdPersonalizationSignals: "allowAdPersonalizationSignals value",
page: "/home",
};
// When
GA4.set(object);
// Then
expect(gtag).toHaveBeenNthCalledWith(1, "set", {
anonymize_ip: true,
referrer: "/signup",
allow_google_signals: "allowAdFeatures value",
allow_ad_personalization_signals: "allowAdPersonalizationSignals value",
page_path: "/home",
});
});
});
describe("Reference", () => {
it("pageview", () => {
// Old https://developers.google.com/analytics/devguides/collection/analyticsjs/pages
// New https://developers.google.com/gtagjs/reference/event#page_view
// Given
const hitType = "pageview";
const path = "/location-pathname";
const title = "title value";
// When / Then
// Without parameters
GA4.send(hitType);
expect(gtag).toHaveBeenNthCalledWith(1, "event", "page_view");
GA4.send({ hitType });
expect(gtag).toHaveBeenNthCalledWith(2, "event", "page_view");
GA4.ga("send", hitType);
expect(gtag).toHaveBeenNthCalledWith(3, "event", "page_view");
// With path parameter
GA4.send({ hitType, page: path });
expect(gtag).toHaveBeenNthCalledWith(4, "event", "page_view", {
page_path: path,
});
GA4.ga("send", hitType, path);
expect(gtag).toHaveBeenNthCalledWith(5, "event", "page_view", {
page_path: path,
});
// With path and title parameter
GA4.send({ hitType, page: path, title });
expect(gtag).toHaveBeenNthCalledWith(6, "event", "page_view", {
page_path: path,
page_title: title,
});
GA4.ga("send", hitType, path, { title });
expect(gtag).toHaveBeenNthCalledWith(7, "event", "page_view", {
page_path: path,
page_title: title,
});
});
});
describe("Web vitals", () => {
it("Web vitals", () => {
// https://github.com/GoogleChrome/web-vitals/blob/main/README.md
function sendToGoogleAnalytics({ name, delta, value, id }) {
GA4.send({
hitType: "event",
eventCategory: "Web Vitals",
eventAction: name,
eventLabel: id,
nonInteraction: true,
// Built-in params:
value: Math.round(name === "CLS" ? delta * 1000 : delta), // Use `delta` so the value can be summed.
// Custom params:
metric_id: id, // Needed to aggregate events.
metric_value: value, // Optional.
metric_delta: delta, // Optional.
// OPTIONAL: any additional params or debug info here.
// See: https://web.dev/debug-web-vitals-in-the-field/
// metric_rating: 'good' | 'ni' | 'poor',
// debug_info: '...',
// ...
});
}
sendToGoogleAnalytics({
name: "CLS",
delta: 12.34,
value: 1,
id: "v2-1632380328370-6426221164013",
});
expect(gtag).toHaveBeenNthCalledWith(1, "event", "CLS", {
event_category: "Web Vitals",
event_label: "v2-1632380328370-6426221164013",
metric_delta: 12.34,
metric_id: "v2-1632380328370-6426221164013",
metric_value: 1,
non_interaction: true,
value: 12340,
});
});
});
});