coveo.analytics 2.30.56 → 2.32.0

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.
Files changed (84) hide show
  1. package/README.md +37 -43
  2. package/dist/browser.mjs +513 -357
  3. package/dist/coveoua.browser.js +1 -1
  4. package/dist/coveoua.browser.js.map +1 -1
  5. package/dist/coveoua.debug.js +542 -359
  6. package/dist/coveoua.debug.js.map +1 -1
  7. package/dist/coveoua.js +1 -1
  8. package/dist/coveoua.js.map +1 -1
  9. package/dist/definitions/client/analytics.d.ts +3 -2
  10. package/dist/definitions/client/runtimeEnvironment.d.ts +1 -1
  11. package/dist/definitions/donottrack.d.ts +1 -0
  12. package/dist/definitions/react-native/index.d.ts +1 -2
  13. package/dist/definitions/version.d.ts +1 -1
  14. package/dist/library.cjs +15350 -3699
  15. package/dist/library.es.js +513 -357
  16. package/dist/library.js +15350 -3699
  17. package/dist/library.mjs +15350 -3699
  18. package/dist/react-native.es.js +634 -460
  19. package/modules/package.json +9 -9
  20. package/package.json +60 -91
  21. package/react-native/package.json +7 -7
  22. package/src/caseAssist/caseAssistActions.ts +51 -50
  23. package/src/caseAssist/caseAssistClient.spec.ts +328 -297
  24. package/src/caseAssist/caseAssistClient.ts +201 -185
  25. package/src/client/analytics.spec.ts +724 -703
  26. package/src/client/analytics.ts +677 -602
  27. package/src/client/analyticsBeaconClient.spec.ts +195 -188
  28. package/src/client/analyticsBeaconClient.ts +99 -88
  29. package/src/client/analyticsFetchClient.ts +77 -61
  30. package/src/client/analyticsRequestClient.ts +17 -17
  31. package/src/client/location.ts +3 -3
  32. package/src/client/measurementProtocolMapper.ts +54 -45
  33. package/src/client/measurementProtocolMapping/baseMeasurementProtocolMapper.ts +48 -44
  34. package/src/client/measurementProtocolMapping/commerceMeasurementProtocolMapper.ts +133 -121
  35. package/src/client/measurementProtocolMapping/serviceMeasurementProtocolMapper.ts +17 -17
  36. package/src/client/noopAnalytics.ts +80 -68
  37. package/src/client/runtimeEnvironment.ts +50 -41
  38. package/src/client/utils.spec.ts +112 -97
  39. package/src/client/utils.ts +39 -39
  40. package/src/cookieutils.spec.ts +59 -0
  41. package/src/cookieutils.ts +40 -39
  42. package/src/coveoua/browser.ts +14 -12
  43. package/src/coveoua/library.ts +4 -4
  44. package/src/coveoua/plugins.ts +36 -34
  45. package/src/coveoua/simpleanalytics.spec.ts +467 -452
  46. package/src/coveoua/simpleanalytics.ts +146 -140
  47. package/src/detector.ts +17 -17
  48. package/src/donottrack.spec.ts +199 -121
  49. package/src/donottrack.ts +31 -8
  50. package/src/events.ts +86 -79
  51. package/src/formatting/format-array-for-coveo-custom-data.spec.ts +13 -12
  52. package/src/formatting/format-array-for-coveo-custom-data.ts +18 -18
  53. package/src/formatting/format-omnibox-metadata.ts +16 -12
  54. package/src/history.spec.ts +197 -195
  55. package/src/history.ts +143 -133
  56. package/src/hook/addDefaultValues.ts +13 -8
  57. package/src/hook/enhanceViewEvent.ts +17 -17
  58. package/src/insight/insightClient.spec.ts +1848 -1717
  59. package/src/insight/insightClient.ts +866 -761
  60. package/src/insight/insightEvents.ts +57 -56
  61. package/src/plugins/BasePlugin.ts +125 -121
  62. package/src/plugins/ec.spec.ts +457 -429
  63. package/src/plugins/ec.ts +202 -199
  64. package/src/plugins/link.spec.ts +183 -159
  65. package/src/plugins/link.ts +88 -85
  66. package/src/plugins/svc.spec.ts +161 -155
  67. package/src/plugins/svc.ts +93 -91
  68. package/src/react-native/index.ts +8 -1
  69. package/src/react-native/react-native-runtime.ts +33 -33
  70. package/src/react-native/react-native-utils.ts +1 -1
  71. package/src/react-native/react-native.spec.ts +31 -21
  72. package/src/searchPage/searchPageClient.spec.ts +1944 -1794
  73. package/src/searchPage/searchPageClient.ts +1261 -1040
  74. package/src/searchPage/searchPageEvents.ts +524 -511
  75. package/src/storage.spec.ts +51 -51
  76. package/src/storage.ts +47 -47
  77. package/dist/definitions/bundle/browser-fetch.d.ts +0 -1
  78. package/dist/definitions/src/coveoua/browser.d.ts +0 -6
  79. package/dist/definitions/src/coveoua/headless.d.ts +0 -6
  80. package/dist/definitions/src/coveoua/library.d.ts +0 -17
  81. package/dist/definitions/src/coveoua/plugins.d.ts +0 -10
  82. package/dist/definitions/src/coveoua/simpleanalytics.d.ts +0 -33
  83. package/dist/definitions/src/react-native/index.d.ts +0 -3
  84. package/dist/definitions/src/react-native/react-native-runtime.d.ts +0 -19
