-
Notifications
You must be signed in to change notification settings - Fork 0
/
cast_analytics.js
384 lines (338 loc) · 11.4 KB
/
cast_analytics.js
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
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
/**
Copyright 2022 Google LLC. All Rights Reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
'use_strict';
import { CastEventType, EventOwner } from './cast_event_types.js';
import { initGoogleAnalytics } from './agents/google_analytics.js';
/**
* @fileoverview This sample demonstrates how to acquire and send Cast
* information to analytics services such as Google Analytics.
*/
/*
* Initialize the Google Analytics agent.
*/
initGoogleAnalytics();
/**
* Modules that handle Cast SDK events. The Tracker class is a template for its
* extending classes. Each should be customized to relay infomation about the
* event captured to its respective backend services through the sendData()
* method.
*/
class Tracker {
constructor(trackerType) {
this.context = cast.framework.CastReceiverContext.getInstance();
this.playerManager = this.context.getPlayerManager();
this.type = trackerType;
this.castEventTypes = [];
}
/**
* Creates the tracker's eventlisteners and binds them to its registered
* SDK event types. Uses the CastEventType mapping imported from
* cast_event_types.js.
*/
startTracking() {
this.castEventTypes.forEach((eventType) => {
if (eventType.owner == EventOwner.PLAYER_MANAGER) {
this.playerManager.addEventListener(eventType.event,
this.handleEvent.bind(this));
}
else if (eventType.owner == EventOwner.CAST_RECEIVER_CONTEXT) {
this.context.addEventListener(eventType.event,
this.handleEvent.bind(this));
}
else {
console.error("Unrecognized CastEventType: "
+ JSON.stringify(eventType));
}
});
}
/**
* Template event handler that should be overidden by implementing classes.
* @param {cast.framework.events.EventType|cast.framework.system.EventType}
* event The event fired by the SDK to be handled by the tracker.
*/
handleEvent(event) {}
/**
* Template event sender that should be overidden by implementing classes.
* Sends the data collected from the event handler to the target analytics
* service providers.
* @param {Object} data The data to be sent to analytics provider backends.
*/
sendData(data) {}
}
/**
* Handles ad related events and send them to an analytics service. Event data
* tracked includes quartile ad events, ad loading time, and break tracking.
*/
class AdsTracker extends Tracker {
constructor() {
super("ad");
this.breakManager = this.playerManager.getBreakManager();
this.breakStarted = false;
this.breakEnded = false;
this.breakClipStarted = false;
this.breakClipEnded = false;
this.breakClipCount = null;
this.breakClipLength = null;
this.breakClipPosition = null;
this.breakClipId = null;
this.breakId = null;
this.qt1 = null;
this.qt2 = null;
this.qt3 = null;
this.castEventTypes = [
CastEventType.BREAK_ENDED,
CastEventType.BREAK_CLIP_STARTED,
CastEventType.BREAK_CLIP_LOADING,
CastEventType.BREAK_CLIP_ENDED,
CastEventType.BREAK_STARTED,
CastEventType.TIME_UPDATE
];
}
/**
* Handles the incoming event if it is a break event type or time update
* while the break is started. When a time update is detected, the type is
* modified to create a custom quartile event with added break and breakClip
* id for additional context.
* @param {cast.framework.events.BreaksEvent|
* cast.framework.events.BreaksEvent.MediaElementEvent} event
* @override
*/
handleEvent(event) {
// Ignore event if the event is not a break started event
if (!this.breakStarted) {
if (event.type == CastEventType.BREAK_STARTED.event) {
this.handleBreakStarted(event);
}
return;
}
// Control the state of the tracker and send data if break has begun.
switch (event.type) {
case CastEventType.TIME_UPDATE.event:
this.handleTimeUpdate(event);
break;
case CastEventType.BREAK_CLIP_STARTED.event:
this.handleBreakClipStarted(event);
break;
case CastEventType.BREAK_CLIP_ENDED.event:
this.handleBreakClipEnded(event);
break;
case CastEventType.BREAK_ENDED.event:
this.handleBreakEnded(event);
break;
}
}
/**
* Handle the break started event. Set flag to begin tracking relevant ad
* break events.
* @param {cast.framework.events.EventType.BREAK_STARTED} event
*/
handleBreakStarted(event) {
let data = {};
this.breakStarted = true;
this.breakId = event.breakId;
data.action = event.type;
data.id = event.breakId;
data.startTime = event.currentMediaTime;
data.position = event.index;
this.sendData(data);
}
/**
* Handle time update event. Used to report quartile ad events.
* @param {cast.framework.events.EventType.TIME_UPDATE} event
*/
handleTimeUpdate(event) {
let data = {};
let currTime = this.breakManager.getBreakClipCurrentTimeSec();
data.id = this.breakClipId;
if (this.qt1 && currTime > this.qt1) {
this.qt1 = null;
data.action = 'BREAK_CLIP_QT1';
} else if (this.qt2 && currTime > this.qt2) {
this.qt2 = null;
data.action = 'BREAK_CLIP_QT2';
} else if (this.qt3 && currTime > this.qt3) {
this.qt3 = null;
data.action = 'BREAK_CLIP_QT3';
} else {
return;
}
this.sendData(data);
}
/**
* Handle break clip started event. Sets up quartile ad tracking.
* @param {cast.framework.events.EventType.BREAK_CLIP_STARTED} event
*/
handleBreakClipStarted(event) {
let data = {};
this.breakClipStarted = true;
this.breakClipId = event.breakClipId;
this.breakClipLength =
this.breakManager.getBreakClipDurationSec();
this.qt1 = this.breakClipLength / 4;
this.qt2 = this.qt1 * 2;
this.qt3 = this.qt1 * 3;
data.id = this.breakClipId;
data.action = event.type;
data.position = event.index;
data.length = this.breakClipLength;
this.sendData(data);
}
/**
* Handle break clip ended event. Resets quartile ad tracking.
* @param {cast.framework.events.EventType.BREAK_CLIP_ENDED} event
*/
handleBreakClipEnded(event) {
let data = {};
data.id = this.breakClipId;
this.breakClipStarted = false;
this.breakClipLength = null;
this.breakClipId = null;
this.qt1 = null;
this.qt2 = null;
this.qt3 = null;
data.action = event.endedReason;
this.sendData(data);
}
/**
* Handle break ended event. Reset tracker to ignore events until new break.
* @param {cast.framework.events.EventType.BREAK_ENDED} event
*/
handleBreakEnded(event) {
let data = {};
data.id = this.breakId;
data.action = event.type;
this.sendData(data);
this.breakId = null;
this.breakStarted = false;
}
/**
* Sends the event data to respective analytics agents.
* @param {Object} data
* @override
*/
sendData(data) {
ga('send', 'event', this.type, data.action, data.id);
}
}
/**
* Tracker that handles profiling the types of senders interacting with the
* receiver. Each sender type is mapped to its senderId through the regular
* expressions stored in this object.
*/
class SenderTracker extends Tracker {
constructor() {
super("sender");
this.senders = {};
this.castEventTypes = [
CastEventType.REQUEST,
CastEventType.SENDER_CONNECTED
];
this.SenderIdRegex = {
TOUCH_CONTROLS: "^__touch_controls__$",
VOICE: "^__inject__$",
CHROME_SENDER: "^[a-z0-9]{8}-[a-z0-9]{4}-[a-z0-9]{4}-[a-z0-9]{4}-"
+ "[a-z0-9]{12}\.[0-9]+\:[0-9]+$",
IOS_SENDER: "^[a-z0-9]{8}-[a-z0-9]{4}-[a-z0-9]{4}-[a-z0-9]{4}-"
+ "[a-z0-9]{12}\.[0-9]+\:[A-Z0-9]{8}-[A-Z0-9]{4}-[A-Z0-9]{4}-"
+ "[A-Z0-9]{4}-[A-Z0-9]{12}$",
ANDROID_SENDER: "^[a-z0-9]{8}-[a-z0-9]{4}-[a-z0-9]{4}-"
+ "[a-z0-9]{4}-[a-z0-9]{12}\.[0-9]+\:"
+ "(com.google.sample.cast.refplayer)-[0-9]+$"
}
}
/**
* Handles the incoming registered event for sender requests and sender
* connected events. Each unique sender id is added as a key to the tracker
* instance's sender property. The event data is sent when a senderId is
* registered for the first time.
* @param {cast.framework.events.category.REQUEST|
* cast.framework.system.EventType.SENDER_CONNECTED} event
* @override
*/
handleEvent(event) {
// Map the event senderId to its senderType if not already registered.
if (!this.senders[event.senderId]) {
this.senders[event.senderId] = this.getSenderType(event.senderId);
let data = {};
data.action = "SENDER_CONNECTED";
data.senderType = this.senders[event.senderId];
if (data.senderType !== "OTHER_SENDER") {
this.sendData(data);
}
}
}
/**
* Obtains the senderType based on the senderId REGEX defined in this class.
* @param {string} senderId The sender identifier to be categorized.
* @return {string} The sender type identified.
*/
getSenderType(senderId) {
let senderType = null;
Object.entries(this.SenderIdRegex).forEach(([currType, regex]) => {
if (RegExp(regex).test(senderId)) {
senderType = currType;
}
});
return senderType || "OTHER_SENDER";
}
/**
* Sends the event data to respective analytics agents.
* @param {Object} data
* @override
*/
sendData(data) {
ga('send', 'event', this.type, data.action, data.senderType);
}
}
/**
* Tracker that determines if the loaded content is suggested by the queue or
* selected by the user. Detection requires that the loaded media to have
* customData with the property isSuggested.
*/
class ContentTracker extends Tracker {
constructor() {
super("content");
this.castEventTypes = [
CastEventType.PLAYER_LOAD_COMPLETE,
];
}
/**
* Handles the registered event. Checks the customData of the media for its
* isSuggested property to determine if the content is suggested or not.
* @param {cast.framework.events.EventType.PLAYER_LOAD_COMPLETE} event
* @override
*/
handleEvent(event) {
let data = {};
if (event.media.customData
&& event.media.customData.isSuggested) {
data.action = "SUGGESTED_CONTENT";
}
else {
data.action = "USER_SELECTED_CONTENT";
}
data.id = event.media.entity
|| event.media.contentId
|| event.media.contentUrl;
this.sendData(data);
}
/**
* Sends the event data to respective analytics agents.
* @param {Object} data
* @override
*/
sendData(data) {
ga('send', 'event', this.type, data.action, data.id);
}
}
export { AdsTracker, SenderTracker, ContentTracker };