-
Notifications
You must be signed in to change notification settings - Fork 5
/
mParticleCore.brs
2302 lines (2174 loc) · 106 KB
/
mParticleCore.brs
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
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
'*************************************************************
' mParticle Roku SDK - Core APIs
' Copyright 2019 mParticle, Inc.
'*************************************************************
'
' mParticleConstants() surface various fields for interacting with the mParticle APIs
'
function mParticleConstants() as object
SDK_VERSION = "2.1.18"
LOG_LEVEL = {
NONE: 0,
ERROR: 1,
INFO: 2,
DEBUG: 3
}
ENVIRONMENT = {
AUTO_DETECT: 0,
FORCE_DEVELOPMENT: 1,
FORCE_PRODUCTION: 2
}
'You may pass in any of these options to the mParticle SDK, via mParticleStart
'or via the Global mParticle options field
DEFAULT_OPTIONS = {
apiKey: "",
apiSecret: "",
environment: ENVIRONMENT.AUTO_DETECT,
logLevel: LOG_LEVEL.ERROR,
enablePinning: true,
certificateDir: "pkg:/source/mparticle/mParticleBundle.crt",
sessionTimeoutMillis: 60 * 1000,
batchUploads: false,
}
SCENEGRAPH_NODES = {
API_CALL_NODE: "mParticleApiCall",
CURRENT_USER_NODE: "mParticleCurrentUser",
IDENTITY_RESULT_NODE: "mParticleIdentityResult",
CURRENT_SESSION_NODE: "mParticleCurrentSession"
}
USER_ATTRIBUTES = {
FIRSTNAME: "$FirstName",
LASTNAME: "$LastName",
ADDRESS: "$Address",
STATE: "$State",
CITY: "$City",
ZIPCODE: "$Zip",
COUNTRY: "$Country",
AGE: "$Age",
GENDER: "$Gender",
MOBILE_NUMBER: "$Mobile"
}
MESSAGE_TYPE = {
SESSION_START: "ss",
SESSION_END: "se",
CUSTOM: "e",
SCREEN: "v",
OPT_OUT: "o",
ERROR: "x",
BREADCRUMB: "b",
APP_STATE_TRANSITION: "ast",
COMMERCE: "cm",
USER_ATTRIBUTE_CHANGE: "uac",
USER_IDENTITY_CHANGE: "uic"
}
CUSTOM_EVENT_TYPE = {
NAVIGATION: "navigation",
LOCATION: "location",
SEARCH: "search",
TRANSACTION: "transaction",
USER_CONTENT: "usercontent",
USER_PREFERENCE: "userpreference",
SOCIAL: "social",
OTHER: "other",
MEDIA: "media"
}
MEDIA_EVENT_NAME = {
PLAY: "Play",
PAUSE: "Pause",
CONTENT_END: "Media Content End",
SESSION_START: "Media Session Start",
SESSION_END: "Media Session End",
SEEK_START: "Seek Start",
SEEK_END: "Seek End",
BUFFER_START: "Buffer Start",
BUFFER_END: "Buffer End",
UPDATE_PLAYHEAD_POSITION: "Update Playhead Position",
AD_CLICK: "Ad Click",
AD_BREAK_START: "Ad Break Start",
AD_BREAK_END: "Ad Break End",
AD_START: "Ad Start",
AD_END: "Ad End",
AD_SKIP: "Ad Skip",
SEGMENT_START: "Segment Start",
SEGMENT_SKIP: "Segment Skip",
SEGMENT_END: "Segment End",
UPDATE_QOS: "Update QoS",
MILESTONE: "Milestone",
SESSION_SUMMARY: "Media Session Summary",
AD_SUMMARY: "Ad Session Summary",
SEGMENT_SUMMARY: "Segment Session Summary"
}
MEDIA_CONTENT_TYPE = {
VIDEO: "Video",
AUDIO: "Audio"
}
MEDIA_STREAM_TYPE = {
LIVE_STREAM: "LiveStream",
ON_DEMAND: "OnDemand",
LINEAR: "Linear",
PODCAST: "Podcast",
AUDIOBOOK: "Audiobook"
}
IDENTITY_TYPE_INT = {
OTHER: 0,
CUSTOMER_ID: 1,
FACEBOOK: 2,
TWITTER: 3,
GOOGLE: 4,
MICROSOFT: 5,
YAHOO: 6,
EMAIL: 7,
FACEBOOK_AUDIENCE_ID: 9,
OTHER2: 10,
OTHER3: 11,
OTHER4: 12,
OTHER5: 13,
OTHER6: 14,
OTHER7: 15,
OTHER8: 16,
OTHER9: 17,
OTHER10: 18,
MOBILE_NUMBER: 19,
PHONE_NUMBER2: 20,
PHONE_NUMBER3: 21,
parseString: function(identityTypeString as string) as integer
IDENTITY_TYPE = mparticleconstants().IDENTITY_TYPE
if (identityTypeString = IDENTITY_TYPE.OTHER) then
return m.OTHER
else if (identityTypeString = IDENTITY_TYPE.CUSTOMER_ID) then
return m.CUSTOMER_ID
else if (identityTypeString = IDENTITY_TYPE.FACEBOOK) then
return m.FACEBOOK
else if (identityTypeString = IDENTITY_TYPE.TWITTER) then
return m.TWITTER
else if (identityTypeString = IDENTITY_TYPE.GOOGLE) then
return m.GOOGLE
else if (identityTypeString = IDENTITY_TYPE.MICROSOFT) then
return m.MICROSOFT
else if (identityTypeString = IDENTITY_TYPE.YAHOO) then
return m.YAHOO
else if (identityTypeString = IDENTITY_TYPE.EMAIL) then
return m.EMAIL
else if (identityTypeString = IDENTITY_TYPE.FACEBOOK_AUDIENCE_ID) then
return m.FACEBOOK_AUDIENCE_ID
else if (identityTypeString = IDENTITY_TYPE.OTHER2) then
return m.OTHER2
else if (identityTypeString = IDENTITY_TYPE.OTHER3) then
return m.OTHER3
else if (identityTypeString = IDENTITY_TYPE.OTHER4) then
return m.OTHER4
else if (identityTypeString = IDENTITY_TYPE.OTHER5) then
return m.OTHER5
else if (identityTypeString = IDENTITY_TYPE.OTHER6) then
return m.OTHER6
else if (identityTypeString = IDENTITY_TYPE.OTHER7) then
return m.OTHER7
else if (identityTypeString = IDENTITY_TYPE.OTHER8) then
return m.OTHER8
else if (identityTypeString = IDENTITY_TYPE.OTHER9) then
return m.OTHER9
else if (identityTypeString = IDENTITY_TYPE.OTHER10) then
return m.OTHER10
else if (identityTypeString = IDENTITY_TYPE.MOBILE_NUMBER) then
return m.MOBILE_NUMBER
else if (identityTypeString = IDENTITY_TYPE.PHONE_NUMBER2) then
return m.PHONE_NUMBER2
else if (identityTypeString = IDENTITY_TYPE.PHONE_NUMBER3) then
return m.PHONE_NUMBER3
end if
return 0
end function
}
IDENTITY_TYPE = {
OTHER: "other",
CUSTOMER_ID: "customerid",
FACEBOOK: "facebook",
TWITTER: "twitter",
GOOGLE: "google",
MICROSOFT: "microsoft",
YAHOO: "yahoo",
EMAIL: "email",
FACEBOOK_AUDIENCE_ID: "facebookcustomaudienceid",
OTHER2: "other2",
OTHER3: "other3",
OTHER4: "other4",
OTHER5: "other5",
OTHER6: "other6",
OTHER7: "other7",
OTHER8: "other8",
OTHER9: "other9",
OTHER10: "other10",
MOBILE_NUMBER: "mobile_number",
PHONE_NUMBER2: "phone_number_2",
PHONE_NUMBER3: "phone_number_3",
isValidIdentityType: function(identityType as string) as boolean
allTypes = m.Keys()
mputils = mparticle()._internal.utils
for each idType in allTypes
if (mputils.isString(m[idType]) and m[idType] = identityType) then
return true
end if
end for
return false
end function
}
'
' Consent Management
'
ConsentState = {
build: function()
consentState = {}
consentState.gdpr = {}
consentState.ccpa = {}
return consentState
end function,
' GDPR
addGDPRConsentState: function(consentState as object, purpose as string, gdprConsent as object)
consentState.gdpr.AddReplace(purpose, gdprConsent)
end function,
getCCPAConsentState: function(consentState as object)
return consentState.ccpa
end function,
getGDPRConsentState: function(consentState as object)
return consentState.gdpr
end function,
removeGDPRConsentState: function(consentState as object, purpose as string)
if (consentState.gdpr.DoesExist(purpose)) then
consentState.gdpr.Delete(purpose)
end if
end function,
' CCPA
setCCPAConsentState: function(consentState as object, ccpaConsent as object)
consentState.ccpa.AddReplace("data_sale_opt_out", ccpaConsent)
end function,
removeCCPAConsentState: function(consentState as object)
consentState.ccpa.Delete("data_sale_opt_out")
end function
}
CCPAConsentState = {
build: function(consented as boolean, timestamp as longinteger)
ccpaConsentState = {}
ccpaConsentState.c = consented
ccpaConsentState.ts = timestamp
return ccpaConsentState
end function
setDocument: function(ccpaConsentState as object, document as string)
ccpaConsentState.d = document
end function,
setConsented: function(ccpaConsentState as object, consented as boolean)
ccpaConsentState.c = consented
end function,
setLocation: function(ccpaConsentState as object, location as string)
ccpaConsentState.l = location
end function,
setTimestamp: function(ccpaConsentState as object, timestamp as longinteger)
ccpaConsentState.ts = timestamp
end function,
setHardwareId: function(ccpaConsentState as object, hardwareId as string)
ccpaConsentState.h = hardwareId
end function,
}
GDPRConsentState = {
build: function(consented as boolean, timestamp as longinteger)
gdprConsentState = {}
gdprConsentState.c = consented
gdprConsentState.ts = timestamp
return gdprConsentState
end function,
setDocument: function(gdprConsentState as object, document as string)
gdprConsentState.d = document
end function,
setConsented: function(gdprConsentState as object, consented as boolean)
gdprConsentState.c = consented
end function,
setLocation: function(gdprConsentState as object, location as string)
gdprConsentState.l = location
end function,
setTimestamp: function(gdprConsentState as object, timestamp as longinteger)
gdprConsentState.ts = timestamp
end function,
setHardwareId: function(gdprConsentState as object, hardwareId as string)
gdprConsentState.h = hardwareId
end function,
}
'
' eCommerce APIs
'
PromotionAction = {
ACTION_TYPE: {
VIEW: "view",
CLICK: "click"
},
build: function(actionType as string, promotionList as object)
return {
an: actionType,
pl: promotionList
}
end function,
}
Promotion = {
build: function(promotionId as string, name as string, creative as string, position as string)
return {
id: promotionId,
nm: name,
cr: creative,
ps: position
}
end function
}
Impression = {
build: function(impressionList as string, productList as object)
return {
pil: impressionList,
pl: productList
}
end function
}
ProductAction = {
ACTION_TYPE: {
ADD_TO_CART: "add_to_cart",
REMOVE_FROM_CART: "remove_from_cart",
CHECKOUT: "checkout",
CLICK: "click",
VIEW: "view",
VIEW_DETAIL: "view_detail",
PURCHASE: "purchase",
REFUND: "refund",
ADD_TO_WISHLIST: "add_to_wishlist",
REMOVE_FROM_WISHLIST: "remove_from_wishlist"
},
build: function(actionType as string, totalAmount as double, productList as object)
return {
an: actionType,
tr: totalAmount,
pl: productList
}
end function,
setCheckoutStep: function(productAction as object, checkoutStep as integer)
productAction.cs = checkoutStep
end function,
setCheckoutOptions: function(productAction as object, checkoutOptions as string)
productAction.co = checkoutOptions
end function,
setProductActionList: function(productAction as object, productActionList as string)
productAction.pal = productActionList
end function,
setProductListSource: function(productAction as object, productListSource as string)
productAction.pls = productListSource
end function,
setTransactionId: function(productAction as object, transactionId as string)
productAction.ti = transactionId
end function,
setAffiliation: function(productAction as object, affiliation as string)
productAction.ta = affiliation
end function,
setTaxAmount: function(productAction as object, taxAmount as double)
productAction.tt = taxAmount
end function,
setShippingAmount: function(productAction as object, shippingAmout as double)
productAction.ts = shippingAmout
end function,
setCouponCode: function(productAction as object, couponCode as string)
productAction.cc = couponCode
end function
}
Product = {
build: function(sku as string, name as string, price = 0 as double, quantity = 1 as integer, customAttributes = {} as object)
product = {}
product.id = sku
product.nm = name
product.pr = price
product.qt = quantity
product.tpa = price * quantity
product.attrs = customAttributes
return product
end function,
setBrand: function(product as object, brand as string)
product.cs = brand
end function,
setCategory: function(product as object, category as string)
product.ca = category
end function,
setVariant: function(product as object, variant as string)
product.va = variant
end function,
setPosition: function(product as object, position as integer)
product.ps = position
end function,
setCouponCode: function(product as object, couponCode as string)
product.cc = couponCode
end function
}
'
' Media APIs
'
MediaSession = {
build: function(contentId as string, title as string, contentType as string, streamType as string, duration = 0 as integer)
session = {}
session.contentId = contentId
session.title = title
session.duration = duration
session.contentType = contentType
session.streamType = streamType
session.mediaSessionId = CreateObject("roDeviceInfo").GetRandomUUID()
session.currentPlayheadPosition = 0
session.mediaContentComplete = false
session.mediaContentTimeSpent = 0
session.mediaSessionSegmentTotal = 0
session.mediaSessionAdTotal = 0
session.mediaTotalAdTimeSpent = 0
session.mediaAdTimeSpentRate = 0.0
session.mediaSessionAdObjects = CreateObject("roArray", 0, true)
return session
end function,
setDuration: function(session as object, duration as integer)
session.duration = duration
end function,
setCurrentPlayheadPosition: function(session as object, playheadPosition as integer)
session.currentPlayheadPosition = playheadPosition
end function,
setAdContent: function(session as object, adContent as object)
session.adContent = adContent
end function,
setAdBreak: function(session as object, adBreak as object)
session.adBreak = adBreak
end function,
setSegment: function(session as object, segment as object)
session.segment = segment
end function,
setMediaSessionStartTime: function(session as object, mediaSessionStartTime as longinteger)
session.mediaSessionStartTime = mediaSessionStartTime
end function,
setMediaSessionEndTime: function(session as object, mediaSessionEndTime as longinteger)
session.mediaSessionEndTime = mediaSessionEndTime
end function,
setMediaContentComplete: function(session as object, mediaContentComplete as boolean)
session.mediaContentComplete = mediaContentComplete
end function,
setMediaContentTimeSpent: function(session as object, mediaContentTimeSpent as longinteger)
session.mediaContentTimeSpent = mediaContentTimeSpent
end function,
setMediaSessionSegmentTotal: function(session as object, mediaSessionSegmentTotal as integer)
session.mediaSessionSegmentTotal = mediaSessionSegmentTotal
end function,
setMediaSessionAdTotal: function(session as object, mediaSessionAdTotal as integer)
session.mediaSessionAdTotal = mediaSessionAdTotal
end function,
setMediaTotalAdTimeSpent: function(session as object, mediaTotalAdTimeSpent as longinteger)
session.mediaTotalAdTimeSpent = mediaTotalAdTimeSpent
end function,
setMediaAdTimeSpentRate: function(session as object, mediaAdTimeSpentRate as double)
session.mediaAdTimeSpentRate = mediaAdTimeSpentRate
end function,
setMediaSessionAdObjects: function(session as object, mediaSessionAdObjects as object)
session.mediaSessionAdObjects = mediaSessionAdObjects
end function
}
AdContent = {
build: function(id as string, title as string)
ad = {}
ad.id = id
ad.title = title
ad.adCompleted = false
ad.adSkipped = false
return ad
end function,
setDuration: function(ad as object, duration as integer)
ad.duration = duration
end function,
setAdvertiser: function(ad as object, advertiser as string)
ad.advertiser = advertiser
end function,
setCampaign: function(ad as object, campaign as string)
ad.campaign = campaign
end function,
setCreative: function(ad as object, creative as string)
ad.creative = creative
end function,
setPlacement: function(ad as object, placement as string)
ad.placement = placement
end function,
setPosition: function(ad as object, position as integer)
ad.position = position
end function,
setSiteId: function(ad as object, siteId as string)
ad.siteId = siteId
end function,
setAdStartTime: function(ad as object, adStartTime as longinteger)
ad.adStartTime = adStartTime
end function,
setAdEndTime: function(ad as object, adEndTime as longinteger)
ad.adEndTime = adEndTime
end function,
setAdCompleted: function(ad as object, adCompleted as boolean)
ad.adCompleted = adCompleted
end function,
setAdSkipped: function(ad as object, adSkipped as boolean)
ad.adSkipped = adSkipped
end function
}
AdBreak = {
build: function(id as string, title as string)
adBreak = {}
adBreak.id = id
adBreak.title = title
return adBreak
end function,
setDuration: function(adBreak as object, duration as integer)
adBreak.duration = duration
end function
}
Segment = {
build: function(title as string, index as integer, duration as integer)
segment = {}
segment.title = title
segment.index = index
segment.duration = duration
segment.segmentCompleted = false
segment.segmentSkipped = false
return segment
end function,
setDuration: function(segment as object, duration as integer)
segment.duration = duration
end function,
setSegmentStartTime: function(segment as object, segmentStartTime as longinteger)
segment.segmentStartTime = segmentStartTime
end function,
setSegmentEndTime: function(segment as object, segmentEndTime as longinteger)
segment.segmentEndTime = segmentEndTime
end function,
setSegmentCompleted: function(segment as object, segmentCompleted as boolean)
segment.segmentCompleted = segmentCompleted
end function,
setSegmentSkipped: function(segment as object, segmentSkipped as boolean)
segment.segmentSkipped = segmentSkipped
end function
}
return {
SDK_VERSION: SDK_VERSION,
LOG_LEVEL: LOG_LEVEL,
DEFAULT_OPTIONS: DEFAULT_OPTIONS,
SCENEGRAPH_NODES: SCENEGRAPH_NODES,
MESSAGE_TYPE: MESSAGE_TYPE,
CUSTOM_EVENT_TYPE: CUSTOM_EVENT_TYPE,
IDENTITY_TYPE: IDENTITY_TYPE,
IDENTITY_TYPE_INT: IDENTITY_TYPE_INT,
ENVIRONMENT: ENVIRONMENT,
MEDIA_EVENT_NAME: MEDIA_EVENT_NAME,
MEDIA_CONTENT_TYPE: MEDIA_CONTENT_TYPE,
MEDIA_STREAM_TYPE: MEDIA_STREAM_TYPE,
ProductAction: ProductAction,
Product: Product,
PromotionAction: PromotionAction
Promotion: Promotion,
Impression: Impression,
ConsentState: ConsentState,
CCPAConsentState: CCPAConsentState,
GDPRConsentState: GDPRConsentState,
MediaSession: MediaSession,
AdContent: AdContent,
AdBreak: AdBreak,
Segment: Segment
}
end function
'
' Retrieve a reference to the global mParticle object.
' Note: this only applies to legacy SDK channels. For Scene Graph, you must use the mParticleSGBridge (below)
'
function mParticle() as object
if (getGlobalAA().mParticleInstance = invalid) then
print "mParticle SDK: mParticle() called prior to mParticleStart!"
end if
return getGlobalAA().mParticleInstance
end function
'
' Initialize mParticle with your API key and secret for a Roku Input Configuration
'
' Optionally pass in additional configuration options, see mParticleConstants().DEFAULT_OPTIONS
' Note: this only applies to legacy SDK channels. For Scene Graph, the mParticleTask will call this
'
function mParticleStart(options as object, messagePort as object)
if (getGlobalAA().mparticleInstance <> invalid) then
mplogger = mparticle()._internal.logger
mplogger.info("mParticleStart called twice.")
return mParticle()
end if
mpLogger = {
PREFIX: "mParticle SDK",
debug: function(message as string) as void
m.printlog(mParticleConstants().LOG_LEVEL.DEBUG, message)
end function,
info: function(message as string) as void
m.printlog(mParticleConstants().LOG_LEVEL.INFO, message)
end function,
error: function(message as string) as void
m.printlog(mParticleConstants().LOG_LEVEL.ERROR, message)
end function,
printlog: function(level as integer, message as string) as void
if (mparticle()._internal.configuration.logLevel >= level) then
print "============== " + m.PREFIX + " ====================="
print message
print "=================================================="
end if
end function
}
mpUtils = {
currentChannelVersion: function() as string
info = CreateObject("roDeviceInfo")
osVersion = info.GetOSVersion()
return osVersion["major"] + "." + osVersion["minor"]
end function,
randomGuid: function() as string
return CreateObject("roDeviceInfo").GetRandomUUID()
end function,
unixTimeMillis: function() as longinteger
date = CreateObject("roDateTime")
currentTime = CreateObject("roLongInteger")
currentTime.SetLongInt(date.asSeconds())
return (currentTime * 1000) + date.getMilliseconds()
end function,
isEmpty: function(input as dynamic) as boolean
if input = invalid
return true
else
if (LCase(type(input)) = "roarray" or LCase(type(input)) = "array")
return input.Count() = 0
end if
if (LCase(type(input)) = "rostring" or LCase(type(input)) = "string")
return Len(input) = 0
end if
end if
return false
end function,
isString: function(input as object) as boolean
return input <> invalid and (LCase(type(input)) = "rostring" or LCase(type(input)) = "string")
end function,
isArray: function(input as object) as boolean
return input <> invalid and (LCase(type(input)) = "roarray" or LCase(type(input)) = "array")
end function
}
mpCreateStorage = function()
storage = {}
'channels can share registry when packaged by the same developer token
'token, so include the channel ID
appInfo = CreateObject("roAppInfo")
storage.mpkeys = {
SECTION_NAME: "mparticle_storage_" + appInfo.getid(),
USER_IDENTITIES: "user_identities",
USER_ATTRIBUTES: "user_attributes",
CONSENT_STATE: "consent_state",
CURRENT_MPID: "current_mpid",
COOKIES: "cookies",
SESSION: "saved_session",
CHANNEL_VERSION: "channel_version",
LTV: "ltv",
OPT_OUT: "opt_out",
DAS: "das",
INTEGRATION_ATTRIBUTES: "integration_attributes"
}
storage.section = CreateObject("roRegistrySection", storage.mpkeys.SECTION_NAME)
storage.cleanCookies = sub()
cookies = m.getCookies()
validCookies = {}
nowSeconds = CreateObject("roDateTime").AsSeconds()
for each cookieKey in cookies
expiration = CreateObject("roDateTime")
expiration.FromISO8601String(cookies[cookieKey].e)
if (expiration.AsSeconds() > nowSeconds) then
validCookie = {}
validCookie[cookieKey] = cookies[cookieKey]
validCookies.append(validCookie)
end if
end for
if (validCookies.Count() <> cookies.Count()) then
m.setCookies(validCookies)
end if
end sub
storage.set = function(key as string, value as string) as boolean
return m.section.Write(key, value)
end function
storage.flush = function() as boolean
return m.section.Flush()
end function
storage.get = function(key as string) as string
return m.section.Read(key)
end function
storage.clear = function()
for each key in m.mpkeys
m.section.delete(key)
end for
m.flush()
end function
storage.setUserIdentity = function(mpid as string, identityType as string, identityValue as string) as void
if (not mparticleConstants().IDENTITY_TYPE.isValidIdentityType(identityType)) then
return
end if
identities = m.getUserIdentities(mpid)
identity = identities.Lookup(identityType)
oldIdentity = identities["i"]
if (identity = invalid and identityValue.len() > 0) then
identities[identityType] = mparticle()._internal.internalModel.UserIdentity(identityType, identityValue)
else
if (identityValue.len() = 0) then
identities.Delete(identityType)
else
identities[identityType].i = identityValue
identities[identityType].f = false
end if
end if
m.set(m.mpkeys.USER_IDENTITIES + mpid, FormatJson(identities))
m.flush()
if (oldIdentity <> identityValue) then
mparticle().logMessage(mparticle().model.UserIdentityChange(identityValue, oldIdentity))
end if
end function
storage.getUserIdentities = function(mpid as string) as object
identityJson = m.get(m.mpkeys.USER_IDENTITIES + mpid)
userIdentities = {}
if (not mparticle()._internal.utils.isEmpty(identityJson)) then
userIdentities = ParseJson(identityJson)
end if
return userIdentities
end function
storage.setUserAttribute = function(mpid as string, attributeKey as string, attributeValue as object) as void
attributes = m.getUserAttributes(mpid)
oldValue = attributes[attributeKey]
attributes[attributeKey] = attributeValue
m.set(m.mpkeys.USER_ATTRIBUTES + mpid, FormatJson(attributes))
m.flush()
if (FormatJSON(attributeValue) <> FormatJSON(oldValue)) then
deleted = false
if (mparticle()._internal.utils.isEmpty(attributeValue) or FormatJSON(attributeValue).len() = 0) then
deleted = true
end if
isNewAttribute = false
if (mparticle()._internal.utils.isEmpty(oldValue) or FormatJSON(oldValue).len() = 0) then
isNewAttribute = true
end if
mparticle().logMessage(mparticle().model.UserAttributeChange(attributeKey, attributeValue, oldValue, deleted, isNewAttribute))
end if
end function
storage.removeUserAttribute = function(mpid as string, attributeKey as string) as void
attributes = m.getUserAttributes(mpid)
oldValue = attributes[attributeKey]
attributes.delete(attributeKey)
m.set(m.mpkeys.USER_ATTRIBUTES + mpid, FormatJson(attributes))
m.flush()
mparticle().logMessage(mparticle().model.UserAttributeChange(attributeKey, invalid, oldValue, true, false))
end function
storage.getUserAttributes = function(mpid as string) as object
attributeJson = m.get(m.mpkeys.USER_ATTRIBUTES + mpid)
userAttributes = {}
if (not mparticle()._internal.utils.isEmpty(attributeJson)) then
userAttributes = ParseJson(attributeJson)
end if
return userAttributes
end function
storage.setIntegrationAttribute = function(mpid as string, integrationId as string, attributeKey as string, attributeValue as object) as void
attributes = m.getIntegrationAttributes(mpid)
if (mparticle()._internal.utils.isEmpty(attributes[integrationId])) then
integrationAttributes = {}
else
interationAttributes = attributes[integrationId]
end if
interationAttributes[attributeKey] = attributeValue
attributes[integrationId] = interationAttributes
m.set(m.mpkeys.USER_ATTRIBUTES + mpid, FormatJson(attributes))
m.flush()
end function
storage.getIntegrationAttributes = function(mpid as string) as object
attributeJson = m.get(m.mpkeys.INTEGRATION_ATTRIBUTES + mpid)
integrationAttributes = {}
if (not mparticle()._internal.utils.isEmpty(attributeJson)) then
integrationAttributes = ParseJson(attributeJson)
end if
return integrationAttributes
end function
storage.setConsentState = function(mpid as string, consentState as object) as void
m.set(m.mpkeys.CONSENT_STATE + mpid, FormatJson(consentState))
m.flush()
end function
storage.getConsentState = function(mpid as string) as object
consentJson = m.get(m.mpkeys.CONSENT_STATE + mpid)
consentState = {}
if (not mparticle()._internal.utils.isEmpty(consentJson)) then
consentState = ParseJson(consentJson)
end if
return consentState
end function
storage.setCurrentMpid = function(mpid as string) as void
m.set(m.mpkeys.CURRENT_MPID, mpid)
m.flush()
end function
storage.getCurrentMpid = function() as string
mpid = m.get(m.mpkeys.CURRENT_MPID)
if (mparticle()._internal.utils.isEmpty(mpid)) then
return "0"
end if
return mpid
end function
storage.getDas = function() as string
das = m.get(m.mpkeys.DAS)
if (mparticle()._internal.utils.isEmpty(das)) then
das = mparticle()._internal.utils.randomGuid()
m.setDas(das)
end if
return das
end function
storage.setDas = function(das as string)
m.set(m.mpkeys.DAS, das)
m.flush()
end function
storage.setCookies = function(cookies as object)
currentCookies = m.getCookies()
currentCookies.append(cookies)
m.set(m.mpkeys.COOKIES, FormatJson(currentCookies))
m.flush()
end function
storage.getCookies = function() as object
if (m.cookies = invalid) then
cookieJson = m.get(m.mpkeys.COOKIES)
if (not mparticle()._internal.utils.isEmpty(cookieJson)) then
m.cookies = ParseJson(cookieJson)
end if
if (m.cookies = invalid) then
m.cookies = {}
end if
end if
return m.cookies
end function
storage.setSession = function(session as object)
if (session <> invalid) then
m.set(m.mpkeys.SESSION, FormatJson(session))
m.flush()
end if
end function
storage.getSession = function() as object
sessionJson = m.get(m.mpkeys.SESSION)
if (not mparticle()._internal.utils.isEmpty(sessionJson)) then
return ParseJson(sessionJson)
else
return invalid
end if
end function
storage.getChannelVersion = function() as string
return m.get(m.mpkeys.CHANNEL_VERSION)
end function
storage.setChannelVersion = function(version as string)
m.set(m.mpkeys.CHANNEL_VERSION, version)
m.flush()
end function
storage.setLtv = function(ltv as double)
m.set(m.mpkeys.LTV, ltv.tostr())
m.flush()
end function
storage.getLtv = function() as double
ltv = m.get(m.mpkeys.LTV)
if (not mparticle()._internal.utils.isEmpty(ltv)) then
return ParseJson(ltv)
else
return 0
end if
end function
storage.setOptOut = function(optOut as boolean)
m.set(m.mpkeys.OPT_OUT, optOut.tostr())
m.flush()
end function
storage.getOptOut = function() as boolean
mpOptOut = m.get(m.mpkeys.OPT_OUT)
return "true" = mpOptOut
end function
return storage
end function
mpInternalModel = {
Batch: function(messages as object) as object
currentMpid = mparticle()._internal.storage.getCurrentMpid()
mpBatch = {}
mpBatch.dbg = mparticle()._internal.configuration.development
mpBatch.dt = "h"
mpBatch.mpid = mparticle()._internal.storage.getCurrentMpid()
mpBatch.ltv = mparticle()._internal.storage.getLtv()
mpBatch.id = mParticle()._internal.utils.randomGuid()
mpBatch.ct = mParticle()._internal.utils.unixTimeMillis()
mpBatch.sdk = mParticleConstants().SDK_VERSION
mpBatch.ui = []
identities = mparticle()._internal.storage.getUserIdentities(currentMpid)
for each identity in identities
mpBatch.ui.push(identities[identity])
end for
mpBatch.ua = mparticle()._internal.storage.getUserAttributes(currentMpid)
mpBatch.msgs = messages
mpBatch.ai = m.ApplicationInformation()
mpBatch.di = m.DeviceInformation()
mpBatch.ck = mparticle()._internal.storage.getCookies()
mpBatch.das = mparticle()._internal.storage.getDas()
mpBatch.con = mparticle()._internal.storage.getConsentState(currentMpid)
mpBatch.ia = mparticle()._internal.storage.getIntegrationAttributes(currentMpid)
mplogger = mparticle()._internal.logger
if (mparticle()._internal.configuration.dataPlanId <> invalid) then
if ((LCase(type(mparticle()._internal.configuration.dataPlanId))) = "rostring") then
mpBatch.ctx = {}
mpBatch.ctx.dpln = {}
mpBatch.ctx.dpln.id = mparticle()._internal.configuration.dataPlanId
if (mparticle()._internal.configuration.dataPlanVersion <> invalid) then
if ((LCase(type(mparticle()._internal.configuration.dataPlanVersion))) = "roint") then
mpBatch.ctx.dpln.v = mparticle()._internal.configuration.dataPlanVersion
else
mplogger.error("Your data plan version must be a integer.")
end if
end if
else
mplogger.error("Your data plan id must be a string.")
end if
end if
return mpBatch
end function,
UserIdentity: function(identityType as string, identityValue as string) as object
return {
n: mParticleConstants().IDENTITY_TYPE_INT.parseString(identityType),
i: identityValue,
dfs: mparticle()._internal.utils.unixTimeMillis(),
f: true
}
end function,
ApplicationInformation: function() as object