forked from Countly/countly-sdk-react-native-bridge
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCountly.js
1192 lines (1083 loc) · 37.2 KB
/
Countly.js
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
/**
* Countly SDK React Native Bridge
* https://github.com/Countly/countly-sdk-react-native-bridge
* @Countly
*/
import {
Platform,
NativeModules,
NativeEventEmitter
} from 'react-native';
import parseErrorStackLib from '../react-native/Libraries/Core/Devtools/parseErrorStack.js';
const CountlyReactNative = NativeModules.CountlyReactNative;
const eventEmitter = new NativeEventEmitter(CountlyReactNative);
const Countly = {};
Countly.serverUrl = "";
Countly.appKey = "";
Countly.messagingMode = {"DEVELOPMENT":"1","PRODUCTION":"0", "ADHOC": "2"};
if (Platform.OS.match("android")) {
Countly.messagingMode.DEVELOPMENT = "2";
}
// countly initialization
Countly.init = async function(serverUrl, appKey, deviceId){
if(deviceId == "") {
deviceId = null;
Countly.logError("init", "Device Id during init can't be empty string");
}
Countly.serverUrl = serverUrl;
Countly.appKey = appKey;
var args = [];
args.push(serverUrl);
args.push(appKey);
args.push(deviceId);
await CountlyReactNative.init(args);
}
Countly.isInitialized = async function(){
// returns a promise
return await CountlyReactNative.isInitialized();
}
Countly.hasBeenCalledOnStart = function(){
// returns a promise
return CountlyReactNative.hasBeenCalledOnStart();
}
// countly sending various types of events
Countly.sendEvent = function(options){
var args = [];
var eventType = "event"; //event, eventWithSum, eventWithSegment, eventWithSumSegment
var segments = {};
if(options.eventSum)
eventType = "eventWithSum";
if(options.segments)
eventType = "eventWithSegment";
if(options.segments && options.eventSum)
eventType = "eventWithSumSegment";
args.push(eventType);
if(options.eventName){
args.push(options.eventName.toString());
}else{
args.push("");
}
if(options.eventCount){
args.push(options.eventCount.toString());
}
else{
args.push("1");
}
if(options.eventSum){
options.eventSum = options.eventSum.toString();
if(options.eventSum.indexOf(".") == -1){
options.eventSum = parseFloat(options.eventSum).toFixed(2);
args.push(options.eventSum);
}else{
args.push(options.eventSum);
}
}
if(options.segments)
segments = options.segments;
for (var event in segments) {
args.push(event);
args.push(segments[event]);
}
CountlyReactNative.event(args);
}
/**
* Enable or disable automatic view tracking
*
* @deprecated in 20.04.6
*
*/
Countly.setViewTracking = async function(boolean) {
if(await CountlyReactNative.isLoggingEnabled()) {
console.log("[CountlyReactNative] setViewTracking is deprecated.");
}
}
/**
* Record custom view to Countly.
*
* @param {string} recordView - name of the view
* @param {Map} segments - allows to add optional segmentation,
* Supported data type for segments values are String, int, double and bool
*/
Countly.recordView = async function(recordView, segments){
var message = await Countly.validateString(recordView, "view name", "recordView");
if(message) {
return message;
}
var args = [];
args.push(String(recordView));
if(!segments){
segments = {};
}
for(var key in segments){
args.push(key);
args.push(segments[key]);
}
CountlyReactNative.recordView(args);
};
/**
* Disable push notifications feature, by default it is enabled.
* Currently implemented for iOS only
* Should be called before Countly init
*/
Countly.disablePushNotifications = function(){
if (!Platform.OS.match("ios")) return "disablePushNotifications : To be implemented";
CountlyReactNative.disablePushNotifications();
}
/**
*
* Set messaging mode for push notifications
* Should be called before Countly init
*/
Countly.pushTokenType = async function(tokenType, channelName, channelDescription){
var message = await Countly.validateString(tokenType, "tokenType", "pushTokenType");
if(message) {
return message;
}
var args = [];
args.push(tokenType);
args.push(channelName || "");
args.push(channelDescription || "");
CountlyReactNative.pushTokenType(args);
}
Countly.sendPushToken = function(options){
var args = [];
args.push(options.token || "");
CountlyReactNative.sendPushToken(args);
}
/**
* This method will ask for permission, enables push notification and send push token to countly server.
* Should be called after Countly init
*/
Countly.askForNotificationPermission = function(){
CountlyReactNative.askForNotificationPermission([]);
}
/**
*
* Set callback to receive push notifications
* @param { callback listner } theListener
*/
Countly.registerForNotification = function(theListener){
var event = eventEmitter.addListener('onCountlyPushNotification', theListener);
CountlyReactNative.registerForNotification([]);
return event;
};
// countly start for android
Countly.start = function(){
CountlyReactNative.start();
}
// countly stop for android
Countly.stop = function(){
CountlyReactNative.stop();
}
/**
* Enable countly internal debugging logs
* Should be called before Countly init
*
* @deprecated in 20.04.6
*
* @function Countly.setLoggingEnabled should be used to enable/disable countly internal debugging logs
*/
Countly.enableLogging = function(){
CountlyReactNative.setLoggingEnabled([true]);
}
/**
* Disable countly internal debugging logs
*
* @deprecated in 20.04.6
*
* @function Countly.setLoggingEnabled should be used to enable/disable countly internal debugging logs
*/
Countly.disableLogging = function(){
CountlyReactNative.setLoggingEnabled([false]);
}
/**
* Set to true if you want to enable countly internal debugging logs
* Should be called before Countly init
*/
Countly.setLoggingEnabled = function(enabled = true){
CountlyReactNative.setLoggingEnabled([enabled]);
}
Countly.onSuccess = function(result){
// alert(result);
}
Countly.onError = function(error){
// alert("error");
// alert(error);
}
Countly.demo = function(){
}
/**
* Set user initial location
* Should be called before init
* @param {ISO Country code for the user's country} countryCode
* @param {Name of the user's city} city
* @param {comma separate lat and lng values. For example, "56.42345,123.45325"} location
* @param {IP address of user's} ipAddress
* */
Countly.setLocationInit = function(countryCode, city, location, ipAddress){
var args = [];
args.push(countryCode || "null");
args.push(city || "null");
args.push(location || "null");
args.push(ipAddress || "null");
CountlyReactNative.setLocationInit(args);
}
Countly.setLocation = function(countryCode, city, location, ipAddress){
var args = [];
args.push(countryCode || "null");
args.push(city || "null");
args.push(location || "null");
args.push(ipAddress || "null");
CountlyReactNative.setLocation(args);
}
Countly.disableLocation = function(){
CountlyReactNative.disableLocation();
}
/**
*
* Get currently used device Id.
* Should be called after Countly init
* */
Countly.getCurrentDeviceId = async function(){
if(!await Countly.isInitialized()) {
var message = "init must be called before getCurrentDeviceId";
Countly.logWarning("getCurrentDeviceId", message);
return message;
}
const result = await CountlyReactNative.getCurrentDeviceId();
return result;
}
Countly.changeDeviceId = async function(newDeviceID, onServer){
var message = await Countly.validateString(newDeviceID, "newDeviceID", "changeDeviceId");
if(message) {
return message;
}
if(onServer === false){
onServer = "0";
}else{
onServer = "1";
}
newDeviceID = newDeviceID.toString();
CountlyReactNative.changeDeviceId([newDeviceID, onServer]);
}
/**
*
* Set to "true" if you want HTTP POST to be used for all requests
* Should be called before Countly init
*/
Countly.setHttpPostForced = function(boolean = true){
var args = [];
args.push(boolean == true ?"1":"0");
CountlyReactNative.setHttpPostForced(args);
}
Countly.isCrashReportingEnabled = false;
/**
* Enable crash reporting to report unhandled crashes to Countly
* Should be called before Countly init
*/
Countly.enableCrashReporting = async function(){
CountlyReactNative.enableCrashReporting();
if (ErrorUtils && !Countly.isCrashReportingEnabled) {
if(await CountlyReactNative.isLoggingEnabled()) {
console.log("[CountlyReactNative] Adding Countly JS error handler.");
}
var previousHandler = ErrorUtils.getGlobalHandler();
ErrorUtils.setGlobalHandler(function (error, isFatal) {
let jsStackTrace = Countly.getStackTrace(error);
let errorTitle;
let stackArr;
if(jsStackTrace == null) {
errorTitle = error.name;
stackArr = error.stack;
}
else {
var fname = jsStackTrace[0].file;
if (fname.startsWith("http")) {
var chunks = fname.split("/");
fname = chunks[chunks.length-1].split("?")[0];
}
errorTitle = `${error.name} (${jsStackTrace[0].methodName}@${fname})`;
const regExp = "(.*)(@?)http(s?).*/(.*)\\?(.*):(.*):(.*)";
stackArr = error.stack.split("\n").map(row => {
row = row.trim();
if (!row.includes("http")) return row;
else {
const matches = row.match(regExp);
return matches && matches.length == 8 ? `${matches[1]}${matches[2]}${matches[4]}(${matches[6]}:${matches[7]})` : row;
}
})
stackArr = stackArr.join("\n");
}
CountlyReactNative.logJSException(errorTitle, error.message.trim(), stackArr);
if (previousHandler) {
previousHandler(error, isFatal);
}
});
}
Countly.isCrashReportingEnabled = true;
}
Countly.getStackTrace = (e) => {
let jsStackTrace = null;
try {
if (Platform.hasOwnProperty("constants")) {
// RN version >= 0.63
if (Platform.constants.reactNativeVersion.minor >= 64)
// RN version >= 0.64
jsStackTrace = parseErrorStackLib(e.stack);
// RN version == 0.63
else jsStackTrace = parseErrorStackLib(e);
}
// RN version < 0.63
else jsStackTrace = parseErrorStackLib(e);
}
catch (e) {
// console.log(e.message);
}
return jsStackTrace;
};
Countly.addCrashLog = function(crashLog){
CountlyReactNative.addCrashLog([crashLog]);
}
Countly.logException = function(exception, nonfatal, segments){
var exceptionArray = exception.split('\n');
var exceptionString = "";
for(var i=0,il=exceptionArray.length;i<il;i++){
exceptionString += "" +exceptionArray[i] +"\n";
}
var args = [];
args.push(exceptionString || "");
args.push(nonfatal || false);
for(var key in segments){
args.push(key);
args.push(segments[key].toString());
}
CountlyReactNative.logException(args);
}
Countly.setCustomCrashSegments = function(segments){
var args = [];
for(var key in segments){
args.push(key.toString());
args.push(segments[key].toString());
}
CountlyReactNative.setCustomCrashSegments(args);
}
Countly.startSession = function(){
CountlyReactNative.startSession();
}
Countly.endSession = function(){
CountlyReactNative.endSession();
}
/**
*
* Set the optional salt to be used for calculating the checksum of requested data which will be sent with each request, using the &checksum field
* Should be called before Countly init
*/
Countly.enableParameterTamperingProtection = async function(salt){
var message = await Countly.validateString(salt, "salt", "enableParameterTamperingProtection");
if(message) {
return message;
}
CountlyReactNative.enableParameterTamperingProtection([salt.toString()]);
}
/**
*
* It will ensure that connection is made with one of the public keys specified
* Should be called before Countly init
*/
Countly.pinnedCertificates = async function(certificateName){
var message = await Countly.validateString(certificateName, "certificateName", "pinnedCertificates");
if(message) {
return message;
}
CountlyReactNative.pinnedCertificates([certificateName]);
}
Countly.startEvent = async function(eventName){
var message = await Countly.validateString(eventName, "eventName", "startEvent");
if(message) {
return message;
}
CountlyReactNative.startEvent([eventName.toString()]);
}
Countly.cancelEvent = async function(eventName){
var message = await Countly.validateString(eventName, "eventName", "cancelEvent");
if(message) {
return message;
}
CountlyReactNative.cancelEvent([eventName.toString()]);
}
Countly.endEvent = function(options){
if(typeof options === "string") {
options = {eventName: options};
}
var args = [];
var eventType = "event"; //event, eventWithSum, eventWithSegment, eventWithSumSegment
var segments = {};
if(options.eventSum)
eventType = "eventWithSum";
if(options.segments)
eventType = "eventWithSegment";
if(options.segments && options.eventSum)
eventType = "eventWithSumSegment";
args.push(eventType);
if(!options.eventName)
options.eventName = "";
args.push(options.eventName.toString());
if(!options.eventCount)
options.eventCount = "1";
args.push(options.eventCount.toString());
if(options.eventSum){
var eventSumTemp = options.eventSum.toString();
if(eventSumTemp.indexOf(".") == -1){
eventSumTemp = parseFloat(eventSumTemp).toFixed(2);
args.push(eventSumTemp);
}else{
args.push(eventSumTemp);
}
}else{
args.push('0.0');
}
if(options.segments)
segments = options.segments;
for (var event in segments) {
args.push(event);
args.push(segments[event]);
}
CountlyReactNative.endEvent(args);
};
// countly sending user data
Countly.setUserData = async function(userData){
var message = null;
if(!userData) {
message = "User profile data should not be null or undefined";
Countly.logError("setUserData", message);
return message;
}
if(typeof userData !== 'object'){
message = "unsupported data type of user data '" + (typeof userData) + "'";
Countly.logWarning("setUserData", message);
return message;
}
var args = [];
for(var key in userData){
if (typeof userData[key] != "string" && key.toString() != "byear")
{
message = "skipping value for key '" + key.toString() + "', due to unsupported data type '" + (typeof userData[key]) + "', its data type should be 'string'";
Countly.logWarning(setUserData, message);
}
}
if(userData.org && !userData.organization) {
userData.organization = userData.org;
delete userData.org;
}
if(userData.byear) {
Countly.validateParseInt(userData.byear, "key byear", "setUserData");
userData.byear = userData.byear.toString();
}
args.push(userData);
CountlyReactNative.setUserData(args);
};
Countly.userData = {};
Countly.userData.setProperty = async function(keyName, keyValue){
var message = await Countly.validateString(keyName, "key", "setProperty");
if(message) {
return message;
}
message = await Countly.validateValidUserData(keyValue, "value", "setProperty");
if(message) {
return message;
}
keyName = keyName.toString();
keyValue = keyValue.toString();
if(keyName && (keyValue || keyValue == "")) {
CountlyReactNative.userData_setProperty([keyName, keyValue]);
}
};
Countly.userData.increment = async function(keyName){
var message = await Countly.validateString(keyName, "key", "setProperty");
if(message) {
return message;
}
keyName = keyName.toString();
if(keyName) {
CountlyReactNative.userData_increment([keyName]);
}
};
Countly.userData.incrementBy = async function(keyName, keyValue){
var message = await Countly.validateString(keyName, "key", "incrementBy");
if(message) {
return message;
}
message = await Countly.validateUserDataValue(keyValue, "value", "incrementBy");
if(message) {
return message;
}
var intValue = parseInt(keyValue).toString();
CountlyReactNative.userData_incrementBy([keyName, intValue]);
};
Countly.userData.multiply = async function(keyName, keyValue){
var message = await Countly.validateString(keyName, "key", "multiply");
if(message) {
return message;
}
message = await Countly.validateUserDataValue(keyValue, "value", "multiply");
if(message) {
return message;
}
var intValue = parseInt(keyValue).toString();
CountlyReactNative.userData_multiply([keyName, intValue]);
};
Countly.userData.saveMax = async function(keyName, keyValue){
var message = await Countly.validateString(keyName, "key", "saveMax");
if(message) {
return message;
}
message = await Countly.validateUserDataValue(keyValue, "value", "saveMax");
if(message) {
return message;
}
var intValue = parseInt(keyValue).toString();
CountlyReactNative.userData_saveMax([keyName, intValue]);
};
Countly.userData.saveMin = async function(keyName, keyValue){
var message = await Countly.validateString(keyName, "key", "saveMin");
if(message) {
return message;
}
message = await Countly.validateUserDataValue(keyValue, "value", "saveMin");
if(message) {
return message;
}
var intValue = parseInt(keyValue).toString();
CountlyReactNative.userData_saveMin([keyName, intValue]);
};
Countly.userData.setOnce = async function(keyName, keyValue){
var message = await Countly.validateString(keyName, "key", "setOnce");
if(message) {
return message;
}
message = await Countly.validateValidUserData(keyValue, "value", "setOnce");
if(message) {
return message;
}
keyValue = keyValue.toString();
if(keyValue || keyValue == "") {
CountlyReactNative.userData_setOnce([keyName, keyValue]);
}
};
Countly.userData.pushUniqueValue = async function(keyName, keyValue){
var message = await Countly.validateString(keyName, "key", "pushUniqueValue");
if(message) {
return message;
}
message = await Countly.validateValidUserData(keyValue, "value", "pushUniqueValue");
if(message) {
return message;
}
keyValue = keyValue.toString();
if(keyValue || keyValue == "") {
CountlyReactNative.userData_pushUniqueValue([keyName, keyValue]);
}
};
Countly.userData.pushValue = async function(keyName, keyValue){
var message = await Countly.validateString(keyName, "key", "pushValue");
if(message) {
return message;
}
message = await Countly.validateValidUserData(keyValue, "value", "pushValue");
if(message) {
return message;
}
keyValue = keyValue.toString();
if(keyValue || keyValue == "") {
CountlyReactNative.userData_pushValue([keyName, keyValue]);
}
};
Countly.userData.pullValue = async function(keyName, keyValue){
var message = await Countly.validateString(keyName, "key", "pullValue");
if(message) {
return message;
}
message = await Countly.validateValidUserData(keyValue, "value", "pullValue");
if(message) {
return message;
}
keyValue = keyValue.toString();
if(keyValue || keyValue == "") {
CountlyReactNative.userData_pullValue([keyName, keyValue]);
}
};
/**
*
* Set that consent should be required for features to work.
* Should be called before Countly init
*/
Countly.setRequiresConsent = function(flag){
CountlyReactNative.setRequiresConsent([flag]);
}
Countly.giveConsent = function(args){
var features = [];
if (typeof args == "string") {
features.push(args);
}
else {
features = args;
}
CountlyReactNative.giveConsent(features);
}
/**
*
* Give consent for specific features before init.
* Should be called after Countly init
*/
Countly.giveConsentInit = async function(args){
var features = [];
if (typeof args == "string") {
features.push(args);
}
else if(Array.isArray(args)) {
features = args;
}
else {
var message = "unsupported data type '" + (typeof args) + "'";
Countly.logWarning("giveConsentInit", message);
}
CountlyReactNative.giveConsentInit(features);
}
Countly.removeConsent = function(args){
var features = [];
if (typeof args == "string") {
features.push(args);
}
else {
features = args;
}
CountlyReactNative.removeConsent(features);
}
/**
*
* Give consent for all features
* Should be called after Countly init
*/
Countly.giveAllConsent = function(){
CountlyReactNative.giveAllConsent();
}
Countly.removeAllConsent = function(){
CountlyReactNative.removeAllConsent();
}
Countly.remoteConfigUpdate = function(callback){
CountlyReactNative.remoteConfigUpdate([], (stringItem) => {
callback(stringItem);
});
}
Countly.updateRemoteConfigForKeysOnly = function(keyNames, callback){
var args = [];
if(keyNames.length){
for(var i=0,il=keyNames.length;i<il;i++){
args.push(keyNames[i])
}
CountlyReactNative.updateRemoteConfigForKeysOnly(args, (stringItem) => {
callback(stringItem);
});
}
}
Countly.updateRemoteConfigExceptKeys = function(keyNames, callback){
var args = [];
if(keyNames.length){
for(var i=0,il=keyNames.length;i<il;i++){
args.push(keyNames[i])
}
CountlyReactNative.updateRemoteConfigExceptKeys(args, (stringItem) => {
callback(stringItem);
});
}
}
Countly.getRemoteConfigValueForKey = function(keyName, callback){
CountlyReactNative.getRemoteConfigValueForKey([keyName.toString() || ""], (value) => {
if (Platform.OS == "android" ) {
try {
value = JSON.parse(value);
}
catch (e) {
// console.log(e.message);
// noop. value will remain string if not JSON parsable and returned as string
}
}
callback(value);
});
}
Countly.getRemoteConfigValueForKeyP = function(keyName){
if (Platform.OS != "android" ) return "To be implemented";
const promise = CountlyReactNative.getRemoteConfigValueForKeyP(keyName);
return promise.then(value => {
if (Platform.OS == "android" ) {
try {
value = JSON.parse(value);
}
catch (e) {
// console.log(e.message);
// noop. value will remain string if not JSON parsable and returned as string
}
}
return value;
})
}
Countly.remoteConfigClearValues = async function(){
const result = await CountlyReactNative.remoteConfigClearValues();
return result;
}
/**
* Set's the text's for the different fields in the star rating dialog. Set value null if for some field you want to keep the old value
*
* @param {String} starRatingTextTitle - dialog's title text (Only for Android)
* @param {String} starRatingTextMessage - dialog's message text
* @param {String} starRatingTextDismiss - dialog's dismiss buttons text (Only for Android)
*/
Countly.setStarRatingDialogTexts = function(starRatingTextTitle, starRatingTextMessage, starRatingTextDismiss){
var args = [];
args.push(starRatingTextTitle);
args.push(starRatingTextMessage);
args.push(starRatingTextDismiss);
CountlyReactNative.setStarRatingDialogTexts(args);
}
Countly.showStarRating = function(callback){
if(!callback){callback = function(){}};
CountlyReactNative.showStarRating([], callback);
}
Countly.showFeedbackPopup = function(widgetId, closeButtonText){
CountlyReactNative.showFeedbackPopup([widgetId.toString() || "", closeButtonText.toString() || "Done"]);
}
/**
* Get a list of available feedback widgets as array of object to handle multiple widgets of same type.
*/
Countly.getFeedbackWidgets = async function(){
const result = await CountlyReactNative.getFeedbackWidgets();
return result;
}
/**
* Get a list of available feedback widgets for this device ID
* @deprecated in 20.11.1 : use 'getFeedbackWidgets' intead of 'getAvailableFeedbackWidgets'.
* Using the old function it will not be possible to see all the available feedback widgets.
* In case there are multiple ones for the same type, only the last one will be returned due to their id being overwritten in the type field.
* The newer function allow also to see the widgets 'name' field which can be further used to filter and identify specific widgets.
*/
Countly.getAvailableFeedbackWidgets = async function(){
const result = await CountlyReactNative.getAvailableFeedbackWidgets();
return result;
}
/**
* Present a chosen feedback widget
*
* @param {Object} feedbackWidget - feeback Widget with id, type and name
* @param {String} closeButtonText - text for cancel/close button
*/
Countly.presentFeedbackWidgetObject = async function(feedbackWidget, closeButtonText){
var message = null;
if(!feedbackWidget) {
message = "feedbackWidget should not be null or undefined";
Countly.logError("presentFeedbackWidgetObject", message);
return message;
}
if(!feedbackWidget.id) {
message = "FeedbackWidget id should not be null or empty";
Countly.logError("presentFeedbackWidgetObject", message);
return message;
}
if(!feedbackWidget.type) {
message = "FeedbackWidget type should not be null or empty";
Countly.logError("presentFeedbackWidgetObject", message);
return message;
}
if (typeof closeButtonText != "string") {
closeButtonText = "";
Countly.logWarning("presentFeedbackWidgetObject", "unsupported data type of closeButtonText : '" + (typeof args) + "'");
}
feedbackWidget.name = feedbackWidget.name || "";
closeButtonText = closeButtonText || "";
CountlyReactNative.presentFeedbackWidget([feedbackWidget.id, feedbackWidget.type, feedbackWidget.name, closeButtonText]);
}
/**
* Present a chosen feedback widget
*
* @param {String} widgetType - type of widget : "nps" or "survey"
* @param {String} widgetId - id of widget to present
* @param {String} closeButtonText - text for cancel/close button
* @deprecated in 20.11.1 : use 'presentFeedbackWidgetObject' intead of 'presentFeedbackWidget'.
*/
Countly.presentFeedbackWidget = function(widgetType, widgetId, closeButtonText){
var feedbackWidget = {
"id": widgetId,
"type": widgetType
};
Countly.presentFeedbackWidgetObject(feedbackWidget, closeButtonText)
}
/**
*
* Events get grouped together and are sent either every minute or after the unsent event count reaches a threshold. By default it is 10
* Should be called before Countly init
*/
Countly.setEventSendThreshold = function(size){
CountlyReactNative.setEventSendThreshold([size.toString() || ""]);
}
Countly.startTrace = function(traceKey){
var args = [];
args.push(traceKey);
CountlyReactNative.startTrace(args);
}
Countly.cancelTrace = function(traceKey){
var args = [];
args.push(traceKey);
CountlyReactNative.cancelTrace(args);
}
Countly.clearAllTraces = function(){
var args = [];
CountlyReactNative.clearAllTraces(args);
}
Countly.endTrace = function(traceKey, customMetric){
var args = [];
args.push(traceKey);
customMetric = customMetric || {};
for(var key in customMetric){
args.push(key.toString());
args.push(customMetric[key].toString());
}
CountlyReactNative.endTrace(args);
}
Countly.recordNetworkTrace = function(networkTraceKey, responseCode, requestPayloadSize, responsePayloadSize, startTime, endTime){
var args = [];
args.push(networkTraceKey);
args.push(responseCode.toString());
args.push(requestPayloadSize.toString());
args.push(responsePayloadSize.toString());
args.push(startTime.toString());
args.push(endTime.toString());
CountlyReactNative.recordNetworkTrace(args);
}
/**
*
* Enable APM features, which includes the recording of app start time.
* Should be called before Countly init
*/
Countly.enableApm = function(){
var args = [];
CountlyReactNative.enableApm(args);
}
/**
*
* Enable campaign attribution reporting to Countly.
* For iOS use "recordAttributionID" instead of "enableAttribution"
* Should be called before Countly init
*/
Countly.enableAttribution = async function(attributionID = "") {
if (Platform.OS.match("ios")) {
if(attributionID == "") {
var message = "attribution Id for iOS can't be empty string";
Countly.logError("enableAttribution", message);
return message;
}
Countly.recordAttributionID(attributionID);
}
else {
CountlyReactNative.enableAttribution();
}
}
/**
*
* set attribution Id for campaign attribution reporting.
* Currently implemented for iOS only
* For Android just call the enableAttribution to enable campaign attribution.
*/
Countly.recordAttributionID = function(attributionID){
if (!Platform.OS.match("ios")) return "recordAttributionID : To be implemented";
var args = [];
args.push(attributionID);
CountlyReactNative.recordAttributionID(args);
}
/**
* Replaces all requests with a different app key with the current app key.
* In request queue, if there are any request whose app key is different than the current app key,
* these requests' app key will be replaced with the current app key.
*/
Countly.replaceAllAppKeysInQueueWithCurrentAppKey = function(){
CountlyReactNative.replaceAllAppKeysInQueueWithCurrentAppKey();
}
/**
* Removes all requests with a different app key in request queue.
* In request queue, if there are any request whose app key is different than the current app key,
* these requests will be removed from request queue.
*/
Countly.removeDifferentAppKeysFromQueue = function(){
CountlyReactNative.removeDifferentAppKeysFromQueue()