-
-
Notifications
You must be signed in to change notification settings - Fork 310
/
GoogleAnalytics.ts
292 lines (263 loc) 路 8.6 KB
/
GoogleAnalytics.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
import * as ua from 'universal-analytics';
import _merge = require('lodash.merge');
import * as murmurhash from 'murmurhash';
import { Analytics, BaseApp, ErrorCode, HandleRequest, Jovo, JovoError } from 'jovo-core';
import { Config, Event, TransactionItem, Transaction } from './interfaces';
export class GoogleAnalytics implements Analytics {
config: Config = {
trackingId: '',
};
visitor: ua.Visitor | undefined;
constructor(config?: Config) {
if (config) {
this.config = _merge(this.config, config);
}
}
install(app: BaseApp) {
if (!this.config.trackingId) {
throw new JovoError(
'trackingId has to be set.',
ErrorCode.ERR_PLUGIN,
'jovo-analytics-googleanalytics',
'',
'You can find your tracking id in Google Analytics by clicking: Admin -> Property Settings -> Tracking Id',
'https://www.jovo.tech/docs/analytics/googleanalytics',
);
}
app.middleware('after.platform.init')!.use(this.setGoogleAnalyticsObject.bind(this));
app.middleware('after.response')!.use(this.track.bind(this));
app.middleware('fail')!.use(this.sendError.bind(this));
}
/**
* Auto send intent data after each response. Also setting sessions and flowErrors
* @param handleRequest
*/
track(handleRequest: HandleRequest) {
const jovo: Jovo = handleRequest.jovo!;
if (!jovo) {
throw new JovoError(
'Jovo object is not set',
ErrorCode.ERR_PLUGIN,
'jovo-analytics-googleanalytics',
);
}
// Validate current request type
const { type: requestType } = jovo.getRoute();
const invalidRequestTypes = ['AUDIOPLAYER'];
if (!this.config.trackDirectives && invalidRequestTypes.includes(requestType)) {
return;
}
// Eiter start or stop the session. If sessionTag is undefined, it will be ignored.
const sessionTag = this.getSessionTag(jovo);
this.visitor!.set('sessionControl', sessionTag);
// Track custom set data as custom metrics.
const customData = jovo.$googleAnalytics.$data;
for (const [key, value] of Object.entries(customData)) {
if (key.startsWith('cm')) {
this.visitor!.set(key, value);
}
}
// Track intent data.
this.visitor!.pageview(this.getPageParameters(jovo), (err: any) => {
if (err) {
throw new JovoError(err.message, ErrorCode.ERR_PLUGIN, 'jovo-analytics-googleanalytics');
}
}).send();
// Detect and send FlowErrors
this.sendUnhandledEvents(jovo);
if (jovo.$inputs) {
for (const [key, value] of Object.entries(jovo.$inputs)) {
if (!value.key) {
continue;
}
const params: Event = {
eventCategory: 'Inputs',
eventAction: value.key, // Input value
eventLabel: key, // Input key
documentPath: jovo.getRoute().path,
};
this.visitor!.event(params, (err: any) => {
if (err) {
throw new JovoError(
err.message,
ErrorCode.ERR_PLUGIN,
'jovo-analytics-googleanalytics',
);
}
}).send();
}
}
}
/**
* Initiates GoogleAnalytics visitor object with fixed parameters.
* @param {object} jovo: Jovo object for data like language or platform
*/
initVisitor(jovo: Jovo) {
const uuid = this.getUserId(jovo);
// Initialize visitor with account id and custom client id
const visitor = ua(this.config.trackingId, uuid, { strictCidFormat: false });
visitor.set('userId', uuid);
visitor.set('dataSource', jovo.getType());
visitor.set('userLanguage', jovo.getLocale());
// Set user id as a custom dimension to track hits on the same scope
visitor.set('cd1', uuid);
this.visitor = visitor;
}
/**
* Tracks uncaught user exceptions.
* @param {object} handleRequest: HandleRequest to act upon
*/
sendError(handleRequest: HandleRequest) {
const jovo: Jovo = handleRequest.jovo!;
if (!jovo) {
throw new JovoError(
'Jovo object is not set',
ErrorCode.ERR_PLUGIN,
'jovo-analytics-googleanalytics',
);
}
// Stop the current tracking session.
this.visitor!.set('sessionControl', 'end');
this.visitor!.pageview(this.getPageParameters(jovo), (err: any) => {
if (err) {
throw new JovoError(err.message, ErrorCode.ERR_PLUGIN, 'jovo-analytics-googleanalytics');
}
})
.exception(handleRequest.error!.name)
.send();
}
/**
* Detects and sends flow errors, ranging from nlu errors to bugs in the skill handler.
* @param {object} jovo: Jovo object
*/
sendUnhandledEvents(jovo: Jovo) {
const intent = jovo.$request!.getIntentName();
const { path } = jovo.getRoute();
// Check if an error in the nlu model occurred.
if (intent === 'AMAZON.FallbackIntent' || intent === 'Default Fallback Intent') {
return this.sendUserEvent(jovo, 'UnhandledEvents', 'NLU_Unhandled');
}
// If the current path is unhandled, an error in the skill handler occurred.
if (path.endsWith('Unhandled')) {
return this.sendUserEvent(jovo, 'UnhandledEvents', 'Skill_Unhandled');
}
}
/**
* Construct pageview parameters, a.k.a intent tracking data.
* @param {object} jovo: Jovo object
* @returns {object} pageParameters: Intent data to track
*/
getPageParameters(jovo: Jovo) {
const { intent, path, type } = jovo.getRoute();
return {
documentPath: path,
documentHostName: type,
documentTitle: intent || type,
};
}
/**
* Generates hash for userId.
* @param {object} jovo: Jovo object
* @returns {string} uuid: Hashed user id
*/
getUserId(jovo: Jovo): string {
const idHash = murmurhash.v3(jovo.$user.getId()!);
const uuid = idHash.toString();
return uuid;
}
/**
* Checks if the current session started or ended.
* @param {object} jovo: Jovo object
* @returns {string | void} sessionTag: Corresponding session tag (start|end|undefined)
*/
getSessionTag(jovo: Jovo): string | void {
if (
jovo.getMappedIntentName() === 'END' ||
jovo.$type.type === 'END' ||
(jovo.$response && jovo.$response.isTell())
) {
return 'end';
}
if (jovo.isNewSession()) {
return 'start';
}
}
/**
* User Events ties users to event category and action
* @param {object} jovo: Jovo object
* @param {string} eventName maps to category -> eventGroup
* @param {string} eventElement maps to action -> instance of eventGroup
*/
sendUserEvent(jovo: Jovo, eventCategory: string, eventAction: string) {
const params: Event = {
eventCategory,
eventAction,
eventLabel: this.getUserId(jovo),
documentPath: jovo.getRoute().path,
};
this.visitor!.event(params, (err: any) => {
if (err) {
throw new JovoError(err.message, ErrorCode.ERR_PLUGIN, 'jovo-analytics-googleanalytics');
}
}).send();
}
/**
* Sets the analytics variable to the instance of this object for making it accessable in skill code
* @param handleRequest
*/
setGoogleAnalyticsObject(handleRequest: HandleRequest) {
const jovo = handleRequest.jovo;
if (!jovo) {
throw new JovoError(
'Jovo object is not set',
ErrorCode.ERR_PLUGIN,
'jovo-analytics-googleanalytics',
);
}
// Initialise visitor object.
this.initVisitor(jovo);
// Initialise googleAnalytics object.
jovo.$googleAnalytics = {
$data: {},
sendEvent: (params: Event) => {
this.visitor!.event(params, (err: any) => {
if (err) {
throw new JovoError(
err.message,
ErrorCode.ERR_PLUGIN,
'jovo-analytics-googleanalytics',
);
}
}).send();
},
sendTransaction: (params: Transaction) => {
this.visitor!.transaction(params, (err: any) => {
if (err) {
throw new JovoError(
err.message,
ErrorCode.ERR_PLUGIN,
'jovo-analytics-googleanalytics',
);
}
}).send();
},
sendItem: (params: TransactionItem) => {
this.visitor!.transaction(params, (err: any) => {
if (err) {
throw new JovoError(
err.message,
ErrorCode.ERR_PLUGIN,
'jovo-analytics-googleanalytics',
);
}
}).send();
},
sendUserEvent: (eventCategory: string, eventAction: string) => {
this.sendUserEvent(jovo, eventCategory, eventAction);
},
setCustomMetric(index: number, value: string | number) {
this.$data[`cm${index}`] = value;
},
};
}
}