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
@@ -1,184 +1,190 @@
1
+ import {vi} from 'vitest';
1
2
  import {SVCPlugin, SVCPluginEventTypes} from './svc';
2
3
  import {createAnalyticsClientMock} from '../../tests/analyticsClientMock';
3
4
 
4
5
  describe('SVC plugin', () => {
5
- let svc: SVCPlugin;
6
- let client: ReturnType<typeof createAnalyticsClientMock>;
7
-
8
- const someUUIDGenerator = jest.fn(() => someUUID);
9
- const someUUID = '13ccebdb-0138-45e8-bf70-884817ead190';
10
- const defaultResult = {
11
- pageViewId: someUUID,
12
- encoding: document.characterSet,
13
- location: 'http://localhost/',
14
- referrer: 'http://somewhere.over/thereferrer',
15
- title: 'MAH PAGE',
16
- screenColor: '24-bit',
17
- screenResolution: '0x0',
18
- time: expect.any(Number),
19
- userAgent: navigator.userAgent,
20
- language: 'en-US',
21
- hitType: SVCPluginEventTypes.event,
22
- eventId: someUUID,
23
- };
24
-
25
- beforeEach(() => {
26
- jest.clearAllMocks();
27
- client = createAnalyticsClientMock();
28
- svc = new SVCPlugin({client, uuidGenerator: someUUIDGenerator as any});
6
+ let svc: SVCPlugin;
7
+ let client: ReturnType<typeof createAnalyticsClientMock>;
8
+
9
+ const someUUIDGenerator = vi.fn(() => someUUID);
10
+ const someUUID = '13ccebdb-0138-45e8-bf70-884817ead190';
11
+ const defaultResult = {
12
+ pageViewId: someUUID,
13
+ encoding: document.characterSet,
14
+ location: 'http://localhost/',
15
+ referrer: 'http://somewhere.over/thereferrer',
16
+ title: 'MAH PAGE',
17
+ screenColor: '24-bit',
18
+ screenResolution: '0x0',
19
+ time: expect.any(Number),
20
+ userAgent: navigator.userAgent,
21
+ language: 'en-US',
22
+ hitType: SVCPluginEventTypes.event,
23
+ eventId: someUUID,
24
+ };
25
+
26
+ beforeEach(() => {
27
+ vi.clearAllMocks();
28
+ client = createAnalyticsClientMock();
29
+ svc = new SVCPlugin({client, uuidGenerator: someUUIDGenerator as any});
30
+ });
31
+
32
+ it('should register a hook in the client', () => {
33
+ expect(client.registerBeforeSendEventHook).toHaveBeenCalledTimes(1);
34
+ });
35
+
36
+ it('should register a mapping for each SVC type', () => {
37
+ expect(client.addEventTypeMapping).toHaveBeenCalledTimes(
38
+ Object.keys(SVCPluginEventTypes).length
39
+ );
40
+ });
41
+
42
+ describe('tickets', () => {
43
+ it('should set the ticket with the specific format and convert known ticket keys into the measurement protocol format when the hook is called', () => {
44
+ svc.setTicket({
45
+ id: 'Some ID',
46
+ subject: 'Some Subject',
47
+ description: 'Some Description',
48
+ category: 'Some Category',
49
+ productId: 'Some Product ID',
50
+ custom: {someCustomProp: 'some custom prop value'},
51
+ });
52
+
53
+ const result = executeRegisteredHook(SVCPluginEventTypes.event, {});
54
+
55
+ expect(result).toEqual({
56
+ ...defaultResult,
57
+ svc_ticket_id: 'Some ID',
58
+ svc_ticket_subject: 'Some Subject',
59
+ svc_ticket_description: 'Some Description',
60
+ svc_ticket_category: 'Some Category',
61
+ svc_ticket_product_id: 'Some Product ID',
62
+ svc_ticket_custom: {someCustomProp: 'some custom prop value'},
63
+ });
29
64
  });
30
65
 
31
- it('should register a hook in the client', () => {
32
- expect(client.registerBeforeSendEventHook).toHaveBeenCalledTimes(1);
33
- });
66
+ it('should append the ticket with the pageview event', () => {
67
+ svc.setTicket({subject: 'Some Subject'});
34
68
 
35
- it('should register a mapping for each SVC type', () => {
36
- expect(client.addEventTypeMapping).toHaveBeenCalledTimes(Object.keys(SVCPluginEventTypes).length);
37
- });
69
+ const result = executeRegisteredHook(SVCPluginEventTypes.pageview, {});
38
70
 
39
- describe('tickets', () => {
40
- it('should set the ticket with the specific format and convert known ticket keys into the measurement protocol format when the hook is called', () => {
41
- svc.setTicket({
42
- id: 'Some ID',
43
- subject: 'Some Subject',
44
- description: 'Some Description',
45
- category: 'Some Category',
46
- productId: 'Some Product ID',
47
- custom: {someCustomProp: 'some custom prop value'},
48
- });
49
-
50
- const result = executeRegisteredHook(SVCPluginEventTypes.event, {});
51
-
52
- expect(result).toEqual({
53
- ...defaultResult,
54
- svc_ticket_id: 'Some ID',
55
- svc_ticket_subject: 'Some Subject',
56
- svc_ticket_description: 'Some Description',
57
- svc_ticket_category: 'Some Category',
58
- svc_ticket_product_id: 'Some Product ID',
59
- svc_ticket_custom: {someCustomProp: 'some custom prop value'},
60
- });
61
- });
62
-
63
- it('should append the ticket with the pageview event', () => {
64
- svc.setTicket({subject: 'Some Subject'});
65
-
66
- const result = executeRegisteredHook(SVCPluginEventTypes.pageview, {});
67
-
68
- expect(result).toEqual({
69
- ...defaultResult,
70
- hitType: SVCPluginEventTypes.pageview,
71
- svc_ticket_subject: 'Some Subject',
72
- });
73
- });
74
-
75
- it('should not append the product with a random event type', () => {
76
- svc.setTicket({subject: 'Some Subject'});
77
-
78
- const result = executeRegisteredHook('🎲', {});
79
-
80
- expect(result).toEqual({});
81
- });
82
-
83
- it('should keep the ticket until a valid event type is used', () => {
84
- svc.setTicket({subject: 'Some Subject'});
85
-
86
- executeRegisteredHook('🎲', {});
87
- executeRegisteredHook('🐟', {});
88
- executeRegisteredHook('💀', {});
89
- const result = executeRegisteredHook(SVCPluginEventTypes.event, {});
90
-
91
- expect(result).toEqual({...defaultResult, svc_ticket_subject: 'Some Subject'});
92
- });
93
-
94
- it('should keep custom metadata in the ticket', () => {
95
- svc.setTicket({subject: '🧀', custom: {verycustom: 'value'}});
96
-
97
- const result = executeRegisteredHook(SVCPluginEventTypes.event, {});
98
-
99
- expect(result).toEqual({
100
- ...defaultResult,
101
- svc_ticket_subject: '🧀',
102
- svc_ticket_custom: {verycustom: 'value'},
103
- });
104
- });
105
-
106
- it('should flush the ticket once they are sent', () => {
107
- svc.setTicket({subject: '🍟', description: 'description'});
108
-
109
- const firstResult = executeRegisteredHook(SVCPluginEventTypes.event, {});
110
- const secondResult = executeRegisteredHook(SVCPluginEventTypes.event, {});
111
-
112
- expect(firstResult).not.toEqual({});
113
- expect(secondResult).toEqual({...defaultResult});
114
- });
71
+ expect(result).toEqual({
72
+ ...defaultResult,
73
+ hitType: SVCPluginEventTypes.pageview,
74
+ svc_ticket_subject: 'Some Subject',
75
+ });
115
76
  });
116
77
 
117
- it('should be able to set an action', () => {
118
- svc.setAction('ok');
78
+ it('should not append the product with a random event type', () => {
79
+ svc.setTicket({subject: 'Some Subject'});
119
80
 
120
- const result = executeRegisteredHook(SVCPluginEventTypes.event, {});
81
+ const result = executeRegisteredHook('🎲', {});
121
82
 
122
- expect(result).toEqual({
123
- ...defaultResult,
124
- svcAction: 'ok',
125
- });
83
+ expect(result).toEqual({});
126
84
  });
127
85
 
128
- it('should flush the action once it is sent', () => {
129
- svc.setAction('ok');
86
+ it('should keep the ticket until a valid event type is used', () => {
87
+ svc.setTicket({subject: 'Some Subject'});
130
88
 
131
- const firstResult = executeRegisteredHook(SVCPluginEventTypes.event, {});
132
- const secondResult = executeRegisteredHook(SVCPluginEventTypes.event, {});
89
+ executeRegisteredHook('🎲', {});
90
+ executeRegisteredHook('🐟', {});
91
+ executeRegisteredHook('💀', {});
92
+ const result = executeRegisteredHook(SVCPluginEventTypes.event, {});
133
93
 
134
- expect(firstResult).not.toEqual({});
135
- expect(secondResult).toEqual({...defaultResult});
94
+ expect(result).toEqual({
95
+ ...defaultResult,
96
+ svc_ticket_subject: 'Some Subject',
97
+ });
136
98
  });
137
99
 
138
- it('should be able to clear all the data', () => {
139
- svc.setTicket({subject: '🍨', description: 'Some desc'});
140
- svc.clearData();
100
+ it('should keep custom metadata in the ticket', () => {
101
+ svc.setTicket({subject: '🧀', custom: {verycustom: 'value'}});
141
102
 
142
- const result = executeRegisteredHook(SVCPluginEventTypes.event, {});
103
+ const result = executeRegisteredHook(SVCPluginEventTypes.event, {});
143
104
 
144
- expect(result).toEqual({...defaultResult});
105
+ expect(result).toEqual({
106
+ ...defaultResult,
107
+ svc_ticket_subject: '🧀',
108
+ svc_ticket_custom: {verycustom: 'value'},
109
+ });
145
110
  });
146
111
 
147
- it('should call the uuidv4 method', async () => {
148
- await executeRegisteredHook(SVCPluginEventTypes.event, {});
149
- await executeRegisteredHook(SVCPluginEventTypes.event, {});
150
- await executeRegisteredHook(SVCPluginEventTypes.event, {});
112
+ it('should flush the ticket once they are sent', () => {
113
+ svc.setTicket({subject: '🍟', description: 'description'});
114
+
115
+ const firstResult = executeRegisteredHook(SVCPluginEventTypes.event, {});
116
+ const secondResult = executeRegisteredHook(SVCPluginEventTypes.event, {});
151
117
 
152
- // One for generating pageViewId, one for each individual event.
153
- expect(someUUIDGenerator).toHaveBeenCalledTimes(1 + 3);
118
+ expect(firstResult).not.toEqual({});
119
+ expect(secondResult).toEqual({...defaultResult});
154
120
  });
121
+ });
155
122
 
156
- it('should update the location when sending a pageview with the page parameter', async () => {
157
- const payload = {
158
- page: '/somepage',
159
- };
160
-
161
- const pageview = await executeRegisteredHook(SVCPluginEventTypes.pageview, payload);
162
- const event = await executeRegisteredHook(SVCPluginEventTypes.event, {});
163
-
164
- expect(pageview).toEqual({
165
- ...defaultResult,
166
- hitType: SVCPluginEventTypes.pageview,
167
- page: payload.page,
168
- location: `${defaultResult.location}${payload.page.substring(1)}`,
169
- });
170
- expect(event).toEqual({
171
- ...defaultResult,
172
- hitType: SVCPluginEventTypes.event,
173
- location: `${defaultResult.location}${payload.page.substring(1)}`,
174
- });
123
+ it('should be able to set an action', () => {
124
+ svc.setAction('ok');
125
+
126
+ const result = executeRegisteredHook(SVCPluginEventTypes.event, {});
127
+
128
+ expect(result).toEqual({
129
+ ...defaultResult,
130
+ svcAction: 'ok',
175
131
  });
132
+ });
133
+
134
+ it('should flush the action once it is sent', () => {
135
+ svc.setAction('ok');
136
+
137
+ const firstResult = executeRegisteredHook(SVCPluginEventTypes.event, {});
138
+ const secondResult = executeRegisteredHook(SVCPluginEventTypes.event, {});
139
+
140
+ expect(firstResult).not.toEqual({});
141
+ expect(secondResult).toEqual({...defaultResult});
142
+ });
176
143
 
177
- const executeRegisteredHook = (eventType: string, payload: any) => {
178
- const [beforeHook] = client.registerBeforeSendEventHook.mock.calls[0];
179
- const [afterHook] = client.registerAfterSendEventHook.mock.calls[0];
180
- const result = beforeHook(eventType, payload);
181
- afterHook(eventType, result);
182
- return result;
144
+ it('should be able to clear all the data', () => {
145
+ svc.setTicket({subject: '🍨', description: 'Some desc'});
146
+ svc.clearData();
147
+
148
+ const result = executeRegisteredHook(SVCPluginEventTypes.event, {});
149
+
150
+ expect(result).toEqual({...defaultResult});
151
+ });
152
+
153
+ it('should call the uuidv4 method', async () => {
154
+ await executeRegisteredHook(SVCPluginEventTypes.event, {});
155
+ await executeRegisteredHook(SVCPluginEventTypes.event, {});
156
+ await executeRegisteredHook(SVCPluginEventTypes.event, {});
157
+
158
+ // One for generating pageViewId, one for each individual event.
159
+ expect(someUUIDGenerator).toHaveBeenCalledTimes(1 + 3);
160
+ });
161
+
162
+ it('should update the location when sending a pageview with the page parameter', async () => {
163
+ const payload = {
164
+ page: '/somepage',
183
165
  };
166
+
167
+ const pageview = await executeRegisteredHook(SVCPluginEventTypes.pageview, payload);
168
+ const event = await executeRegisteredHook(SVCPluginEventTypes.event, {});
169
+
170
+ expect(pageview).toEqual({
171
+ ...defaultResult,
172
+ hitType: SVCPluginEventTypes.pageview,
173
+ page: payload.page,
174
+ location: `${defaultResult.location}${payload.page.substring(1)}`,
175
+ });
176
+ expect(event).toEqual({
177
+ ...defaultResult,
178
+ hitType: SVCPluginEventTypes.event,
179
+ location: `${defaultResult.location}${payload.page.substring(1)}`,
180
+ });
181
+ });
182
+
183
+ const executeRegisteredHook = (eventType: string, payload: any) => {
184
+ const [beforeHook] = client.registerBeforeSendEventHook.mock.calls[0];
185
+ const [afterHook] = client.registerAfterSendEventHook.mock.calls[0];
186
+ const result = beforeHook(eventType, payload);
187
+ afterHook(eventType, result);
188
+ return result;
189
+ };
184
190
  });
@@ -4,112 +4,114 @@ import {convertTicketToMeasurementProtocol} from '../client/measurementProtocolM
4
4
  import {BasePlugin, BasePluginEventTypes, PluginClass, PluginOptions} from './BasePlugin';
5
5
 
6
6
  export const SVCPluginEventTypes = {
7
- ...BasePluginEventTypes,
7
+ ...BasePluginEventTypes,
8
8
  };
9
9
 
10
10
  const allSVCEventTypes = Object.keys(SVCPluginEventTypes).map(
11
- (key) => SVCPluginEventTypes[key as keyof typeof SVCPluginEventTypes],
11
+ (key) => SVCPluginEventTypes[key as keyof typeof SVCPluginEventTypes]
12
12
  );
13
13
 
14
14
  export type CustomValues = {
15
- [key: string]: string | number | boolean;
15
+ [key: string]: string | number | boolean;
16
16
  };
17
17
 
18
18
  export interface TicketProperties {
19
- id?: string;
20
- subject?: string;
21
- description?: string;
22
- category?: string;
23
- productId?: string;
24
- custom?: CustomValues;
19
+ id?: string;
20
+ subject?: string;
21
+ description?: string;
22
+ category?: string;
23
+ productId?: string;
24
+ custom?: CustomValues;
25
25
  }
26
26
 
27
27
  export type Ticket = TicketProperties;
28
28
 
29
29
  export class SVCPlugin extends BasePlugin {
30
- public static readonly Id = 'svc';
31
- private ticket: Ticket = {};
32
-
33
- constructor({client, uuidGenerator = uuidv4}: PluginOptions) {
34
- super({client, uuidGenerator});
35
- }
36
-
37
- public getApi(name: string): Function | null {
38
- const superCall: Function | null = super.getApi(name);
39
- if (superCall !== null) return superCall;
40
- switch (name) {
41
- case 'setTicket':
42
- return this.setTicket;
43
- default:
44
- return null;
45
- }
46
- }
47
-
48
- protected addHooks(): void {
49
- this.addHooksForEvent();
50
- this.addHooksForPageView();
51
- this.addHooksForSVCEvents();
52
- }
53
-
54
- setTicket(ticket: Ticket) {
55
- this.ticket = ticket;
56
- }
57
-
58
- protected clearPluginData() {
59
- this.ticket = {};
60
- }
61
-
62
- private addHooksForSVCEvents() {
63
- this.client.registerBeforeSendEventHook((eventType, ...[payload]) => {
64
- return allSVCEventTypes.indexOf(eventType) !== -1 ? this.addSVCDataToPayload(eventType, payload) : payload;
65
- });
66
- this.client.registerAfterSendEventHook((eventType, ...[payload]) => {
67
- if (allSVCEventTypes.indexOf(eventType) !== -1) {
68
- this.updateLocationInformation(eventType, payload);
69
- }
70
- return payload;
71
- });
72
- }
73
-
74
- private addHooksForPageView() {
75
- this.client.addEventTypeMapping(SVCPluginEventTypes.pageview, {
76
- newEventType: EventType.collect,
77
- variableLengthArgumentsNames: ['page'],
78
- addVisitorIdParameter: true,
79
- usesMeasurementProtocol: true,
80
- });
81
- }
82
-
83
- private addHooksForEvent() {
84
- this.client.addEventTypeMapping(SVCPluginEventTypes.event, {
85
- newEventType: EventType.collect,
86
- variableLengthArgumentsNames: ['eventCategory', 'eventAction', 'eventLabel', 'eventValue'],
87
- addVisitorIdParameter: true,
88
- usesMeasurementProtocol: true,
89
- });
90
- }
91
-
92
- private addSVCDataToPayload(eventType: string, payload: any) {
93
- const svcPayload = {
94
- ...this.getLocationInformation(eventType, payload),
95
- ...this.getDefaultContextInformation(eventType),
96
- ...(this.action ? {svcAction: this.action} : {}),
97
- ...(Object.keys(this.actionData ?? {}).length > 0 ? {svcActionData: this.actionData} : {}),
98
- };
99
-
100
- const ticketPayload = this.getTicketPayload();
101
- this.clearData();
102
-
103
- return {
104
- ...ticketPayload,
105
- ...svcPayload,
106
- ...payload,
107
- };
108
- }
109
-
110
- private getTicketPayload() {
111
- return convertTicketToMeasurementProtocol(this.ticket);
30
+ public static readonly Id = 'svc';
31
+ private ticket: Ticket = {};
32
+
33
+ constructor({client, uuidGenerator = uuidv4}: PluginOptions) {
34
+ super({client, uuidGenerator});
35
+ }
36
+
37
+ public getApi(name: string): Function | null {
38
+ const superCall: Function | null = super.getApi(name);
39
+ if (superCall !== null) return superCall;
40
+ switch (name) {
41
+ case 'setTicket':
42
+ return this.setTicket;
43
+ default:
44
+ return null;
112
45
  }
46
+ }
47
+
48
+ protected addHooks(): void {
49
+ this.addHooksForEvent();
50
+ this.addHooksForPageView();
51
+ this.addHooksForSVCEvents();
52
+ }
53
+
54
+ setTicket(ticket: Ticket) {
55
+ this.ticket = ticket;
56
+ }
57
+
58
+ protected clearPluginData() {
59
+ this.ticket = {};
60
+ }
61
+
62
+ private addHooksForSVCEvents() {
63
+ this.client.registerBeforeSendEventHook((eventType, ...[payload]) => {
64
+ return allSVCEventTypes.indexOf(eventType) !== -1
65
+ ? this.addSVCDataToPayload(eventType, payload)
66
+ : payload;
67
+ });
68
+ this.client.registerAfterSendEventHook((eventType, ...[payload]) => {
69
+ if (allSVCEventTypes.indexOf(eventType) !== -1) {
70
+ this.updateLocationInformation(eventType, payload);
71
+ }
72
+ return payload;
73
+ });
74
+ }
75
+
76
+ private addHooksForPageView() {
77
+ this.client.addEventTypeMapping(SVCPluginEventTypes.pageview, {
78
+ newEventType: EventType.collect,
79
+ variableLengthArgumentsNames: ['page'],
80
+ addVisitorIdParameter: true,
81
+ usesMeasurementProtocol: true,
82
+ });
83
+ }
84
+
85
+ private addHooksForEvent() {
86
+ this.client.addEventTypeMapping(SVCPluginEventTypes.event, {
87
+ newEventType: EventType.collect,
88
+ variableLengthArgumentsNames: ['eventCategory', 'eventAction', 'eventLabel', 'eventValue'],
89
+ addVisitorIdParameter: true,
90
+ usesMeasurementProtocol: true,
91
+ });
92
+ }
93
+
94
+ private addSVCDataToPayload(eventType: string, payload: any) {
95
+ const svcPayload = {
96
+ ...this.getLocationInformation(eventType, payload),
97
+ ...this.getDefaultContextInformation(eventType),
98
+ ...(this.action ? {svcAction: this.action} : {}),
99
+ ...(Object.keys(this.actionData ?? {}).length > 0 ? {svcActionData: this.actionData} : {}),
100
+ };
101
+
102
+ const ticketPayload = this.getTicketPayload();
103
+ this.clearData();
104
+
105
+ return {
106
+ ...ticketPayload,
107
+ ...svcPayload,
108
+ ...payload,
109
+ };
110
+ }
111
+
112
+ private getTicketPayload() {
113
+ return convertTicketToMeasurementProtocol(this.ticket);
114
+ }
113
115
  }
114
116
 
115
117
  export const SVC: PluginClass = SVCPlugin;
@@ -1,3 +1,7 @@
1
- export {ReactNativeRuntime, ReactNativeRuntimeOptions, ReactNativeStorage} from './react-native-runtime';
1
+ export {
2
+ ReactNativeRuntime,
3
+ ReactNativeRuntimeOptions,
4
+ ReactNativeStorage,
5
+ } from './react-native-runtime';
2
6
  export * from '../coveoua/headless';
3
7
  import 'react-native-get-random-values';
@@ -8,42 +8,42 @@ import {buildBaseUrl} from '../client/analytics';
8
8
  export type ReactNativeStorage = WebStorage;
9
9
 
10
10
  export interface ReactNativeRuntimeOptions {
11
- /**
12
- * Mandatory Storage implementation.
13
- *
14
- * We recommend using a storage library. There are a few options presented in the readme: https://github.com/coveo/coveo.analytics.js#using-react-native
15
- */
16
- storage: ReactNativeStorage;
17
- /**
18
- * Key for storing the visitor ID value.
19
- * @defaut visitorId
20
- */
21
- visitorIdKey?: string;
22
- token?: string;
23
- version?: string;
24
- endpoint?: string;
25
- preprocessRequest?: PreprocessAnalyticsRequest;
11
+ /**
12
+ * Mandatory Storage implementation.
13
+ *
14
+ * We recommend using a storage library. There are a few options presented in the readme: https://github.com/coveo/coveo.analytics.js#using-react-native
15
+ */
16
+ storage: ReactNativeStorage;
17
+ /**
18
+ * Key for storing the visitor ID value.
19
+ * @default visitorId
20
+ */
21
+ visitorIdKey?: string;
22
+ token?: string;
23
+ version?: string;
24
+ endpoint?: string;
25
+ preprocessRequest?: PreprocessAnalyticsRequest;
26
26
  }
27
27
 
28
28
  export class ReactNativeRuntime implements IRuntimeEnvironment {
29
- public client: AnalyticsFetchClient;
30
- public storage: ReactNativeStorage;
29
+ public client: AnalyticsFetchClient;
30
+ public storage: ReactNativeStorage;
31
31
 
32
- constructor(options: ReactNativeRuntimeOptions) {
33
- const visitorIdKey = options.visitorIdKey ?? 'visitorId';
34
- this.storage = options.storage;
35
- this.client = new AnalyticsFetchClient({
36
- preprocessRequest: options.preprocessRequest,
37
- token: options.token,
38
- baseUrl: buildBaseUrl(options.endpoint, options.version),
39
- visitorIdProvider: {
40
- getCurrentVisitorId: async () => (await this.storage.getItem(visitorIdKey)) || uuidv4(),
41
- setCurrentVisitorId: (visitorId: string) => this.storage.setItem(visitorIdKey, visitorId),
42
- },
43
- });
44
- }
32
+ constructor(options: ReactNativeRuntimeOptions) {
33
+ const visitorIdKey = options.visitorIdKey ?? 'visitorId';
34
+ this.storage = options.storage;
35
+ this.client = new AnalyticsFetchClient({
36
+ preprocessRequest: options.preprocessRequest,
37
+ token: options.token,
38
+ baseUrl: buildBaseUrl(options.endpoint, options.version),
39
+ visitorIdProvider: {
40
+ getCurrentVisitorId: async () => (await this.storage.getItem(visitorIdKey)) || uuidv4(),
41
+ setCurrentVisitorId: (visitorId: string) => this.storage.setItem(visitorIdKey, visitorId),
42
+ },
43
+ });
44
+ }
45
45
 
46
- public getClientDependingOnEventType() {
47
- return this.client;
48
- }
46
+ public getClientDependingOnEventType() {
47
+ return this.client;
48
+ }
49
49
  }
@@ -5,5 +5,5 @@ export const ReactNativeRuntimeWarning = `
5
5
  `;
6
6
 
7
7
  export function isReactNative() {
8
- return typeof navigator != 'undefined' && navigator.product == 'ReactNative';
8
+ return typeof navigator != 'undefined' && navigator.product == 'ReactNative';
9
9
  }