coveo.analytics 2.19.16 → 2.20.3

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.
@@ -1,5 +1,6 @@
1
1
  export { CoveoSearchPageClient, SearchPageClientProvider } from '../searchPage/searchPageClient';
2
2
  export { CaseAssistClient, CaseAssistClientProvider } from '../caseAssist/caseAssistClient';
3
+ export { CoveoInsightClient, InsightClientProvider } from '../insight/insightClient';
3
4
  export { CoveoAnalyticsClient, AnalyticsClientSendEventHook } from '../client/analytics';
4
5
  export { PreprocessAnalyticsRequest } from '../client/analyticsRequestClient';
5
6
  export * as history from '../history';
@@ -9,4 +9,5 @@ export { IRuntimeEnvironment } from '../client/runtimeEnvironment';
9
9
  export { CoveoUA, getCurrentClient, handleOneAnalyticsEvent } from './simpleanalytics';
10
10
  export { CoveoSearchPageClient, SearchPageClientProvider } from '../searchPage/searchPageClient';
11
11
  export { CaseAssistClient, CaseAssistClientProvider } from '../caseAssist/caseAssistClient';
12
+ export { CoveoInsightClient, InsightClientProvider } from '../insight/insightClient';
12
13
  export { analytics, donottrack, history, SimpleAnalytics, storage };
@@ -0,0 +1,43 @@
1
+ import { AnalyticsClient, ClientOptions } from '../client/analytics';
2
+ import { SearchEventRequest } from '../events';
3
+ import { FacetBaseMeta, FacetMetadata, FacetSortMeta, FacetStateMetadata, QueryErrorMeta, SearchPageEvents } from '../searchPage/searchPageEvents';
4
+ export interface InsightClientProvider {
5
+ getSearchEventRequestPayload: () => Omit<SearchEventRequest, 'actionCause' | 'searchQueryUid'>;
6
+ getSearchUID: () => string;
7
+ getBaseMetadata: () => Record<string, any>;
8
+ getPipeline: () => string;
9
+ getOriginContext?: () => string;
10
+ getOriginLevel1: () => string;
11
+ getOriginLevel2: () => string;
12
+ getOriginLevel3: () => string;
13
+ getLanguage: () => string;
14
+ getIsAnonymous: () => boolean;
15
+ getFacetState?: () => FacetStateMetadata[];
16
+ }
17
+ export interface InsightClientOptions extends ClientOptions {
18
+ enableAnalytics: boolean;
19
+ }
20
+ export declare class CoveoInsightClient {
21
+ private opts;
22
+ private provider;
23
+ coveoAnalyticsClient: AnalyticsClient;
24
+ constructor(opts: Partial<InsightClientOptions>, provider: InsightClientProvider);
25
+ disable(): void;
26
+ enable(): void;
27
+ logInterfaceLoad(): Promise<void | import("../events").SearchEventResponse>;
28
+ logFetchMoreResults(): Promise<void | import("../events").CustomEventResponse>;
29
+ logFacetSelect(meta: FacetMetadata): Promise<void | import("../events").SearchEventResponse>;
30
+ logFacetDeselect(meta: FacetMetadata): Promise<void | import("../events").SearchEventResponse>;
31
+ logFacetUpdateSort(meta: FacetSortMeta): Promise<void | import("../events").SearchEventResponse>;
32
+ logFacetClearAll(meta: FacetBaseMeta): Promise<void | import("../events").SearchEventResponse>;
33
+ logFacetShowMore(meta: FacetBaseMeta): Promise<void | import("../events").CustomEventResponse>;
34
+ logFacetShowLess(meta: FacetBaseMeta): Promise<void | import("../events").CustomEventResponse>;
35
+ logQueryError(meta: QueryErrorMeta): Promise<void | import("../events").CustomEventResponse>;
36
+ logCustomEvent(event: SearchPageEvents, metadata?: Record<string, any>): Promise<void | import("../events").CustomEventResponse>;
37
+ logSearchEvent(event: SearchPageEvents, metadata?: Record<string, any>): Promise<void | import("../events").SearchEventResponse>;
38
+ private getBaseCustomEventRequest;
39
+ private getBaseSearchEventRequest;
40
+ private getBaseEventRequest;
41
+ private getOrigins;
42
+ private getClientId;
43
+ }
@@ -1,5 +1,6 @@
1
1
  export { CoveoSearchPageClient, SearchPageClientProvider } from '../searchPage/searchPageClient';
