coveo.analytics 2.20.1 → 2.20.4
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.
- package/dist/coveoua.browser.js +1 -1
- package/dist/coveoua.browser.js.map +1 -1
- package/dist/coveoua.js +1 -1
- package/dist/coveoua.js.map +1 -1
- package/dist/definitions/coveoua/headless.d.ts +1 -0
- package/dist/definitions/coveoua/library.d.ts +1 -0
- package/dist/definitions/insight/insightClient.d.ts +43 -0
- package/dist/definitions/src/coveoua/headless.d.ts +1 -0
- package/dist/definitions/src/coveoua/library.d.ts +1 -0
- package/dist/library.es.js +88 -1
- package/dist/library.js +131 -0
- package/package.json +1 -1
- package/src/coveoua/headless.ts +1 -0
- package/src/coveoua/library.ts +1 -0
- package/src/insight/insightClient.spec.ts +223 -0
- package/src/insight/insightClient.ts +150 -0
|
@@ -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
|
+
}
|