forked from segmentio/analytics-swift
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ComscoreDestination.swift
692 lines (561 loc) · 26.4 KB
/
ComscoreDestination.swift
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
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
//
// ComscoreDestination.swift
// ComscoreDestination
//
// Created by Cody Garvin on 9/21/21.
//
import Segment
import ComScore
import CoreMedia
/**
An implementation of the Comscore Analytics device mode destination as a plugin.
*/
// NOTE: You can see this plugin in use in the DestinationsExample application.
//
// This plugin is NOT SUPPORTED by Segment. It is here merely as an example,
// and for your convenience should you find it useful.
//
// Copyright (c) 2022 Segment
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
//
class ComscoreDestination: DestinationPlugin {
let timeline = Timeline()
let type = PluginType.destination
let key = "comScore"
weak var analytics: Analytics? = nil
private var comscoreSettings: ComscoreSettings?
private var comscoreEnrichment: ComscoreEnrichment?
private var streamAnalytics: SCORStreamingAnalytics?
private var configurationLabels = [String: Any]()
func update(settings: Settings, type: UpdateType) {
// Skip if you have a singleton and don't want to keep updating via settings.
guard type == .initial else { return }
// Set up the enrichment plugin
comscoreEnrichment = ComscoreEnrichment()
analytics?.add(plugin: comscoreEnrichment!)
// Grab the settings and assign them for potential later usage.
// Note: Since integrationSettings is generic, strongly type the variable.
guard let tempSettings: ComscoreSettings = settings.integrationSettings(forPlugin: self) else { return }
comscoreSettings = tempSettings
// Set the update mode
if tempSettings.foregroundOnly && tempSettings.autoUpdate {
SCORAnalytics.configuration().usagePropertiesAutoUpdateMode = .foregroundOnly
} else if tempSettings.autoUpdate {
SCORAnalytics.configuration().usagePropertiesAutoUpdateMode = .foregroundAndBackground
} else {
SCORAnalytics.configuration().usagePropertiesAutoUpdateMode = .disabled
}
SCORAnalytics.configuration().usagePropertiesAutoUpdateInterval = Int32(tempSettings.autoUpdateInterval)
SCORAnalytics.configuration().liveTransmissionMode = SCORLiveTransmissionModeLan // No bridge to NS_ENUM, must use full name
SCORAnalytics.configuration().applicationName = tempSettings.appName
// Start off with the partner configuration
let publisherConfiguration = SCORPublisherConfiguration { builder in
builder?.publisherId = tempSettings.c2
builder?.secureTransmissionEnabled = tempSettings.useHTTPS
}
let partnerConfiguration = SCORPartnerConfiguration { builder in
builder?.partnerId = Self.partnerId
}
SCORAnalytics.configuration().addClient(with: partnerConfiguration)
SCORAnalytics.configuration().addClient(with: publisherConfiguration)
SCORAnalytics.start()
}
func identify(event: IdentifyEvent) -> IdentifyEvent? {
if let traits = event.traits?.dictionaryValue {
let mappedTraits = convertToStringFormatFrom(data: traits)
for (key, value) in mappedTraits {
SCORAnalytics.configuration().setPersistentLabelWithName(key, value: value)
analytics?.log(message: "SCORAnalytics.configuration.setPersistentLabelWithName(\(key), value: \(value))",
kind: .debug)
}
}
return event
}
func track(event: TrackEvent) -> TrackEvent? {
// Determine if there are any video streaming calls, one is executed
// return so the hiddenEvent can not be fired
if let properties = event.properties {
if parsedVideoInstruction(event: event, properties: properties) {
return event
}
}
var hiddenLabels = ["name": event.event]
if let properties = event.properties?.dictionaryValue {
hiddenLabels.merge(convertToStringFormatFrom(data: properties)) { _, new in
new
}
}
SCORAnalytics.notifyHiddenEvent(withLabels: hiddenLabels)
analytics?.log(message: "SCORAnalytics.notifyHiddenEvent(withLabels: \(hiddenLabels))",
kind: .debug)
return event
}
func screen(event: ScreenEvent) -> ScreenEvent? {
if let eventName = event.name {
let viewLabels = ["name": eventName]
SCORAnalytics.notifyViewEvent(withLabels: viewLabels)
analytics?.log(message: "SCORAnalytics.notifyViewEvent(withLabels: \(viewLabels)", kind: .debug)
}
return event
}
func flush() {
SCORAnalytics.flushOfflineCache()
analytics?.log(message: "SCORAnalytics.flushOfflineCache()", kind: .debug)
}
}
// Example of what settings may look like.
private struct ComscoreSettings: Codable {
let c2: String
let autoUpdate: Bool
let autoUpdateInterval: Int
let useHTTPS: Bool
let appName: String
let publisherSecret: String?
let foregroundOnly: Bool
}
// Rules for converting keys and values to the proper formats that bridge
// from Segment to the Partner SDK. These are only examples.
private extension ComscoreDestination {
static let partnerId = "23243060"
static let adPropertiesMap = [
"adClassificationType": "ns_st_ct",
"assetId": "ns_st_ami",
"asset_id": "ns_st_ami",
"c3": "c3",
"c4": "c4",
"c6": "c6",
"publisher": "ns_st_pu",
"title": "ns_st_amt",
"totalLength": "ns_st_cl",
"total_length": "ns_st_cl",
"type": "ns_st_ad",
]
static let contentPropertiesMap = [
"assetId": "ns_st_ci",
"asset_id": "ns_st_ci",
"channel": "ns_st_st",
"contentClassificationType": "ns_st_ct",
"c3": "c3",
"c4": "c4",
"c6": "c6",
"digitalAirdate": "ns_st_ddt",
"episode": "ns_st_en",
"fullEpisode": "ns_st_ce",
"full_episode": "ns_st_ce",
"genre": "ns_st_ge",
"podId": "ns_st_pn",
"pod_id": "ns_st_pn",
"program": "ns_st_pr",
"publisher": "ns_st_pu",
"season": "ns_st_sn",
"title": "ns_st_ep",
"totalLength": "ns_st_cl",
"total_length": "ns_st_cl",
"tvAirdate": "ns_st_tdt",
]
static let playbackPropertiesMap = [
"bitrate": "ns_st_br",
"full_screen": "ns_st_ws",
"fullScreen": "ns_st_ws",
"fullscreen": "ns_st_ws",
"sound": "ns_st_vo",
"videoPlayer": "ns_st_mp",
"video_player": "ns_st_mp",
"c3": "c3",
"c4": "c4",
"c6": "c6"
]
func convertToStringFormatFrom(data: Dictionary<String, Any>) -> Dictionary<String, String> {
var returnDictionary = [String: String]()
for (key, value) in data {
var updatedValue = value
if isValid(data: updatedValue) {
if let arrayData = updatedValue as? Array<String> {
updatedValue = arrayData.joined(separator: ",")
}
returnDictionary[key] = "\(updatedValue)"
}
}
return returnDictionary
}
func isValid(data: Any) -> Bool {
var result = data is String || (data is Array<Any> && (data as! Array<Any>).count > 0) || data is Int || data is Double
if result, let tempData = data as? String {
result = !tempData.isEmpty
}
return result
}
func parsedVideoInstruction(event: TrackEvent, properties: JSON) -> Bool {
switch event.event {
case "Video Playback Started":
videoPlaybackStarted(event: event, properties: properties)
return true
case "Video Playback Paused",
"Video Playback Interrupted":
videoPlaybackPaused(event: event, properties: properties)
return true
case "Video Playback Buffer Started":
videoPlaybackBufferStarted(event: event, properties: properties)
return true
case "Video Playback Buffer Completed":
videoPlaybackBufferCompleted(event: event, properties: properties)
return true
case "Video Playback Seek Started":
videoPlaybackSeekStarted(event: event, properties: properties)
return true
case "Video Playback Seek Completed",
"Video Playback Resumed":
videoPlaybackSeekCompleted(event: event, properties: properties)
return true
case "Video Content Started":
videoContentStarted(event: event, properties: properties)
return true
case "Video Content Playing":
videoContentPlaying(event: event, properties: properties)
return true
case "Video Content Completed":
videoContentCompleted(event: event, properties: properties)
return true
case "Video Ad Started":
videoAdStarted(event: event, properties: properties)
return true
case "Video Ad Playing":
videoAdPlaying(event: event, properties: properties)
return true
case "Video Ad Completed":
videoAdCompleted(event: event, properties: properties)
return true
default:
analytics?.log(message: "No video track calls", kind: .debug)
}
return false
}
// MARK: - Playback methods
// MARK: -
func videoPlaybackStarted(event: TrackEvent, properties: JSON) {
streamAnalytics = SCORStreamingAnalytics()
let convertedProperties = properties.dictionaryValue
let map = ["ns_st_mp" : "\(convertedProperties?["video_player"] ?? "*null")",
"ns_st_ci" : "\(convertedProperties?["content_asset_id"] ?? "0")"]
streamAnalytics?.createPlaybackSession()
streamAnalytics?.configuration().addLabels(map)
streamAnalytics?.setMetadata(instantiateContentMetaData(properties: map))
configurationLabels = configurationLabels.merging(map) { $1 }
analytics?.log(message: "streamAnalytics.createPlaybackSessionWithLabels: \(map)", kind: .debug)
}
func videoPlaybackPaused(event: TrackEvent, properties: JSON) {
if let map = mappedPlaybackProperties(event: event, properties: properties) {
streamAnalytics?.configuration().addLabels(map)
configurationLabels = configurationLabels.merging(map) { $1 }
streamAnalytics?.setMetadata(instantiateContentMetaData(properties: map))
}
analytics?.log(message: "streamAnalytics.notifyPause", kind: .debug)
}
func videoPlaybackBufferStarted(event: TrackEvent, properties: JSON) {
if let map = mappedPlaybackProperties(event: event, properties: properties) {
streamAnalytics?.configuration().addLabels(map)
configurationLabels = configurationLabels.merging(map) { $1 }
streamAnalytics?.setMetadata(instantiateContentMetaData(properties: map))
}
movePosition(properties)
streamAnalytics?.notifyBufferStart()
analytics?.log(message: "streamAnalytics.notifyBufferStart", kind: .debug)
}
func videoPlaybackBufferCompleted(event: TrackEvent, properties: JSON) {
if let map = mappedPlaybackProperties(event: event, properties: properties) {
streamAnalytics?.configuration().addLabels(map)
configurationLabels = configurationLabels.merging(map) { $1 }
streamAnalytics?.setMetadata(instantiateContentMetaData(properties: map))
}
movePosition(properties)
streamAnalytics?.notifyBufferStop()
analytics?.log(message: "streamAnalytics.notifyBufferStop", kind: .debug)
}
func videoPlaybackSeekStarted(event: TrackEvent, properties: JSON) {
if let map = mappedPlaybackProperties(event: event, properties: properties) {
streamAnalytics?.configuration().addLabels(map)
configurationLabels = configurationLabels.merging(map) { $1 }
streamAnalytics?.setMetadata(instantiateContentMetaData(properties: map))
}
seekPosition(properties)
streamAnalytics?.notifySeekStart()
analytics?.log(message: "streamAnalytics.notifySeekStart", kind: .debug)
}
func videoPlaybackSeekCompleted(event: TrackEvent, properties: JSON) {
if let map = mappedPlaybackProperties(event: event, properties: properties) {
streamAnalytics?.configuration().addLabels(map)
configurationLabels = configurationLabels.merging(map) { $1 }
streamAnalytics?.setMetadata(instantiateContentMetaData(properties: map))
}
seekPosition(properties)
streamAnalytics?.notifyPlay()
analytics?.log(message: "streamAnalytics.notifyPlay", kind: .debug)
}
// MARK: - Content Methods
func videoContentStarted(event: TrackEvent, properties: JSON) {
if let map = mappedContentProperties(event: event, properties: properties) {
streamAnalytics?.configuration().addLabels(map)
configurationLabels = configurationLabels.merging(map) { $1 }
streamAnalytics?.setMetadata(instantiateContentMetaData(properties: map))
}
movePosition(properties)
streamAnalytics?.notifyPlay()
analytics?.log(message: "streamAnalytics.notifyPlay", kind: .debug)
}
func videoContentPlaying(event: TrackEvent, properties: JSON) {
if let map = mappedContentProperties(event: event, properties: properties) {
streamAnalytics?.configuration().addLabels(map)
configurationLabels = configurationLabels.merging(map) { $1 }
// The presence of ns_st_ad on the StreamingAnalytics's asset means that we just exited an ad break, so
// we need to call setAsset with the content metadata. If ns_st_ad is not present, that means the last
// observed event was related to content, in which case a setAsset call should not be made (because asset
// did not change).
if let _ = configurationLabels["ns_st_ad"] {
streamAnalytics?.setMetadata(instantiateContentMetaData(properties: map))
}
}
movePosition(properties)
streamAnalytics?.notifyPlay()
analytics?.log(message: "streamAnalytics.notifyPlay", kind: .debug)
}
func videoContentCompleted(event: TrackEvent, properties: JSON) {
streamAnalytics?.notifyEnd()
configurationLabels = [String: Any]()
analytics?.log(message: "streamAnalytics.notifyPlay", kind: .debug)
}
// MARK: - Ad Methods
func videoAdStarted(event: TrackEvent, properties: JSON) {
// The ID for content is not available on Ad Start events, however it will be available on the current
// StreamingAnalytics's asset. This is because ns_st_ci will have already been set via asset_id in a
// Content Started calls (if this is a mid or post-roll), or via content_asset_id on Video Playback
// Started (if this is a pre-roll).
let contentId = configurationLabels["ns_st_ci"] as? String ?? "0"
if var map = mappedAdProperties(event: event, properties: properties) {
map["ns_st_ci"] = contentId
let contentMetadata = instantiateContentMetaData(properties: map)
if let tempProperties = properties.dictionaryValue {
var mediaType = SCORStreamingAdvertisementType.other
if let adType = tempProperties["type"] as? String {
if adType == "pre-roll" {
mediaType = SCORStreamingAdvertisementType.onDemandPreRoll
} else if adType == "mid-roll" {
mediaType = SCORStreamingAdvertisementType.onDemandMidRoll
} else if adType == "post-roll" {
mediaType = SCORStreamingAdvertisementType.onDemandPostRoll
}
}
let advertisingMetadata = SCORStreamingAdvertisementMetadata { builder in
builder?.setMediaType(mediaType)
builder?.setCustomLabels(map)
builder?.setRelatedContentMetadata(contentMetadata)
}
streamAnalytics?.setMetadata(advertisingMetadata)
}
}
movePosition(properties)
streamAnalytics?.notifyPlay()
analytics?.log(message: "streamAnalytics.notifyPlay", kind: .debug)
}
func videoAdPlaying(event: TrackEvent, properties: JSON) {
movePosition(properties)
streamAnalytics?.notifyPlay()
analytics?.log(message: "streamAnalytics.notifyPlay", kind: .debug)
}
func videoAdCompleted(event: TrackEvent, properties: JSON) {
movePosition(properties)
streamAnalytics?.notifyEnd()
analytics?.log(message: "streamAnalytics.notifyEnd", kind: .debug)
}
}
// MARK: - Helper methods
private extension ComscoreDestination {
func mappedPlaybackProperties(event: TrackEvent, properties: JSON) -> Dictionary<String, String>? {
// Pull out the values for the event out of the enrichment plugin
let options = comscoreEnrichment?.fetchAndRemoveMetricsFor(key: event.messageId ?? "0")
var bps = "*null"
var fullscreen = "norm"
if let tempProperties = properties.dictionaryValue {
bps = convertFromKBPSToBPS(source: tempProperties, key: "bitrate")
fullscreen = returnFullScreenStatus(source: tempProperties, key: "full_screen")
}
let returnMap = try? properties.mapTransform(Self.playbackPropertiesMap) { key, value in
switch key {
case "c3", "c4", "c6":
return "\(options?[key] ?? "*null")"
case "ns_st_br":
return bps
case "ns_st_ws":
return fullscreen
default:
return value
}
}
return returnMap?.dictionaryValue as? [String: String]
}
func mappedContentProperties(event: TrackEvent, properties: JSON) -> Dictionary<String, String>? {
// Pull out the values for the event out of the enrichment plugin
let options = comscoreEnrichment?.fetchAndRemoveMetricsFor(key: event.messageId ?? "0")
var totalLength = "0"
if let tempProperties = properties.dictionaryValue {
totalLength = convertFromSecondsToMilliseconds(source: tempProperties, key: "total_length")
}
let digitalAirdate = "\(options?["digitalAirdate"] ?? "*null")"
let tvAirdate = "\(options?["tvAirdate"] ?? "*null")"
let contentClassification = "\(options?["contentClassificationType"] ?? "vc00")"
let returnMap = try? properties.mapTransform(Self.contentPropertiesMap, valueTransform: { key, value in
switch key {
case "c3", "c4", "c6":
return "\(options?[key] ?? "*null")"
case "ns_st_ddt":
return digitalAirdate
case "ns_st_tdt":
return tvAirdate
case "ns_st_ct":
return contentClassification
case "ns_st_cl":
return totalLength
default:
return value
}
})
return returnMap?.dictionaryValue as? [String: String]
}
func mappedAdProperties(event: TrackEvent, properties: JSON) -> Dictionary<String, String>? {
// Pull out the values for the event out of the enrichment plugin
let options = comscoreEnrichment?.fetchAndRemoveMetricsFor(key: event.messageId ?? "0")
var adType = "1"
var totalLength = "0"
if let tempProperties = properties.dictionaryValue {
adType = defaultAdType(source: tempProperties, key: "type")
totalLength = convertFromSecondsToMilliseconds(source: tempProperties, key: "total_length")
}
let adClassification = "\(options?["adClassificationType"] ?? "va00")"
let returnMap = try? properties.mapTransform(Self.adPropertiesMap, valueTransform: { key, value in
switch key {
case "c3", "c4", "c6":
return "\(options?[key] ?? "*null")"
case "ns_st_ct":
return adClassification
case "ns_st_ad":
return adType
case "ns_st_cl":
return totalLength
default:
return value
}
})
return returnMap?.dictionaryValue as? [String: String]
}
// comScore expects bitrate to converted from KBPS be BPS
func convertFromKBPSToBPS(source: [String: Any], key: String) -> String {
var returnValue = "*null"
if let kbps = source[key] as? Int {
returnValue = "\(kbps * 1000)"
} else if let kbps = source[key] as? String,
let kbpsValue = Int(kbps) {
returnValue = "\(kbpsValue * 1000)"
}
return returnValue
}
// comScore expects milliseconds to be converted from seconds to milliseconds
func convertFromSecondsToMilliseconds(source: [String: Any], key: String) -> String {
var returnValue = "0"
if let seconds = source[key] as? Int {
returnValue = "\(seconds * 1000)"
} else if let seconds = source[key] as? String,
let secondsValue = Int(seconds) {
returnValue = "\(secondsValue * 1000)"
}
return returnValue
}
func returnFullScreenStatus(source: [String: Any], key: String) -> String {
if let value = source[key] as? Bool,
value == true {
return "full"
} else {
return "norm"
}
}
func instantiateContentMetaData(properties: [String: Any]) -> SCORStreamingContentMetadata? {
let contentMetaData = SCORStreamingContentMetadata { builder in
builder?.setCustomLabels(properties)
if properties.keys.contains("ns_st_ge") {
builder?.setGenreName("ns_st_ge")
}
}
return contentMetaData
}
func movePosition(_ properties: JSON) {
if let position = properties.dictionaryValue?["position"] as? Int {
streamAnalytics?.start(fromPosition: position)
}
}
func seekPosition(_ properties: JSON) {
if let position = properties.dictionaryValue?["seek_position"] as? Int {
streamAnalytics?.start(fromPosition: position)
}
}
func defaultAdType(source: [String: Any], key: String) -> String {
var returnAdType = "1"
if let value = source[key] as? String {
if value == "pre-roll" || value == "mid-roll" || value == "post-roll" {
returnAdType = value
}
}
return returnAdType
}
}
// MARK: - Comscore Enrichment Plugin
private class ComscoreEnrichment: EventPlugin {
var type: PluginType = .before
var analytics: Analytics?
static let comscoreKey = "videoMetricDictionaryClassification"
// The dictionary being stored off should look like:
private var videoMetrics = [String: [String: String]]()
func track(event: TrackEvent) -> TrackEvent? {
var returnEvent = event
if let properties = event.properties?.dictionaryValue,
var comscoreProperties = properties[Self.comscoreKey] as? [String: String],
!comscoreProperties.isEmpty, let messageId = event.messageId {
// Store off the options
videoMetrics[messageId] = comscoreProperties
// Filter out the properties
comscoreProperties = comscoreProperties.filter { (key, value) in
return key != Self.comscoreKey
}
// Build the updated comscore properties back into JSON
do {
returnEvent.properties = try JSON(comscoreProperties)
} catch {
analytics?.log(message: "Could not convert comscore properties", kind: .debug)
}
}
return returnEvent
}
/// Fetches the appropriate options payload if one existed for that particular message Id
/// - Parameter key: The messageId related to the original event
/// - Returns: An optional payload if one is found for the messageId
func fetchAndRemoveMetricsFor(key: String) -> [String: String]? {
let returnMetrics = videoMetrics[key]
// Remove the metrics now
videoMetrics.removeValue(forKey: key)
return returnMetrics
}
}