2
2
  export { CaseAssistClient, CaseAssistClientProvider } from '../caseAssist/caseAssistClient';
3
+ export { CoveoInsightClient, InsightClientProvider } from '../insight/insightClient';
3
4
  export { CoveoAnalyticsClient, AnalyticsClientSendEventHook } from '../client/analytics';
4
5
  export { PreprocessAnalyticsRequest } from '../client/analyticsRequestClient';
5
6
  export * as history from '../history';
@@ -9,4 +9,5 @@ export { IRuntimeEnvironment } from '../client/runtimeEnvironment';
9
9
  export { CoveoUA, getCurrentClient, handleOneAnalyticsEvent } from './simpleanalytics';
10
10
  export { CoveoSearchPageClient, SearchPageClientProvider } from '../searchPage/searchPageClient';
11
11
  export { CaseAssistClient, CaseAssistClientProvider } from '../caseAssist/caseAssistClient';
12
+ export { CoveoInsightClient, InsightClientProvider } from '../insight/insightClient';
12
13
  export { analytics, donottrack, history, SimpleAnalytics, storage };
@@ -1235,8 +1235,8 @@ class CoveoSearchPageClient {
1235
1235
  constructor(opts, provider) {
1236
1236
  this.opts = opts;
1237
1237
  this.provider = provider;
1238
- this.coveoAnalyticsClient =
1239
- opts.enableAnalytics === false ? new NoopAnalytics() : new CoveoAnalyticsClient(opts);
1238
+ const shouldDisableAnalytics = opts.enableAnalytics === false || doNotTrack();
1239
+ this.coveoAnalyticsClient = shouldDisableAnalytics ? new NoopAnalytics() : new CoveoAnalyticsClient(opts);
1240
1240
  }
1241
1241
  disable() {
1242
1242
  if (this.coveoAnalyticsClient instanceof CoveoAnalyticsClient) {
@@ -1681,7 +1681,7 @@ class CaseAssistClient {
1681
1681
  var _a;
1682
1682
  this.options = options;
1683
1683
  this.provider = provider;
1684
- const analyticsEnabled = (_a = options.enableAnalytics) !== null && _a !== void 0 ? _a : true;
1684
+ const analyticsEnabled = ((_a = options.enableAnalytics) !== null && _a !== void 0 ? _a : true) && !doNotTrack();
1685
1685
  this.coveoAnalyticsClient = analyticsEnabled ? new CoveoAnalyticsClient(options) : new NoopAnalytics();
1686
1686
  this.svc = new SVCPlugin({ client: this.coveoAnalyticsClient });
1687
1687
  }
@@ -1765,4 +1765,91 @@ class CaseAssistClient {
1765
1765
  }
1766
1766
  }
1767
1767
 
1768
- export { CaseAssistClient, CoveoAnalyticsClient, CoveoSearchPageClient, history };
1768
+ class CoveoInsightClient {
1769
+ constructor(opts, provider) {
1770
+ this.opts = opts;
1771
+ this.provider = provider;
1772
+ const shouldDisableAnalytics = opts.enableAnalytics === false || doNotTrack();
1773
+ this.coveoAnalyticsClient = shouldDisableAnalytics ? new NoopAnalytics() : new CoveoAnalyticsClient(opts);
1774
+ }
1775
+ disable() {
1776
+ if (this.coveoAnalyticsClient instanceof CoveoAnalyticsClient) {
1777
+ this.coveoAnalyticsClient.clear();
1778
+ }
1779
+ this.coveoAnalyticsClient = new NoopAnalytics();
1780
+ }
1781
+ enable() {
1782
+ this.coveoAnalyticsClient = new CoveoAnalyticsClient(this.opts);
1783
+ }
1784
+ logInterfaceLoad() {
1785
+ return this.logSearchEvent(SearchPageEvents.interfaceLoad);
1786
+ }
1787
+ logFetchMoreResults() {
1788
+ return this.logCustomEvent(SearchPageEvents.pagerScrolling, { type: 'getMoreResults' });
1789
+ }
1790
+ logFacetSelect(meta) {
1791
+ return this.logSearchEvent(SearchPageEvents.facetSelect, meta);
1792
+ }
1793
+ logFacetDeselect(meta) {
1794
+ return this.logSearchEvent(SearchPageEvents.facetDeselect, meta);
1795
+ }
1796
+ logFacetUpdateSort(meta) {
1797
+ return this.logSearchEvent(SearchPageEvents.facetUpdateSort, meta);
1798
+ }
1799
+ logFacetClearAll(meta) {
1800
+ return this.logSearchEvent(SearchPageEvents.facetClearAll, meta);
1801
+ }
1802
+ logFacetShowMore(meta) {
1803
+ return this.logCustomEvent(SearchPageEvents.facetShowMore, meta);
1804
+ }
1805
+ logFacetShowLess(meta) {
1806
+ return this.logCustomEvent(SearchPageEvents.facetShowLess, meta);
1807
+ }
1808
+ logQueryError(meta) {
1809
+ return this.logCustomEvent(SearchPageEvents.queryError, meta);
1810
+ }
1811
+ logCustomEvent(event, metadata) {
1812
+ return __awaiter(this, void 0, void 0, function* () {
1813
+ const customData = Object.assign(Object.assign({}, this.provider.getBaseMetadata()), metadata);
1814
+ const payload = Object.assign(Object.assign({}, (yield this.getBaseCustomEventRequest(customData))), { eventType: CustomEventsTypes[event], eventValue: event });
1815
+ return this.coveoAnalyticsClient.sendCustomEvent(payload);
1816
+ });
1817
+ }
1818
+ logSearchEvent(event, metadata) {
1819
+ return __awaiter(this, void 0, void 0, function* () {
1820
+ return this.coveoAnalyticsClient.sendSearchEvent(yield this.getBaseSearchEventRequest(event, metadata));
1821
+ });
1822
+ }
1823
+ getBaseCustomEventRequest(metadata) {
1824
+ return __awaiter(this, void 0, void 0, function* () {
1825
+ return Object.assign(Object.assign({}, (yield this.getBaseEventRequest(metadata))), { lastSearchQueryUid: this.provider.getSearchUID() });
1826
+ });
1827
+ }
1828
+ getBaseSearchEventRequest(event, metadata) {
1829
+ return __awaiter(this, void 0, void 0, function* () {
1830
+ return Object.assign(Object.assign(Object.assign({}, (yield this.getBaseEventRequest(metadata))), this.provider.getSearchEventRequestPayload()), { searchQueryUid: this.provider.getSearchUID(), queryPipeline: this.provider.getPipeline(), actionCause: event });
1831
+ });
1832
+ }
1833
+ getBaseEventRequest(metadata) {
1834
+ return __awaiter(this, void 0, void 0, function* () {
1835
+ const customData = Object.assign(Object.assign({}, this.provider.getBaseMetadata()), metadata);
1836
+ return Object.assign(Object.assign({}, this.getOrigins()), { customData, language: this.provider.getLanguage(), facetState: this.provider.getFacetState ? this.provider.getFacetState() : [], anonymous: this.provider.getIsAnonymous(), clientId: yield this.getClientId() });
1837
+ });
1838
+ }
1839
+ getOrigins() {
1840
+ var _a, _b;
1841
+ return {
1842
+ originContext: (_b = (_a = this.provider).getOriginContext) === null || _b === void 0 ? void 0 : _b.call(_a),
1843
+ originLevel1: this.provider.getOriginLevel1(),
1844
+ originLevel2: this.provider.getOriginLevel2(),
1845
+ originLevel3: this.provider.getOriginLevel3(),
1846
+ };
1847
+ }
1848
+ getClientId() {
1849
+ return this.coveoAnalyticsClient instanceof CoveoAnalyticsClient
1850
+ ? this.coveoAnalyticsClient.getCurrentVisitorId()
1851
+ : undefined;
1852
+ }
1853
+ }
1854
+
1855
+ export { CaseAssistClient, CoveoAnalyticsClient, CoveoInsightClient, CoveoSearchPageClient, history };
package/dist/library.js CHANGED
@@ -83678,8 +83678,8 @@ var CoveoSearchPageClient = (function () {
83678
83678
  function CoveoSearchPageClient(opts, provider) {
83679
83679
  this.opts = opts;
83680
83680
  this.provider = provider;
83681
- this.coveoAnalyticsClient =
83682
- opts.enableAnalytics === false ? new NoopAnalytics() : new CoveoAnalyticsClient(opts);
83681
+ var shouldDisableAnalytics = opts.enableAnalytics === false || doNotTrack();
83682
+ this.coveoAnalyticsClient = shouldDisableAnalytics ? new NoopAnalytics() : new CoveoAnalyticsClient(opts);
83683
83683
  }
83684
83684
  CoveoSearchPageClient.prototype.disable = function () {
83685
83685
  if (this.coveoAnalyticsClient instanceof CoveoAnalyticsClient) {
@@ -84061,7 +84061,7 @@ var CaseAssistClient = (function () {
84061
84061
  var _a;
84062
84062
  this.options = options;
84063
84063
  this.provider = provider;
84064
- var analyticsEnabled = (_a = options.enableAnalytics) !== null && _a !== void 0 ? _a : true;
84064
+ var analyticsEnabled = ((_a = options.enableAnalytics) !== null && _a !== void 0 ? _a : true) && !doNotTrack();
84065
84065
  this.coveoAnalyticsClient = analyticsEnabled ? new CoveoAnalyticsClient(options) : new NoopAnalytics();
84066
84066
  this.svc = new SVCPlugin({ client: this.coveoAnalyticsClient });
84067
84067
  }
@@ -84146,8 +84146,139 @@ var CaseAssistClient = (function () {
84146
84146
  return CaseAssistClient;
84147
84147
  }());
84148
84148
 
84149
+ var CoveoInsightClient = (function () {
84150
+ function CoveoInsightClient(opts, provider) {
84151
+ this.opts = opts;
84152
+ this.provider = provider;
84153
+ var shouldDisableAnalytics = opts.enableAnalytics === false || doNotTrack();
84154
+ this.coveoAnalyticsClient = shouldDisableAnalytics ? new NoopAnalytics() : new CoveoAnalyticsClient(opts);
84155
+ }
84156
+ CoveoInsightClient.prototype.disable = function () {
84157
+ if (this.coveoAnalyticsClient instanceof CoveoAnalyticsClient) {
84158
+ this.coveoAnalyticsClient.clear();
84159
+ }
84160
+ this.coveoAnalyticsClient = new NoopAnalytics();
84161
+ };
84162
+ CoveoInsightClient.prototype.enable = function () {
84163
+ this.coveoAnalyticsClient = new CoveoAnalyticsClient(this.opts);
84164
+ };
84165
+ CoveoInsightClient.prototype.logInterfaceLoad = function () {
84166
+ return this.logSearchEvent(SearchPageEvents.interfaceLoad);
84167
+ };
84168
+ CoveoInsightClient.prototype.logFetchMoreResults = function () {
84169
+ return this.logCustomEvent(SearchPageEvents.pagerScrolling, { type: 'getMoreResults' });
84170
+ };
84171
+ CoveoInsightClient.prototype.logFacetSelect = function (meta) {
84172
+ return this.logSearchEvent(SearchPageEvents.facetSelect, meta);
84173
+ };
84174
+ CoveoInsightClient.prototype.logFacetDeselect = function (meta) {
84175
+ return this.logSearchEvent(SearchPageEvents.facetDeselect, meta);
84176
+ };
84177
+ CoveoInsightClient.prototype.logFacetUpdateSort = function (meta) {
84178
+ return this.logSearchEvent(SearchPageEvents.facetUpdateSort, meta);
84179
+ };
84180
+ CoveoInsightClient.prototype.logFacetClearAll = function (meta) {
84181
+ return this.logSearchEvent(SearchPageEvents.facetClearAll, meta);
84182
+ };
84183
+ CoveoInsightClient.prototype.logFacetShowMore = function (meta) {
84184
+ return this.logCustomEvent(SearchPageEvents.facetShowMore, meta);
84185
+ };
84186
+ CoveoInsightClient.prototype.logFacetShowLess = function (meta) {
84187
+ return this.logCustomEvent(SearchPageEvents.facetShowLess, meta);
84188
+ };
84189
+ CoveoInsightClient.prototype.logQueryError = function (meta) {
84190
+ return this.logCustomEvent(SearchPageEvents.queryError, meta);
84191
+ };
84192
+ CoveoInsightClient.prototype.logCustomEvent = function (event, metadata) {
84193
+ return __awaiter(this, void 0, void 0, function () {
84194
+ var customData, payload, _a;
84195
+ return __generator(this, function (_b) {
84196
+ switch (_b.label) {
84197
+ case 0:
84198
+ customData = __assign(__assign({}, this.provider.getBaseMetadata()), metadata);
84199
+ _a = [{}];
84200
+ return [4, this.getBaseCustomEventRequest(customData)];
84201
+ case 1:
84202
+ payload = __assign.apply(void 0, [__assign.apply(void 0, _a.concat([(_b.sent())])), { eventType: CustomEventsTypes[event], eventValue: event }]);
84203
+ return [2, this.coveoAnalyticsClient.sendCustomEvent(payload)];
84204
+ }
84205
+ });
84206
+ });
84207
+ };
84208
+ CoveoInsightClient.prototype.logSearchEvent = function (event, metadata) {
84209
+ return __awaiter(this, void 0, void 0, function () {
84210
+ var _a, _b;
84211
+ return __generator(this, function (_c) {
84212
+ switch (_c.label) {
84213
+ case 0:
84214
+ _b = (_a = this.coveoAnalyticsClient).sendSearchEvent;
84215
+ return [4, this.getBaseSearchEventRequest(event, metadata)];
84216
+ case 1: return [2, _b.apply(_a, [_c.sent()])];
84217
+ }
84218
+ });
84219
+ });
84220
+ };
84221
+ CoveoInsightClient.prototype.getBaseCustomEventRequest = function (metadata) {
84222
+ return __awaiter(this, void 0, void 0, function () {
84223
+ var _a;
84224
+ return __generator(this, function (_b) {
84225
+ switch (_b.label) {
84226
+ case 0:
84227
+ _a = [{}];
84228
+ return [4, this.getBaseEventRequest(metadata)];
84229
+ case 1: return [2, __assign.apply(void 0, [__assign.apply(void 0, _a.concat([(_b.sent())])), { lastSearchQueryUid: this.provider.getSearchUID() }])];
84230
+ }
84231
+ });
84232
+ });
84233
+ };
84234
+ CoveoInsightClient.prototype.getBaseSearchEventRequest = function (event, metadata) {
84235
+ return __awaiter(this, void 0, void 0, function () {
84236
+ var _a;
84237
+ return __generator(this, function (_b) {
84238
+ switch (_b.label) {
84239
+ case 0:
84240
+ _a = [{}];
84241
+ return [4, this.getBaseEventRequest(metadata)];
84242
+ case 1: return [2, __assign.apply(void 0, [__assign.apply(void 0, [__assign.apply(void 0, _a.concat([(_b.sent())])), this.provider.getSearchEventRequestPayload()]), { searchQueryUid: this.provider.getSearchUID(), queryPipeline: this.provider.getPipeline(), actionCause: event }])];
84243
+ }
84244
+ });
84245
+ });
84246
+ };
84247
+ CoveoInsightClient.prototype.getBaseEventRequest = function (metadata) {
84248
+ return __awaiter(this, void 0, void 0, function () {
84249
+ var customData, _a, _b;
84250
+ return __generator(this, function (_c) {
84251
+ switch (_c.label) {
84252
+ case 0:
84253
+ customData = __assign(__assign({}, this.provider.getBaseMetadata()), metadata);
84254
+ _a = [__assign({}, this.getOrigins())];
84255
+ _b = { customData: customData, language: this.provider.getLanguage(), facetState: this.provider.getFacetState ? this.provider.getFacetState() : [], anonymous: this.provider.getIsAnonymous() };
84256
+ return [4, this.getClientId()];
84257
+ case 1: return [2, __assign.apply(void 0, _a.concat([(_b.clientId = _c.sent(), _b)]))];
84258
+ }
84259
+ });
84260
+ });
84261
+ };
84262
+ CoveoInsightClient.prototype.getOrigins = function () {
84263
+ var _a, _b;
84264
+ return {
84265
+ originContext: (_b = (_a = this.provider).getOriginContext) === null || _b === void 0 ? void 0 : _b.call(_a),
84266
+ originLevel1: this.provider.getOriginLevel1(),
84267
+ originLevel2: this.provider.getOriginLevel2(),
84268
+ originLevel3: this.provider.getOriginLevel3(),
84269
+ };
84270
+ };
84271
+ CoveoInsightClient.prototype.getClientId = function () {
84272
+ return this.coveoAnalyticsClient instanceof CoveoAnalyticsClient
84273
+ ? this.coveoAnalyticsClient.getCurrentVisitorId()
84274
+ : undefined;
84275
+ };
84276
+ return CoveoInsightClient;
84277
+ }());
84278
+
84149
84279
  exports.CaseAssistClient = CaseAssistClient;
84150
84280
  exports.CoveoAnalyticsClient = CoveoAnalyticsClient;
84281
+ exports.CoveoInsightClient = CoveoInsightClient;
84151
84282
  exports.CoveoSearchPageClient = CoveoSearchPageClient;
84152
84283
  exports.CoveoUA = CoveoUA;
84153
84284
  exports.SimpleAnalytics = simpleanalytics;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "coveo.analytics",
3
- "version": "2.19.16",
3
+ "version": "2.20.3",
4
4
  "description": "📈 Coveo analytics client (node and browser compatible) ",
5
5
  "main": "dist/library.js",
6
6
  "module": "dist/library.es.js",
@@ -6,11 +6,16 @@ import {
6
6
  DocumentSuggestion,
7
7
  FieldSuggestion,
8
8
  } from './caseAssistActions';
9
-
10
9
  import {mockFetch} from '../../tests/fetchMock';
11
10
  import {TicketProperties} from '../plugins/svc';
12
-
13
11
  const {fetchMock, fetchMockBeforeEach} = mockFetch();
12
+ import doNotTrack from '../donottrack';
13
+ jest.mock('../donottrack', () => {
14
+ return {
15
+ default: jest.fn(),
16
+ doNotTrack: jest.fn(),
17
+ };
18
+ });
14
19
 
15
20
  describe('CaseAssistClient', () => {
16
21
  const defaultSearchHub = 'origin-level-1';
@@ -151,6 +156,18 @@ describe('CaseAssistClient', () => {
151
156
  expect(fetchMock.called()).toBe(false);
152
157
  });
153
158
 
159
+ it('should not send events when doNotTrack is enabled', async () => {
160
+ (doNotTrack as jest.Mock).mockImplementationOnce(() => true);
161
+
162
+ client = new CaseAssistClient({});
163
+
164
+ await client.logEnterInterface({
165
+ ticket: emptyTicket,
166
+ });
167
+
168
+ expect(fetchMock.called()).toBe(false);
169
+ });
170
+
154
171
  it('should not send events after #disable function is called', async () => {
155
172
  client.disable();
156
173
 
@@ -1,5 +1,6 @@
1
1
  import CoveoAnalyticsClient, {AnalyticsClient, ClientOptions} from '../client/analytics';
2
2
  import {NoopAnalytics} from '../client/noopAnalytics';
3
+ import doNotTrack from '../donottrack';
3
4
  import {SVCPlugin} from '../plugins/svc';
4
5
  import {
5
6
  CaseAssistActions,
@@ -29,7 +30,7 @@ export class CaseAssistClient {
29
30
  private svc: SVCPlugin;
30
31
 
31
32
  constructor(private options: Partial<CaseAssistClientOptions>, private provider?: CaseAssistClientProvider) {
32
- const analyticsEnabled = options.enableAnalytics ?? true;
33
+ const analyticsEnabled = (options.enableAnalytics ?? true) && !doNotTrack();
33
34
 
34
35
  this.coveoAnalyticsClient = analyticsEnabled ? new CoveoAnalyticsClient(options) : new NoopAnalytics();
35
36
  this.svc = new SVCPlugin({client: this.coveoAnalyticsClient});
@@ -1,5 +1,6 @@
1
1
  export {CoveoSearchPageClient, SearchPageClientProvider} from '../searchPage/searchPageClient';
2
2
  export {CaseAssistClient, CaseAssistClientProvider} from '../caseAssist/caseAssistClient';
3
+ export {CoveoInsightClient, InsightClientProvider} from '../insight/insightClient';
3
4
  export {CoveoAnalyticsClient, AnalyticsClientSendEventHook} from '../client/analytics';
4
5
  export {PreprocessAnalyticsRequest} from '../client/analyticsRequestClient';
5
6
  export * as history from '../history';
@@ -9,5 +9,6 @@ export {IRuntimeEnvironment} from '../client/runtimeEnvironment';
9
9
  export {CoveoUA, getCurrentClient, handleOneAnalyticsEvent} from './simpleanalytics';
10
10
  export {CoveoSearchPageClient, SearchPageClientProvider} from '../searchPage/searchPageClient';
11
11
  export {CaseAssistClient, CaseAssistClientProvider} from '../caseAssist/caseAssistClient';
12
+ export {CoveoInsightClient, InsightClientProvider} from '../insight/insightClient';
12
13
 
13
14
  export {analytics, donottrack, history, SimpleAnalytics, storage};
@@ -0,0 +1,223 @@
1
+ import {mockFetch} from '../../tests/fetchMock';
2
+ import CoveoAnalyticsClient from '../client/analytics';
3
+ import {NoopAnalytics} from '../client/noopAnalytics';
4
+ import {CustomEventsTypes, SearchPageEvents} from '../searchPage/searchPageEvents';
5
+ import {CoveoInsightClient, InsightClientProvider} from './insightClient';
6
+ import doNotTrack from '../donottrack';
7
+
8
+ jest.mock('../donottrack', () => {
9
+ return {
10
+ default: jest.fn(),
11
+ doNotTrack: jest.fn(),
12
+ };
13
+ });
14
+
15
+ const {fetchMock, fetchMockBeforeEach} = mockFetch();
16
+ describe('InsightClient', () => {
17
+ const fakeFacetState = [
18
+ {
19
+ valuePosition: 0,
20
+ value: 'foo',
21
+ state: 'selected' as const,
22
+ facetPosition: 1,
23
+ displayValue: 'foobar',
24
+ facetType: 'specific' as const,
25
+ field: '@foo',
26
+ id: 'bar',
27
+ title: 'title',
28
+ },
29
+ ];
30
+
31
+ let client: CoveoInsightClient;
32
+
33
+ const provider: InsightClientProvider = {
34
+ getBaseMetadata: () => ({foo: 'bar'}),
35
+ getSearchEventRequestPayload: () => ({
36
+ queryText: 'queryText',
37
+ responseTime: 123,
38
+ }),
39
+ getSearchUID: () => 'my-uid',
40
+ getPipeline: () => 'my-pipeline',
41
+ getOriginContext: () => 'origin-context',
42
+ getOriginLevel1: () => 'origin-level-1',
43
+ getOriginLevel2: () => 'origin-level-2',
44
+ getOriginLevel3: () => 'origin-level-3',
45
+ getLanguage: () => 'en',
46
+ getFacetState: () => fakeFacetState,
47
+ getIsAnonymous: () => false,
48
+ };
49
+
50
+ const initClient = () => {
51
+ return new CoveoInsightClient({}, provider);
52
+ };
53
+
54
+ const expectOrigins = () => ({
55
+ originContext: 'origin-context',
56
+ originLevel1: 'origin-level-1',
57
+ originLevel2: 'origin-level-2',
58
+ originLevel3: 'origin-level-3',
59
+ });
60
+
61
+ const expectMatchPayload = (actionCause: SearchPageEvents, meta = {}) => {
62
+ const [, {body}] = fetchMock.lastCall();
63
+ const customData = {foo: 'bar', ...meta};
64
+ expect(JSON.parse(body.toString())).toMatchObject({
65
+ queryText: 'queryText',
66
+ responseTime: 123,
67
+ queryPipeline: 'my-pipeline',
68
+ actionCause,
69
+ customData,
70
+ facetState: fakeFacetState,
71
+ language: 'en',
72
+ clientId: 'visitor-id',
73
+ ...expectOrigins(),
74
+ });
75
+ };
76
+
77
+ const expectMatchCustomEventPayload = (actionCause: SearchPageEvents, meta = {}) => {
78
+ const [, {body}] = fetchMock.lastCall();
79
+ const customData = {foo: 'bar', ...meta};
80
+ expect(JSON.parse(body.toString())).toMatchObject({
81
+ eventValue: actionCause,
82
+ eventType: CustomEventsTypes[actionCause],
83
+ lastSearchQueryUid: 'my-uid',
84
+ customData,
85
+ language: 'en',
86
+ clientId: 'visitor-id',
87
+ ...expectOrigins(),
88
+ });
89
+ };
90
+
91
+ beforeEach(() => {
92
+ fetchMockBeforeEach();
93
+
94
+ client = initClient();
95
+ client.coveoAnalyticsClient.runtime.storage.setItem('visitorId', 'visitor-id');
96
+ fetchMock.mock(/.*/, {
97
+ visitId: 'visit-id',
98
+ });
99
+ });
100
+
101
+ afterEach(() => {
102
+ fetchMock.reset();
103
+ });
104
+
105
+ it('should send proper payload for #interfaceLoad', async () => {
106
+ await client.logInterfaceLoad();
107
+ expectMatchPayload(SearchPageEvents.interfaceLoad);
108
+ });
109
+
110
+ it('should send proper payload for #fetchMoreResults', async () => {
111
+ await client.logFetchMoreResults();
112
+ expectMatchCustomEventPayload(SearchPageEvents.pagerScrolling, {type: 'getMoreResults'});
113
+ });
114
+
115
+ it('should send proper payload for #logFacetSelect', async () => {
116
+ const meta = {
117
+ facetField: '@foo',
118
+ facetId: 'bar',
119
+ facetTitle: 'title',
120
+ facetValue: 'qwerty',
121
+ };
122
+
123
+ await client.logFacetSelect(meta);
124
+ expectMatchPayload(SearchPageEvents.facetSelect, meta);
125
+ });
126
+
127
+ it('should send proper payload for #logFacetDeselect', async () => {
128
+ const meta = {
129
+ facetField: '@foo',
130
+ facetId: 'bar',
131
+ facetTitle: 'title',
132
+ facetValue: 'qwerty',
133
+ };
134
+
135
+ await client.logFacetDeselect(meta);
136
+ expectMatchPayload(SearchPageEvents.facetDeselect, meta);
137
+ });
138
+
139
+ it('should send proper payload for #logFacetUpdateSort', async () => {
140
+ const meta = {
141
+ facetField: '@foo',
142
+ facetId: 'bar',
143
+ facetTitle: 'title',
144
+ criteria: 'bazz',
145
+ };
146
+ await client.logFacetUpdateSort(meta);
147
+ expectMatchPayload(SearchPageEvents.facetUpdateSort, meta);
148
+ });
149
+
150
+ it('should send proper payload for #logFacetClearAll', async () => {
151
+ const meta = {
152
+ facetField: '@foo',
153
+ facetId: 'bar',
154
+ facetTitle: 'title',
155
+ };
156
+ await client.logFacetClearAll(meta);
157
+ expectMatchPayload(SearchPageEvents.facetClearAll, meta);
158
+ });
159
+
160
+ it('should send proper payload for #logFacetShowMore', async () => {
161
+ const meta = {
162
+ facetField: '@foo',
163
+ facetId: 'bar',
164
+ facetTitle: 'title',
165
+ };
166
+ await client.logFacetShowMore(meta);
167
+ expectMatchCustomEventPayload(SearchPageEvents.facetShowMore, meta);
168
+ });
169
+
170
+ it('should send proper payload for #logFacetShowLess', async () => {
171
+ const meta = {
172
+ facetField: '@foo',
173
+ facetId: 'bar',
174
+ facetTitle: 'title',
175
+ };
176
+ await client.logFacetShowLess(meta);
177
+ expectMatchCustomEventPayload(SearchPageEvents.facetShowLess, meta);
178
+ });
179
+
180
+ it('should send proper payload for #logQueryError', async () => {
181
+ const meta = {
182
+ query: 'q',
183
+ aq: 'aq',
184
+ cq: 'cq',
185
+ dq: 'dq',
186
+ errorMessage: 'boom',
187
+ errorType: 'a bad one',
188
+ };
189
+ await client.logQueryError(meta);
190
+ expectMatchCustomEventPayload(SearchPageEvents.queryError, meta);
191
+ });
192
+
193
+ it('should enable analytics tracking by default', () => {
194
+ const c = new CoveoInsightClient({}, provider);
195
+ expect(c.coveoAnalyticsClient instanceof CoveoAnalyticsClient).toBe(true);
196
+ });
197
+
198
+ it('should allow disabling analytics on initialization', () => {
199
+ const c = new CoveoInsightClient({enableAnalytics: false}, provider);
200
+ expect(c.coveoAnalyticsClient instanceof NoopAnalytics).toBe(true);
201
+ });
202
+
203
+ it('should allow disabling analytics after initialization', () => {
204
+ const c = new CoveoInsightClient({enableAnalytics: true}, provider);
205
+ expect(c.coveoAnalyticsClient instanceof CoveoAnalyticsClient).toBe(true);
206
+ c.disable();
207
+ expect(c.coveoAnalyticsClient instanceof NoopAnalytics).toBe(true);
208
+ });
209
+
210
+ it('should disable analytics when doNotTrack is enabled', async () => {
211
+ (doNotTrack as jest.Mock).mockImplementationOnce(() => true);
212
+
213
+ const c = new CoveoInsightClient({}, provider);
214
+ expect(c.coveoAnalyticsClient instanceof NoopAnalytics).toBe(true);
215
+ });
216
+
217
+ it('should allow enabling analytics after initialization', () => {
218
+ const c = new CoveoInsightClient({enableAnalytics: false}, provider);
219
+ expect(c.coveoAnalyticsClient instanceof NoopAnalytics).toBe(true);
220
+ c.enable();
221
+ expect(c.coveoAnalyticsClient instanceof CoveoAnalyticsClient).toBe(true);
222
+ });
223
+ });