coveo.analytics 2.20.16 → 2.20.17

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.
@@ -21,6 +21,7 @@ export declare type EventTypeConfig = {
21
21
  newEventType: EventType;
22
22
  variableLengthArgumentsNames?: string[];
23
23
  addVisitorIdParameter?: boolean;
24
+ addClientIdParameter?: boolean;
24
25
  usesMeasurementProtocol?: boolean;
25
26
  };
26
27
  export interface AnalyticsClient {
@@ -38,6 +39,7 @@ export interface AnalyticsClient {
38
39
  addEventTypeMapping(eventType: string, eventConfig: EventTypeConfig): void;
39
40
  runtime: IRuntimeEnvironment;
40
41
  readonly currentVisitorId: string;
42
+ getCurrentVisitorId?(): Promise<string>;
41
43
  }
42
44
  export interface BufferedRequest {
43
45
  eventType: EventType;
@@ -879,13 +879,19 @@ class CoveoAnalyticsClient {
879
879
  }
880
880
  resolveParameters(eventType, ...payload) {
881
881
  return __awaiter(this, void 0, void 0, function* () {
882
- const { variableLengthArgumentsNames = [], addVisitorIdParameter = false, usesMeasurementProtocol = false } = this.eventTypeMapping[eventType] || {};
882
+ const { variableLengthArgumentsNames = [], addVisitorIdParameter = false, usesMeasurementProtocol = false, addClientIdParameter = false, } = this.eventTypeMapping[eventType] || {};
883
883
  const processVariableArgumentNamesStep = (currentPayload) => variableLengthArgumentsNames.length > 0
884
884
  ? this.parseVariableArgumentsPayload(variableLengthArgumentsNames, currentPayload)
885
885
  : currentPayload[0];
886
886
  const addVisitorIdStep = (currentPayload) => __awaiter(this, void 0, void 0, function* () {
887
887
  return (Object.assign(Object.assign({}, currentPayload), { visitorId: addVisitorIdParameter ? yield this.getCurrentVisitorId() : '' }));
888
888
  });
889
+ const addClientIdStep = (currentPayload) => __awaiter(this, void 0, void 0, function* () {
890
+ if (addClientIdParameter) {
891
+ return Object.assign(Object.assign({}, currentPayload), { clientId: yield this.getCurrentVisitorId() });
892
+ }
893
+ return currentPayload;
894
+ });
889
895
  const setAnonymousUserStep = (currentPayload) => usesMeasurementProtocol ? this.ensureAnonymousUserWhenUsingApiKey(currentPayload) : currentPayload;
890
896
  const processBeforeSendHooksStep = (currentPayload) => this.beforeSendHooks.reduce((promisePayload, current) => __awaiter(this, void 0, void 0, function* () {
891
897
  const payload = yield promisePayload;
@@ -894,6 +900,7 @@ class CoveoAnalyticsClient {
894
900
  const parametersToSend = yield [
895
901
  processVariableArgumentNamesStep,
896
902
  addVisitorIdStep,
903
+ addClientIdStep,
897
904
  setAnonymousUserStep,
898
905
  processBeforeSendHooksStep,
899
906
  ].reduce((payloadPromise, step) => __awaiter(this, void 0, void 0, function* () {
package/dist/library.js CHANGED
@@ -82696,12 +82696,12 @@ var CoveoAnalyticsClient = (function () {
82696
82696
  payload[_i - 1] = arguments[_i];
82697
82697
  }
82698
82698
  return __awaiter(this, void 0, void 0, function () {
82699
- var _a, _b, variableLengthArgumentsNames, _c, addVisitorIdParameter, _d, usesMeasurementProtocol, processVariableArgumentNamesStep, addVisitorIdStep, setAnonymousUserStep, processBeforeSendHooksStep, parametersToSend;
82699
+ var _a, _b, variableLengthArgumentsNames, _c, addVisitorIdParameter, _d, usesMeasurementProtocol, _e, addClientIdParameter, processVariableArgumentNamesStep, addVisitorIdStep, addClientIdStep, setAnonymousUserStep, processBeforeSendHooksStep, parametersToSend;
82700
82700
  var _this = this;
82701
- return __generator(this, function (_e) {
82702
- switch (_e.label) {
82701
+ return __generator(this, function (_f) {
82702
+ switch (_f.label) {
82703
82703
  case 0:
82704
- _a = this.eventTypeMapping[eventType] || {}, _b = _a.variableLengthArgumentsNames, variableLengthArgumentsNames = _b === void 0 ? [] : _b, _c = _a.addVisitorIdParameter, addVisitorIdParameter = _c === void 0 ? false : _c, _d = _a.usesMeasurementProtocol, usesMeasurementProtocol = _d === void 0 ? false : _d;
82704
+ _a = this.eventTypeMapping[eventType] || {}, _b = _a.variableLengthArgumentsNames, variableLengthArgumentsNames = _b === void 0 ? [] : _b, _c = _a.addVisitorIdParameter, addVisitorIdParameter = _c === void 0 ? false : _c, _d = _a.usesMeasurementProtocol, usesMeasurementProtocol = _d === void 0 ? false : _d, _e = _a.addClientIdParameter, addClientIdParameter = _e === void 0 ? false : _e;
82705
82705
  processVariableArgumentNamesStep = function (currentPayload) {
82706
82706
  return variableLengthArgumentsNames.length > 0
82707
82707
  ? _this.parseVariableArgumentsPayload(variableLengthArgumentsNames, currentPayload)
@@ -82726,6 +82726,20 @@ var CoveoAnalyticsClient = (function () {
82726
82726
  }
82727
82727
  });
82728
82728
  }); };
82729
+ addClientIdStep = function (currentPayload) { return __awaiter(_this, void 0, void 0, function () {
82730
+ var _a, _b;
82731
+ return __generator(this, function (_c) {
82732
+ switch (_c.label) {
82733
+ case 0:
82734
+ if (!addClientIdParameter) return [3, 2];
82735
+ _a = [__assign({}, currentPayload)];
82736
+ _b = {};
82737
+ return [4, this.getCurrentVisitorId()];
82738
+ case 1: return [2, __assign.apply(void 0, _a.concat([(_b.clientId = _c.sent(), _b)]))];
82739
+ case 2: return [2, currentPayload];
82740
+ }
82741
+ });
82742
+ }); };
82729
82743
  setAnonymousUserStep = function (currentPayload) {
82730
82744
  return usesMeasurementProtocol ? _this.ensureAnonymousUserWhenUsingApiKey(currentPayload) : currentPayload;
82731
82745
  };
@@ -82746,6 +82760,7 @@ var CoveoAnalyticsClient = (function () {
82746
82760
  return [4, [
82747
82761
  processVariableArgumentNamesStep,
82748
82762
  addVisitorIdStep,
82763
+ addClientIdStep,
82749
82764
  setAnonymousUserStep,
82750
82765
  processBeforeSendHooksStep,
82751
82766
  ].reduce(function (payloadPromise, step) { return __awaiter(_this, void 0, void 0, function () {
@@ -82761,7 +82776,7 @@ var CoveoAnalyticsClient = (function () {
82761
82776
  });
82762
82777
  }); }, Promise.resolve(payload))];
82763
82778
  case 1:
82764
- parametersToSend = _e.sent();
82779
+ parametersToSend = _f.sent();
82765
82780
  return [2, parametersToSend];
82766
82781
  }
82767
82782
  });
@@ -83397,6 +83412,7 @@ var CoveoUA = (function () {
83397
83412
  return _this.plugins.require(pluginKey, pluginOptions_1);
83398
83413
  });
83399
83414
  this.client.registerBeforeSendEventHook(function (eventType, payload) { return (__assign(__assign({}, payload), _this.params)); });
83415
+ this.client.addEventTypeMapping(EventType.view, { newEventType: EventType.view, addClientIdParameter: true });
83400
83416
  }
83401
83417
  else {
83402
83418
  throw new Error("You must pass either your token or a valid object when you call 'init'");
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "coveo.analytics",
3
- "version": "2.20.16",
3
+ "version": "2.20.17",
4
4
  "description": "📈 Coveo analytics client (node and browser compatible) ",
5
5
  "main": "dist/library.js",
6
6
  "module": "dist/library.es.js",
@@ -54,6 +54,7 @@ export type EventTypeConfig = {
54
54
  newEventType: EventType;
55
55
  variableLengthArgumentsNames?: string[];
56
56
  addVisitorIdParameter?: boolean;
57
+ addClientIdParameter?: boolean;
57
58
  usesMeasurementProtocol?: boolean;
58
59
  };
59
60
 
@@ -75,6 +76,7 @@ export interface AnalyticsClient {
75
76
  * @deprecated
76
77
  */
77
78
  readonly currentVisitorId: string;
79
+ getCurrentVisitorId?(): Promise<string>; // TODO: v3 make required
78
80
  }
79
81
 
80
82
  export interface BufferedRequest {
@@ -205,8 +207,12 @@ export class CoveoAnalyticsClient implements AnalyticsClient, VisitorIdProvider
205
207
  }
206
208
 
207
209
  async resolveParameters(eventType: EventType | string, ...payload: VariableArgumentsPayload) {
208
- const {variableLengthArgumentsNames = [], addVisitorIdParameter = false, usesMeasurementProtocol = false} =
209
- this.eventTypeMapping[eventType] || {};
210
+ const {
211
+ variableLengthArgumentsNames = [],
212
+ addVisitorIdParameter = false,
213
+ usesMeasurementProtocol = false,
214
+ addClientIdParameter = false,
215
+ } = this.eventTypeMapping[eventType] || {};
210
216
 
211
217
  const processVariableArgumentNamesStep: ProcessPayloadStep = (currentPayload) =>
212
218
  variableLengthArgumentsNames.length > 0
@@ -216,6 +222,15 @@ export class CoveoAnalyticsClient implements AnalyticsClient, VisitorIdProvider
216
222
  ...currentPayload,
217
223
  visitorId: addVisitorIdParameter ? await this.getCurrentVisitorId() : '',
218
224
  });
225
+ const addClientIdStep: AsyncProcessPayloadStep = async (currentPayload) => {
226
+ if (addClientIdParameter) {
227
+ return {
228
+ ...currentPayload,
229
+ clientId: await this.getCurrentVisitorId(),
230
+ };
231
+ }
232
+ return currentPayload;
233
+ };
219
234
  const setAnonymousUserStep: ProcessPayloadStep = (currentPayload) =>
220
235
  usesMeasurementProtocol ? this.ensureAnonymousUserWhenUsingApiKey(currentPayload) : currentPayload;
221
236
  const processBeforeSendHooksStep: AsyncProcessPayloadStep = (currentPayload) =>
@@ -227,6 +242,7 @@ export class CoveoAnalyticsClient implements AnalyticsClient, VisitorIdProvider
227
242
  const parametersToSend = await [
228
243
  processVariableArgumentNamesStep,
229
244
  addVisitorIdStep,
245
+ addClientIdStep,
230
246
  setAnonymousUserStep,
231
247
  processBeforeSendHooksStep,
232
248
  ].reduce(async (payloadPromise, step) => {
@@ -1,11 +1,12 @@
1
1
  import {handleOneAnalyticsEvent} from './simpleanalytics';
2
- import {createAnalyticsClientMock} from '../../tests/analyticsClientMock';
2
+ import {createAnalyticsClientMock, visitorIdMock} from '../../tests/analyticsClientMock';
3
3
  import {EC} from '../plugins/ec';
4
4
  import {SVC} from '../plugins/svc';
5
5
  import {TestPlugin} from '../../tests/pluginMock';
6
6
  import {uuidv4} from '../client/crypto';
7
7
  import {PluginOptions} from '../plugins/BasePlugin';
8
8
  import {mockFetch} from '../../tests/fetchMock';
9
+ import {CookieStorage} from '../storage';
9
10
 
10
11
  jest.mock('../plugins/svc', () => {
11
12
  const SVC = jest.fn().mockImplementation(() => {});
@@ -22,6 +23,11 @@ jest.mock('../plugins/ec', () => {
22
23
  };
23
24
  });
24
25
 
26
+ const uuidv4Mock = jest.fn();
27
+ jest.mock('../client/crypto', () => ({
28
+ uuidv4: () => uuidv4Mock(),
29
+ }));
30
+
25
31
  const {fetchMock, fetchMockBeforeEach} = mockFetch();
26
32
 
27
33
  class TestPluginWithSpy extends TestPlugin {
@@ -46,7 +52,10 @@ describe('simpleanalytics', () => {
46
52
  jest.clearAllMocks();
47
53
  fetchMockBeforeEach();
48
54
 
55
+ new CookieStorage().removeItem('visitorId');
56
+ localStorage.clear();
49
57
  fetchMock.mock('*', {});
58
+ uuidv4Mock.mockImplementationOnce(() => visitorIdMock);
50
59
  handleOneAnalyticsEvent('reset');
51
60
  });
52
61
 
@@ -199,7 +208,16 @@ describe('simpleanalytics', () => {
199
208
 
200
209
  expect(fetchMock.calls().length).toBe(1);
201
210
  expect(fetchMock.lastUrl()).toBe(`${analyticsEndpoint}/pageview`);
202
- expect(JSON.parse(fetchMock.lastCall()[1].body.toString())).toEqual({somedata: 'asd'});
211
+ expect(JSON.parse(fetchMock.lastCall()![1]!.body!.toString())).toEqual({somedata: 'asd'});
212
+ });
213
+
214
+ it('can send view event with clientId', async () => {
215
+ handleOneAnalyticsEvent('init', 'MYTOKEN', {plugins: []});
216
+ await handleOneAnalyticsEvent('send', 'view');
217
+
218
+ expect(fetchMock.calls().length).toBe(1);
219
+ expect(fetchMock.lastUrl()).toBe(`${analyticsEndpoint}/view?visitor=${visitorIdMock}`);
220
+ expect(JSON.parse(fetchMock.lastCall()![1]!.body!.toString()).clientId).toBe(visitorIdMock);
203
221
  });
204
222
 
205
223
  it('can send any event to the endpoint', async () => {
@@ -227,7 +245,7 @@ describe('simpleanalytics', () => {
227
245
 
228
246
  expect(fetchMock.calls().length).toBe(1);
229
247
  expect(fetchMock.lastUrl()).toBe(`${analyticsEndpoint}/${someRandomEventName}`);
230
- expect(JSON.parse(fetchMock.lastCall()[1].body.toString())).toEqual({userId: 'something'});
248
+ expect(JSON.parse(fetchMock.lastCall()![1]!.body!.toString())).toEqual({userId: 'something'});
231
249
  });
232
250
 
233
251
  it('can set parameters using an object', async () => {
@@ -239,7 +257,7 @@ describe('simpleanalytics', () => {
239
257
 
240
258
  expect(fetchMock.calls().length).toBe(1);
241
259
  expect(fetchMock.lastUrl()).toBe(`${analyticsEndpoint}/${someRandomEventName}`);
242
- expect(JSON.parse(fetchMock.lastCall()[1].body.toString())).toEqual({userId: 'something'});
260
+ expect(JSON.parse(fetchMock.lastCall()![1]!.body!.toString())).toEqual({userId: 'something'});
243
261
  });
244
262
  });
245
263
 
@@ -360,7 +378,7 @@ describe('simpleanalytics', () => {
360
378
 
361
379
  expect(fetchMock.calls().length).toBe(1);
362
380
  expect(fetchMock.lastUrl()).toBe(`${analyticsEndpoint}/${someRandomEventName}`);
363
- expect(JSON.parse(fetchMock.lastCall()[1].body.toString())).toEqual({});
381
+ expect(JSON.parse(fetchMock.lastCall()![1]!.body!.toString())).toEqual({});
364
382
  });
365
383
  });
366
384
 
@@ -1,4 +1,4 @@
1
- import {AnyEventResponse, SendEventArguments, VariableArgumentsPayload} from '../events';
1
+ import {AnyEventResponse, EventType, SendEventArguments, VariableArgumentsPayload} from '../events';
2
2
  import {AnalyticsClient, CoveoAnalyticsClient, Endpoints} from '../client/analytics';
3
3
  import {Plugins} from './plugins';
4
4
  import {PluginOptions} from '../plugins/BasePlugin';
@@ -45,6 +45,7 @@ export class CoveoUA {
45
45
  ...payload,
46
46
  ...this.params,
47
47
  }));
48
+ this.client.addEventTypeMapping(EventType.view, {newEventType: EventType.view, addClientIdParameter: true});
48
49
  } else {
49
50
  throw new Error(`You must pass either your token or a valid object when you call 'init'`);
50
51
  }