-
Notifications
You must be signed in to change notification settings - Fork 25
/
CountlyReactNative.m
1367 lines (1198 loc) · 53.2 KB
/
CountlyReactNative.m
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
#import <React/RCTBridge.h>
#import <React/RCTEventDispatcher.h>
#import <React/RCTLog.h>
#import <React/RCTUtils.h>
#import "Countly.h"
#import "CountlyCommon.h"
#import "CountlyConfig.h"
#import "CountlyConnectionManager.h"
#import "CountlyReactNative.h"
#import "CountlyRemoteConfig.h"
#ifndef COUNTLY_EXCLUDE_PUSHNOTIFICATIONS
#import "CountlyRNPushNotifications.h"
#endif
#if DEBUG
#define COUNTLY_RN_LOG(fmt, ...) CountlyRNInternalLog(fmt, ##__VA_ARGS__)
#else
#define COUNTLY_RN_LOG(...)
#endif
@interface CountlyFeedbackWidget ()
+ (CountlyFeedbackWidget *)createWithDictionary:(NSDictionary *)dictionary;
@end
NSString *const kCountlyReactNativeSDKVersion = @"24.4.1";
NSString *const kCountlyReactNativeSDKName = @"js-rnb-ios";
CLYPushTestMode const CLYPushTestModeProduction = @"CLYPushTestModeProduction";
CountlyConfig *config = nil; // alloc here
NSMutableArray<CLYFeature> *countlyFeatures = nil;
NSArray<CountlyFeedbackWidget *> *feedbackWidgetList = nil;
BOOL enablePushNotifications = true;
NSString *const NAME_KEY = @"name";
NSString *const USERNAME_KEY = @"username";
NSString *const EMAIL_KEY = @"email";
NSString *const ORG_KEY = @"organization";
NSString *const PHONE_KEY = @"phone";
NSString *const PICTURE_KEY = @"picture";
NSString *const PICTURE_PATH_KEY = @"picturePath";
NSString *const GENDER_KEY = @"gender";
NSString *const BYEAR_KEY = @"byear";
NSString *const CUSTOM_KEY = @"custom";
NSString *const widgetShownCallbackName = @"widgetShownCallback";
NSString *const widgetClosedCallbackName = @"widgetClosedCallback";
NSString *const ratingWidgetCallbackName = @"ratingWidgetCallback";
NSString *const pushNotificationCallbackName = @"pushNotificationCallback";
@implementation CountlyReactNative
NSString *const kCountlyNotificationPersistencyKey = @"kCountlyNotificationPersistencyKey";
- (instancetype)init {
if (self = [super init]) {
}
#ifndef COUNTLY_EXCLUDE_PUSHNOTIFICATIONS
[CountlyRNPushNotifications.sharedInstance setCountlyReactNative:self];
#endif
return self;
}
+ (BOOL)requiresMainQueueSetup
{
return NO;
}
- (NSArray<NSString *> *)supportedEvents {
return @[ pushNotificationCallbackName, ratingWidgetCallbackName, widgetShownCallbackName, widgetClosedCallbackName ];
}
RCT_EXPORT_MODULE();
RCT_REMAP_METHOD(init, params : (NSArray *)arguments initWithResolver : (RCTPromiseResolveBlock)resolve rejecter : (RCTPromiseRejectBlock)reject) {
dispatch_async(dispatch_get_main_queue(), ^{
COUNTLY_RN_LOG(@"Initializing...");
NSString *args = [arguments objectAtIndex:0];
NSData *data = [args dataUsingEncoding:NSUTF8StringEncoding];
id jsonOutput = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
[self populateConfig:jsonOutput];
CountlyCommon.sharedInstance.SDKName = kCountlyReactNativeSDKName;
CountlyCommon.sharedInstance.SDKVersion = kCountlyReactNativeSDKVersion;
#ifndef COUNTLY_EXCLUDE_PUSHNOTIFICATIONS
if (enablePushNotifications) {
[self addCountlyFeature:CLYPushNotifications];
}
#endif
if (config.host != nil && [config.host length] > 0) {
dispatch_async(dispatch_get_main_queue(), ^{
[[Countly sharedInstance] startWithConfig:config];
#ifndef COUNTLY_EXCLUDE_PUSHNOTIFICATIONS
[CountlyRNPushNotifications.sharedInstance recordPushActions];
#endif
resolve(@"Success");
});
}
});
}
- (void) populateConfig:(id) json {
if (config == nil) {
config = CountlyConfig.new;
}
NSString *serverurl = json[@"serverURL"];
NSString *appkey = json[@"appKey"];
NSString *deviceID = json[@"deviceID"];
config.appKey = appkey;
config.host = serverurl;
config.enrollABOnRCDownload = true;
if (deviceID != nil && deviceID != (NSString *)[NSNull null] && ![deviceID isEqual:@""]) {
if ([deviceID isEqual:@"TemporaryDeviceID"]) {
config.deviceID = CLYTemporaryDeviceID;
} else {
config.deviceID = deviceID;
}
}
if (json[@"loggingEnabled"]) {
config.enableDebug = YES;
config.internalLogLevel = CLYInternalLogLevelVerbose;
} else {
config.enableDebug = NO;
}
if (json[@"shouldRequireConsent"]) {
config.requiresConsent = YES;
}
if (json[@"tamperingProtectionSalt"]) {
config.secretSalt = json[@"tamperingProtectionSalt"];
}
if (json[@"consents"]) {
config.consents = json[@"consents"];
}
if (json[@"starRatingTextMessage"]) {
config.starRatingMessage = json[@"starRatingTextMessage"];
}
// Limits -----------------------------------------------
// maxKeyLength
NSNumber *maxKeyLength = json[@"maxKeyLength"];
if (maxKeyLength) {
[config.sdkInternalLimits setMaxKeyLength:[maxKeyLength intValue]];
}
NSNumber *maxValueSize = json[@"maxValueSize"];
if (maxValueSize) {
[config.sdkInternalLimits setMaxValueSize:[maxValueSize intValue]];
}
NSNumber *maxSegmentationValues = json[@"maxSegmentationValues"];
if (maxSegmentationValues) {
[config.sdkInternalLimits setMaxSegmentationValues:[maxSegmentationValues intValue]];
}
NSNumber *maxBreadcrumbCount = json[@"maxBreadcrumbCount"];
if (maxBreadcrumbCount) {
[config.sdkInternalLimits setMaxBreadcrumbCount:[maxBreadcrumbCount intValue]];
}
NSNumber *maxStackTraceLineLength = json[@"maxStackTraceLineLength"];
if (maxStackTraceLineLength) {
[config.sdkInternalLimits setMaxStackTraceLineLength:[maxStackTraceLineLength intValue]];
}
NSNumber *maxStackTraceLinesPerThread = json[@"maxStackTraceLinesPerThread"];
if (maxStackTraceLinesPerThread) {
[config.sdkInternalLimits setMaxStackTraceLinesPerThread:[maxStackTraceLinesPerThread intValue]];
}
// Limits End -------------------------------------------
// APM ------------------------------------------------
NSNumber *enableForegroundBackground = json[@"enableForegroundBackground"];
if (enableForegroundBackground) {
config.apm.enableForegroundBackgroundTracking = [enableForegroundBackground boolValue];
}
NSNumber *enableManualAppLoaded = json[@"enableManualAppLoaded"];
if (enableManualAppLoaded) {
config.apm.enableManualAppLoadedTrigger = [enableManualAppLoaded boolValue];
}
NSNumber *trackAppStartTime = json[@"trackAppStartTime"];
if (trackAppStartTime) {
config.apm.enableAppStartTimeTracking = [trackAppStartTime boolValue];
}
NSNumber *startTSOverride = json[@"startTSOverride"];
if (startTSOverride) {
[config.apm setAppStartTimestampOverride:[startTSOverride longLongValue]];
}
// Legacy APM
if (json[@"enableApm"]) {
config.enablePerformanceMonitoring = YES;
}
// APM END --------------------------------------------
if (json[@"crashReporting"]) {
[self addCountlyFeature:CLYCrashReporting];
}
#ifndef COUNTLY_EXCLUDE_PUSHNOTIFICATIONS
NSDictionary *pushJson = json[@"pushNotification"];
if (pushJson) {
config.sendPushTokenAlways = YES;
config.pushTestMode = CLYPushTestModeProduction;
NSString *tokenType = pushJson[@"tokenType"];
if ([tokenType isEqualToString:@"1"]) {
config.pushTestMode = CLYPushTestModeDevelopment;
} else if ([tokenType isEqualToString:@"2"]) {
config.pushTestMode = CLYPushTestModeTestFlightOrAdHoc;
}
CountlyPushNotifications.sharedInstance.pushTestMode = config.pushTestMode;
}
#endif
if (json[@"attributionID"]) {
NSString *attributionID = json[@"attributionID"];
if (CountlyCommon.sharedInstance.hasStarted) {
[Countly.sharedInstance recordAttributionID:attributionID];
} else {
config.attributionID = attributionID;
}
}
if (json[@"locationCountryCode"]) {
NSString *countryCode = json[@"locationCountryCode"];
NSString *city = json[@"locationCity"];
NSString *locationString = json[@"locationGpsCoordinates"];
NSString *ipAddress = json[@"locationIpAddress"];
if (locationString != nil && ![locationString isEqualToString:@"null"]) {
CLLocationCoordinate2D locationCoordinate = [self getCoordinate:locationString];
config.location = locationCoordinate;
}
if (city != nil && ![city isEqualToString:@"null"]) {
config.city = city;
}
if (countryCode != nil && ![countryCode isEqualToString:@"null"]) {
config.ISOCountryCode = countryCode;
}
if (ipAddress != nil && ![ipAddress isEqualToString:@"null"]) {
config.IP = ipAddress;
}
}
if (json[@"campaignType"]) {
config.campaignType = json[@"campaignType"];
config.campaignData = json[@"campaignData"];
}
if (json[@"attributionValues"]) {
config.indirectAttribution = json[@"attributionValues"];
}
}
RCT_EXPORT_METHOD(recordEvent : (NSDictionary *)arguments) {
dispatch_async(dispatch_get_main_queue(), ^{
NSString *eventName = [arguments objectForKey:@"n"];
NSNumber *countNumber = [arguments objectForKey:@"c"];
int countInt = [countNumber intValue];
NSNumber *sumNumber = [arguments objectForKey:@"s"];
float sumFloat = [sumNumber floatValue];
NSMutableDictionary *dict = nil;
NSArray *segments = [arguments objectForKey:@"g"];
if (segments != nil) {
dict = [[NSMutableDictionary alloc] init];
for (int i = 0, il = (int)segments.count; i < il; i += 2) {
dict[[segments objectAtIndex:i]] = [segments objectAtIndex:i + 1];
}
}
[[Countly sharedInstance] recordEvent:eventName segmentation:dict count:countInt sum:sumFloat];
});
}
RCT_EXPORT_METHOD(recordView : (NSArray *)arguments) {
dispatch_async(dispatch_get_main_queue(), ^{
NSString *recordView = [arguments objectAtIndex:0];
NSMutableDictionary *dict = [[NSMutableDictionary alloc] init];
for (int i = 1, il = (int)arguments.count; i < il; i += 2) {
dict[[arguments objectAtIndex:i]] = [arguments objectAtIndex:i + 1];
}
[Countly.sharedInstance recordView:recordView segmentation:dict];
});
}
RCT_EXPORT_METHOD(setLoggingEnabled : (NSArray *)arguments) {
dispatch_async(dispatch_get_main_queue(), ^{
BOOL boolean = [[arguments objectAtIndex:0] boolValue];
if (config == nil) {
config = CountlyConfig.new;
}
if (boolean) {
config.enableDebug = YES;
config.internalLogLevel = CLYInternalLogLevelVerbose;
} else {
config.enableDebug = NO;
}
});
}
RCT_REMAP_METHOD(setUserData, params : (NSArray *)arguments setUserDataWithResolver : (RCTPromiseResolveBlock)resolve rejecter : (RCTPromiseRejectBlock)reject) {
dispatch_async(dispatch_get_main_queue(), ^{
NSDictionary *userData = [arguments objectAtIndex:0];
[self setUserDataIntenral:userData];
[Countly.user save];
resolve(@"Success");
});
}
RCT_EXPORT_METHOD(disablePushNotifications) {
#ifndef COUNTLY_EXCLUDE_PUSHNOTIFICATIONS
dispatch_async(dispatch_get_main_queue(), ^{
enablePushNotifications = false;
});
#endif
}
RCT_EXPORT_METHOD(sendPushToken : (NSArray *)arguments) {
#ifndef COUNTLY_EXCLUDE_PUSHNOTIFICATIONS
dispatch_async(dispatch_get_main_queue(), ^{
NSString *token = [arguments objectAtIndex:0];
NSString *messagingMode = @"1";
if (config.pushTestMode == nil || [config.pushTestMode isEqual:@""] || [config.pushTestMode isEqualToString:CLYPushTestModeTestFlightOrAdHoc]) {
messagingMode = @"0";
}
NSString *urlString = [@"" stringByAppendingFormat:@"%@?device_id=%@&app_key=%@&token_session=1&test_mode=%@&ios_token=%@", config.host, [Countly.sharedInstance deviceID], config.appKey, messagingMode, token];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
[request setHTTPMethod:@"GET"];
[request setURL:[NSURL URLWithString:urlString]];
});
#endif
}
RCT_EXPORT_METHOD(pushTokenType : (NSArray *)arguments) {
#ifndef COUNTLY_EXCLUDE_PUSHNOTIFICATIONS
dispatch_async(dispatch_get_main_queue(), ^{
if (config == nil) {
config = CountlyConfig.new;
}
config.sendPushTokenAlways = YES;
config.pushTestMode = CLYPushTestModeProduction;
NSString *tokenType = [arguments objectAtIndex:0];
if ([tokenType isEqualToString:@"1"]) {
config.pushTestMode = CLYPushTestModeDevelopment;
} else if ([tokenType isEqualToString:@"2"]) {
config.pushTestMode = CLYPushTestModeTestFlightOrAdHoc;
}
CountlyPushNotifications.sharedInstance.pushTestMode = config.pushTestMode;
});
#endif
}
RCT_EXPORT_METHOD(askForNotificationPermission : (NSArray *)arguments) {
#ifndef COUNTLY_EXCLUDE_PUSHNOTIFICATIONS
[CountlyRNPushNotifications.sharedInstance askForNotificationPermission];
#endif
}
RCT_EXPORT_METHOD(registerForNotification : (NSArray *)arguments) {
#ifndef COUNTLY_EXCLUDE_PUSHNOTIFICATIONS
[CountlyRNPushNotifications.sharedInstance registerForNotification];
#endif
};
#ifndef COUNTLY_EXCLUDE_PUSHNOTIFICATIONS
- (void)notificationCallback:(NSString *_Nullable)notificationJson {
[self sendEventWithName:pushNotificationCallbackName body:notificationJson];
}
+ (void)startObservingNotifications {
[CountlyRNPushNotifications.sharedInstance startObservingNotifications];
}
+ (void)onNotification:(NSDictionary *_Nullable)notification {
[CountlyRNPushNotifications.sharedInstance onNotification:notification];
}
+ (void)onNotificationResponse:(UNNotificationResponse *_Nullable)response {
[CountlyRNPushNotifications.sharedInstance onNotificationResponse:response];
}
#endif
+ (void)log:(NSString *)theMessage {
if (config.enableDebug == YES) {
COUNTLY_RN_LOG(theMessage);
}
}
RCT_REMAP_METHOD(getCurrentDeviceId, getCurrentDeviceIdWithResolver : (RCTPromiseResolveBlock)resolve rejecter : (RCTPromiseRejectBlock)reject) {
dispatch_async(dispatch_get_main_queue(), ^{
id value = [Countly.sharedInstance deviceID];
if (value) {
resolve(value);
} else {
NSString *value = @"deviceIdNotFound";
resolve(value);
}
});
}
RCT_REMAP_METHOD(getDeviceIDType, getDeviceIDTypeWithResolver : (RCTPromiseResolveBlock)resolve rejecter : (RCTPromiseRejectBlock)reject) {
dispatch_async(dispatch_get_main_queue(), ^{
CLYDeviceIDType deviceIDType = [Countly.sharedInstance deviceIDType];
NSNumber *deviceIDTypeInt = NULL;
if ([deviceIDType isEqualToString:CLYDeviceIDTypeCustom]) {
deviceIDTypeInt = @20202;
} else if ([deviceIDType isEqualToString:CLYDeviceIDTypeTemporary]) {
deviceIDTypeInt = @30303;
} else {
deviceIDTypeInt = @10101;
}
resolve(deviceIDTypeInt);
});
}
RCT_EXPORT_METHOD(changeDeviceId : (NSArray *)arguments) {
dispatch_async(dispatch_get_main_queue(), ^{
NSString *newDeviceID = [arguments objectAtIndex:0];
if ([newDeviceID isEqual:@"TemporaryDeviceID"]) {
newDeviceID = CLYTemporaryDeviceID;
}
NSString *onServerString = [arguments objectAtIndex:1];
if ([onServerString isEqual:@"1"]) {
[Countly.sharedInstance setNewDeviceID:newDeviceID onServer:YES];
} else {
[Countly.sharedInstance setNewDeviceID:newDeviceID onServer:NO];
}
});
}
RCT_EXPORT_METHOD(setHttpPostForced : (NSArray *)arguments) {
dispatch_async(dispatch_get_main_queue(), ^{
NSString *isPost = [arguments objectAtIndex:0];
if (config == nil) {
config = CountlyConfig.new;
}
if ([isPost isEqual:@"1"]) {
config.alwaysUsePOST = YES;
} else {
config.alwaysUsePOST = NO;
}
});
}
RCT_EXPORT_METHOD(enableParameterTamperingProtection : (NSArray *)arguments) {
dispatch_async(dispatch_get_main_queue(), ^{
NSString *salt = [arguments objectAtIndex:0];
if (config == nil) {
config = CountlyConfig.new;
}
config.secretSalt = salt;
});
}
RCT_EXPORT_METHOD(pinnedCertificates : (NSArray *)arguments) {
dispatch_async(dispatch_get_main_queue(), ^{
NSString *certificateName = [arguments objectAtIndex:0];
if (config == nil) {
config = CountlyConfig.new;
}
config.pinnedCertificates = @[ certificateName ];
});
}
RCT_EXPORT_METHOD(startEvent : (NSArray *)arguments) {
dispatch_async(dispatch_get_main_queue(), ^{
NSString *startEvent = [arguments objectAtIndex:0];
[Countly.sharedInstance startEvent:startEvent];
});
}
RCT_EXPORT_METHOD(cancelEvent : (NSArray *)arguments) {
dispatch_async(dispatch_get_main_queue(), ^{
NSString *cancelEvent = [arguments objectAtIndex:0];
[Countly.sharedInstance cancelEvent:cancelEvent];
});
}
RCT_EXPORT_METHOD(endEvent : (NSDictionary *)arguments) {
dispatch_async(dispatch_get_main_queue(), ^{
NSString *eventName = [arguments objectForKey:@"n"];
NSNumber *countNumber = [arguments objectForKey:@"c"];
int countInt = [countNumber intValue];
NSNumber *sumNumber = [arguments objectForKey:@"s"];
float sumFloat = [sumNumber floatValue];
NSMutableDictionary *dict = nil;
NSArray *segments = [arguments objectForKey:@"g"];
if (segments != nil) {
dict = [[NSMutableDictionary alloc] init];
for (int i = 0, il = (int)segments.count; i < il; i += 2) {
dict[[segments objectAtIndex:i]] = [segments objectAtIndex:i + 1];
}
}
[[Countly sharedInstance] endEvent:eventName segmentation:dict count:countInt sum:sumFloat];
});
}
RCT_EXPORT_METHOD(setLocationInit : (NSArray *)arguments) {
dispatch_async(dispatch_get_main_queue(), ^{
if (config == nil) {
config = CountlyConfig.new;
}
NSString *countryCode = [arguments objectAtIndex:0];
NSString *city = [arguments objectAtIndex:1];
NSString *locationString = [arguments objectAtIndex:2];
NSString *ipAddress = [arguments objectAtIndex:3];
if (locationString != nil && ![locationString isEqualToString:@"null"] && [locationString containsString:@","]) {
@try {
NSArray *locationArray = [locationString componentsSeparatedByString:@","];
NSString *latitudeString = [locationArray objectAtIndex:0];
NSString *longitudeString = [locationArray objectAtIndex:1];
double latitudeDouble = [latitudeString doubleValue];
double longitudeDouble = [longitudeString doubleValue];
config.location = (CLLocationCoordinate2D){latitudeDouble, longitudeDouble};
} @catch (NSException *exception) {
COUNTLY_RN_LOG(@"Invalid location: %@", locationString);
}
}
if (city != nil && ![city isEqualToString:@"null"]) {
config.city = city;
}
if (countryCode != nil && ![countryCode isEqualToString:@"null"]) {
config.ISOCountryCode = countryCode;
}
if (ipAddress != nil && ![ipAddress isEqualToString:@"null"]) {
config.IP = ipAddress;
}
});
}
RCT_EXPORT_METHOD(setLocation : (NSArray *)arguments) {
dispatch_async(dispatch_get_main_queue(), ^{
NSString *countryCode = [arguments objectAtIndex:0];
NSString *city = [arguments objectAtIndex:1];
NSString *gpsCoordinate = [arguments objectAtIndex:2];
NSString *ipAddress = [arguments objectAtIndex:3];
if ([@"null" isEqualToString:city]) {
city = nil;
}
if ([@"null" isEqualToString:countryCode]) {
countryCode = nil;
}
if ([@"null" isEqualToString:gpsCoordinate]) {
gpsCoordinate = nil;
}
if ([@"null" isEqualToString:ipAddress]) {
ipAddress = nil;
}
CLLocationCoordinate2D locationCoordinate = [self getCoordinate:gpsCoordinate];
[Countly.sharedInstance recordLocation:locationCoordinate city:city ISOCountryCode:countryCode IP:ipAddress];
});
}
RCT_EXPORT_METHOD(disableLocation) {
dispatch_async(dispatch_get_main_queue(), ^{
[Countly.sharedInstance disableLocationInfo];
});
}
- (CLLocationCoordinate2D)getCoordinate:(NSString *)gpsCoordinate {
CLLocationCoordinate2D locationCoordinate = kCLLocationCoordinate2DInvalid;
if (gpsCoordinate) {
if ([gpsCoordinate containsString:@","]) {
@try {
NSArray *locationArray = [gpsCoordinate componentsSeparatedByString:@","];
if (locationArray.count > 2) {
COUNTLY_RN_LOG(@"Invalid location Coordinates:[%@], it should contains only two comma seperated values", gpsCoordinate);
}
NSString *latitudeString = [locationArray objectAtIndex:0];
NSString *longitudeString = [locationArray objectAtIndex:1];
double latitudeDouble = [latitudeString doubleValue];
double longitudeDouble = [longitudeString doubleValue];
if (latitudeDouble == 0 || longitudeDouble == 0) {
COUNTLY_RN_LOG(@"Invalid location Coordinates, One of the values parsed to a 0, double check that given coordinates are correct:[%@]", gpsCoordinate);
}
locationCoordinate = (CLLocationCoordinate2D){latitudeDouble, longitudeDouble};
} @catch (NSException *exception) {
COUNTLY_RN_LOG(@"Invalid location Coordinates:[%@], Exception occurred while parsing Coordinates:[%@]", gpsCoordinate, exception);
}
} else {
COUNTLY_RN_LOG(@"Invalid location Coordinates:[%@], lat and long values should be comma separated", gpsCoordinate);
}
}
return locationCoordinate;
}
RCT_EXPORT_METHOD(enableCrashReporting) {
dispatch_async(dispatch_get_main_queue(), ^{
if (config == nil) {
config = CountlyConfig.new;
}
[self addCountlyFeature:CLYCrashReporting];
});
}
RCT_EXPORT_METHOD(addCrashLog : (NSArray *)arguments) {
dispatch_async(dispatch_get_main_queue(), ^{
NSString *logs = [arguments objectAtIndex:0];
[Countly.sharedInstance recordCrashLog:logs];
});
}
RCT_EXPORT_METHOD(setCustomCrashSegments : (NSArray *)arguments) {
dispatch_async(dispatch_get_main_queue(), ^{
NSMutableDictionary *dict = [[NSMutableDictionary alloc] init];
for (int i = 0, il = (int)arguments.count; i < il; i += 2) {
dict[[arguments objectAtIndex:i]] = [arguments objectAtIndex:i + 1];
}
if (config == nil) {
config = CountlyConfig.new;
}
config.crashSegmentation = dict;
});
}
RCT_EXPORT_METHOD(logException : (NSArray *)arguments) {
dispatch_async(dispatch_get_main_queue(), ^{
NSString *execption = [arguments objectAtIndex:0];
NSString *nonfatal = [arguments objectAtIndex:1];
NSArray *nsException = [execption componentsSeparatedByString:@"\n"];
NSMutableDictionary *dict = [[NSMutableDictionary alloc] init];
for (int i = 2, il = (int)arguments.count; i < il; i += 2) {
dict[[arguments objectAtIndex:i]] = [arguments objectAtIndex:i + 1];
}
[dict setObject:nonfatal forKey:@"nonfatal"];
NSException *myException = [NSException exceptionWithName:@"Exception" reason:execption userInfo:dict];
[Countly.sharedInstance recordHandledException:myException withStackTrace:nsException];
});
}
RCT_EXPORT_METHOD(logJSException : (NSString *)errTitle withMessage : (NSString *)message withStack : (NSString *)stackTrace) {
dispatch_async(dispatch_get_main_queue(), ^{
NSException *myException = [NSException exceptionWithName:errTitle reason:message userInfo:@{@"nonfatal" : @"1"}];
NSArray *stack = [stackTrace componentsSeparatedByString:@"\n"];
[Countly.sharedInstance recordHandledException:myException withStackTrace:stack];
});
}
RCT_REMAP_METHOD(userData_setProperty, params : (NSArray *)arguments userDataSetPropertyWithResolver : (RCTPromiseResolveBlock)resolve rejecter : (RCTPromiseRejectBlock)reject) {
dispatch_async(dispatch_get_main_queue(), ^{
NSString *keyName = [arguments objectAtIndex:0];
NSString *keyValue = [arguments objectAtIndex:1];
[Countly.user set:keyName value:keyValue];
[Countly.user save];
resolve(@"Success");
});
}
RCT_REMAP_METHOD(userData_increment, params : (NSArray *)arguments userDataIncrementWithResolver : (RCTPromiseResolveBlock)resolve rejecter : (RCTPromiseRejectBlock)reject) {
dispatch_async(dispatch_get_main_queue(), ^{
NSString *keyName = [arguments objectAtIndex:0];
[Countly.user increment:keyName];
[Countly.user save];
resolve(@"Success");
});
}
RCT_REMAP_METHOD(userData_incrementBy, params : (NSArray *)arguments userDataIncrementByWithResolver : (RCTPromiseResolveBlock)resolve rejecter : (RCTPromiseRejectBlock)reject) {
dispatch_async(dispatch_get_main_queue(), ^{
NSString *keyName = [arguments objectAtIndex:0];
NSString *keyValue = [arguments objectAtIndex:1];
int keyValueInteger = [keyValue intValue];
[Countly.user incrementBy:keyName value:[NSNumber numberWithInt:keyValueInteger]];
[Countly.user save];
resolve(@"Success");
});
}
RCT_REMAP_METHOD(userData_multiply, params : (NSArray *)arguments userDataMultiplyWithResolver : (RCTPromiseResolveBlock)resolve rejecter : (RCTPromiseRejectBlock)reject) {
dispatch_async(dispatch_get_main_queue(), ^{
NSString *keyName = [arguments objectAtIndex:0];
NSString *keyValue = [arguments objectAtIndex:1];
int keyValueInteger = [keyValue intValue];
[Countly.user multiply:keyName value:[NSNumber numberWithInt:keyValueInteger]];
[Countly.user save];
resolve(@"Success");
});
}
RCT_REMAP_METHOD(userData_saveMax, params : (NSArray *)arguments userDataSaveMaxWithResolver : (RCTPromiseResolveBlock)resolve rejecter : (RCTPromiseRejectBlock)reject) {
dispatch_async(dispatch_get_main_queue(), ^{
NSString *keyName = [arguments objectAtIndex:0];
NSString *keyValue = [arguments objectAtIndex:1];
int keyValueInteger = [keyValue intValue];
[Countly.user max:keyName value:[NSNumber numberWithInt:keyValueInteger]];
[Countly.user save];
resolve(@"Success");
});
}
RCT_REMAP_METHOD(userData_saveMin, params : (NSArray *)arguments userDataSaveMinWithResolver : (RCTPromiseResolveBlock)resolve rejecter : (RCTPromiseRejectBlock)reject) {
dispatch_async(dispatch_get_main_queue(), ^{
NSString *keyName = [arguments objectAtIndex:0];
NSString *keyValue = [arguments objectAtIndex:1];
int keyValueInteger = [keyValue intValue];
[Countly.user min:keyName value:[NSNumber numberWithInt:keyValueInteger]];
[Countly.user save];
resolve(@"Success");
});
}
RCT_REMAP_METHOD(userData_setOnce, params : (NSArray *)arguments userDataSetOnce : (RCTPromiseResolveBlock)resolve rejecter : (RCTPromiseRejectBlock)reject) {
dispatch_async(dispatch_get_main_queue(), ^{
NSString *keyName = [arguments objectAtIndex:0];
NSString *keyValue = [arguments objectAtIndex:1];
[Countly.user setOnce:keyName value:keyValue];
[Countly.user save];
resolve(@"Success");
});
}
RCT_REMAP_METHOD(userData_pushUniqueValue, params : (NSArray *)arguments userDataPushUniqueValueWithResolver : (RCTPromiseResolveBlock)resolve rejecter : (RCTPromiseRejectBlock)reject) {
dispatch_async(dispatch_get_main_queue(), ^{
NSString *keyName = [arguments objectAtIndex:0];
NSString *keyValue = [arguments objectAtIndex:1];
[Countly.user pushUnique:keyName value:keyValue];
[Countly.user save];
resolve(@"Success");
});
}
RCT_REMAP_METHOD(userData_pushValue, params : (NSArray *)arguments userDataPushValueWithResolver : (RCTPromiseResolveBlock)resolve rejecter : (RCTPromiseRejectBlock)reject) {
dispatch_async(dispatch_get_main_queue(), ^{
NSString *keyName = [arguments objectAtIndex:0];
NSString *keyValue = [arguments objectAtIndex:1];
[Countly.user push:keyName value:keyValue];
[Countly.user save];
resolve(@"Success");
});
}
RCT_REMAP_METHOD(userData_pullValue, params : (NSArray *)arguments userDataPullValueWithResolver : (RCTPromiseResolveBlock)resolve rejecter : (RCTPromiseRejectBlock)reject) {
dispatch_async(dispatch_get_main_queue(), ^{
NSString *keyName = [arguments objectAtIndex:0];
NSString *keyValue = [arguments objectAtIndex:1];
[Countly.user pull:keyName value:keyValue];
[Countly.user save];
resolve(@"Success");
});
}
RCT_REMAP_METHOD(userDataBulk_setUserProperties, params : (NSDictionary *)userProperties userDataBulkSetUserPropertiesWithResolver : (RCTPromiseResolveBlock)resolve rejecter : (RCTPromiseRejectBlock)reject) {
dispatch_async(dispatch_get_main_queue(), ^{
[self setUserDataIntenral:userProperties];
NSDictionary *customeProperties = [self removePredefinedUserProperties:userProperties];
Countly.user.custom = customeProperties;
resolve(@"Success");
});
}
RCT_REMAP_METHOD(userDataBulk_save, params : (NSArray *)arguments userDataBulkSaveWithResolver : (RCTPromiseResolveBlock)resolve rejecter : (RCTPromiseRejectBlock)reject) {
dispatch_async(dispatch_get_main_queue(), ^{
[Countly.user save];
resolve(@"Success");
});
}
RCT_REMAP_METHOD(userDataBulk_setProperty, params : (NSArray *)arguments userDataBulkSetPropertyWithResolver : (RCTPromiseResolveBlock)resolve rejecter : (RCTPromiseRejectBlock)reject) {
dispatch_async(dispatch_get_main_queue(), ^{
NSString *keyName = [arguments objectAtIndex:0];
NSString *keyValue = [arguments objectAtIndex:1];
[Countly.user set:keyName value:keyValue];
resolve(@"Success");
});
}
RCT_REMAP_METHOD(userDataBulk_increment, params : (NSArray *)arguments userDataBulkIncrementWithResolver : (RCTPromiseResolveBlock)resolve rejecter : (RCTPromiseRejectBlock)reject) {
dispatch_async(dispatch_get_main_queue(), ^{
NSString *keyName = [arguments objectAtIndex:0];
[Countly.user increment:keyName];
resolve(@"Success");
});
}
RCT_REMAP_METHOD(userDataBulk_incrementBy, params : (NSArray *)arguments userDataBulkIncrementByWithResolver : (RCTPromiseResolveBlock)resolve rejecter : (RCTPromiseRejectBlock)reject) {
dispatch_async(dispatch_get_main_queue(), ^{
NSString *keyName = [arguments objectAtIndex:0];
NSString *keyValue = [arguments objectAtIndex:1];
int keyValueInteger = [keyValue intValue];
[Countly.user incrementBy:keyName value:[NSNumber numberWithInt:keyValueInteger]];
resolve(@"Success");
});
}
RCT_REMAP_METHOD(userDataBulk_multiply, params : (NSArray *)arguments userDataBulkMultiplyWithResolver : (RCTPromiseResolveBlock)resolve rejecter : (RCTPromiseRejectBlock)reject) {
dispatch_async(dispatch_get_main_queue(), ^{
NSString *keyName = [arguments objectAtIndex:0];
NSString *keyValue = [arguments objectAtIndex:1];
int keyValueInteger = [keyValue intValue];
[Countly.user multiply:keyName value:[NSNumber numberWithInt:keyValueInteger]];
resolve(@"Success");
});
}
RCT_REMAP_METHOD(userDataBulk_saveMax, params : (NSArray *)arguments userDataBulkSaveMaxWithResolver : (RCTPromiseResolveBlock)resolve rejecter : (RCTPromiseRejectBlock)reject) {
dispatch_async(dispatch_get_main_queue(), ^{
NSString *keyName = [arguments objectAtIndex:0];
NSString *keyValue = [arguments objectAtIndex:1];
int keyValueInteger = [keyValue intValue];
[Countly.user max:keyName value:[NSNumber numberWithInt:keyValueInteger]];
resolve(@"Success");
});
}
RCT_REMAP_METHOD(userDataBulk_saveMin, params : (NSArray *)arguments userDataBulkSaveMinWithResolver : (RCTPromiseResolveBlock)resolve rejecter : (RCTPromiseRejectBlock)reject) {
dispatch_async(dispatch_get_main_queue(), ^{
NSString *keyName = [arguments objectAtIndex:0];
NSString *keyValue = [arguments objectAtIndex:1];
int keyValueInteger = [keyValue intValue];
[Countly.user min:keyName value:[NSNumber numberWithInt:keyValueInteger]];
resolve(@"Success");
});
}
RCT_REMAP_METHOD(userDataBulk_setOnce, params : (NSArray *)arguments userDataBulkSetOnceWithResolver : (RCTPromiseResolveBlock)resolve rejecter : (RCTPromiseRejectBlock)reject) {
dispatch_async(dispatch_get_main_queue(), ^{
NSString *keyName = [arguments objectAtIndex:0];
NSString *keyValue = [arguments objectAtIndex:1];
[Countly.user setOnce:keyName value:keyValue];
resolve(@"Success");
});
}
RCT_REMAP_METHOD(userDataBulk_pushUniqueValue, params : (NSArray *)arguments userDataBulkPushUniqueValueWithResolver : (RCTPromiseResolveBlock)resolve rejecter : (RCTPromiseRejectBlock)reject) {
dispatch_async(dispatch_get_main_queue(), ^{
NSString *keyName = [arguments objectAtIndex:0];
NSString *keyValue = [arguments objectAtIndex:1];
[Countly.user pushUnique:keyName value:keyValue];
resolve(@"Success");
});
}
RCT_REMAP_METHOD(userDataBulk_pushValue, params : (NSArray *)arguments userDataBulkPushValueWithResolver : (RCTPromiseResolveBlock)resolve rejecter : (RCTPromiseRejectBlock)reject) {
dispatch_async(dispatch_get_main_queue(), ^{
NSString *keyName = [arguments objectAtIndex:0];
NSString *keyValue = [arguments objectAtIndex:1];
[Countly.user push:keyName value:keyValue];
resolve(@"Success");
});
}
RCT_REMAP_METHOD(userDataBulk_pullValue, params : (NSArray *)arguments userDataBulkPullValueWithResolver : (RCTPromiseResolveBlock)resolve rejecter : (RCTPromiseRejectBlock)reject) {
dispatch_async(dispatch_get_main_queue(), ^{
NSString *keyName = [arguments objectAtIndex:0];
NSString *keyValue = [arguments objectAtIndex:1];
[Countly.user pull:keyName value:keyValue];
resolve(@"Success");
});
}
RCT_EXPORT_METHOD(setRequiresConsent : (NSArray *)arguments) {
dispatch_async(dispatch_get_main_queue(), ^{
if (config == nil) {
config = CountlyConfig.new;
}
BOOL consentFlag = [[arguments objectAtIndex:0] boolValue];
config.requiresConsent = consentFlag;
});
}
RCT_EXPORT_METHOD(giveConsentInit : (NSArray *)arguments) {
dispatch_async(dispatch_get_main_queue(), ^{
if (config == nil) {
config = CountlyConfig.new;
}
config.consents = arguments;
});
}
RCT_EXPORT_METHOD(recordDirectAttribution : (NSArray *)arguments) {
dispatch_async(dispatch_get_main_queue(), ^{
NSString *campaignType = [arguments objectAtIndex:0];
NSString *campaignData = [arguments objectAtIndex:1];
if (CountlyCommon.sharedInstance.hasStarted) {
[Countly.sharedInstance recordDirectAttributionWithCampaignType:campaignType andCampaignData:campaignData];
} else {
config.campaignType = campaignType;
config.campaignData = campaignData;
}
});
}
RCT_EXPORT_METHOD(recordIndirectAttribution : (NSArray *)arguments) {
dispatch_async(dispatch_get_main_queue(), ^{
NSDictionary *attributionValues = [arguments objectAtIndex:0];
if (CountlyCommon.sharedInstance.hasStarted) {
[Countly.sharedInstance recordIndirectAttribution:attributionValues];
} else {
config.indirectAttribution = attributionValues;
}
});
}
RCT_EXPORT_METHOD(giveConsent : (NSArray *)arguments) {
dispatch_async(dispatch_get_main_queue(), ^{
[Countly.sharedInstance giveConsentForFeatures:arguments];
});
}
RCT_EXPORT_METHOD(removeConsent : (NSArray *)arguments) {
dispatch_async(dispatch_get_main_queue(), ^{
[Countly.sharedInstance cancelConsentForFeatures:arguments];
});
}
RCT_EXPORT_METHOD(giveAllConsent) {
dispatch_async(dispatch_get_main_queue(), ^{
[Countly.sharedInstance giveConsentForFeature:CLYConsentLocation];
[Countly.sharedInstance giveConsentForAllFeatures];
});
}
RCT_EXPORT_METHOD(removeAllConsent) {
dispatch_async(dispatch_get_main_queue(), ^{
[Countly.sharedInstance cancelConsentForAllFeatures];
});
}
RCT_EXPORT_METHOD(remoteConfigUpdate : (NSArray *)arguments callback : (RCTResponseSenderBlock)callback) {
dispatch_async(dispatch_get_main_queue(), ^{
[Countly.sharedInstance updateRemoteConfigWithCompletionHandler:^(NSError *error) {
if (!error) {
NSArray *result = @[ @"Remote Config is updated and ready to use!" ];
callback(@[ result ]);
} else {
NSString *returnString = [NSString stringWithFormat:@"There was an error while updating Remote Config: %@", error];
NSArray *result = @[ returnString ];
callback(@[ result ]);
}
}];
});
}
RCT_EXPORT_METHOD(updateRemoteConfigForKeysOnly : (NSArray *)arguments callback : (RCTResponseSenderBlock)callback) {
dispatch_async(dispatch_get_main_queue(), ^{
NSMutableArray *randomSelection = [[NSMutableArray alloc] init];
for (int i = 0; i < (int)arguments.count; i++) {
[randomSelection addObject:[arguments objectAtIndex:i]];
}
NSArray *keyNames = [randomSelection copy];
[Countly.sharedInstance updateRemoteConfigOnlyForKeys:keyNames
completionHandler:^(NSError *error) {
if (!error) {
NSArray *result = @[ @"Remote Config is updated only for given keys and ready to use!" ];
callback(@[ result ]);
} else {
NSString *returnString = [NSString stringWithFormat:@"There was an error while updating Remote Config: %@", error];
NSArray *result = @[ returnString ];
callback(@[ result ]);
}
}];
});
}
RCT_EXPORT_METHOD(updateRemoteConfigExceptKeys : (NSArray *)arguments callback : (RCTResponseSenderBlock)callback) {
dispatch_async(dispatch_get_main_queue(), ^{
NSMutableArray *randomSelection = [[NSMutableArray alloc] init];
for (int i = 0; i < (int)arguments.count; i++) {
[randomSelection addObject:[arguments objectAtIndex:i]];
}
NSArray *keyNames = [randomSelection copy];
[Countly.sharedInstance updateRemoteConfigExceptForKeys:keyNames
completionHandler:^(NSError *error) {