coveo.analytics 2.26.3 → 2.26.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -110,6 +110,7 @@ export declare class CoveoAnalyticsClient implements AnalyticsClient, VisitorIdP
110
110
  private lowercaseKeys;
111
111
  private validateParams;
112
112
  private ensureAnonymousUserWhenUsingApiKey;
113
+ private limit;
113
114
  private get baseUrl();
114
115
  }
115
116
  export default CoveoAnalyticsClient;
@@ -1 +1 @@
1
- export declare const libVersion = "2.26.3";
1
+ export declare const libVersion = "2.26.5";
@@ -593,7 +593,7 @@ function sha1(bytes) {
593
593
  const v5 = v35('v5', 0x50, sha1);
594
594
  var uuidv5 = v5;
595
595
 
596
- const libVersion = "2.26.3" ;
596
+ const libVersion = "2.26.5" ;
597
597
 
598
598
  const getFormattedLocation = (location) => `${location.protocol}//${location.hostname}${location.pathname.indexOf('/') === 0 ? location.pathname : `/${location.pathname}`}${location.search}`;
599
599
 
@@ -1377,7 +1377,7 @@ class CoveoAnalyticsClient {
1377
1377
  return __awaiter(this, void 0, void 0, function* () {
1378
1378
  const { usesMeasurementProtocol = false } = this.eventTypeMapping[eventType] || {};
1379
1379
  const cleanPayloadStep = (currentPayload) => this.removeEmptyPayloadValues(currentPayload, eventType);
1380
- const validateParams = (currentPayload) => this.validateParams(currentPayload);
1380
+ const validateParams = (currentPayload) => this.validateParams(currentPayload, eventType);
1381
1381
  const processMeasurementProtocolConversionStep = (currentPayload) => usesMeasurementProtocol ? convertKeysToMeasurementProtocol(currentPayload) : currentPayload;
1382
1382
  const removeUnknownParameters = (currentPayload) => usesMeasurementProtocol ? this.removeUnknownParameters(currentPayload) : currentPayload;
1383
1383
  const processCustomParameters = (currentPayload) => usesMeasurementProtocol
@@ -1579,13 +1579,27 @@ class CoveoAnalyticsClient {
1579
1579
  });
1580
1580
  return result;
1581
1581
  }
1582
- validateParams(payload) {
1582
+ validateParams(payload, eventType) {
1583
1583
  const { anonymizeIp } = payload, rest = __rest(payload, ["anonymizeIp"]);
1584
1584
  if (anonymizeIp !== undefined) {
1585
1585
  if (['0', 'false', 'undefined', 'null', '{}', '[]', ''].indexOf(`${anonymizeIp}`.toLowerCase()) == -1) {
1586
- rest['anonymizeIp'] = 1;
1586
+ rest.anonymizeIp = 1;
1587
1587
  }
1588
1588
  }
1589
+ if (eventType == EventType.view ||
1590
+ eventType == EventType.click ||
1591
+ eventType == EventType.search ||
1592
+ eventType == EventType.custom) {
1593
+ rest.originLevel3 = this.limit(rest.originLevel3, 128);
1594
+ }
1595
+ if (eventType == EventType.view) {
1596
+ rest.location = this.limit(rest.location, 128);
1597
+ }
1598
+ if (eventType == 'pageview' || eventType == 'event') {
1599
+ rest.referrer = this.limit(rest.referrer, 2048);
1600
+ rest.location = this.limit(rest.location, 2048);
1601
+ rest.page = this.limit(rest.page, 2048);
1602
+ }
1589
1603
  return rest;
1590
1604
  }
1591
1605
  ensureAnonymousUserWhenUsingApiKey(payload) {
@@ -1598,6 +1612,12 @@ class CoveoAnalyticsClient {
1598
1612
  return payload;
1599
1613
  }
1600
1614
  }
1615
+ limit(input, length) {
1616
+ if (typeof input !== 'string') {
1617
+ return input;
1618
+ }
1619
+ return input.substring(0, length);
1620
+ }
1601
1621
  get baseUrl() {
1602
1622
  return buildBaseUrl(this.options.endpoint, this.options.version);
1603
1623
  }
package/dist/library.js CHANGED
@@ -740,7 +740,7 @@ function sha1(bytes) {
740
740
  const v5 = v35('v5', 0x50, sha1);
741
741
  var uuidv5 = v5;
742
742
 
743
- var libVersion = "2.26.3" ;
743
+ var libVersion = "2.26.5" ;
744
744
 
745
745
  var getFormattedLocation = function (location) {
746
746
  return location.protocol + "//" + location.hostname + (location.pathname.indexOf('/') === 0 ? location.pathname : "/" + location.pathname) + location.search;
@@ -83306,7 +83306,7 @@ var CoveoAnalyticsClient = (function () {
83306
83306
  cleanPayloadStep = function (currentPayload) {
83307
83307
  return _this.removeEmptyPayloadValues(currentPayload, eventType);
83308
83308
  };
83309
- validateParams = function (currentPayload) { return _this.validateParams(currentPayload); };
83309
+ validateParams = function (currentPayload) { return _this.validateParams(currentPayload, eventType); };
83310
83310
  processMeasurementProtocolConversionStep = function (currentPayload) {
83311
83311
  return usesMeasurementProtocol ? convertKeysToMeasurementProtocol(currentPayload) : currentPayload;
83312
83312
  };
@@ -83617,13 +83617,27 @@ var CoveoAnalyticsClient = (function () {
83617
83617
  });
83618
83618
  return result;
83619
83619
  };
83620
- CoveoAnalyticsClient.prototype.validateParams = function (payload) {
83620
+ CoveoAnalyticsClient.prototype.validateParams = function (payload, eventType) {
83621
83621
  var anonymizeIp = payload.anonymizeIp, rest = __rest(payload, ["anonymizeIp"]);
83622
83622
  if (anonymizeIp !== undefined) {
83623
83623
  if (['0', 'false', 'undefined', 'null', '{}', '[]', ''].indexOf(("" + anonymizeIp).toLowerCase()) == -1) {
83624
- rest['anonymizeIp'] = 1;
83624
+ rest.anonymizeIp = 1;
83625
83625
  }
83626
83626
  }
83627
+ if (eventType == EventType.view ||
83628
+ eventType == EventType.click ||
83629
+ eventType == EventType.search ||
83630
+ eventType == EventType.custom) {
83631
+ rest.originLevel3 = this.limit(rest.originLevel3, 128);
83632
+ }
83633
+ if (eventType == EventType.view) {
83634
+ rest.location = this.limit(rest.location, 128);
83635
+ }
83636
+ if (eventType == 'pageview' || eventType == 'event') {
83637
+ rest.referrer = this.limit(rest.referrer, 2048);
83638
+ rest.location = this.limit(rest.location, 2048);
83639
+ rest.page = this.limit(rest.page, 2048);
83640
+ }
83627
83641
  return rest;
83628
83642
  };
83629
83643
  CoveoAnalyticsClient.prototype.ensureAnonymousUserWhenUsingApiKey = function (payload) {
@@ -83636,6 +83650,12 @@ var CoveoAnalyticsClient = (function () {
83636
83650
  return payload;
83637
83651
  }
83638
83652
  };
83653
+ CoveoAnalyticsClient.prototype.limit = function (input, length) {
83654
+ if (typeof input !== 'string') {
83655
+ return input;
83656
+ }
83657
+ return input.substring(0, length);
83658
+ };
83639
83659
  Object.defineProperty(CoveoAnalyticsClient.prototype, "baseUrl", {
83640
83660
  get: function () {
83641
83661
  return buildBaseUrl(this.options.endpoint, this.options.version);
@@ -655,7 +655,7 @@ const addPageViewToHistory = (pageViewValue) => __awaiter(void 0, void 0, void 0
655
655
  yield store.addElementAsync(historyElement);
656
656
  });
657
657
 
658
- const libVersion = "2.26.3" ;
658
+ const libVersion = "2.26.5" ;
659
659
 
660
660
  const getFormattedLocation = (location) => `${location.protocol}//${location.hostname}${location.pathname.indexOf('/') === 0 ? location.pathname : `/${location.pathname}`}${location.search}`;
661
661
 
@@ -1378,7 +1378,7 @@ class CoveoAnalyticsClient {
1378
1378
  return __awaiter(this, void 0, void 0, function* () {
1379
1379
  const { usesMeasurementProtocol = false } = this.eventTypeMapping[eventType] || {};
1380
1380
  const cleanPayloadStep = (currentPayload) => this.removeEmptyPayloadValues(currentPayload, eventType);
1381
- const validateParams = (currentPayload) => this.validateParams(currentPayload);
1381
+ const validateParams = (currentPayload) => this.validateParams(currentPayload, eventType);
1382
1382
  const processMeasurementProtocolConversionStep = (currentPayload) => usesMeasurementProtocol ? convertKeysToMeasurementProtocol(currentPayload) : currentPayload;
1383
1383
  const removeUnknownParameters = (currentPayload) => usesMeasurementProtocol ? this.removeUnknownParameters(currentPayload) : currentPayload;
1384
1384
  const processCustomParameters = (currentPayload) => usesMeasurementProtocol
@@ -1580,13 +1580,27 @@ class CoveoAnalyticsClient {
1580
1580
  });
1581
1581
  return result;
1582
1582
  }
1583
- validateParams(payload) {
1583
+ validateParams(payload, eventType) {
1584
1584
  const { anonymizeIp } = payload, rest = __rest(payload, ["anonymizeIp"]);
1585
1585
  if (anonymizeIp !== undefined) {
1586
1586
  if (['0', 'false', 'undefined', 'null', '{}', '[]', ''].indexOf(`${anonymizeIp}`.toLowerCase()) == -1) {
1587
- rest['anonymizeIp'] = 1;
1587
+ rest.anonymizeIp = 1;
1588
1588
  }
1589
1589
  }
1590
+ if (eventType == EventType.view ||
1591
+ eventType == EventType.click ||
1592
+ eventType == EventType.search ||
1593
+ eventType == EventType.custom) {
1594
+ rest.originLevel3 = this.limit(rest.originLevel3, 128);
1595
+ }
1596
+ if (eventType == EventType.view) {
1597
+ rest.location = this.limit(rest.location, 128);
1598
+ }
1599
+ if (eventType == 'pageview' || eventType == 'event') {
1600
+ rest.referrer = this.limit(rest.referrer, 2048);
1601
+ rest.location = this.limit(rest.location, 2048);
1602
+ rest.page = this.limit(rest.page, 2048);
1603
+ }
1590
1604
  return rest;
1591
1605
  }
1592
1606
  ensureAnonymousUserWhenUsingApiKey(payload) {
@@ -1599,6 +1613,12 @@ class CoveoAnalyticsClient {
1599
1613
  return payload;
1600
1614
  }
1601
1615
  }
1616
+ limit(input, length) {
1617
+ if (typeof input !== 'string') {
1618
+ return input;
1619
+ }
1620
+ return input.substring(0, length);
1621
+ }
1602
1622
  get baseUrl() {
1603
1623
  return buildBaseUrl(this.options.endpoint, this.options.version);
1604
1624
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "coveo.analytics",
3
- "version": "2.26.3",
3
+ "version": "2.26.5",
4
4
  "description": "📈 Coveo analytics client (node and browser compatible) ",
5
5
  "main": "dist/library.js",
6
6
  "module": "dist/library.es.js",
@@ -6,7 +6,7 @@ import {
6
6
  DocumentSuggestion,
7
7
  FieldSuggestion,
8
8
  } from './caseAssistActions';
9
- import {mockFetch} from '../../tests/fetchMock';
9
+ import {mockFetch, lastCallBody} from '../../tests/fetchMock';
10
10
  import {TicketProperties} from '../plugins/svc';
11
11
  const {fetchMock, fetchMockBeforeEach} = mockFetch();
12
12
  import doNotTrack from '../donottrack';
@@ -90,8 +90,8 @@ describe('CaseAssistClient', () => {
90
90
  };
91
91
 
92
92
  const expectMatchPayload = (actionName: CaseAssistActions, actionData: Object, ticket: TicketProperties) => {
93
- const [, {body}] = fetchMock.lastCall();
94
- const content = JSON.parse(body.toString());
93
+ const body: string = lastCallBody(fetchMock);
94
+ const content = JSON.parse(body);
95
95
 
96
96
  expectMatchActionPayload(content, actionName, actionData);
97
97
  expectMatchTicketPayload(content, ticket);
@@ -125,7 +125,7 @@ describe('CaseAssistClient', () => {
125
125
  if (typeof propValue !== 'undefined') {
126
126
  if (typeof propValue === 'object') {
127
127
  expect(content).toHaveProperty(propName);
128
- expect(content[propName]).toMatchObject(propValue);
128
+ expect(content[propName]).toMatchObject(propValue as object);
129
129
  } else {
130
130
  expect(content).toHaveProperty(propName, propValue);
131
131
  }
@@ -7,7 +7,6 @@ import {mockFetch} from '../../tests/fetchMock';
7
7
  import {BrowserRuntime} from './runtimeEnvironment';
8
8
  import * as doNotTrack from '../donottrack';
9
9
  import {Cookie} from '../cookieutils';
10
- import {v4 as uuidv4} from 'uuid';
11
10
  import {CoveoLinkParam} from '../plugins/link';
12
11
 
13
12
  const aVisitorId = '123';
@@ -144,6 +143,42 @@ describe('Analytics', () => {
144
143
  });
145
144
  });
146
145
 
146
+ describe('should truncate the maxlength for URL parameters at 128 characters for ua events', () => {
147
+ const desiredMax: number = 128;
148
+ const longUrl: string = 'http://coveo.com/?q=' + 'a'.repeat(desiredMax);
149
+ expect(longUrl.length).toBeGreaterThan(desiredMax);
150
+ async function testEventType(type: EventType, url: string) {
151
+ mockFetchRequestForEventType(type);
152
+ await client.sendEvent(type, {
153
+ location: type == EventType.view ? url : undefined,
154
+ originLevel3: url,
155
+ });
156
+ const [body] = getParsedBodyCalls();
157
+ if (type == EventType.view) expect(body.location.length).toBeLessThanOrEqual(desiredMax);
158
+ expect(body.originLevel3.length).toBeLessThanOrEqual(desiredMax);
159
+ }
160
+ it('for view events', () => testEventType(EventType.view, longUrl));
161
+ it('for click events', () => testEventType(EventType.click, longUrl));
162
+ it('for search events', () => testEventType(EventType.search, longUrl));
163
+ it('for custom events', () => testEventType(EventType.custom, longUrl));
164
+ });
165
+
166
+ describe('url truncation is null safe', () => {
167
+ async function testAttributeTruncation(url: any) {
168
+ mockFetchRequestForEventType(EventType.view);
169
+ await client.sendEvent(EventType.view, {
170
+ location: url,
171
+ originLevel3: url,
172
+ });
173
+ const [body] = getParsedBodyCalls();
174
+ expect(body.location).toBe(url == null ? undefined : url);
175
+ expect(body.originLevel3).toBe(url == null ? undefined : url);
176
+ }
177
+ it('for undefined urls', () => testAttributeTruncation(undefined));
178
+ it('for null urls', () => testAttributeTruncation(null));
179
+ it('for non-string urls', () => testAttributeTruncation(12345));
180
+ });
181
+
147
182
  it('should not remove #queryText for search events even if empty', async () => {
148
183
  mockFetchRequestForEventType(EventType.search);
149
184
  await client.sendEvent(EventType.search, {
@@ -337,7 +337,7 @@ export class CoveoAnalyticsClient implements AnalyticsClient, VisitorIdProvider
337
337
 
338
338
  const cleanPayloadStep: ProcessPayloadStep = (currentPayload) =>
339
339
  this.removeEmptyPayloadValues(currentPayload, eventType);
340
- const validateParams: ProcessPayloadStep = (currentPayload) => this.validateParams(currentPayload);
340
+ const validateParams: ProcessPayloadStep = (currentPayload) => this.validateParams(currentPayload, eventType);
341
341
  const processMeasurementProtocolConversionStep: ProcessPayloadStep = (currentPayload) =>
342
342
  usesMeasurementProtocol ? convertKeysToMeasurementProtocol(currentPayload) : currentPayload;
343
343
  const removeUnknownParameters: ProcessPayloadStep = (currentPayload) =>
@@ -577,13 +577,30 @@ export class CoveoAnalyticsClient implements AnalyticsClient, VisitorIdProvider
577
577
  return result;
578
578
  }
579
579
 
580
- private validateParams(payload: IRequestPayload): IRequestPayload {
580
+ private validateParams(payload: IRequestPayload, eventType: EventType | string): IRequestPayload {
581
581
  const {anonymizeIp, ...rest} = payload;
582
582
  if (anonymizeIp !== undefined) {
583
583
  if (['0', 'false', 'undefined', 'null', '{}', '[]', ''].indexOf(`${anonymizeIp}`.toLowerCase()) == -1) {
584
- rest['anonymizeIp'] = 1;
584
+ rest.anonymizeIp = 1;
585
585
  }
586
586
  }
587
+
588
+ if (
589
+ eventType == EventType.view ||
590
+ eventType == EventType.click ||
591
+ eventType == EventType.search ||
592
+ eventType == EventType.custom
593
+ ) {
594
+ rest.originLevel3 = this.limit(rest.originLevel3, 128);
595
+ }
596
+ if (eventType == EventType.view) {
597
+ rest.location = this.limit(rest.location, 128);
598
+ }
599
+ if (eventType == 'pageview' || eventType == 'event') {
600
+ rest.referrer = this.limit(rest.referrer, 2048);
601
+ rest.location = this.limit(rest.location, 2048);
602
+ rest.page = this.limit(rest.page, 2048);
603
+ }
587
604
  return rest;
588
605
  }
589
606
 
@@ -597,6 +614,13 @@ export class CoveoAnalyticsClient implements AnalyticsClient, VisitorIdProvider
597
614
  }
598
615
  }
599
616
 
617
+ private limit(input: string, length: number): string | undefined | null {
618
+ if (typeof input !== 'string') {
619
+ return input;
620
+ }
621
+ return input.substring(0, length);
622
+ }
623
+
600
624
  private get baseUrl(): string {
601
625
  return buildBaseUrl(this.options.endpoint, this.options.version);
602
626
  }
@@ -3,7 +3,7 @@ import {createAnalyticsClientMock, visitorIdMock} from '../../tests/analyticsCli
3
3
  import {TestPlugin} from '../../tests/pluginMock';
4
4
  import {v4 as uuidv4} from 'uuid';
5
5
  import {PluginOptions} from '../plugins/BasePlugin';
6
- import {mockFetch} from '../../tests/fetchMock';
6
+ import {mockFetch, lastCallBody} from '../../tests/fetchMock';
7
7
  import {CookieStorage} from '../storage';
8
8
  import {libVersion} from '../version';
9
9
 
@@ -194,7 +194,7 @@ describe('simpleanalytics', () => {
194
194
 
195
195
  expect(fetchMock.calls().length).toBe(1);
196
196
  expect(fetchMock.lastUrl()).toBe(`${analyticsEndpoint}/pageview`);
197
- expect(JSON.parse(fetchMock.lastCall()![1]!.body!.toString())).toEqual({somedata: 'asd'});
197
+ expect(JSON.parse(lastCallBody(fetchMock))).toEqual({somedata: 'asd'});
198
198
  });
199
199
 
200
200
  it('can send view event with clientId', async () => {
@@ -203,7 +203,7 @@ describe('simpleanalytics', () => {
203
203
 
204
204
  expect(fetchMock.calls().length).toBe(1);
205
205
  expect(fetchMock.lastUrl()).toBe(`${analyticsEndpoint}/view?visitor=${visitorIdMock}`);
206
- expect(JSON.parse(fetchMock.lastCall()![1]!.body!.toString()).clientId).toBe(visitorIdMock);
206
+ expect(JSON.parse(lastCallBody(fetchMock)).clientId).toBe(visitorIdMock);
207
207
  });
208
208
 
209
209
  it('can send any event to the endpoint', async () => {
@@ -221,6 +221,32 @@ describe('simpleanalytics', () => {
221
221
  expect(fetchMock.calls().length).toBe(1);
222
222
  expect(fetchMock.lastUrl()).toBe(`https://myproxyendpoint.com/rest/v15/analytics/${someRandomEventName}`);
223
223
  });
224
+
225
+ describe('will restrict referrers and url lengths', () => {
226
+ const max: number = 2048;
227
+ const reallyLongPath1: string = 'http://coveo.com/?q=' + 'a'.repeat(max);
228
+ const reallyLongPath2: string = 'http://coveo.com/?q=' + 'b'.repeat(max);
229
+ expect(reallyLongPath1.length).toBeGreaterThan(max);
230
+ expect(reallyLongPath2.length).toBeGreaterThan(max);
231
+ it('does this for pageviews', async () => {
232
+ coveoua('init', 'MYTOKEN');
233
+ await coveoua('send', 'pageview', reallyLongPath1);
234
+ await coveoua('send', 'pageview', reallyLongPath2);
235
+ const body = JSON.parse(lastCallBody(fetchMock));
236
+ expect(body.dr.length).toBeLessThanOrEqual(max);
237
+ expect(body.dl.length).toBeLessThanOrEqual(max);
238
+ expect(body.dp.length).toBeLessThanOrEqual(max);
239
+ });
240
+ it('does this for generic events', async () => {
241
+ coveoua('init', 'MYTOKEN');
242
+ await coveoua('send', 'pageview', reallyLongPath1);
243
+ await coveoua('send', 'pageview', reallyLongPath2);
244
+ await coveoua('send', 'event');
245
+ const body = JSON.parse(lastCallBody(fetchMock));
246
+ expect(body.dr.length).toBeLessThanOrEqual(max);
247
+ expect(body.dl.length).toBeLessThanOrEqual(max);
248
+ });
249
+ });
224
250
  });
225
251
 
226
252
  describe('set', () => {
@@ -231,7 +257,7 @@ describe('simpleanalytics', () => {
231
257
 
232
258
  expect(fetchMock.calls().length).toBe(1);
233
259
  expect(fetchMock.lastUrl()).toBe(`${analyticsEndpoint}/${someRandomEventName}`);
234
- expect(JSON.parse(fetchMock.lastCall()![1]!.body!.toString())).toEqual({userId: 'something'});
260
+ expect(JSON.parse(lastCallBody(fetchMock))).toEqual({userId: 'something'});
235
261
  });
236
262
 
237
263
  it('can set parameters using an object', async () => {
@@ -243,7 +269,7 @@ describe('simpleanalytics', () => {
243
269
 
244
270
  expect(fetchMock.calls().length).toBe(1);
245
271
  expect(fetchMock.lastUrl()).toBe(`${analyticsEndpoint}/${someRandomEventName}`);
246
- expect(JSON.parse(fetchMock.lastCall()![1]!.body!.toString())).toEqual({userId: 'something'});
272
+ expect(JSON.parse(lastCallBody(fetchMock))).toEqual({userId: 'something'});
247
273
  });
248
274
 
249
275
  it('can set a custom_website parameter on a collect event', async () => {
@@ -252,7 +278,7 @@ describe('simpleanalytics', () => {
252
278
  await coveoua('send', 'pageview');
253
279
 
254
280
  expect(fetchMock.calls().length).toBe(1);
255
- let result = JSON.parse(fetchMock.lastCall()![1]!.body!.toString());
281
+ let result = JSON.parse(lastCallBody(fetchMock));
256
282
  expect(result).toHaveProperty('context_website', 'MY_WEBSITE');
257
283
  });
258
284
 
@@ -262,7 +288,7 @@ describe('simpleanalytics', () => {
262
288
  await coveoua('send', 'pageview');
263
289
 
264
290
  expect(fetchMock.calls().length).toBe(1);
265
- let result = JSON.parse(fetchMock.lastCall()![1]!.body!.toString());
291
+ let result = JSON.parse(lastCallBody(fetchMock));
266
292
  expect(Object.keys(result).length).toBe(11);
267
293
  });
268
294
 
@@ -272,7 +298,7 @@ describe('simpleanalytics', () => {
272
298
  await coveoua('send', 'pageview');
273
299
 
274
300
  expect(fetchMock.calls().length).toBe(1);
275
- let result = JSON.parse(fetchMock.lastCall()![1]!.body!.toString());
301
+ let result = JSON.parse(lastCallBody(fetchMock));
276
302
  expect(Object.keys(result).length).toBe(11);
277
303
  });
278
304
 
@@ -282,7 +308,7 @@ describe('simpleanalytics', () => {
282
308
  await coveoua('send', 'pageview');
283
309
 
284
310
  expect(fetchMock.calls().length).toBe(1);
285
- let result = JSON.parse(fetchMock.lastCall()![1]!.body!.toString());
311
+ let result = JSON.parse(lastCallBody(fetchMock));
286
312
  expect(Object.keys(result).length).toBe(11);
287
313
  });
288
314
 
@@ -292,7 +318,7 @@ describe('simpleanalytics', () => {
292
318
  await coveoua('send', 'pageview');
293
319
 
294
320
  expect(fetchMock.calls().length).toBe(1);
295
- let result = JSON.parse(fetchMock.lastCall()![1]!.body!.toString());
321
+ let result = JSON.parse(lastCallBody(fetchMock));
296
322
  expect(Object.keys(result).length).toBe(11);
297
323
  });
298
324
 
@@ -302,7 +328,7 @@ describe('simpleanalytics', () => {
302
328
  await coveoua('send', 'view', {somedata: 'something'});
303
329
 
304
330
  expect(fetchMock.calls().length).toBe(1);
305
- let result = JSON.parse(fetchMock.lastCall()![1]!.body!.toString());
331
+ let result = JSON.parse(lastCallBody(fetchMock));
306
332
  expect(result).toHaveProperty('somedata', 'something');
307
333
  expect(result).toHaveProperty('customData.context_website', 'MY_WEBSITE');
308
334
  });
@@ -313,7 +339,7 @@ describe('simpleanalytics', () => {
313
339
  await coveoua('send', 'view', {somedata: 'something'});
314
340
 
315
341
  expect(fetchMock.calls().length).toBe(1);
316
- let result = JSON.parse(fetchMock.lastCall()![1]!.body!.toString());
342
+ let result = JSON.parse(lastCallBody(fetchMock));
317
343
  expect(result).toHaveProperty('somedata', 'something');
318
344
  expect(result).not.toHaveProperty('customData');
319
345
  });
@@ -324,7 +350,7 @@ describe('simpleanalytics', () => {
324
350
  await coveoua('send', 'view', {somedata: 'something'});
325
351
 
326
352
  expect(fetchMock.calls().length).toBe(1);
327
- let result = JSON.parse(fetchMock.lastCall()![1]!.body!.toString());
353
+ let result = JSON.parse(lastCallBody(fetchMock));
328
354
  expect(result).toHaveProperty('somedata', 'something');
329
355
  expect(result).not.toHaveProperty('customData');
330
356
  });
@@ -335,7 +361,7 @@ describe('simpleanalytics', () => {
335
361
  await coveoua('send', 'view', {somedata: 'something'});
336
362
 
337
363
  expect(fetchMock.calls().length).toBe(1);
338
- let result = JSON.parse(fetchMock.lastCall()![1]!.body!.toString());
364
+ let result = JSON.parse(lastCallBody(fetchMock));
339
365
  expect(result).toHaveProperty('somedata', 'something');
340
366
  expect(result).not.toHaveProperty('customData');
341
367
  });
@@ -346,7 +372,7 @@ describe('simpleanalytics', () => {
346
372
  await coveoua('send', 'view', {somedata: 'something'});
347
373
 
348
374
  expect(fetchMock.calls().length).toBe(1);
349
- let result = JSON.parse(fetchMock.lastCall()![1]!.body!.toString());
375
+ let result = JSON.parse(lastCallBody(fetchMock));
350
376
  expect(result).toHaveProperty('somedata', 'something');
351
377
  expect(result).not.toHaveProperty('customData');
352
378
  });
@@ -357,7 +383,7 @@ describe('simpleanalytics', () => {
357
383
  await coveoua('send', 'view', {somedata: 'something', customData: {context_website: 'MY_OTHER_WEBSITE'}});
358
384
 
359
385
  expect(fetchMock.calls().length).toBe(1);
360
- let result = JSON.parse(fetchMock.lastCall()![1]!.body!.toString());
386
+ let result = JSON.parse(lastCallBody(fetchMock));
361
387
  expect(result).toHaveProperty('somedata', 'something');
362
388
  expect(result).toHaveProperty('customData.context_website', 'MY_OTHER_WEBSITE');
363
389
  });
@@ -471,7 +497,7 @@ describe('simpleanalytics', () => {
471
497
 
472
498
  expect(fetchMock.calls().length).toBe(1);
473
499
  expect(fetchMock.lastUrl()).toBe(`${analyticsEndpoint}/${someRandomEventName}`);
474
- expect(JSON.parse(fetchMock.lastCall()![1]!.body!.toString())).toEqual({});
500
+ expect(JSON.parse(lastCallBody(fetchMock))).toEqual({});
475
501
  });
476
502
  });
477
503
 
@@ -16,25 +16,25 @@ describe('doNotTrack', () => {
16
16
  if (hasNav) {
17
17
  Object.defineProperty(<any>navigator, 'globalPrivacyControl', {
18
18
  get() {
19
- return options.navigatorGlobalPrivacyControl;
19
+ return options!.navigatorGlobalPrivacyControl;
20
20
  },
21
21
  configurable: true,
22
22
  });
23
23
  Object.defineProperty(<any>navigator, 'doNotTrack', {
24
24
  get() {
25
- return options.navigatorDoNotTrack;
25
+ return options!.navigatorDoNotTrack;
26
26
  },
27
27
  configurable: true,
28
28
  });
29
29
  Object.defineProperty(<any>navigator, 'msDoNotTrack', {
30
30
  get() {
31
- return options.navigatorMsDoNotTrack;
31
+ return options!.navigatorMsDoNotTrack;
32
32
  },
33
33
  configurable: true,
34
34
  });
35
35
  Object.defineProperty(<any>window, 'doNotTrack', {
36
36
  get() {
37
- return options.windowDoNotTrack;
37
+ return options!.windowDoNotTrack;
38
38
  },
39
39
  configurable: true,
40
40
  });
@@ -1,4 +1,4 @@
1
- import {mockFetch} from '../../tests/fetchMock';
1
+ import {lastCallBody, mockFetch} from '../../tests/fetchMock';
2
2
  import CoveoAnalyticsClient from '../client/analytics';
3
3
  import {NoopAnalytics} from '../client/noopAnalytics';
4
4
  import {
@@ -77,9 +77,9 @@ describe('InsightClient', () => {
77
77
  });
78
78
 
79
79
  const expectMatchPayload = (actionCause: SearchPageEvents | InsightEvents, meta = {}) => {
80
- const [, {body}] = fetchMock.lastCall();
80
+ const body: string = lastCallBody(fetchMock);
81
81
  const customData = {foo: 'bar', ...meta};
82
- expect(JSON.parse(body.toString())).toMatchObject({
82
+ expect(JSON.parse(body)).toMatchObject({
83
83
  queryText: 'queryText',
84
84
  responseTime: 123,
85
85
  queryPipeline: 'my-pipeline',
@@ -93,9 +93,9 @@ describe('InsightClient', () => {
93
93
  };
94
94
 
95
95
  const expectMatchCustomEventPayload = (actionCause: SearchPageEvents | InsightEvents, meta = {}) => {
96
- const [, {body}] = fetchMock.lastCall();
96
+ const body: string = lastCallBody(fetchMock);
97
97
  const customData = {foo: 'bar', ...meta};
98
- expect(JSON.parse(body.toString())).toMatchObject({
98
+ expect(JSON.parse(body)).toMatchObject({
99
99
  eventValue: actionCause,
100
100
  eventType: CustomEventsTypes[actionCause],
101
101
  lastSearchQueryUid: 'my-uid',
@@ -107,7 +107,7 @@ describe('InsightClient', () => {
107
107
  };
108
108
 
109
109
  const expectMatchDocumentPayload = (actionCause: SearchPageEvents, doc: PartialDocumentInformation, meta = {}) => {
110
- const [, {body}] = fetchMock.lastCall();
110
+ const body: string = lastCallBody(fetchMock);
111
111
  const customData = {foo: 'bar', ...meta};
112
112
  expect(JSON.parse(body.toString())).toMatchObject({
113
113
  actionCause,
@@ -3,13 +3,12 @@ import {
3
3
  SearchPageEvents,
4
4
  PartialDocumentInformation,
5
5
  CustomEventsTypes,
6
- SmartSnippetFeedbackReason,
7
6
  OmniboxSuggestionsMetadata,
8
7
  StaticFilterToggleValueMetadata,
9
8
  } from './searchPageEvents';
10
9
  import CoveoAnalyticsClient from '../client/analytics';
11
10
  import {NoopAnalytics} from '../client/noopAnalytics';
12
- import {mockFetch} from '../../tests/fetchMock';
11
+ import {mockFetch, lastCallBody} from '../../tests/fetchMock';
13
12
  import doNotTrack from '../donottrack';
14
13
  jest.mock('../donottrack', () => {
15
14
  return {
@@ -114,9 +113,9 @@ describe('SearchPageClient', () => {
114
113
  });
115
114
 
116
115
  const expectMatchPayload = (actionCause: SearchPageEvents, meta = {}) => {
117
- const [, {body}] = fetchMock.lastCall();
116
+ const body: string = lastCallBody(fetchMock);
118
117
  const customData = {foo: 'bar', ...customDataFromMiddleware, ...meta};
119
- expect(JSON.parse(body.toString())).toEqual({
118
+ expect(JSON.parse(body)).toEqual({
120
119
  queryText: 'queryText',
121
120
  responseTime: 123,
122
121
  searchQueryUid: provider.getSearchUID(),
@@ -142,9 +141,9 @@ describe('SearchPageClient', () => {
142
141
  };
143
142
 
144
143
  const expectMatchDocumentPayload = (actionCause: SearchPageEvents, doc: PartialDocumentInformation, meta = {}) => {
145
- const [, {body}] = fetchMock.lastCall();
144
+ const body: string = lastCallBody(fetchMock);
146
145
  const customData = {foo: 'bar', ...customDataFromMiddleware, ...meta};
147
- expect(JSON.parse(body.toString())).toEqual({
146
+ expect(JSON.parse(body)).toEqual({
148
147
  anonymous: false,
149
148
  actionCause,
150
149
  customData,
@@ -164,9 +163,9 @@ describe('SearchPageClient', () => {
164
163
  meta = {},
165
164
  eventType = CustomEventsTypes[actionCause]
166
165
  ) => {
167
- const [, {body}] = fetchMock.lastCall();
166
+ const body: string = lastCallBody(fetchMock);
168
167
  const customData = {foo: 'bar', ...customDataFromMiddleware, ...meta};
169
- expect(JSON.parse(body.toString())).toEqual({
168
+ expect(JSON.parse(body)).toEqual({
170
169
  anonymous: false,
171
170
  eventValue: actionCause,
172
171
  eventType,
@@ -182,9 +181,9 @@ describe('SearchPageClient', () => {
182
181
  };
183
182
 
184
183
  const expectMatchCustomEventWithTypePayload = (eventValue: string, eventType: string, meta = {}) => {
185
- const [, {body}] = fetchMock.lastCall();
184
+ const body: string = lastCallBody(fetchMock);
186
185
  const customData = {foo: 'bar', ...customDataFromMiddleware, ...meta};
187
- expect(JSON.parse(body.toString())).toEqual({
186
+ expect(JSON.parse(body)).toEqual({
188
187
  anonymous: false,
189
188
  eventValue,
190
189
  eventType,