package/src/plugins/ec.ts CHANGED
@@ -1,241 +1,244 @@
1
1
  import {EventType} from '../events';
2
2
  import {v4 as uuidv4} from 'uuid';
3
3
  import {
4
- convertProductToMeasurementProtocol,
5
- convertImpressionListToMeasurementProtocol,
4
+ convertProductToMeasurementProtocol,
5
+ convertImpressionListToMeasurementProtocol,
6
6
  } from '../client/measurementProtocolMapping/commerceMeasurementProtocolMapper';
7
7
  import {BasePlugin, BasePluginEventTypes, PluginClass, PluginOptions} from './BasePlugin';
8
8
  import {coerceToNumber} from '../client/utils';
9
9
 
10
10
  export const ECPluginEventTypes = {
11
- ...BasePluginEventTypes,
11
+ ...BasePluginEventTypes,
12
12
  };
13
13
 
14
14
  const allECEventTypes = Object.keys(ECPluginEventTypes).map(
15
- (key) => ECPluginEventTypes[key as keyof typeof ECPluginEventTypes],
15
+ (key) => ECPluginEventTypes[key as keyof typeof ECPluginEventTypes]
16
16
  );
17
17
 
18
18
  // From https://stackoverflow.com/a/49725198/497731
19
19
  type RequireAtLeastOne<T, Keys extends keyof T = keyof T> = Pick<T, Exclude<keyof T, Keys>> &
20
- {
21
- [K in Keys]-?: Required<Pick<T, K>> & Partial<Pick<T, Exclude<Keys, K>>>;
22
- }[Keys];
20
+ {
21
+ [K in Keys]-?: Required<Pick<T, K>> & Partial<Pick<T, Exclude<Keys, K>>>;
22
+ }[Keys];
23
23
 
24
24
  export type CustomValues = {
25
- [key: string]: string | number | boolean;
25
+ [key: string]: string | number | boolean;
26
26
  };
27
27
 
28
28
  export interface CoveoExtensionProperties {
29
- group?: string;
29
+ group?: string;
30
30
  }
31
31
 
32
32
  export interface ProductProperties extends CoveoExtensionProperties {
33
- id?: string;
34
- name?: string;
35
- brand?: string;
36
- category?: string;
37
- variant?: string;
38
- price?: number;
39
- quantity?: number;
40
- coupon?: string;
41
- position?: number;
42
- custom?: CustomValues;
33
+ id?: string;
34
+ name?: string;
35
+ brand?: string;
36
+ category?: string;
37
+ variant?: string;
38
+ price?: number;
39
+ quantity?: number;
40
+ coupon?: string;
41
+ position?: number;
42
+ custom?: CustomValues;
43
43
  }
44
44
 
45
45
  export type Product = RequireAtLeastOne<ProductProperties, 'id' | 'name'>;
46
46
 
