-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathCDVMParticle.m
More file actions
720 lines (608 loc) · 30.4 KB
/
Copy pathCDVMParticle.m
File metadata and controls
720 lines (608 loc) · 30.4 KB
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
#import <Cordova/CDV.h>
@import mParticle_Apple_SDK;
@interface CDVMParticle : CDVPlugin
@end
@implementation CDVMParticle
- (void)logEvent:(CDVInvokedUrlCommand*)command {
[self.commandDelegate runInBackground:^{
NSString *eventName = [command.arguments objectAtIndex:0];
NSInteger type = [[command.arguments objectAtIndex:1] intValue];
NSDictionary *attributes = [command.arguments objectAtIndex:2];
[[MParticle sharedInstance] logEvent:eventName eventType:type eventInfo:attributes];
CDVPluginResult* pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK];
[self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
}];
}
- (void)logMPEvent:(CDVInvokedUrlCommand*)command {
[self.commandDelegate runInBackground:^{
NSMutableDictionary *serializedEvent = [command.arguments objectAtIndex:0];
MPEvent *event = [CDVMParticle MPEvent:serializedEvent];
[[MParticle sharedInstance] logEvent:event];
CDVPluginResult* pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK];
[self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
}];
}
- (void)logCommerceEvent:(CDVInvokedUrlCommand*)command {
[self.commandDelegate runInBackground:^{
NSMutableDictionary *serializedCommerceEvent = [command.arguments objectAtIndex:0];
MPCommerceEvent *commerceEvent = [CDVMParticle MPCommerceEvent:serializedCommerceEvent];
[[MParticle sharedInstance] logEvent:commerceEvent];
CDVPluginResult* pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK];
[self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
}];
}
- (void)logScreenEvent:(CDVInvokedUrlCommand*)command {
[self.commandDelegate runInBackground:^{
NSString *screenName = [command.arguments objectAtIndex:0];
NSDictionary *attributes = [command.arguments objectAtIndex:1];
[[MParticle sharedInstance] logScreen:screenName eventInfo:attributes];
CDVPluginResult* pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK];
[self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
}];
}
- (void)setATTStatus:(CDVInvokedUrlCommand*)command {
[self.commandDelegate runInBackground:^{
NSInteger status = [[command.arguments objectAtIndex:0] intValue];
NSNumber *timestamp = [command.arguments objectAtIndex:1];
[[MParticle sharedInstance] setATTStatus:status withATTStatusTimestampMillis:timestamp];
CDVPluginResult* pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK];
[self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
}];
}
- (void)addGDPRConsentState:(CDVInvokedUrlCommand*)command {
[self.commandDelegate runInBackground:^{
NSMutableDictionary *serializedConsent = [command.arguments objectAtIndex:0];
NSString *purpose = [command.arguments objectAtIndex:1];
MPGDPRConsent *consent = [CDVMParticle MPGDPRConsent:serializedConsent];
MPConsentState *consentState = [[MParticle sharedInstance].identity.currentUser.consentState copy];
[consentState addGDPRConsentState:consent purpose:purpose];
[MParticle sharedInstance].identity.currentUser.consentState = consentState;
CDVPluginResult* pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK];
[self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
}];
}
- (void)removeGDPRConsentStateWithPurpose:(CDVInvokedUrlCommand*)command {
[self.commandDelegate runInBackground:^{
NSString *purpose = [command.arguments objectAtIndex:0];
[[MParticle sharedInstance].identity.currentUser.consentState removeGDPRConsentStateWithPurpose:purpose];
CDVPluginResult* pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK];
[self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
}];
}
- (void)addCCPAConsentState:(CDVInvokedUrlCommand*)command {
[self.commandDelegate runInBackground:^{
NSMutableDictionary *serializedConsent = [command.arguments objectAtIndex:0];
MPCCPAConsent *consent = [CDVMParticle MPCCPAConsent:serializedConsent];
MPConsentState *consentState = [[MParticle sharedInstance].identity.currentUser.consentState copy];
[consentState setCCPAConsentState:consent];
[MParticle sharedInstance].identity.currentUser.consentState = consentState;
CDVPluginResult* pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK];
[self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
}];
}
- (void)removeCCPAConsentState:(CDVInvokedUrlCommand*)command {
[self.commandDelegate runInBackground:^{
[[MParticle sharedInstance].identity.currentUser.consentState removeCCPAConsentState];
CDVPluginResult* pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK];
[self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
}];
}
- (void)setUserAttribute:(CDVInvokedUrlCommand*)command {
[self.commandDelegate runInBackground:^{
NSString *userId = [command.arguments objectAtIndex:0];
NSString *key = [command.arguments objectAtIndex:1];
NSString *value = [command.arguments objectAtIndex:2];
MParticleUser *selectedUser = [[[MParticle sharedInstance] identity] getUser:[NSNumber numberWithLong:userId.longLongValue]];
if (selectedUser != nil) {
[selectedUser setUserAttribute:key value:value];
}
CDVPluginResult* pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK];
[self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
}];
}
- (void)setUserAttributeArray:(CDVInvokedUrlCommand*)command {
[self.commandDelegate runInBackground:^{
NSString *userId = [command.arguments objectAtIndex:0];
NSString *key = [command.arguments objectAtIndex:1];
NSArray *values = [command.arguments objectAtIndex:2];
MParticleUser *selectedUser = [[[MParticle sharedInstance] identity] getUser:[NSNumber numberWithLong:userId.longLongValue]];
if (selectedUser != nil) {
[selectedUser setUserAttributeList:key values:values];
}
CDVPluginResult* pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK];
[self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
}];
}
- (void)setUserTag:(CDVInvokedUrlCommand*)command {
[self.commandDelegate runInBackground:^{
NSString *userId = [command.arguments objectAtIndex:0];
NSString *tag = [command.arguments objectAtIndex:1];
MParticleUser *selectedUser = [[[MParticle sharedInstance] identity] getUser:[NSNumber numberWithLong:userId.longLongValue]];
if (selectedUser != nil) {
[selectedUser setUserTag:tag];
}
CDVPluginResult* pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK];
[self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
}];
}
- (void)removeUserAttribute:(CDVInvokedUrlCommand*)command {
[self.commandDelegate runInBackground:^{
NSString *userId = [command.arguments objectAtIndex:0];
NSString *key = [command.arguments objectAtIndex:1];
MParticleUser *selectedUser = [[[MParticle sharedInstance] identity] getUser:[NSNumber numberWithLong:userId.longLongValue]];
if (selectedUser != nil) {
[selectedUser removeUserAttribute:key];
}
CDVPluginResult* pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK];
[self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
}];
}
- (void)identify:(CDVInvokedUrlCommand*)command {
[self.commandDelegate runInBackground:^{
NSMutableDictionary *serializedrequest = [command.arguments objectAtIndex:0];
MPIdentityApiRequest *request = [CDVMParticle MPIdentityApiRequest:serializedrequest];
[[[MParticle sharedInstance] identity] identify:request completion:^(MPIdentityApiResult * _Nullable apiResult, NSError * _Nullable error) {
CDVPluginResult *pluginResult = nil;
NSMutableDictionary *cordovaError;
if (error) {
cordovaError = [[NSMutableDictionary alloc] initWithCapacity:4];
if ([NSNumber numberWithInteger:error.code] != nil) {
[cordovaError setObject:[NSNumber numberWithInteger:error.code] forKey:@"httpCode"];
}
if (error.localizedDescription != nil) {
[cordovaError setObject:error.localizedDescription forKey:@"message"];
}
pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsDictionary:cordovaError];
[self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
} else {
pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsString:apiResult.user.userId.stringValue];
[self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
}
}];
}];
}
- (void)login:(CDVInvokedUrlCommand*)command {
[self.commandDelegate runInBackground:^{
NSMutableDictionary *serializedrequest = [command.arguments objectAtIndex:0];
MPIdentityApiRequest *request = [CDVMParticle MPIdentityApiRequest:serializedrequest];
[[[MParticle sharedInstance] identity] login:request completion:^(MPIdentityApiResult * _Nullable apiResult, NSError * _Nullable error) {
CDVPluginResult *pluginResult = nil;
NSMutableDictionary *cordovaError;
if (error) {
cordovaError = [[NSMutableDictionary alloc] initWithCapacity:4];
if ([NSNumber numberWithInteger:error.code] != nil) {
[cordovaError setObject:[NSNumber numberWithInteger:error.code] forKey:@"httpCode"];
}
if (error.localizedDescription != nil) {
[cordovaError setObject:error.localizedDescription forKey:@"message"];
}
pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsDictionary:cordovaError];
[self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
} else {
pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsString:apiResult.user.userId.stringValue];
[self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
}
}];
}];
}
- (void)logout:(CDVInvokedUrlCommand*)command {
[self.commandDelegate runInBackground:^{
NSMutableDictionary *serializedrequest = [command.arguments objectAtIndex:0];
MPIdentityApiRequest *request = [CDVMParticle MPIdentityApiRequest:serializedrequest];
[[[MParticle sharedInstance] identity] logout:request completion:^(MPIdentityApiResult * _Nullable apiResult, NSError * _Nullable error) {
CDVPluginResult *pluginResult = nil;
NSMutableDictionary *cordovaError;
if (error) {
cordovaError = [[NSMutableDictionary alloc] initWithCapacity:4];
if ([NSNumber numberWithInteger:error.code] != nil) {
[cordovaError setObject:[NSNumber numberWithInteger:error.code] forKey:@"httpCode"];
}
if (error.localizedDescription != nil) {
[cordovaError setObject:error.localizedDescription forKey:@"message"];
}
pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsDictionary:cordovaError];
[self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
} else {
pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsString:apiResult.user.userId.stringValue];
[self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
}
}];
}];
}
- (void)modify:(CDVInvokedUrlCommand*)command {
[self.commandDelegate runInBackground:^{
NSMutableDictionary *serializedrequest = [command.arguments objectAtIndex:0];
MPIdentityApiRequest *request = [CDVMParticle MPIdentityApiRequest:serializedrequest];
[[[MParticle sharedInstance] identity] modify:request completion:^(MPIdentityApiResult * _Nullable apiResult, NSError * _Nullable error) {
CDVPluginResult *pluginResult = nil;
NSMutableDictionary *cordovaError;
if (error) {
cordovaError = [[NSMutableDictionary alloc] initWithCapacity:4];
if ([NSNumber numberWithInteger:error.code] != nil) {
[cordovaError setObject:[NSNumber numberWithInteger:error.code] forKey:@"httpCode"];
}
if (error.localizedDescription != nil) {
[cordovaError setObject:error.localizedDescription forKey:@"message"];
}
pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsDictionary:cordovaError];
[self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
} else {
pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsString:apiResult.user.userId.stringValue];
[self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
}
}];
}];
}
- (void)getCurrentUser:(CDVInvokedUrlCommand*)command {
[self.commandDelegate runInBackground:^{
CDVPluginResult *pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsString:[[[MParticle sharedInstance] identity] currentUser].userId.stringValue];
[self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
}];
}
- (void)getUserIdentities:(CDVInvokedUrlCommand*)command {
[self.commandDelegate runInBackground:^{
NSNumber *userId = [command.arguments objectAtIndex:0];
MParticleUser *selectedUser = [[[MParticle sharedInstance] identity] getUser:userId];
// Convert the identities dictionary to use string keys instead of enum values
NSDictionary *identities = [selectedUser identities];
NSMutableDictionary *convertedIdentities = [NSMutableDictionary dictionary];
[identities enumerateKeysAndObjectsUsingBlock:^(NSNumber *key, id value, BOOL *stop) {
NSString *stringKey = [CDVMParticle StringFromIdentityType:(MPIdentity)key.intValue];
if (stringKey) {
convertedIdentities[stringKey] = value;
}
}];
CDVPluginResult *pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsDictionary:convertedIdentities];
[self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
}];
}
- (void)selectPlacements:(CDVInvokedUrlCommand*)command {
[self.commandDelegate runInBackground:^{
NSString *identifier = [command.arguments objectAtIndex:0];
NSDictionary *attributes = [command.arguments objectAtIndex:1];
NSDictionary *configDict = [command.arguments objectAtIndex:2];
MPRoktConfig *config = [[MPRoktConfig alloc] init];
NSString *colorModeStr = configDict[@"colorMode"][@"value"];
if ([colorModeStr isEqualToString:@"LIGHT"]) {
config.colorMode = MPColorModeLight;
} else if ([colorModeStr isEqualToString:@"DARK"]) {
config.colorMode = MPColorModeDark;
} else {
config.colorMode = MPColorModeSystem;
}
NSDictionary *cacheConfig = configDict[@"cacheConfig"];
if (cacheConfig) {
config.cacheDuration = @([cacheConfig[@"cacheDurationInSeconds"] longLongValue]);
config.cacheAttributes = cacheConfig[@"cacheAttributes"];
}
[[MParticle sharedInstance].rokt selectPlacements:identifier
attributes:attributes
embeddedViews:nil
config:config
callbacks:nil];
CDVPluginResult* pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK];
[self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
}];
}
typedef NS_ENUM(NSUInteger, MPCDVCommerceEventAction) {
MPCDVCommerceEventActionAddToCart = 1,
MPCDVCommerceEventActionRemoveFromCart,
MPCDVCommerceEventActionCheckout,
MPCDVCommerceEventActionCheckoutOptions,
MPCDVCommerceEventActionClick,
MPCDVCommerceEventActionViewDetail,
MPCDVCommerceEventActionPurchase,
MPCDVCommerceEventActionRefund,
MPCDVCommerceEventActionAddToWishList,
MPCDVCommerceEventActionRemoveFromWishlist
};
+ (MPCommerceEventAction)MPCommerceEventAction:(NSNumber *)json {
int actionInt = [json intValue];
MPCommerceEventAction action;
switch (actionInt) {
case MPCDVCommerceEventActionAddToCart:
action = MPCommerceEventActionAddToCart;
break;
case MPCDVCommerceEventActionRemoveFromCart:
action = MPCommerceEventActionRemoveFromCart;
break;
case MPCDVCommerceEventActionCheckout:
action = MPCommerceEventActionCheckout;
break;
case MPCDVCommerceEventActionCheckoutOptions:
action = MPCommerceEventActionCheckoutOptions;
break;
case MPCDVCommerceEventActionClick:
action = MPCommerceEventActionClick;
break;
case MPCDVCommerceEventActionViewDetail:
action = MPCommerceEventActionViewDetail;
break;
case MPCDVCommerceEventActionPurchase:
action = MPCommerceEventActionPurchase;
break;
case MPCDVCommerceEventActionRefund:
action = MPCommerceEventActionRefund;
break;
case MPCDVCommerceEventActionAddToWishList:
action = MPCommerceEventActionAddToWishList;
break;
case MPCDVCommerceEventActionRemoveFromWishlist:
action = MPCommerceEventActionRemoveFromWishlist;
break;
default:
action = MPCommerceEventActionAddToCart;
NSAssert(NO, @"Invalid commerce event action");
break;
}
return action;
}
+ (MPCommerceEvent *)MPCommerceEvent:(NSMutableDictionary *)json {
BOOL isProductAction = json[@"productActionType"] != nil;
BOOL isPromotion = json[@"promotionActionType"] != nil;
BOOL isImpression = json[@"impressions"] != nil;
BOOL isValid = isProductAction || isPromotion || isImpression;
MPCommerceEvent *commerceEvent = nil;
if (!isValid) {
NSAssert(NO, @"Invalid commerce event");
return commerceEvent;
}
if (isProductAction) {
MPCommerceEventAction action = [CDVMParticle MPCommerceEventAction:json[@"productActionType"]];
commerceEvent = [[MPCommerceEvent alloc] initWithAction:action];
}
else if (isPromotion) {
MPPromotionContainer *promotionContainer = [CDVMParticle MPPromotionContainer:json];
commerceEvent = [[MPCommerceEvent alloc] initWithPromotionContainer:promotionContainer];
}
else {
commerceEvent = [[MPCommerceEvent alloc] initWithImpressionName:nil product:nil];
}
commerceEvent.checkoutOptions = json[@"checkoutOptions"];
commerceEvent.currency = json[@"currency"];
commerceEvent.productListName = json[@"productActionListName"];
commerceEvent.productListSource = json[@"productActionListSource"];
commerceEvent.screenName = json[@"screenName"];
commerceEvent.transactionAttributes = [CDVMParticle MPTransactionAttributes:json[@"transactionAttributes"]];\
if (json[@"productActionType"] != nil) {
commerceEvent.action = [CDVMParticle MPCommerceEventAction:json[@"productActionType"]];
}
if (json[@"checkoutStep"] != nil) {
commerceEvent.checkoutStep = [json[@"checkoutStep"] intValue];
}
commerceEvent.nonInteractive = [json[@"nonInteractive"] boolValue];
if (json[@"shouldUploadEvent"] != nil) {
commerceEvent.shouldUploadEvent = [json[@"shouldUploadEvent"] boolValue];
}
if (json[@"customAttributes"] != nil) {
commerceEvent.customAttributes = json[@"customAttributes"];
}
NSMutableArray *products = [NSMutableArray array];
NSArray *jsonProducts = json[@"products"];
[jsonProducts enumerateObjectsUsingBlock:^(id _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
MPProduct *product = [CDVMParticle MPProduct:obj];
[products addObject:product];
}];
[commerceEvent addProducts:products];
NSArray *jsonImpressions = json[@"impressions"];
[jsonImpressions enumerateObjectsUsingBlock:^(NSDictionary *jsonImpression, NSUInteger idx, BOOL * _Nonnull stop) {
NSString *listName = jsonImpression[@"impressionListName"];
NSArray *jsonProducts = jsonImpression[@"products"];
[jsonProducts enumerateObjectsUsingBlock:^(id _Nonnull jsonProduct, NSUInteger idx, BOOL * _Nonnull stop) {
MPProduct *product = [CDVMParticle MPProduct:jsonProduct];
[commerceEvent addImpression:product listName:listName];
}];
}];
return commerceEvent;
}
+ (MPPromotionContainer *)MPPromotionContainer:(NSMutableDictionary *)json {
MPPromotionAction promotionAction = (MPPromotionAction)[json[@"promotionActionType"] intValue];
MPPromotionContainer *promotionContainer = [[MPPromotionContainer alloc] initWithAction:promotionAction promotion:nil];
NSArray *jsonPromotions = json[@"promotions"];
[jsonPromotions enumerateObjectsUsingBlock:^(id _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
MPPromotion *promotion = [CDVMParticle MPPromotion:obj];
[promotionContainer addPromotion:promotion];
}];
return promotionContainer;
}
+ (MPPromotion *)MPPromotion:(NSMutableDictionary *)json {
MPPromotion *promotion = [[MPPromotion alloc] init];
promotion.creative = json[@"creative"];
promotion.name = json[@"name"];
promotion.position = json[@"position"];
promotion.promotionId = json[@"id"];
return promotion;
}
+ (MPTransactionAttributes *)MPTransactionAttributes:(NSMutableDictionary *)json {
MPTransactionAttributes *transactionAttributes = [[MPTransactionAttributes alloc] init];
transactionAttributes.affiliation = json[@"affiliation"];
transactionAttributes.couponCode = json[@"couponCode"];
transactionAttributes.shipping = json[@"shipping"];
transactionAttributes.tax = json[@"tax"];
transactionAttributes.revenue = json[@"revenue"];
transactionAttributes.transactionId = json[@"transactionId"];
return transactionAttributes;
}
+ (MPProduct *)MPProduct:(NSMutableDictionary *)json {
MPProduct *product = [[MPProduct alloc] init];
product.brand = json[@"brand"];
product.category = json[@"category"];
product.couponCode = json[@"couponCode"];
product.name = json[@"name"];
product.price = json[@"price"];
product.sku = json[@"sku"];
product.variant = json[@"variant"];
product.position = [json[@"position"] intValue];
product.quantity = json[@"quantity"] ?: @1;
NSDictionary *jsonAttributes = json[@"customAttributes"];
for (NSString *key in jsonAttributes) {
NSString *value = jsonAttributes[key];
[product setObject:value forKeyedSubscript:key];
}
return product;
}
+ (MPEvent *)MPEvent:(NSMutableDictionary *)json {
MPEvent *event = [[MPEvent alloc] init];
event.category = json[@"category"];
event.duration = json[@"duration"];
event.endTime = json[@"endTime"];
event.customAttributes = json[@"info"];
event.name = json[@"name"];
event.startTime = json[@"startTime"];
event.type = [json[@"type"] integerValue];
if (json[@"shouldUploadEvent"] != nil) {
event.shouldUploadEvent = [json[@"shouldUploadEvent"] boolValue];
}
NSDictionary *jsonFlags = json[@"customFlags"];
for (NSString *key in jsonFlags) {
NSString *value = jsonFlags[key];
[event addCustomFlag:value withKey:key];
}
return event;
}
+ (MPIdentityApiRequest *)MPIdentityApiRequest:(NSMutableDictionary *)json {
MPIdentityApiRequest *request = [[MPIdentityApiRequest alloc] init];
for (NSString *key in json) {
NSString *value = json[key];
[request setIdentity:value identityType:[CDVMParticle ConvertIdentityType:key]];
}
return request;
}
+ (MPIdentity)ConvertIdentityType:(NSString *)val {
if ([val isEqual: @"customerId"]) {
return MPIdentityCustomerId;
} else if ([val isEqual: @"facebook"]) {
return MPIdentityFacebook;
} else if ([val isEqual: @"twitter"]) {
return MPIdentityTwitter;
} else if ([val isEqual: @"google"]) {
return MPIdentityGoogle;
} else if ([val isEqual: @"microsoft"]) {
return MPIdentityMicrosoft;
} else if ([val isEqual: @"yahoo"]) {
return MPIdentityYahoo;
} else if ([val isEqual: @"email"]) {
return MPIdentityEmail;
} else if ([val isEqual: @"alias"]) {
return MPIdentityAlias;
} else if ([val isEqual: @"facebookCustom"]) {
return MPIdentityFacebookCustomAudienceId;
} else if ([val isEqual: @"other2"]) {
return MPIdentityOther2;
} else if ([val isEqual: @"other3"]) {
return MPIdentityOther3;
} else if ([val isEqual: @"other4"]) {
return MPIdentityOther4;
} else if ([val isEqual: @"other5"]) {
return MPIdentityOther5;
} else if ([val isEqual: @"other6"]) {
return MPIdentityOther6;
} else if ([val isEqual: @"other7"]) {
return MPIdentityOther7;
} else if ([val isEqual: @"other8"]) {
return MPIdentityOther8;
} else if ([val isEqual: @"other9"]) {
return MPIdentityOther9;
} else if ([val isEqual: @"other10"]) {
return MPIdentityOther10;
} else if ([val isEqual: @"mobileNumber"]) {
return MPIdentityMobileNumber;
} else if ([val isEqual: @"phoneNumber2"]) {
return MPIdentityPhoneNumber2;
} else if ([val isEqual: @"phoneNumber3"]) {
return MPIdentityPhoneNumber3;
} else if ([val isEqual: @"iosIDFA"]) {
return MPIdentityIOSAdvertiserId;
} else if ([val isEqual: @"iosIDFV"]) {
return MPIdentityIOSVendorId;
} else if ([val isEqual: @"pushToken"]) {
return MPIdentityPushToken;
} else if ([val isEqual: @"deviceApplicationStamp"]) {
return MPIdentityDeviceApplicationStamp;
} else {
return MPIdentityOther;
}
}
+ (MPGDPRConsent *)MPGDPRConsent:(NSMutableDictionary *)json {
NSString *dateString = json[@"timestamp"];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
NSDate *timestamp = [dateFormatter dateFromString:dateString];
MPGDPRConsent *consentState = [[MPGDPRConsent alloc] init];
consentState.consented = [json[@"consented"] boolValue];
consentState.document = json[@"document"];
consentState.timestamp = timestamp;
consentState.location = json[@"location"];
consentState.hardwareId = json[@"hardwareId"];
return consentState;
}
+ (MPCCPAConsent *)MPCCPAConsent:(NSMutableDictionary *)json {
NSString *dateString = json[@"timestamp"];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
NSDate *timestamp = [dateFormatter dateFromString:dateString];
MPCCPAConsent *consentState = [[MPCCPAConsent alloc] init];
consentState.consented = [json[@"consented"] boolValue];
consentState.document = json[@"document"];
consentState.timestamp = timestamp;
consentState.location = json[@"location"];
consentState.hardwareId = json[@"hardwareId"];
return consentState;
}
+ (NSString *)StringFromIdentityType:(MPIdentity)identityType {
switch (identityType) {
case MPIdentityCustomerId:
return @"customerId";
case MPIdentityFacebook:
return @"facebook";
case MPIdentityTwitter:
return @"twitter";
case MPIdentityGoogle:
return @"google";
case MPIdentityMicrosoft:
return @"microsoft";
case MPIdentityYahoo:
return @"yahoo";
case MPIdentityEmail:
return @"email";
case MPIdentityAlias:
return @"alias";
case MPIdentityFacebookCustomAudienceId:
return @"facebookCustom";
case MPIdentityOther2:
return @"other2";
case MPIdentityOther3:
return @"other3";
case MPIdentityOther4:
return @"other4";
case MPIdentityOther5:
return @"other5";
case MPIdentityOther6:
return @"other6";
case MPIdentityOther7:
return @"other7";
case MPIdentityOther8:
return @"other8";
case MPIdentityOther9:
return @"other9";
case MPIdentityOther10:
return @"other10";
case MPIdentityMobileNumber:
return @"mobileNumber";
case MPIdentityPhoneNumber2:
return @"phoneNumber2";
case MPIdentityPhoneNumber3:
return @"phoneNumber3";
case MPIdentityIOSAdvertiserId:
return @"iosIDFA";
case MPIdentityIOSVendorId:
return @"iosIDFV";
case MPIdentityPushToken:
return @"pushToken";
case MPIdentityDeviceApplicationStamp:
return @"deviceApplicationStamp";
default:
return @"other";
}
}
@end