1
1
package com .segment .analytics .android .integrations .comscore ;
2
2
3
3
import static com .segment .analytics .internal .Utils .isNullOrEmpty ;
4
-
4
+ import com .comscore .streaming .AdvertisementMetadata ;
5
+ import com .comscore .streaming .ContentMetadata ;
5
6
import com .comscore .streaming .StreamingAnalytics ;
6
-
7
7
import com .segment .analytics .Properties ;
8
8
import com .segment .analytics .ValueMap ;
9
9
import com .segment .analytics .integrations .IdentifyPayload ;
20
20
public class ComScoreIntegration extends Integration <Void > {
21
21
@ SuppressWarnings ("WeakerAccess" )
22
22
public static final Factory FACTORY =
23
- new Factory () {
24
- @ Override
25
- public Integration <?> create (ValueMap settings , com .segment .analytics .Analytics analytics ) {
26
- return new ComScoreIntegration (analytics , settings );
27
- }
28
-
29
- @ Override
30
- public String key () {
31
- return COMSCORE_KEY ;
32
- }
33
- };
23
+ new Factory () {
24
+ @ Override
25
+ public Integration <?> create (ValueMap settings , com .segment .analytics .Analytics analytics ) {
26
+ return new ComScoreIntegration (analytics , settings );
27
+ }
28
+
29
+ @ Override
30
+ public String key () {
31
+ return COMSCORE_KEY ;
32
+ }
33
+ };
34
34
35
35
private final static String COMSCORE_KEY = "comScore" ;
36
36
private final static String PARTNER_ID = "24186693" ;
@@ -46,26 +46,27 @@ public String key() {
46
46
}
47
47
48
48
ComScoreIntegration (
49
- com .segment .analytics .Analytics analytics ,
50
- ValueMap destinationSettings ,
51
- ComScoreAnalytics comScoreAnalytics ) {
49
+ com .segment .analytics .Analytics analytics ,
50
+ ValueMap destinationSettings ,
51
+ ComScoreAnalytics comScoreAnalytics ) {
52
52
53
53
this .comScoreAnalytics = comScoreAnalytics ;
54
54
this .settings = new Settings (destinationSettings );
55
55
this .logger = analytics .logger (COMSCORE_KEY );
56
56
57
57
comScoreAnalytics .start (analytics .getApplication (), PARTNER_ID , settings .toPublisherConfiguration ());
58
+ settings .analyticsConfig ();
58
59
}
59
60
60
61
/**
61
62
* Store a value for {@param k} in {@param asset} by checking {@param comScoreOptions} first and
62
63
* falling back to {@param properties}. Uses {@code "*null"} it not found in either.
63
64
*/
64
65
private void setNullIfNotProvided (
65
- Map <String , String > asset ,
66
- Map <String , ?> comScoreOptions ,
67
- Map <String , ?> stringProperties ,
68
- String key ) {
66
+ Map <String , String > asset ,
67
+ Map <String , ?> comScoreOptions ,
68
+ Map <String , ?> stringProperties ,
69
+ String key ) {
69
70
String option = getStringOrDefaultValue (comScoreOptions , key , null );
70
71
if (option != null ) {
71
72
asset .put (key , option );
@@ -81,7 +82,7 @@ private void setNullIfNotProvided(
81
82
}
82
83
83
84
private Map <String , String > mapSpecialKeys (
84
- Properties properties , Map <String , String > mapper ) {
85
+ Properties properties , Map <String , String > mapper ) {
85
86
Map <String , String > asset = new LinkedHashMap <>(mapper .size ());
86
87
87
88
// Map special keys and preserve only the special keys.
@@ -98,9 +99,9 @@ private Map<String, String> mapSpecialKeys(
98
99
}
99
100
100
101
private Map <String , String > buildPlaybackAsset (
101
- Properties properties ,
102
- Map <String , ?> options ,
103
- Map <String , String > mapper ) {
102
+ Properties properties ,
103
+ Map <String , ?> options ,
104
+ Map <String , String > mapper ) {
104
105
105
106
Map <String , String > asset = mapSpecialKeys (properties , mapper );
106
107
@@ -118,9 +119,9 @@ private Map<String, String> buildPlaybackAsset(
118
119
}
119
120
120
121
private Map <String , String > buildContentAsset (
121
- Properties properties ,
122
- Map <String , ?> options ,
123
- Map <String , String > mapper ) {
122
+ Properties properties ,
123
+ Map <String , ?> options ,
124
+ Map <String , String > mapper ) {
124
125
125
126
Map <String , String > asset = mapSpecialKeys (properties , mapper );
126
127
@@ -157,9 +158,9 @@ private Map<String, String> buildContentAsset(
157
158
}
158
159
159
160
private Map <String , String > buildAdAsset (
160
- Properties properties ,
161
- Map <String , ?> options ,
162
- Map <String , String > mapper ) {
161
+ Properties properties ,
162
+ Map <String , ?> options ,
163
+ Map <String , String > mapper ) {
163
164
164
165
Map <String , String > asset = mapSpecialKeys (properties , mapper );
165
166
@@ -185,6 +186,7 @@ private Map<String, String> buildAdAsset(
185
186
default :
186
187
asset .put ("ns_st_ad" , "1" );
187
188
}
189
+
188
190
setNullIfNotProvided (asset , options , properties , "c3" );
189
191
setNullIfNotProvided (asset , options , properties , "c4" );
190
192
setNullIfNotProvided (asset , options , properties , "c6" );
@@ -199,7 +201,7 @@ private Map<String, String> buildAdAsset(
199
201
* have a String representation.
200
202
*/
201
203
private String getStringOrDefaultValue (
202
- Map <String , ?> m , String key , String defaultValue ) {
204
+ Map <String , ?> m , String key , String defaultValue ) {
203
205
Object value = m .get (key );
204
206
if (value instanceof String ) {
205
207
return (String ) value ;
@@ -212,7 +214,7 @@ private String getStringOrDefaultValue(
212
214
}
213
215
214
216
private void trackVideoPlayback (
215
- TrackPayload track , Properties properties , Map <String , Object > comScoreOptions ) {
217
+ TrackPayload track , Properties properties , Map <String , Object > comScoreOptions ) {
216
218
String name = track .event ();
217
219
long playbackPosition = properties .getLong ("playbackPosition" , 0 );
218
220
@@ -221,61 +223,66 @@ private void trackVideoPlayback(
221
223
playbackMapper .put ("sound" , "ns_st_vo" );
222
224
223
225
Map <String , String > playbackAsset =
224
- buildPlaybackAsset (properties , comScoreOptions , playbackMapper );
226
+ buildPlaybackAsset (properties , comScoreOptions , playbackMapper );
225
227
226
228
if (name .equals ("Video Playback Started" )) {
227
229
streamingAnalytics = comScoreAnalytics .createStreamingAnalytics ();
228
230
streamingAnalytics .createPlaybackSession ();
229
- streamingAnalytics .setLabels (playbackAsset );
231
+ streamingAnalytics .getConfiguration (). addLabels (playbackAsset );
230
232
231
233
// The label ns_st_ci must be set through a setAsset call
232
234
Map <String , String > contentIdMapper = new LinkedHashMap <>();
233
235
contentIdMapper .put ("assetId" , "ns_st_ci" );
234
236
235
237
Map <String , String > contentIdAsset = mapSpecialKeys (properties , contentIdMapper );
236
- streamingAnalytics .getPlaybackSession ().setAsset (contentIdAsset );
238
+
239
+ streamingAnalytics .setMetadata (getContentMetadata (contentIdAsset ));
237
240
return ;
238
241
}
239
242
240
243
if (streamingAnalytics == null ) {
241
244
logger .verbose (
242
- "streamingAnalytics instance not initialized correctly. Please call Video Playback Started to initialize." );
245
+ "streamingAnalytics instance not initialized correctly. Please call Video Playback Started to initialize." );
243
246
return ;
244
247
}
245
-
246
- streamingAnalytics .setLabels (playbackAsset );
248
+ streamingAnalytics .getConfiguration ().addLabels (playbackAsset );
247
249
248
250
switch (name ) {
249
251
case "Video Playback Paused" :
250
252
case "Video Playback Interrupted" :
251
- streamingAnalytics .notifyPause (playbackPosition );
253
+ streamingAnalytics .notifyPause ();
252
254
logger .verbose ("streamingAnalytics.notifyPause(%s)" , playbackPosition );
253
255
break ;
254
256
case "Video Playback Buffer Started" :
255
- streamingAnalytics .notifyBufferStart (playbackPosition );
257
+ streamingAnalytics .startFromPosition (playbackPosition );
258
+ streamingAnalytics .notifyBufferStart ();
256
259
logger .verbose ("streamingAnalytics.notifyBufferStart(%s)" , playbackPosition );
257
260
break ;
258
261
case "Video Playback Buffer Completed" :
259
- streamingAnalytics .notifyBufferStop (playbackPosition );
262
+ streamingAnalytics .startFromPosition (playbackPosition );
263
+ streamingAnalytics .notifyBufferStop ();
260
264
logger .verbose ("streamingAnalytics.notifyBufferStop(%s)" , playbackPosition );
261
265
break ;
262
266
case "Video Playback Seek Started" :
263
- streamingAnalytics .notifySeekStart (playbackPosition );
267
+ streamingAnalytics .notifySeekStart ();
264
268
logger .verbose ("streamingAnalytics.notifySeekStart(%s)" , playbackPosition );
265
269
break ;
266
270
case "Video Playback Seek Completed" :
267
- streamingAnalytics .notifyPlay (playbackPosition );
271
+ streamingAnalytics .startFromPosition (playbackPosition );
272
+ streamingAnalytics .notifyPlay ();
268
273
logger .verbose ("streamingAnalytics.notifyEnd(%s)" , playbackPosition );
269
274
break ;
270
275
case "Video Playback Resumed" :
271
- streamingAnalytics .notifyPlay (playbackPosition );
276
+ streamingAnalytics .startFromPosition (playbackPosition );
277
+ streamingAnalytics .notifyPlay ();
272
278
logger .verbose ("streamingAnalytics.notifyPlay(%s)" , playbackPosition );
273
279
break ;
274
280
}
275
281
}
276
282
283
+
277
284
private void trackVideoContent (
278
- TrackPayload track , Properties properties , Map <String , Object > comScoreOptions ) {
285
+ TrackPayload track , Properties properties , Map <String , Object > comScoreOptions ) {
279
286
String name = track .event ();
280
287
long playbackPosition = properties .getLong ("playbackPosition" , 0 );
281
288
@@ -291,19 +298,20 @@ private void trackVideoContent(
291
298
contentMapper .put ("podId" , "ns_st_pn" );
292
299
293
300
Map <String , String > contentAsset =
294
- buildContentAsset (properties , comScoreOptions , contentMapper );
301
+ buildContentAsset (properties , comScoreOptions , contentMapper );
295
302
296
303
if (streamingAnalytics == null ) {
297
304
logger .verbose (
298
- "streamingAnalytics instance not initialized correctly. Please call Video Playback Started to initialize." );
305
+ "streamingAnalytics instance not initialized correctly. Please call Video Playback Started to initialize." );
299
306
return ;
300
307
}
301
308
302
309
switch (name ) {
303
310
case "Video Content Started" :
304
- streamingAnalytics .getPlaybackSession ().setAsset (contentAsset );
305
- logger .verbose ("streamingAnalytics.getPlaybackSession().setAsset(%s)" , contentAsset );
306
- streamingAnalytics .notifyPlay (playbackPosition );
311
+ streamingAnalytics .setMetadata (getContentMetadata (contentAsset ));
312
+ logger .verbose ("streamingAnalytics.setMetadata(%s)" , contentAsset );
313
+ streamingAnalytics .startFromPosition (playbackPosition );
314
+ streamingAnalytics .notifyPlay ();
307
315
logger .verbose ("streamingAnalytics.notifyPlay(%s)" , playbackPosition );
308
316
break ;
309
317
@@ -312,25 +320,25 @@ private void trackVideoContent(
312
320
// we need to call setAsset with the content metadata. If ns_st_ad is not present, that means the last
313
321
// observed event was related to content, in which case a setAsset call should not be made (because asset
314
322
// did not change).
315
- if (streamingAnalytics .getPlaybackSession (). getAsset ().containsLabel ("ns_st_ad" )) {
316
- streamingAnalytics .getPlaybackSession (). setAsset (contentAsset );
317
- logger .verbose ("streamingAnalytics.getPlaybackSession().setAsset (%s)" , contentAsset );
323
+ if (streamingAnalytics .getConfiguration ().containsLabel ("ns_st_ad" )) {
324
+ streamingAnalytics .setMetadata ( getContentMetadata (contentAsset ) );
325
+ logger .verbose ("streamingAnalytics.setMetadata (%s)" , contentAsset );
318
326
}
319
327
320
- streamingAnalytics .notifyPlay (playbackPosition );
328
+ streamingAnalytics .startFromPosition (playbackPosition );
329
+ streamingAnalytics .notifyPlay ();
321
330
logger .verbose ("streamingAnalytics.notifyEnd(%s)" , playbackPosition );
322
-
323
331
break ;
324
332
325
333
case "Video Content Completed" :
326
- streamingAnalytics .notifyEnd (playbackPosition );
334
+ streamingAnalytics .notifyEnd ();
327
335
logger .verbose ("streamingAnalytics.notifyEnd(%s)" , playbackPosition );
328
336
break ;
329
337
}
330
338
}
331
339
332
340
public void trackVideoAd (
333
- TrackPayload track , Properties properties , Map <String , Object > comScoreOptions ) {
341
+ TrackPayload track , Properties properties , Map <String , Object > comScoreOptions ) {
334
342
String name = track .event ();
335
343
long playbackPosition = properties .getLong ("playbackPosition" , 0 );
336
344
@@ -343,7 +351,7 @@ public void trackVideoAd(
343
351
344
352
if (streamingAnalytics == null ) {
345
353
logger .verbose (
346
- "streamingAnalytics instance not initialized correctly. Please call Video Playback Started to initialize." );
354
+ "streamingAnalytics instance not initialized correctly. Please call Video Playback Started to initialize." );
347
355
return ;
348
356
}
349
357
@@ -352,29 +360,41 @@ public void trackVideoAd(
352
360
// The ID for content is not available on Ad Start events, however it will be available on the current
353
361
// StreamingAnalytics's asset. This is because ns_st_ci will have already been set on Content Started
354
362
// calls (if this is a mid or post-roll), or on Video Playback Started (if this is a pre-roll).
355
- String contentId = streamingAnalytics .getPlaybackSession ().getAsset ().getLabel ("ns_st_ci" );
356
-
363
+ String contentId = streamingAnalytics .getConfiguration ().getLabel ("ns_st_ci" );
357
364
if (!isNullOrEmpty (contentId )) {
358
365
adAsset .put ("ns_st_ci" , contentId );
359
366
}
360
367
361
- streamingAnalytics .getPlaybackSession ().setAsset (adAsset );
362
- logger .verbose ("streamingAnalytics.getPlaybackSession().setAsset(%s)" , adAsset );
363
- streamingAnalytics .notifyPlay (playbackPosition );
368
+ streamingAnalytics .setMetadata (getAdvertisementMetadata (adAsset ));
369
+ logger .verbose ("streamingAnalytics.setMetadata(%s)" , adAsset );
370
+ streamingAnalytics .startFromPosition (playbackPosition );
371
+ streamingAnalytics .notifyPlay ();
364
372
logger .verbose ("streamingAnalytics.notifyPlay(%s)" , playbackPosition );
365
373
break ;
366
374
367
375
case "Video Ad Playing" :
368
- streamingAnalytics .notifyPlay (playbackPosition );
376
+ streamingAnalytics .startFromPosition (playbackPosition );
377
+ streamingAnalytics .notifyPlay ();
369
378
logger .verbose ("streamingAnalytics.notifyPlay(%s)" , playbackPosition );
370
379
break ;
371
380
372
381
case "Video Ad Completed" :
373
- streamingAnalytics .notifyEnd (playbackPosition );
382
+ streamingAnalytics .notifyEnd ();
374
383
logger .verbose ("streamingAnalytics.notifyEnd(%s)" , playbackPosition );
375
384
break ;
376
385
}
377
386
}
387
+ private ContentMetadata getContentMetadata (Map <String , String > asset ){
388
+ return new ContentMetadata .Builder ()
389
+ .customLabels (asset )
390
+ .build ();
391
+ }
392
+
393
+ private AdvertisementMetadata getAdvertisementMetadata (Map <String , String > adAsset ){
394
+ return new AdvertisementMetadata .Builder ()
395
+ .customLabels (adAsset )
396
+ .build ();
397
+ }
378
398
379
399
@ Override
380
400
public void track (TrackPayload track ) {
@@ -430,8 +450,8 @@ public void screen(ScreenPayload screen) {
430
450
String name = screen .name ();
431
451
String category = screen .category ();
432
452
HashMap <String , String > properties =
433
- (HashMap <String , String >) //
434
- screen .properties ().toStringMap ();
453
+ (HashMap <String , String >) //
454
+ screen .properties ().toStringMap ();
435
455
properties .put ("name" , name );
436
456
properties .put ("category" , category );
437
457
comScoreAnalytics .notifyViewEvent (properties );
0 commit comments