47
47
  export interface ImpressionProperties extends CoveoExtensionProperties {
48
- id?: string;
49
- name?: string;
50
- list?: string;
51
- brand?: string;
52
- category?: string;
53
- variant?: string;
54
- position?: number;
55
- price?: number;
56
- custom?: CustomValues;
48
+ id?: string;
49
+ name?: string;
50
+ list?: string;
51
+ brand?: string;
52
+ category?: string;
53
+ variant?: string;
54
+ position?: number;
55
+ price?: number;
56
+ custom?: CustomValues;
57
57
  }
58
58
 
59
59
  export type Impression = RequireAtLeastOne<ImpressionProperties, 'id' | 'name'>;
60
60
  export type BaseImpression = Omit<Impression, 'list'>;
61
61
  export interface ImpressionList {
62
- listName?: string;
63
- impressions: BaseImpression[];
62
+ listName?: string;
63
+ impressions: BaseImpression[];
64
64
  }
65
65
 
66
66
  export class ECPlugin extends BasePlugin {
67
- public static readonly Id = 'ec';
68
- private products: Product[] = [];
69
- private impressions: Impression[] = [];
70
-
71
- constructor({client, uuidGenerator = uuidv4}: PluginOptions) {
72
- super({client, uuidGenerator});
73
- }
74
-
75
- public getApi(name: string): Function | null {
76
- const superCall: Function | null = super.getApi(name);
77
- if (superCall !== null) return superCall;
78
- switch (name) {
79
- case 'addProduct':
80
- return this.addProduct;
81
- case 'addImpression':
82
- return this.addImpression;
83
- default:
84
- return null;
85
- }
86
- }
87
-
88
- protected addHooks(): void {
89
- this.addHooksForPageView();
90
- this.addHooksForEvent();
91
- this.addHooksForECEvents();
92
- }
93
-
94
- addProduct(product: Product) {
95
- this.products.push(product);
96
- }
97
-
98
- addImpression(impression: Impression) {
99
- this.impressions.push(impression);
100
- }
101
-
102
- protected clearPluginData() {
103
- this.products = [];
104
- this.impressions = [];
105
- }
106
-
107
- private addHooksForECEvents() {
108
- this.client.registerBeforeSendEventHook((eventType, ...[payload]) => {
109
- return allECEventTypes.indexOf(eventType) !== -1 ? this.addECDataToPayload(eventType, payload) : payload;
110
- });
111
- this.client.registerAfterSendEventHook((eventType, ...[payload]) => {
112
- if (allECEventTypes.indexOf(eventType) !== -1) {
113
- this.updateLocationInformation(eventType, payload);
114
- }
115
- return payload;
116
- });
117
- }
118
-
119
- private addHooksForPageView() {
120
- this.client.addEventTypeMapping(ECPluginEventTypes.pageview, {
121
- newEventType: EventType.collect,
122
- variableLengthArgumentsNames: ['page'],
123
- addVisitorIdParameter: true,
124
- usesMeasurementProtocol: true,
125
- });
126
- }
127
-
128
- private addHooksForEvent() {
129
- this.client.addEventTypeMapping(ECPluginEventTypes.event, {
130
- newEventType: EventType.collect,
131
- variableLengthArgumentsNames: ['eventCategory', 'eventAction', 'eventLabel', 'eventValue'],
132
- addVisitorIdParameter: true,
133
- usesMeasurementProtocol: true,
134
- });
135
- }
136
-
137
- private addECDataToPayload(eventType: string, payload: any) {
138
- const ecPayload = {
139
- ...this.getLocationInformation(eventType, payload),
140
- ...this.getDefaultContextInformation(eventType),
141
- ...(this.action ? {action: this.action} : {}),
142
- ...(this.actionData || {}),
67
+ public static readonly Id = 'ec';
68
+ private products: Product[] = [];
69
+ private impressions: Impression[] = [];
70
+
71
+ constructor({client, uuidGenerator = uuidv4}: PluginOptions) {
72
+ super({client, uuidGenerator});
73
+ }
74
+
75
+ public getApi(name: string): Function | null {
76
+ const superCall: Function | null = super.getApi(name);
77
+ if (superCall !== null) return superCall;
78
+ switch (name) {
79
+ case 'addProduct':
80
+ return this.addProduct;
81
+ case 'addImpression':
82
+ return this.addImpression;
83
+ default:
84
+ return null;
85
+ }
86
+ }
87
+
88
+ protected addHooks(): void {
89
+ this.addHooksForPageView();
90
+ this.addHooksForEvent();
91
+ this.addHooksForECEvents();
92
+ }
93
+
94
+ addProduct(product: Product) {
95
+ this.products.push(product);
96
+ }
97
+
98
+ addImpression(impression: Impression) {
99
+ this.impressions.push(impression);
100
+ }
101
+
102
+ protected clearPluginData() {
103
+ this.products = [];
104
+ this.impressions = [];
105
+ }
106
+
107
+ private addHooksForECEvents() {
108
+ this.client.registerBeforeSendEventHook((eventType, ...[payload]) => {
109
+ return allECEventTypes.indexOf(eventType) !== -1
110
+ ? this.addECDataToPayload(eventType, payload)
111
+ : payload;
112
+ });
113
+ this.client.registerAfterSendEventHook((eventType, ...[payload]) => {
114
+ if (allECEventTypes.indexOf(eventType) !== -1) {
115
+ this.updateLocationInformation(eventType, payload);
116
+ }
117
+ return payload;
118
+ });
119
+ }
120
+
121
+ private addHooksForPageView() {
122
+ this.client.addEventTypeMapping(ECPluginEventTypes.pageview, {
123
+ newEventType: EventType.collect,
124
+ variableLengthArgumentsNames: ['page'],
125
+ addVisitorIdParameter: true,
126
+ usesMeasurementProtocol: true,
127
+ });
128
+ }
129
+
130
+ private addHooksForEvent() {
131
+ this.client.addEventTypeMapping(ECPluginEventTypes.event, {
132
+ newEventType: EventType.collect,
133
+ variableLengthArgumentsNames: ['eventCategory', 'eventAction', 'eventLabel', 'eventValue'],
134
+ addVisitorIdParameter: true,
135
+ usesMeasurementProtocol: true,
136
+ });
137
+ }
138
+
139
+ private addECDataToPayload(eventType: string, payload: any) {
140
+ const ecPayload = {
141
+ ...this.getLocationInformation(eventType, payload),
142
+ ...this.getDefaultContextInformation(eventType),
143
+ ...(this.action ? {action: this.action} : {}),
144
+ ...(this.actionData || {}),
145
+ };
146
+
147
+ const productPayload = this.getProductPayload();
148
+ const impressionPayload = this.getImpressionPayload();
149
+
150
+ this.clearData();
151
+
152
+ return {
153
+ ...impressionPayload,
154
+ ...productPayload,
155
+ ...ecPayload,
156
+ ...payload,
157
+ };
158
+ }
159
+
160
+ private getProductPayload() {
161
+ return this.products
162
+ .map((product) => this.assureProductValidity(product))
163
+ .reduce((newPayload, product, index) => {
164
+ return {
165
+ ...newPayload,
166
+ ...convertProductToMeasurementProtocol(this.convertNumberTypes(product), index),
143
167
  };
144
-
145
- const productPayload = this.getProductPayload();
146
- const impressionPayload = this.getImpressionPayload();
147
-
148
- this.clearData();
149
-
168
+ }, {});
169
+ }
170
+
171
+ private convertNumberTypes(product: Product): Product {
172
+ let updatedProduct: Product = {...product};
173
+ if ('quantity' in updatedProduct) {
174
+ updatedProduct.quantity = coerceToNumber(updatedProduct.quantity);
175
+ }
176
+ if ('position' in updatedProduct) {
177
+ updatedProduct.position = coerceToNumber(updatedProduct.position);
178
+ }
179
+ return updatedProduct;
180
+ }
181
+
182
+ private getImpressionPayload() {
183
+ const impressionsByList = this.getImpressionsByList();
184
+ return impressionsByList
185
+ .map(
186
+ ({impressions, ...rest}) =>
187
+ ({
188
+ ...rest,
189
+ impressions: impressions.map((baseImpression) =>
190
+ this.assureBaseImpressionValidity(baseImpression)
191
+ ),
192
+ }) as ImpressionList
193
+ )
194
+ .reduce((newPayload, impressionList, index) => {
150
195
  return {
151
- ...impressionPayload,
152
- ...productPayload,
153
- ...ecPayload,
154
- ...payload,
196
+ ...newPayload,
197
+ ...convertImpressionListToMeasurementProtocol(impressionList, index, 'pi'),
155
198
  };
156
- }
157
-
158
- private getProductPayload() {
159
- return this.products
160
- .map((product) => this.assureProductValidity(product))
161
- .reduce((newPayload, product, index) => {
162
- return {
163
- ...newPayload,
164
- ...convertProductToMeasurementProtocol(this.convertNumberTypes(product), index),
165
- };
166
- }, {});
167
- }
168
-
169
- private convertNumberTypes(product: Product): Product {
170
- let updatedProduct: Product = {...product};
171
- if ('quantity' in updatedProduct) {
172
- updatedProduct.quantity = coerceToNumber(updatedProduct.quantity);
173
- }
174
- if ('position' in updatedProduct) {
175
- updatedProduct.position = coerceToNumber(updatedProduct.position);
176
- }
177
- return updatedProduct;
178
- }
179
-
180
- private getImpressionPayload() {
181
- const impressionsByList = this.getImpressionsByList();
182
- return impressionsByList
183
- .map(
184
- ({impressions, ...rest}) =>
185
- ({
186
- ...rest,
187
- impressions: impressions.map((baseImpression) =>
188
- this.assureBaseImpressionValidity(baseImpression),
189
- ),
190
- }) as ImpressionList,
191
- )
192
- .reduce((newPayload, impressionList, index) => {
193
- return {
194
- ...newPayload,
195
- ...convertImpressionListToMeasurementProtocol(impressionList, index, 'pi'),
196
- };
197
- }, {});
198
- }
199
-
200
- private assureProductValidity(product: Product) {
201
- const {position, ...productRest} = product;
202
- if (position !== undefined && position < 1) {
203
- console.warn(
204
- `The position for product '${product.name || product.id}' must be greater ` + `than 0 when provided.`,
205
- );
206
-
207
- return productRest;
208
- }
209
-
210
- return product;
211
- }
212
-
213
- private assureBaseImpressionValidity(baseImpression: BaseImpression) {
214
- const {position, ...baseImpressionRest} = baseImpression;
215
- if (position !== undefined && position < 1) {
216
- console.warn(
217
- `The position for impression '${baseImpression.name || baseImpression.id}'` +
218
- ` must be greater than 0 when provided.`,
219
- );
220
-
221
- return baseImpressionRest;
222
- }
223
-
224
- return baseImpression;
225
- }
226
-
227
- private getImpressionsByList() {
228
- return this.impressions.reduce((lists, impression) => {
229
- const {list: listName, ...baseImpression} = impression;
230
- const list = lists.find((list) => list.listName === listName);
231
- if (list) {
232
- list.impressions.push(baseImpression);
233
- } else {
234
- lists.push({listName: listName, impressions: [baseImpression]});
235
- }
236
- return lists;
237
- }, [] as ImpressionList[]);
238
- }
199
+ }, {});
200
+ }
201
+
202
+ private assureProductValidity(product: Product) {
203
+ const {position, ...productRest} = product;
204
+ if (position !== undefined && position < 1) {
205
+ console.warn(
206
+ `The position for product '${product.name || product.id}' must be greater ` +
207
+ `than 0 when provided.`
208
+ );
209
+
210
+ return productRest;
211
+ }
212
+
213
+ return product;
214
+ }
215
+
216
+ private assureBaseImpressionValidity(baseImpression: BaseImpression) {
217
+ const {position, ...baseImpressionRest} = baseImpression;
218
+ if (position !== undefined && position < 1) {
219
+ console.warn(
220
+ `The position for impression '${baseImpression.name || baseImpression.id}'` +
221
+ ` must be greater than 0 when provided.`
222
+ );
223
+
224
+ return baseImpressionRest;
225
+ }
226
+
227
+ return baseImpression;
228
+ }
229
+
230
+ private getImpressionsByList() {
231
+ return this.impressions.reduce((lists, impression) => {
232
+ const {list: listName, ...baseImpression} = impression;
233
+ const list = lists.find((list) => list.listName === listName);
234
+ if (list) {
235
+ list.impressions.push(baseImpression);
236
+ } else {
237
+ lists.push({listName: listName, impressions: [baseImpression]});
238
+ }
239
+ return lists;
240
+ }, [] as ImpressionList[]);
241
+ }
239
242
  }
240
243
 
241
244
  export const EC: PluginClass = ECPlugin;