coveo.analytics 2.30.55 → 2.31.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 (86) hide show
  1. package/README.md +37 -43
  2. package/dist/browser.mjs +525 -357
  3. package/dist/coveoua.browser.js +1 -1
  4. package/dist/coveoua.browser.js.map +1 -1
  5. package/dist/coveoua.debug.js +559 -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 -1
  13. package/dist/definitions/searchPage/searchPageClient.d.ts +3 -1
  14. package/dist/definitions/searchPage/searchPageEvents.d.ts +6 -0
  15. package/dist/definitions/version.d.ts +1 -1
  16. package/dist/library.cjs +15367 -3699
  17. package/dist/library.es.js +525 -357
  18. package/dist/library.js +15367 -3699
  19. package/dist/library.mjs +15367 -3699
  20. package/dist/react-native.es.js +646 -460
  21. package/modules/package.json +9 -9
  22. package/package.json +60 -91
  23. package/react-native/package.json +7 -7
  24. package/src/caseAssist/caseAssistActions.ts +51 -50
  25. package/src/caseAssist/caseAssistClient.spec.ts +328 -297
  26. package/src/caseAssist/caseAssistClient.ts +201 -185
  27. package/src/client/analytics.spec.ts +724 -703
  28. package/src/client/analytics.ts +677 -602
  29. package/src/client/analyticsBeaconClient.spec.ts +195 -188
  30. package/src/client/analyticsBeaconClient.ts +99 -88
  31. package/src/client/analyticsFetchClient.ts +77 -61
  32. package/src/client/analyticsRequestClient.ts +17 -17
  33. package/src/client/location.ts +3 -3
  34. package/src/client/measurementProtocolMapper.ts +54 -45
  35. package/src/client/measurementProtocolMapping/baseMeasurementProtocolMapper.ts +48 -44
  36. package/src/client/measurementProtocolMapping/commerceMeasurementProtocolMapper.ts +133 -121
  37. package/src/client/measurementProtocolMapping/serviceMeasurementProtocolMapper.ts +17 -17
  38. package/src/client/noopAnalytics.ts +80 -68
  39. package/src/client/runtimeEnvironment.ts +50 -41
  40. package/src/client/utils.spec.ts +112 -97
  41. package/src/client/utils.ts +39 -39
  42. package/src/cookieutils.spec.ts +59 -0
  43. package/src/cookieutils.ts +40 -39
  44. package/src/coveoua/browser.ts +14 -12
  45. package/src/coveoua/library.ts +4 -4
  46. package/src/coveoua/plugins.ts +36 -34
  47. package/src/coveoua/simpleanalytics.spec.ts +467 -452
  48. package/src/coveoua/simpleanalytics.ts +146 -140
  49. package/src/detector.ts +17 -17
  50. package/src/donottrack.spec.ts +199 -121
  51. package/src/donottrack.ts +31 -8
  52. package/src/events.ts +86 -79
  53. package/src/formatting/format-array-for-coveo-custom-data.spec.ts +13 -12
  54. package/src/formatting/format-array-for-coveo-custom-data.ts +18 -18
  55. package/src/formatting/format-omnibox-metadata.ts +16 -12
  56. package/src/history.spec.ts +197 -195
  57. package/src/history.ts +143 -133
  58. package/src/hook/addDefaultValues.ts +13 -8
  59. package/src/hook/enhanceViewEvent.ts +17 -17
  60. package/src/insight/insightClient.spec.ts +1848 -1717
  61. package/src/insight/insightClient.ts +866 -761
  62. package/src/insight/insightEvents.ts +57 -56
  63. package/src/plugins/BasePlugin.ts +125 -121
  64. package/src/plugins/ec.spec.ts +457 -429
  65. package/src/plugins/ec.ts +202 -199
  66. package/src/plugins/link.spec.ts +183 -159
  67. package/src/plugins/link.ts +88 -85
  68. package/src/plugins/svc.spec.ts +161 -155
  69. package/src/plugins/svc.ts +93 -91
  70. package/src/react-native/index.ts +5 -1
  71. package/src/react-native/react-native-runtime.ts +33 -33
  72. package/src/react-native/react-native-utils.ts +1 -1
  73. package/src/react-native/react-native.spec.ts +22 -21
  74. package/src/searchPage/searchPageClient.spec.ts +1944 -1766
  75. package/src/searchPage/searchPageClient.ts +1261 -1029
  76. package/src/searchPage/searchPageEvents.ts +529 -500
  77. package/src/storage.spec.ts +51 -51
  78. package/src/storage.ts +47 -47
  79. package/dist/definitions/bundle/browser-fetch.d.ts +0 -1
  80. package/dist/definitions/src/coveoua/browser.d.ts +0 -6
  81. package/dist/definitions/src/coveoua/headless.d.ts +0 -6
  82. package/dist/definitions/src/coveoua/library.d.ts +0 -17
  83. package/dist/definitions/src/coveoua/plugins.d.ts +0 -10
  84. package/dist/definitions/src/coveoua/simpleanalytics.d.ts +0 -33
  85. package/dist/definitions/src/react-native/index.d.ts +0 -3
  86. 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;