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.
@@ -0,0 +1,150 @@
1
+ import CoveoAnalyticsClient, {AnalyticsClient, ClientOptions} from '../client/analytics';
2
+ import {NoopAnalytics} from '../client/noopAnalytics';
3
+ import doNotTrack from '../donottrack';
4
+ import {CustomEventRequest, SearchEventRequest} from '../events';
5
+ import {
6
+ CustomEventsTypes,
7
+ FacetBaseMeta,
8
+ FacetMetadata,
9
+ FacetSortMeta,
10
+ FacetStateMetadata,
11
+ QueryErrorMeta,
12
+ SearchPageEvents,
13
+ } from '../searchPage/searchPageEvents';
14
+
15
+ export interface InsightClientProvider {
16
+ getSearchEventRequestPayload: () => Omit<SearchEventRequest, 'actionCause' | 'searchQueryUid'>;
17
+ getSearchUID: () => string;
18
+ getBaseMetadata: () => Record<string, any>;
19
+ getPipeline: () => string;
20
+ getOriginContext?: () => string;
21
+ getOriginLevel1: () => string;
22
+ getOriginLevel2: () => string;
23
+ getOriginLevel3: () => string;
24
+ getLanguage: () => string;
25
+ getIsAnonymous: () => boolean;
26
+ getFacetState?: () => FacetStateMetadata[];
27
+ }
28
+
29
+ export interface InsightClientOptions extends ClientOptions {
30
+ enableAnalytics: boolean;
31
+ }
32
+
33
+ export class CoveoInsightClient {
34
+ public coveoAnalyticsClient: AnalyticsClient;
35
+
36
+ constructor(private opts: Partial<InsightClientOptions>, private provider: InsightClientProvider) {
37
+ const shouldDisableAnalytics = opts.enableAnalytics === false || doNotTrack();
38
+ this.coveoAnalyticsClient = shouldDisableAnalytics ? new NoopAnalytics() : new CoveoAnalyticsClient(opts);
39
+ }
40
+
41
+ public disable() {
42
+ if (this.coveoAnalyticsClient instanceof CoveoAnalyticsClient) {
43
+ this.coveoAnalyticsClient.clear();
44
+ }
45
+ this.coveoAnalyticsClient = new NoopAnalytics();
46
+ }
47
+
48
+ public enable() {
49
+ this.coveoAnalyticsClient = new CoveoAnalyticsClient(this.opts);
50
+ }
51
+
52
+ public logInterfaceLoad() {
53
+ return this.logSearchEvent(SearchPageEvents.interfaceLoad);
54
+ }
55
+
56
+ public logFetchMoreResults() {
57
+ return this.logCustomEvent(SearchPageEvents.pagerScrolling, {type: 'getMoreResults'});
58
+ }
59
+
60
+ public logFacetSelect(meta: FacetMetadata) {
61
+ return this.logSearchEvent(SearchPageEvents.facetSelect, meta);
62
+ }
63
+
64
+ public logFacetDeselect(meta: FacetMetadata) {
65
+ return this.logSearchEvent(SearchPageEvents.facetDeselect, meta);
66
+ }
67
+
68
+ public logFacetUpdateSort(meta: FacetSortMeta) {
69
+ return this.logSearchEvent(SearchPageEvents.facetUpdateSort, meta);
70
+ }
71
+
72
+ public logFacetClearAll(meta: FacetBaseMeta) {
73
+ return this.logSearchEvent(SearchPageEvents.facetClearAll, meta);
74
+ }
75
+
76
+ public logFacetShowMore(meta: FacetBaseMeta) {
77
+ return this.logCustomEvent(SearchPageEvents.facetShowMore, meta);
78
+ }
79
+
80
+ public logFacetShowLess(meta: FacetBaseMeta) {
81
+ return this.logCustomEvent(SearchPageEvents.facetShowLess, meta);
82
+ }
83
+
84
+ public logQueryError(meta: QueryErrorMeta) {
85
+ return this.logCustomEvent(SearchPageEvents.queryError, meta);
86
+ }
87
+
88
+ public async logCustomEvent(event: SearchPageEvents, metadata?: Record<string, any>) {
89
+ const customData = {...this.provider.getBaseMetadata(), ...metadata};
90
+
91
+ const payload: CustomEventRequest = {
92
+ ...(await this.getBaseCustomEventRequest(customData)),
93
+ eventType: CustomEventsTypes[event]!,
94
+ eventValue: event,
95
+ };
96
+
97
+ return this.coveoAnalyticsClient.sendCustomEvent(payload);
98
+ }
99
+
100
+ public async logSearchEvent(event: SearchPageEvents, metadata?: Record<string, any>) {
101
+ return this.coveoAnalyticsClient.sendSearchEvent(await this.getBaseSearchEventRequest(event, metadata));
102
+ }
103
+
104
+ private async getBaseCustomEventRequest(metadata?: Record<string, any>) {
105
+ return {
106
+ ...(await this.getBaseEventRequest(metadata)),
107
+ lastSearchQueryUid: this.provider.getSearchUID(),
108
+ };
109
+ }
110
+
111
+ private async getBaseSearchEventRequest(
112
+ event: SearchPageEvents,
113
+ metadata?: Record<string, any>
114
+ ): Promise<SearchEventRequest> {
115
+ return {
116
+ ...(await this.getBaseEventRequest(metadata)),
117
+ ...this.provider.getSearchEventRequestPayload(),
118
+ searchQueryUid: this.provider.getSearchUID(),
119
+ queryPipeline: this.provider.getPipeline(),
120
+ actionCause: event,
121
+ };
122
+ }
123
+
124
+ private async getBaseEventRequest(metadata?: Record<string, any>) {
125
+ const customData = {...this.provider.getBaseMetadata(), ...metadata};
126
+ return {
127
+ ...this.getOrigins(),
128
+ customData,
129
+ language: this.provider.getLanguage(),
130
+ facetState: this.provider.getFacetState ? this.provider.getFacetState() : [],
131
+ anonymous: this.provider.getIsAnonymous(),
132
+ clientId: await this.getClientId(),
133
+ };
134
+ }
135
+
136
+ private getOrigins() {
137
+ return {
138
+ originContext: this.provider.getOriginContext?.(),
139
+ originLevel1: this.provider.getOriginLevel1(),
140
+ originLevel2: this.provider.getOriginLevel2(),
141
+ originLevel3: this.provider.getOriginLevel3(),
142
+ };
143
+ }
144
+
145
+ private getClientId() {
146
+ return this.coveoAnalyticsClient instanceof CoveoAnalyticsClient
147
+ ? this.coveoAnalyticsClient.getCurrentVisitorId()
148
+ : undefined;
149
+ }
150
+ }
@@ -10,7 +10,13 @@ import {
10
10
  import CoveoAnalyticsClient from '../client/analytics';
11
11
  import {NoopAnalytics} from '../client/noopAnalytics';
12
12
  import {mockFetch} from '../../tests/fetchMock';
13
-
13
+ import doNotTrack from '../donottrack';
14
+ jest.mock('../donottrack', () => {
15
+ return {
16
+ default: jest.fn(),
17
+ doNotTrack: jest.fn(),
18
+ };
19
+ });
14
20
  const {fetchMock, fetchMockBeforeEach} = mockFetch();
15
21
 
16
22
  describe('SearchPageClient', () => {
@@ -651,6 +657,13 @@ describe('SearchPageClient', () => {
651
657
  expect(c.coveoAnalyticsClient instanceof NoopAnalytics).toBe(true);
652
658
  });
653
659
 
660
+ it('should disable analytics when doNotTrack is enabled', async () => {
661
+ (doNotTrack as jest.Mock).mockImplementationOnce(() => true);
662
+
663
+ const c = new CoveoSearchPageClient({}, provider);
664
+ expect(c.coveoAnalyticsClient instanceof NoopAnalytics).toBe(true);
665
+ });
666
+
654
667
  it('should allow disabling analytics after initialization', () => {
655
668
  const c = new CoveoSearchPageClient({enableAnalytics: true}, provider);
656
669
  expect(c.coveoAnalyticsClient instanceof CoveoAnalyticsClient).toBe(true);
@@ -28,6 +28,7 @@ import {
28
28
  } from './searchPageEvents';
29
29
  import {NoopAnalytics} from '../client/noopAnalytics';
30
30
  import {formatOmniboxMetadata} from '../formatting/format-omnibox-metadata';
31
+ import doNotTrack from '../donottrack';
31
32
 
32
33
  export interface SearchPageClientProvider {
33
34
  getBaseMetadata: () => Record<string, any>;
@@ -51,8 +52,8 @@ export class CoveoSearchPageClient {
51
52
  public coveoAnalyticsClient: AnalyticsClient;
52
53
 
53
54
  constructor(private opts: Partial<SearchPageClientOptions>, private provider: SearchPageClientProvider) {
54
- this.coveoAnalyticsClient =
55
- opts.enableAnalytics === false ? new NoopAnalytics() : new CoveoAnalyticsClient(opts);
55
+ const shouldDisableAnalytics = opts.enableAnalytics === false || doNotTrack();
56
+ this.coveoAnalyticsClient = shouldDisableAnalytics ? new NoopAnalytics() : new CoveoAnalyticsClient(opts);
56
57
  }
57
58
 
58
59
  public disable() {