-
Notifications
You must be signed in to change notification settings - Fork 27
Expand file tree
/
Copy pathRNMParticle.m
More file actions
679 lines (577 loc) · 26.1 KB
/
RNMParticle.m
File metadata and controls
679 lines (577 loc) · 26.1 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
#import "RNMParticle.h"
#if defined(__has_include) && __has_include(<mParticle_Apple_SDK/mParticle.h>)
#import <mParticle_Apple_SDK/mParticle.h>
#elif defined(__has_include) && __has_include(<mParticle_Apple_SDK_NoLocation/mParticle.h>)
#import <mParticle_Apple_SDK_NoLocation/mParticle.h>
#else
#import "mParticle.h"
#endif
#if defined(__has_include) && __has_include(<mParticle_Apple_SDK/mParticle_Apple_SDK-Swift.h>)
#import <mParticle_Apple_SDK/mParticle_Apple_SDK-Swift.h>
#elif defined(__has_include) && __has_include(<mParticle_Apple_SDK_NoLocation/mParticle_Apple_SDK-Swift.h>)
#import <mParticle_Apple_SDK_NoLocation/mParticle_Apple_SDK-Swift.h>
#else
#import "mParticle_Apple_SDK-Swift.h"
#endif
#import <React/RCTConvert.h>
@interface MParticleUser ()
- (void)setUserId:(NSNumber *)userId;
@end
@implementation RNMParticle
RCT_EXTERN void RCTRegisterModule(Class);
+ (NSString *)moduleName {
return @"MParticle";
}
+ (void)load {
RCTRegisterModule(self);
}
RCT_EXPORT_METHOD(upload)
{
[[MParticle sharedInstance] upload];
}
RCT_EXPORT_METHOD(setLocation:(double)latitude longitude:(double)longitude)
{
CLLocation *newLocation = [[CLLocation alloc] initWithLatitude:latitude longitude:longitude];
[MParticle sharedInstance].location = newLocation;
}
RCT_EXPORT_METHOD(setUploadInterval:(NSInteger)uploadInterval)
{
[[MParticle sharedInstance] setUploadInterval:uploadInterval];
}
RCT_EXPORT_METHOD(logEvent:(NSString *)eventName type:(NSInteger)type attributes:(NSDictionary *)attributes)
{
[[MParticle sharedInstance] logEvent:eventName eventType:type eventInfo:attributes];
}
RCT_EXPORT_METHOD(logMPEvent:(MPEvent *)event)
{
[[MParticle sharedInstance] logEvent:event];
}
RCT_EXPORT_METHOD(logCommerceEvent:(MPCommerceEvent *)commerceEvent)
{
[[MParticle sharedInstance] logCommerceEvent:commerceEvent];
}
RCT_EXPORT_METHOD(logScreenEvent:(NSString *)screenName attributes:(NSDictionary *)attributes shouldUploadEvent:(BOOL)shouldUploadEvent)
{
[[MParticle sharedInstance] logScreen:screenName eventInfo:attributes shouldUploadEvent:shouldUploadEvent];
}
RCT_EXPORT_METHOD(setATTStatus:(NSInteger)status withATTStatusTimestampMillis:(nonnull NSNumber *)timestamp)
{
[[MParticle sharedInstance] setATTStatus:status withATTStatusTimestampMillis:timestamp];
}
RCT_EXPORT_METHOD(setATTStatus:(NSInteger)status)
{
[[MParticle sharedInstance] setATTStatus:status withATTStatusTimestampMillis:nil];
}
RCT_EXPORT_METHOD(setOptOut:(BOOL)optOut)
{
[[MParticle sharedInstance] setOptOut:optOut];
}
RCT_EXPORT_METHOD(getOptOut:(RCTResponseSenderBlock)completion)
{
BOOL optedOut = [[MParticle sharedInstance] optOut];
completion(@[[NSNumber numberWithBool:optedOut]]);
}
RCT_EXPORT_METHOD(addGDPRConsentState:(MPGDPRConsent *)gdprConsentState purpose:(NSString *)purpose)
{
MParticleUser *user = [MParticle sharedInstance].identity.currentUser;
MPConsentState *consentState = user.consentState ? user.consentState : [[MPConsentState alloc] init];
[consentState addGDPRConsentState:gdprConsentState purpose:purpose];
user.consentState = consentState;
}
RCT_EXPORT_METHOD(removeGDPRConsentStateWithPurpose:(NSString *)purpose)
{
MParticleUser *user = [MParticle sharedInstance].identity.currentUser;
MPConsentState *consentState = user.consentState ? user.consentState : [[MPConsentState alloc] init];
[consentState removeGDPRConsentStateWithPurpose:purpose];
user.consentState = consentState;
}
RCT_EXPORT_METHOD(setCCPAConsentState:(MPCCPAConsent *)consent)
{
MParticleUser *user = [MParticle sharedInstance].identity.currentUser;
MPConsentState *consentState = user.consentState ? user.consentState : [[MPConsentState alloc] init];
[consentState setCCPAConsentState:consent];
user.consentState = consentState;
}
RCT_EXPORT_METHOD(removeCCPAConsentState)
{
MParticleUser *user = [MParticle sharedInstance].identity.currentUser;
MPConsentState *consentState = user.consentState ? user.consentState : [[MPConsentState alloc] init];
[consentState removeCCPAConsentState];
user.consentState = consentState;
}
#if TARGET_OS_IOS == 1
RCT_EXPORT_METHOD(logPushRegistration:(NSString *)iosToken androidField:(NSString *)androidField)
{
if (iosToken != nil) {
NSData* pushTokenData = [iosToken dataUsingEncoding:NSUTF8StringEncoding];
MParticle* instance = [MParticle sharedInstance];
instance.pushNotificationToken = pushTokenData;
}
}
#endif
RCT_EXPORT_METHOD(isKitActive:(nonnull NSNumber*)kitId completion:(RCTResponseSenderBlock)completion)
{
BOOL active = [[MParticle sharedInstance] isKitActive:kitId];
completion(@[[NSNumber numberWithBool:active]]);
}
RCT_EXPORT_METHOD(getAttributions:(RCTResponseSenderBlock)completion)
{
NSDictionary<NSNumber *, MPAttributionResult *> *attributions = [[MParticle sharedInstance]attributionInfo];
NSMutableDictionary*dictionary = [[NSMutableDictionary alloc]init];
for (NSNumber *kitId in attributions) {
MPAttributionResult *attributionResult = attributions[kitId];
NSMutableDictionary *attributionDict = [[NSMutableDictionary alloc]initWithCapacity:2];
if (attributionResult.linkInfo != nil) {
attributionDict[@"linkParameters"] = attributionResult.linkInfo;
}
if (attributionResult.kitCode != nil) {
attributionDict[@"kitId"] = attributionResult.kitCode;
}
dictionary[[kitId stringValue]] = attributionDict;
}
completion(@[dictionary]);
}
RCT_EXPORT_METHOD(setUserAttribute:(NSString *)userId key:(NSString *)key value:(NSString *)value)
{
MParticleUser *selectedUser = [[MParticleUser alloc] init];
selectedUser.userId = [NSNumber numberWithLong:userId.longLongValue];
[selectedUser setUserAttribute:key
value:value];
}
RCT_EXPORT_METHOD(setUserAttributeArray:(NSString *)userId key:(NSString *)key values:(NSArray *)values)
{
MParticleUser *selectedUser = [[MParticleUser alloc] init];
selectedUser.userId = [NSNumber numberWithLong:userId.longLongValue];
[selectedUser setUserAttributeList:key
values:values];
}
RCT_EXPORT_METHOD(getUserAttributes:(NSString *)userId completion:(RCTResponseSenderBlock)completion)
{
MParticleUser *selectedUser = [[MParticleUser alloc] init];
selectedUser.userId = [NSNumber numberWithLong:userId.longLongValue];
completion(@[[NSNull null], [selectedUser userAttributes]]);
}
RCT_EXPORT_METHOD(setUserTag:(NSString *)userId tag:(NSString *)tag)
{
MParticleUser *selectedUser = [[MParticleUser alloc] init];
selectedUser.userId = [NSNumber numberWithLong:userId.longLongValue];
[selectedUser setUserTag:tag];
}
RCT_EXPORT_METHOD(removeUserAttribute:(NSString *)userId key:(NSString *)key)
{
MParticleUser *selectedUser = [[MParticleUser alloc] init];
selectedUser.userId = [NSNumber numberWithLong:userId.longLongValue];
[selectedUser removeUserAttribute:key];
}
RCT_EXPORT_METHOD(incrementUserAttribute:(NSString *)userId key:(NSString *)key value:(NSNumber * _Nonnull)value)
{
MParticleUser *selectedUser = [[MParticleUser alloc] init];
selectedUser.userId = [NSNumber numberWithLong:userId.longLongValue];
[selectedUser incrementUserAttribute:key
byValue:value];
}
RCT_EXPORT_METHOD(identify:(MPIdentityApiRequest *)identityRequest completion:(RCTResponseSenderBlock)completion)
{
[[[MParticle sharedInstance] identity] identify:identityRequest completion:^(MPIdentityApiResult * _Nullable apiResult, NSError * _Nullable error) {
NSMutableDictionary *reactError;
if (error) {
reactError = [[NSMutableDictionary alloc] initWithCapacity:4];
MPIdentityHTTPErrorResponse *response = error.userInfo[mParticleIdentityErrorKey];
if ([response isKindOfClass:[NSString class]]) {
[reactError setObject:response forKey:@"message"];
} else {
if ([NSNumber numberWithLong:response.httpCode] != nil) {
[reactError setObject:[NSNumber numberWithLong:response.httpCode] forKey:@"httpCode"];
}
if ([NSNumber numberWithInt:response.code] != nil) {
[reactError setObject:[NSNumber numberWithInt:response.code] forKey:@"responseCode"];
}
if (response.message != nil) {
[reactError setObject:response.message forKey:@"message"];
}
}
if (apiResult != nil) {
completion(@[reactError, apiResult.user.userId.stringValue, apiResult.previousUser.userId.stringValue]);
} else {
completion(@[reactError, @0]);
}
} else {
completion(@[[NSNull null], apiResult.user.userId.stringValue, apiResult.previousUser.userId.stringValue]);
}
}];
}
RCT_EXPORT_METHOD(login:(MPIdentityApiRequest *)identityRequest completion:(RCTResponseSenderBlock)completion)
{
[[[MParticle sharedInstance] identity] login:identityRequest completion:^(MPIdentityApiResult * _Nullable apiResult, NSError * _Nullable error) {
NSMutableDictionary *reactError;
if (error) {
reactError = [[NSMutableDictionary alloc] initWithCapacity:4];
MPIdentityHTTPErrorResponse *response = error.userInfo[mParticleIdentityErrorKey];
if ([response isKindOfClass:[NSString class]]) {
[reactError setObject:response forKey:@"message"];
} else {
if ([NSNumber numberWithLong:response.httpCode] != nil) {
[reactError setObject:[NSNumber numberWithLong:response.httpCode] forKey:@"httpCode"];
}
if ([NSNumber numberWithInt:response.code] != nil) {
[reactError setObject:[NSNumber numberWithInt:response.code] forKey:@"responseCode"];
}
if (response.message != nil) {
[reactError setObject:response.message forKey:@"message"];
}
}
if (apiResult != nil) {
completion(@[reactError, apiResult.user.userId.stringValue, apiResult.previousUser.userId.stringValue]);
} else {
completion(@[reactError, @0]);
}
} else {
completion(@[[NSNull null], apiResult.user.userId.stringValue, apiResult.previousUser.userId.stringValue]);
}
}];
}
RCT_EXPORT_METHOD(logout:(MPIdentityApiRequest *)identityRequest completion:(RCTResponseSenderBlock)completion)
{
[[[MParticle sharedInstance] identity] logout:identityRequest completion:^(MPIdentityApiResult * _Nullable apiResult, NSError * _Nullable error) {
NSMutableDictionary *reactError;
if (error) {
reactError = [[NSMutableDictionary alloc] initWithCapacity:4];
MPIdentityHTTPErrorResponse *response = error.userInfo[mParticleIdentityErrorKey];
if ([response isKindOfClass:[NSString class]]) {
[reactError setObject:response forKey:@"message"];
} else {
if ([NSNumber numberWithLong:response.httpCode] != nil) {
[reactError setObject:[NSNumber numberWithLong:response.httpCode] forKey:@"httpCode"];
}
if ([NSNumber numberWithInt:response.code] != nil) {
[reactError setObject:[NSNumber numberWithInt:response.code] forKey:@"responseCode"];
}
if (response.message != nil) {
[reactError setObject:response.message forKey:@"message"];
}
}
if (apiResult != nil) {
completion(@[reactError, apiResult.user.userId.stringValue, apiResult.previousUser.userId.stringValue]);
} else {
completion(@[reactError, @0]);
}
} else {
completion(@[[NSNull null], apiResult.user.userId.stringValue, apiResult.previousUser.userId.stringValue]);
}
}];
}
RCT_EXPORT_METHOD(modify:(MPIdentityApiRequest *)identityRequest completion:(RCTResponseSenderBlock)completion)
{
[[[MParticle sharedInstance] identity] modify:identityRequest completion:^(MPIdentityApiResult * _Nullable apiResult, NSError * _Nullable error) {
NSMutableDictionary *reactError;
if (error) {
reactError = [[NSMutableDictionary alloc] initWithCapacity:4];
MPIdentityHTTPErrorResponse *response = error.userInfo[mParticleIdentityErrorKey];
if ([response isKindOfClass:[NSString class]]) {
[reactError setObject:response forKey:@"message"];
} else {
if ([NSNumber numberWithLong:response.httpCode] != nil) {
[reactError setObject:[NSNumber numberWithLong:response.httpCode] forKey:@"httpCode"];
}
if ([NSNumber numberWithInt:response.code] != nil) {
[reactError setObject:[NSNumber numberWithInt:response.code] forKey:@"responseCode"];
}
if (response.message != nil) {
[reactError setObject:response.message forKey:@"message"];
}
}
if (apiResult != nil) {
completion(@[reactError, apiResult.user.userId.stringValue, [NSNull null]]);
} else {
completion(@[reactError, @0]);
}
} else {
completion(@[[NSNull null], apiResult.user.userId.stringValue, [NSNull null]]);
}
}];
}
RCT_EXPORT_METHOD(aliasUsers:(MPAliasRequest *) aliasRequest completion:(RCTResponseSenderBlock)completion)
{
BOOL success = [[[MParticle sharedInstance] identity] aliasUsers:aliasRequest];
completion(@[[NSNull null], success ? @"true" : @"false"]);
}
RCT_EXPORT_METHOD(getCurrentUserWithCompletion:(RCTResponseSenderBlock)completion)
{
MParticleUser *currentUser = [[MParticle sharedInstance] identity].currentUser;
completion(@[[NSNull null], currentUser.userId.stringValue, @(currentUser.isLoggedIn)]);
}
RCT_EXPORT_METHOD(getUserIdentities:(NSString *)userId completion:(RCTResponseSenderBlock)completion)
{
MParticleUser *selectedUser = [[MParticleUser alloc] init];
selectedUser.userId = [NSNumber numberWithLong:userId.longLongValue];
completion(@[[NSNull null], [selectedUser identities]]);
}
RCT_EXPORT_METHOD(getFirstSeen:(NSString *)userId completion:(RCTResponseSenderBlock)completion)
{
MParticleUser *selectedUser = [[MParticleUser alloc] init];
selectedUser.userId = [NSNumber numberWithLong:userId.longLongValue];
completion(@[[NSNull null], [selectedUser firstSeen]]);
}
RCT_EXPORT_METHOD(getLastSeen:(NSString *)userId completion:(RCTResponseSenderBlock)completion)
{
MParticleUser *selectedUser = [[MParticleUser alloc] init];
selectedUser.userId = [NSNumber numberWithLong:userId.longLongValue];
completion(@[[NSNull null], [selectedUser lastSeen]]);
}
RCT_EXPORT_METHOD(getSession:(RCTResponseSenderBlock)completion)
{
MParticleSession *session = [MParticle sharedInstance].currentSession;
if (session && session.UUID) {
completion(@[session.UUID]);
} else {
completion(@[[NSNull null]]);
}
}
@end
typedef NS_ENUM(NSUInteger, MPReactCommerceEventAction) {
MPReactCommerceEventActionAddToCart = 1,
MPReactCommerceEventActionRemoveFromCart,
MPReactCommerceEventActionCheckout,
MPReactCommerceEventActionCheckoutOptions,
MPReactCommerceEventActionClick,
MPReactCommerceEventActionViewDetail,
MPReactCommerceEventActionPurchase,
MPReactCommerceEventActionRefund,
MPReactCommerceEventActionAddToWishList,
MPReactCommerceEventActionRemoveFromWishlist
};
@interface RCTConvert (MPCommerceEvent)
+ (MPCommerceEvent *)MPCommerceEvent:(id)json;
+ (MPPromotionContainer *)MPPromotionContainer:(id)json;
+ (MPPromotion *)MPPromotion:(id)json;
+ (MPTransactionAttributes *)MPTransactionAttributes:(id)json;
+ (MPProduct *)MPProduct:(id)json;
+ (MPCommerceEventAction)MPCommerceEventAction:(id)json;
+ (MPIdentityApiRequest *)MPIdentityApiRequest:(id)json;
+ (MPIdentityApiResult *)MPIdentityApiResult:(id)json;
+ (MPAliasRequest *)MPAliasRequest:(id)json;
+ (MParticleUser *)MParticleUser:(id)json;
+ (MPEvent *)MPEvent:(id)json;
+ (MPGDPRConsent *)MPGDPRConsent:(id)json;
+ (MPCCPAConsent *)MPCCPAConsent:(id)json;
@end
@implementation RCTConvert (MPCommerceEvent)
+ (MPCommerceEvent *)MPCommerceEvent:(id)json {
BOOL isProductAction = json[@"productActionType"] != nil;
BOOL isPromotion = json[@"promotionActionType"] != nil;
BOOL isImpression = json[@"impressions"] != nil;
NSAssert(isProductAction || isPromotion || isImpression, @"Invalid commerce event");
MPCommerceEvent *commerceEvent = nil;
if (isProductAction) {
MPCommerceEventAction action = [RCTConvert MPCommerceEventAction:json[@"productActionType"]];
commerceEvent = [[MPCommerceEvent alloc] initWithAction:action];
}
else if (isPromotion) {
MPPromotionContainer *promotionContainer = [RCTConvert 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 = [RCTConvert MPTransactionAttributes:json[@"transactionAttributes"]];
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 = [RCTConvert 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 = [RCTConvert MPProduct:jsonProduct];
[commerceEvent addImpression:product listName:listName];
}];
}];
return commerceEvent;
}
+ (MPPromotionContainer *)MPPromotionContainer:(id)json {
MPPromotionAction promotionAction = [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 = [RCTConvert MPPromotion:obj];
[promotionContainer addPromotion:promotion];
}];
return promotionContainer;
}
+ (MPPromotion *)MPPromotion:(id)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:(id)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:(id)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"] intValue]);
NSDictionary *jsonAttributes = json[@"customAttributes"];
for (NSString *key in jsonAttributes) {
NSString *value = jsonAttributes[key];
[product setObject:value forKeyedSubscript:key];
}
return product;
}
+ (MPCommerceEventAction)MPCommerceEventAction:(NSNumber *)json {
int actionInt = [json intValue];
MPCommerceEventAction action;
switch (actionInt) {
case MPReactCommerceEventActionAddToCart:
action = MPCommerceEventActionAddToCart;
break;
case MPReactCommerceEventActionRemoveFromCart:
action = MPCommerceEventActionRemoveFromCart;
break;
case MPReactCommerceEventActionCheckout:
action = MPCommerceEventActionCheckout;
break;
case MPReactCommerceEventActionCheckoutOptions:
action = MPCommerceEventActionCheckoutOptions;
break;
case MPReactCommerceEventActionClick:
action = MPCommerceEventActionClick;
break;
case MPReactCommerceEventActionViewDetail:
action = MPCommerceEventActionViewDetail;
break;
case MPReactCommerceEventActionPurchase:
action = MPCommerceEventActionPurchase;
break;
case MPReactCommerceEventActionRefund:
action = MPCommerceEventActionRefund;
break;
case MPReactCommerceEventActionAddToWishList:
action = MPCommerceEventActionAddToWishList;
break;
case MPReactCommerceEventActionRemoveFromWishlist:
action = MPCommerceEventActionRemoveFromWishlist;
break;
default:
action = MPCommerceEventActionAddToCart;
NSAssert(NO, @"Invalid commerce event action");
break;
}
return action;
}
+ (MPIdentityApiRequest *)MPIdentityApiRequest:(id)json {
MPIdentityApiRequest *request = [MPIdentityApiRequest requestWithEmptyUser];
for (NSString *key in json) {
if ([key isEqualToString:@"email"]) {
request.email = json[@"email"];
} else if ([key isEqualToString:@"customerId"]) {
request.customerId = json[@"customerId"];
} else if ((key.intValue <= MPIdentityDeviceApplicationStamp) && (MPIdentityOther <= key.intValue)) {
NSString *value = json[key];
MPIdentity identityType = (MPIdentity)key.intValue;
[request setIdentity:value identityType:identityType];
} else {
NSAssert(NO, @"Invalid identity request - Please only use MPIdentity keys");
}
}
return request;
}
+ (MPIdentityApiResult *)MPIdentityApiResult:(id)json {
MPIdentityApiResult *result = [[MPIdentityApiResult alloc] init];
id obj = json[@"user"];
result.user = [RCTConvert MParticleUser:obj];
return result;
}
+ (MPAliasRequest *)MPAliasRequest:(id)json {
NSString *destinationMpidString = json[@"destinationMpid"];
NSString *sourceMpidString = json[@"sourceMpid"];
NSString *startTime = json[@"startTime"];
NSString *endTime = json[@"endTime"];
NSNumber *destinationMpid = [NSNumber numberWithLong:destinationMpidString.longLongValue];
NSNumber *sourceMpid = [NSNumber numberWithLong:sourceMpidString.longLongValue];
if (startTime != nil || endTime != nil) {
MParticleUser *destinationUser = [[MParticleUser alloc] init];
MParticleUser *sourceUser = [[MParticleUser alloc] init];
destinationUser.userId = destinationMpid;
sourceUser.userId = sourceMpid;
return [MPAliasRequest requestWithSourceUser:sourceUser destinationUser: destinationUser];
} else {
return [MPAliasRequest requestWithSourceMPID:sourceMpid destinationMPID:destinationMpid startTime:[NSDate dateWithTimeIntervalSince1970:startTime.longLongValue] endTime:[NSDate dateWithTimeIntervalSince1970:endTime.longLongValue]];
}
}
+ (MParticleUser *)MParticleUser:(id)json {
MParticleUser *user = [[MParticleUser alloc] init];
user.userId = json[@"userId"];
return user;
}
+ (MPEvent *)MPEvent:(id)json {
MPEvent *event = [[MPEvent alloc] init];
event.category = json[@"category"];
event.duration = json[@"duration"];
event.endTime = json[@"endTime"];
event.info = json[@"info"];
event.name = json[@"name"];
event.startTime = json[@"startTime"];
event.type = [json[@"type"] intValue];
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;
}
+ (MPGDPRConsent *)MPGDPRConsent:(id)json {
MPGDPRConsent *mpConsent = [[MPGDPRConsent alloc] init];
mpConsent.consented = [RCTConvert BOOL:json[@"consented"]];
mpConsent.document = json[@"document"];
mpConsent.timestamp = [RCTConvert NSDate:json[@"timestamp"]];
mpConsent.location = json[@"location"];
mpConsent.hardwareId = json[@"hardwareId"];
return mpConsent;
}
+ (MPCCPAConsent *)MPCCPAConsent:(id)json {
MPCCPAConsent *mpConsent = [[MPCCPAConsent alloc] init];
mpConsent.consented = [RCTConvert BOOL:json[@"consented"]];
mpConsent.document = json[@"document"];
mpConsent.timestamp = [RCTConvert NSDate:json[@"timestamp"]];
mpConsent.location = json[@"location"];
mpConsent.hardwareId = json[@"hardwareId"];
return mpConsent;
}
@end