-
Notifications
You must be signed in to change notification settings - Fork 71
Expand file tree
/
Copy pathapp-utils.js
More file actions
967 lines (861 loc) · 31.1 KB
/
app-utils.js
File metadata and controls
967 lines (861 loc) · 31.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
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
import Toast from 'react-native-toast-message';
import {
Platform,
Linking
} from 'react-native';
const CleverTap = require('clevertap-react-native');
const toastsQueue = [];
var toastIsShowing = false;
export const showToast = (text1, text2) => {
if (toastIsShowing) {
toastsQueue.push([text1, text2]);
return;
}
toastIsShowing = true;
const queueNext = () => {
toastIsShowing = false;
if (toastsQueue.length > 0) {
let item = toastsQueue.shift();
showToast(...item);
}
};
Toast.show({
type: 'info',
position: 'bottom',
visibilityTime: 2000,
text1: text1,
text2: text2 ?? '',
onHide: queueNext,
});
};
export const set_userProfile = () => {
showToast('User Profile Updated');
CleverTap.profileSet({
Name: 'testUserA1',
Identity: '123456',
Email: 'test@test.com',
custom1: 123,
// 'DOB' This case sensitive key can have the value as either a Date object or a string in the format 'yyyy-mm-dd'
DOB: new Date('2025-03-03T06:35:31'),
// DOB: '1995-03-03'
});
};
export const set_userProfileWithNestedProperties = () => {
const profile = {
Name: 'testUserNested',
Identity: '123456',
Email: 'nested@test.com',
JoiningDate : new Date('2025-03-03T06:35:31'),
Address: {
street: '123 Main St',
city: 'San Francisco',
state: 'CA',
zipCode: 94105,
coordinates: {
lat: 37.7749,
lng: -122.4194
}
},
Preferences: {
newsletter: true,
notifications: {
email: true,
push: false,
sms: true
},
subscriptionDate: new Date('2026-03-03T06:35:31'),
categories: ['sports', 'tech', 'news'],
dateProps: [new Date('2025-03-03T06:35:31'), new Date('2026-03-03T06:35:31'), new Date('2025-03-03T06:35:31')]
}
};
showToast('User Profile with Nested Properties', JSON.stringify(profile));
console.log('Profile Push with nested properties: ', JSON.stringify(profile));
CleverTap.profileSet(profile);
};
// Identity_Management
export const onUser_Login = () => {
showToast('User Profile Updated');
// On user Login
CleverTap.onUserLogin({
Name: 'testUserA1',
Identity: new Date().getTime() + '',
Email: new Date().getTime() + 'testmobile@test.com',
custom1: 123,
birthdate: new Date('1992-12-22T06:35:31'),
});
};
export const onUser_LoginWithNestedProperties = () => {
const profile = {
Name: 'testUserLogin',
Identity: new Date().getTime() + '',
Email: new Date().getTime() + 'logintest@test.com',
Company: {
name: 'TechCorp',
department: 'Engineering',
role: 'Senior Developer',
location: {
office: 'HQ',
floor: 5
}
},
Settings: {
theme: 'dark',
language: 'en',
privacy: {
shareData: false,
analytics: true
}
}
};
showToast('User Login with Nested Properties', JSON.stringify(profile));
console.log('OnUserLogin with nested properties: ', JSON.stringify(profile));
CleverTap.onUserLogin(profile);
};
export const getCleverTap_id = () => {
// Below method is deprecated since 0.6.0, please check index.js for deprecation, instead use CleverTap.getCleverTapID()
/*CleverTap.profileGetCleverTapID((err, res) => {
console.log('CleverTapID', res, err);
alert(`CleverTapID: \n ${res}`);
});*/
// Use below newly added method
CleverTap.getCleverTapID((err, res) => {
console.log('CleverTapID', res, err);
alert(`CleverTapID: \n ${res}`);
});
};
// Location
export const set_userLocation = () => {
showToast('User Location set');
CleverTap.setLocation(34.15, -118.2);
};
// Locale
export const set_Locale = () => {
showToast('User Locale set');
CleverTap.setLocale("en_IN");
};
// Events
export const pushevent = () => {
showToast('Event Recorded');
// Recording an Event
// CleverTap.recordEvent('testEvent');
// CleverTap.recordEvent('Send Basic Push');
// CleverTap.recordEvent('testEventWithProps', {start: new Date(), foo: 'bar'});
CleverTap.recordEvent('pushEvent');
};
export const recordEventWithNestedProperties = () => {
const eventProps = {
'Product Name': 'Premium Subscription',
'Amount': 99.99,
'Currency': 'USD',
'Payment Details': {
method: 'credit_card',
provider: 'Stripe',
cardType: 'Visa',
lastFourDigits: '4242',
billingAddress: {
street: '456 Market St',
city: 'New York',
state: 'NY',
zipCode: 10001
}
},
'User Metadata': {
isPremium: true,
tier: 'gold',
features: ['feature1', 'feature2', 'feature3'],
limits: {
apiCalls: 10000,
storage: 100
}
},
'timestamp': new Date()
};
showToast('Event with Nested Properties', 'Product Purchased');
console.log('Recording event with nested properties: ', JSON.stringify(eventProps));
CleverTap.recordEvent('Product Purchased', eventProps);
};
export const pushChargedEvent = () => {
showToast('Charged Event Recorded');
// Recording an Event
CleverTap.recordChargedEvent(
{ totalValue: 20, category: 'books', purchase_date: new Date() },
[
{
title: 'book1',
published_date: new Date('2010-12-12T06:35:31'),
author: 'ABC',
},
{ title: 'book2', published_date: new Date('2000-12-12T06:35:31') },
{ title: 'book3', published_date: new Date(), author: 'XYZ' },
],
);
};
export const getUserEventLog = () => {
CleverTap.getUserEventLog("testEvent", (err, res) => {
console.log('User Event Log: ', res);
showToast(`User Event Log: ${JSON.stringify(res)}`);
});
};
export const getUserEventLogCount = () => {
CleverTap.getUserEventLogCount("testEvent", (err, res) => {
console.log('User Event Log Count: ', res);
showToast(`User Event Log Count: ${res}`);
});
};
export const getUserLastVisitTs = () => {
CleverTap.getUserLastVisitTs((err, res) => {
console.log('User Last Visit Timestamp: ', res);
showToast(`User Last Visit Timestamp: ${res}`);
});
};
export const getUserAppLaunchCount = () => {
CleverTap.getUserAppLaunchCount((err, res) => {
console.log('User App Launch Count: ', res);
showToast(`User App Launch Count: ${res}`);
});
};
export const getUserEventLogHistory = () => {
CleverTap.getUserEventLogHistory((err, res) => {
console.log('User Event Log History: ', res);
showToast(`User Event Log History: ${JSON.stringify(res)}`);
});
};
// App Inbox
export const show_appInbox = () => {
// Show Inbox
CleverTap.showInbox({
navBarTitle: 'My App Inbox',
navBarTitleColor: '#FF0000',
navBarColor: '#FFFFFF',
inboxBackgroundColor: '#AED6F1',
backButtonColor: '#00FF00',
unselectedTabColor: '#0000FF',
selectedTabColor: '#FF0000',
selectedTabIndicatorColor: '#000000',
noMessageText: 'No message(s)',
noMessageTextColor: '#FF0000',
});
};
export const show_appInboxwithTabs = () => {
// Show Inbox
CleverTap.showInbox({
tabs: ['Offers', 'Promotions'],
navBarTitle: 'My App Inbox',
navBarTitleColor: '#FF0000',
navBarColor: '#FFFFFF',
inboxBackgroundColor: '#AED6F1',
backButtonColor: '#00FF00',
unselectedTabColor: '#0000FF',
selectedTabColor: '#FF0000',
selectedTabIndicatorColor: '#000000',
noMessageText: 'No message(s)',
noMessageTextColor: '#FF0000',
firstTabTitle: "First Tab",
});
};
export const get_TotalMessageCount = () => {
// Get the total message count
CleverTap.getInboxMessageCount((err, res) => {
console.log('Total Messages: ', res, err);
showToast(`Total Messages: \n ${res}`);
});
};
export const get_UnreadMessageCount = () => {
// Get the count of unread messages
CleverTap.getInboxMessageUnreadCount((err, res) => {
console.log('Unread Messages: ', res, err);
showToast(`Unread Messages: \n ${res}`);
});
};
export const Get_All_InboxMessages = () => {
// Get All Inbox Messages
CleverTap.getAllInboxMessages((err, res) => {
console.log('All Inbox Messages: ', res, err);
showToast(`All Inbox Messages: \n ${res}`);
// Uncomment to print payload data.
// printInboxMessagesArray(res);
});
};
export const get_All_InboxUnreadMessages = () => {
// Get all unread messages
CleverTap.getUnreadInboxMessages((err, res) => {
console.log('Unread Inbox Messages: ', res, err);
showToast(`Unread Inbox Messages: \n ${res}`);
// Uncomment to print payload data.
// printInboxMessagesArray(res);
});
};
export const Get_InboxMessageForId = () => {
// Get inbox Id
CleverTap.getInboxMessageForId('Message Id', (err, res) => {
console.log('marking message read = ' + res);
showToast(`marking message read: \n ${res}`);
// Uncomment to print payload data.
// printInboxMessageMap(res);
});
};
export const delete_InboxMessageForId = () => {
showToast('Check Console for values');
CleverTap.deleteInboxMessageForId('Message Id');
};
export const markRead_InboxMessageForId = () => {
showToast('Check Console for values');
CleverTap.markReadInboxMessageForId('Message Id');
};
export const pushInboxNotificationViewed = () => {
showToast('Check Console for values');
CleverTap.pushInboxNotificationViewedEventForId('Message Id');
};
export const pushInboxNotificationClicked = () => {
showToast('Check Console for values');
CleverTap.pushInboxNotificationClickedEventForId('Message Id');
};
export const printInboxMessagesArray = (data) => {
if (data != null) {
console.log('Total Inbox Message count = ' + data.length);
data.forEach(inboxMessage => {
printInboxMessageMap(inboxMessage);
});
}
};
export const printInboxMessageMap = (inboxMessage) => {
if (inboxMessage != null) {
console.log('Inbox Message wzrk_id = ' + inboxMessage['wzrk_id']);
let msg = inboxMessage['msg'];
console.log('Type of Inbox = ' + msg['type']);
let content = msg['content'];
content.forEach(element => {
let title = element['title'];
let message = element['message'];
console.log('Inbox Message Title = ' + title['text'] + ' and message = ' + message['text']);
let action = element['action'];
let links = action['links'];
links.forEach(link => {
console.log('Inbox Message have link type = ' + link['type']);
});
});
}
};
// Push Notifications
export const create_NotificationChannel = () => {
showToast('Notification Channel Created');
// Creating Notification Channel
CleverTap.createNotificationChannel(
'CtRNS',
'Clever Tap React Native Testing',
'CT React Native Testing',
1,
true,
);
CleverTap.createNotificationChannel(
'BRTesting',
'Clever Tap BR Testing',
'CT BR Testing',
1,
true,
);
CleverTap.createNotificationChannel(
'PTTesting',
'Clever Tap PT Testing',
'CT PT Testing',
1,
true,
);
};
export const delete_NotificationChannel = () => {
showToast('Notification Channel Deleted');
// Delete Notification Channel
CleverTap.deleteNotificationChannel('CtRNS');
};
export const create_NotificationChannelGroup = () => {
showToast('Notification Channel Group Created');
// Creating a group notification channel
CleverTap.createNotificationChannelGroup(
'Offers',
'All Offers related notifications',
);
};
export const delete_NotificationChannelGroup = () => {
showToast('Notification Channel Group Deleted');
// Delete a group notification channel
CleverTap.deleteNotificationChannelGroup('Offers');
};
export const pushFcmRegistrationId = () => {
showToast('Registered FCM Id for Push');
// Setting up a Push Notification
if (Platform.OS === 'android') {
// Use only during custom implementation and make sure that FCM credentials used to generate token are same as CleverTap
// or else two different tokens will be pushed to BackEnd resulting in unwanted behavior
// => https://github.com/CleverTap/clevertap-react-native/issues/166
// => https://developer.clevertap.com/docs/android#section-custom-android-push-notifications-handling
CleverTap.setFCMPushToken('1000test000token000fcm');
}
};
export const pushBaiduRegistrationId = () => {
showToast('Registered Baidu Id for Push');
if (Platform.OS === 'android') {
CleverTap.pushRegistrationToken("my_bps_token", {
type: 'bps',
prefKey: 'bps_token',
className: 'com.clevertap.android.bps.BaiduPushProvider',
messagingSDKClassName: 'com.baidu.android.pushservice.PushMessageReceiver'
});
}
};
export const pushHuaweiRegistrationId = () => {
showToast('Registered HPS Id for Push');
if (Platform.OS === 'android') {
CleverTap.pushRegistrationToken("my_hms_token", {
type: 'hps',
prefKey: 'hps_token',
className: 'com.clevertap.android.hms.HmsPushProvider',
messagingSDKClassName: 'com.huawei.hms.push.HmsMessageService'
});
}
};
export const create_notification = () => {
// createNotification in your custom implementation => https://developer.clevertap.com/docs/android#section-custom-android-push-notifications-handling
// Please note, extras passed in below method is just for showcase, you need to pass the one that you receive from FCM
CleverTap.createNotification({
wzrk_acct_id: '88R-R54-5Z6Z',
nm: 'Testing 1..2..3..',
nt: 'Test event',
pr: 'max',
wzrk_pivot: 'wzrk_default',
wzrk_ttl_s: '2419200',
wzrk_cid: 'CtRNS',
wzrk_pid: new Date().getTime(),
wzrk_rnv: false,
wzrk_ttl: '1627053990',
wzrk_push_amp: false,
wzrk_bc: '',
wzrk_bi: '2',
wzrk_dt: 'FIREBASE',
wzrk_id: '1624627506_20210625',
wzrk_pn: true,
});
};
export const createNotificationChannelWithSound = () => {
// https://developer.clevertap.com/docs/add-a-sound-file-to-your-android-app
CleverTap.createNotificationChannelWithSound(
'CtRNS',
'Clever Tap React Native Testing',
'CT React Native Testing',
1,
true,
'glitch.mp3',
);
};
export const createNotificationChannelWithGroupId = () => {
// https://developer.clevertap.com/docs/android#section-push-notifications-for-android-o
CleverTap.createNotificationChannelWithGroupId(
'offersMonthly',
'Monthly Offers',
'Offers given at every month',
1,
'Offers',
true,
);
CleverTap.createNotificationChannelWithGroupId(
'offersQuarterly',
'Quarterly Offers',
'Offers given at every Quarter',
1,
'Offers',
true,
);
};
export const createNotificationChannelWithGroupIdAndSound = () => {
// https://developer.clevertap.com/docs/android#section-push-notifications-for-android-o
CleverTap.createNotificationChannelWithGroupIdAndSound(
'offersMonthly',
'Monthly Offers',
'Offers given at every month',
1,
'Offers',
true,
'glitch.mp3',
);
CleverTap.createNotificationChannelWithGroupIdAndSound(
'offersQuarterly',
'Quarterly Offers',
'Offers given at every Quarter',
1,
'Offers',
true,
'glitch.mp3',
);
};
// Native Display
export const getUnitID = () => {
CleverTap.getDisplayUnitForId('Unit Id', (err, res) => {
console.log('Get Display Unit for Id:', res, err);
showToast(`Get Display Unit for Id: ${res}`);
// Uncomment to access payload.
// printDisplayUnit(res);
});
};
export const getAllDisplayUnits = () => {
CleverTap.getAllDisplayUnits((err, res) => {
console.log('All Display Units: ', res, err);
showToast(`All Display Units: ${res}`);
// Uncomment to access payload.
// printDisplayUnitsPayload(res);
});
};
// Product Config
export const productConfig = () => {
showToast('Product Configuration set to default');
CleverTap.setDefaultsMap({
text_color: 'red',
msg_count: 100,
price: 100.5,
is_shown: true,
json: '{"key":"val"}',
});
};
export const fetch = () => {
CleverTap.fetch();
};
export const activate = () => {
CleverTap.activate();
};
export const fetchAndActivate = () => {
CleverTap.fetchAndActivate();
};
export const fetchwithMinIntervalinsec = () => {
CleverTap.fetchWithMinimumIntervalInSeconds(60);
};
export const setMinimumFetchIntervalInSeconds = () => {
CleverTap.setMinimumFetchIntervalInSeconds(60);
};
export const getBoolean = () => {
CleverTap.getProductConfigBoolean('is_shown', (err, res) => {
console.log('PC is_shown val in boolean :', res, err);
showToast(`PC is_shown val in boolean: ${res}`);
});
};
export const getLong = () => {
CleverTap.getNumber('msg_count', (err, res) => {
console.log('PC is_shown val in number(long) :', res, err);
showToast(`PC is_shown val in number(long): ${res}`);
});
};
export const getDouble = () => {
CleverTap.getNumber('price', (err, res) => {
console.log('PC price val in number :', res, err);
showToast(`PC is_shown val in number(double) : ${res}`);
});
};
export const getString = () => {
CleverTap.getProductConfigString('text_color', (err, res) => {
console.log('PC text_color val in string :', res, err);
showToast(`PC is_shown val in String : ${res}`);
});
};
export const getStrings = () => {
CleverTap.getProductConfigString('json', (err, res) => {
console.log('PC json val in string :', res, err);
showToast(`PC json val in String: ${res}`);
});
};
export const reset_config = () => {
CleverTap.resetProductConfig();
};
export const getLastFetchTimeStampInMillis = () => {
CleverTap.getLastFetchTimeStampInMillis((err, res) => {
console.log('LastFetchTimeStampInMillis in string: ', res, err);
showToast(`LastFetchTimeStampInMillis in string: ${res}`);
});
};
// Feature flag
export const getFeatureFlag = () => {
CleverTap.getFeatureFlag('is_dark_mode', false, (err, res) => {
console.log('FF is_dark_mode val in boolean :', res, err);
showToast(`FF is_dark_mode val in boolean: ${res}`);
});
};
// App Personalisation
export const enablePersonalization = () => {
CleverTap.enablePersonalization();
showToast('Personalization enabled');
};
// Attributions
export const GetCleverTapAttributionIdentifier = () => {
// Below method is deprecated since 0.6.0, please check index.js for deprecation, use CleverTap.getCleverTapID(callback) instead
// Default Instance
CleverTap.profileGetCleverTapAttributionIdentifier((err, res) => {
console.log('CleverTapAttributionIdentifier', res, err);
showToast(`CleverTapAttributionIdentifier: ${res}`);
});
};
// CleverTap Listeners
export const _handleOpenUrl = (event, from) => {
console.log('handleOpenUrl', event.url, from);
showToast(`handleOpenUrl: ${event.url}, ${from}`);
};
export const removeCleverTapAPIListeners = () => {
// Clean up listeners
Linking.removeEventListener('url', _handleOpenUrl);
CleverTap.removeListener(CleverTap.CleverTapProfileDidInitialize);
CleverTap.removeListener(CleverTap.CleverTapProfileSync);
CleverTap.removeListener(CleverTap.CleverTapInAppNotificationDismissed);
CleverTap.removeListener(CleverTap.CleverTapInAppNotificationShowed);
CleverTap.removeListener(CleverTap.CleverTapInboxDidInitialize);
CleverTap.removeListener(CleverTap.CleverTapInboxMessagesDidUpdate);
CleverTap.removeListener(CleverTap.CleverTapInboxMessageButtonTapped);
CleverTap.removeListener(CleverTap.CleverTapDisplayUnitsLoaded);
CleverTap.removeListener(CleverTap.CleverTapInAppNotificationButtonTapped);
CleverTap.removeListener(CleverTap.CleverTapFeatureFlagsDidUpdate);
CleverTap.removeListener(CleverTap.CleverTapProductConfigDidInitialize);
CleverTap.removeListener(CleverTap.CleverTapProductConfigDidFetch);
CleverTap.removeListener(CleverTap.CleverTapProductConfigDidActivate);
CleverTap.removeListener(CleverTap.CleverTapPushNotificationClicked);
CleverTap.removeListener(CleverTap.CleverTapPushPermissionResponseReceived);
showToast('Listeners removed successfully');
};
export const addCleverTapAPIListeners = (fromClick) => {
// Add listeners for CleverTap Events
CleverTap.addListener(CleverTap.CleverTapProfileDidInitialize, event => {
_handleCleverTapEvent(CleverTap.CleverTapProfileDidInitialize, event);
});
CleverTap.addListener(CleverTap.CleverTapProfileSync, event => {
_handleCleverTapEvent(CleverTap.CleverTapProfileSync, event);
});
CleverTap.addListener(
CleverTap.CleverTapInAppNotificationDismissed,
event => {
_handleCleverTapInAppEvent(
CleverTap.CleverTapInAppNotificationDismissed,
event,
);
},
);
CleverTap.addListener(CleverTap.CleverTapInAppNotificationShowed, event => {
_handleCleverTapInAppEvent(CleverTap.CleverTapInAppNotificationShowed, event);
});
CleverTap.addListener(CleverTap.CleverTapInboxDidInitialize, event => {
_handleCleverTapInboxEvent(CleverTap.CleverTapInboxDidInitialize, event);
});
CleverTap.addListener(CleverTap.CleverTapInboxMessagesDidUpdate, event => {
_handleCleverTapInboxEvent(
CleverTap.CleverTapInboxMessagesDidUpdate,
event,
);
});
CleverTap.addListener(CleverTap.CleverTapInboxMessageButtonTapped, event => {
_handleCleverTapInboxEvent(
CleverTap.CleverTapInboxMessageButtonTapped,
event,
);
});
CleverTap.addListener(CleverTap.CleverTapInboxMessageTapped, event => {
_handleCleverTapInboxEvent(CleverTap.CleverTapInboxMessageTapped, event);
});
CleverTap.addListener(CleverTap.CleverTapDisplayUnitsLoaded, event => {
_handleCleverTapDisplayUnitsLoaded(
CleverTap.CleverTapDisplayUnitsLoaded,
event,
);
});
CleverTap.addListener(
CleverTap.CleverTapInAppNotificationButtonTapped,
event => {
_handleCleverTapInAppEvent(
CleverTap.CleverTapInAppNotificationButtonTapped,
event,
);
},
);
CleverTap.addListener(CleverTap.CleverTapFeatureFlagsDidUpdate, event => {
_handleCleverTapEvent(CleverTap.CleverTapFeatureFlagsDidUpdate, event);
});
CleverTap.addListener(
CleverTap.CleverTapProductConfigDidInitialize,
event => {
_handleCleverTapEvent(
CleverTap.CleverTapProductConfigDidInitialize,
event,
);
},
);
CleverTap.addListener(CleverTap.CleverTapProductConfigDidFetch, event => {
_handleCleverTapEvent(CleverTap.CleverTapProductConfigDidFetch, event);
});
CleverTap.addListener(CleverTap.CleverTapProductConfigDidActivate, event => {
_handleCleverTapEvent(CleverTap.CleverTapProductConfigDidActivate, event);
});
CleverTap.addListener(CleverTap.CleverTapPushNotificationClicked, event => {
_handleCleverTapPushEvent(
CleverTap.CleverTapPushNotificationClicked,
event,
);
});
CleverTap.addListener(
CleverTap.CleverTapPushPermissionResponseReceived,
event => {
_handleCleverTapPushEvent(
CleverTap.CleverTapPushPermissionResponseReceived,
event,
);
},
);
if (fromClick) {
showToast('Listeners added successfully');
}
};
// CleverTap Event Handlers
export const _handleCleverTapEvent = (eventName, event) => {
console.log('handleCleverTapEvent', eventName, event);
showToast(`${eventName} called!`);
// Uncomment to access payload for each events.
// if (eventName == 'CleverTapProfileDidInitialize') {
// console.log('Profile did initialized with cleverTapID: '+ event['CleverTapID']);
// }
// if (eventName == 'CleverTapProfileSync') {
// console.log('Profile data updated with updates: ', event['updates']);
// }
};
export const _handleCleverTapInboxEvent = (eventName, event) => {
console.log('handleCleverTapInbox', eventName, event);
showToast(`${eventName} called!`);
// Uncomment to access payload for each events.
// if (eventName == CleverTap.CleverTapInboxMessageTapped) {
// let contentPageIndex = event.contentPageIndex;
// let buttonIndex = event.buttonIndex;
// var data = event.data;
// let inboxMessageClicked = data.msg;
// console.log(
// 'App Inbox ->',
// 'InboxItemClicked at page-index ' +
// contentPageIndex +
// ' with button-index ' +
// buttonIndex,
// );
// //The contentPageIndex corresponds to the page index of the content, which ranges from 0 to the total number of pages for carousel templates. For non-carousel templates, the value is always 0, as they only have one page of content.
// let messageContentObject = inboxMessageClicked.content[contentPageIndex];
// //The buttonIndex corresponds to the CTA button clicked (0, 1, or 2). A value of -1 indicates the app inbox body/message clicked.
// if (buttonIndex != -1) {
// //button is clicked
// let buttonObject = messageContentObject.action.links[buttonIndex];
// let buttonType = buttonObject.type;
// switch (buttonType) {
// case 'copy':
// //this type copies the associated text to the clipboard
// let copiedText = buttonObject.copyText.text;
// console.log(
// 'App Inbox ->',
// 'copied text to Clipboard: ' + copiedText,
// );
// //_dismissAppInbox()
// break;
// case 'url':
// //this type fires the DeepLink
// let firedDeepLinkUrl = buttonObject.url.android.text;
// console.log(
// 'App Inbox ->',
// 'fired DeepLink url: ' + firedDeepLinkUrl,
// );
// //_dismissAppInbox();
// break;
// case 'kv':
// //this type contains the custom key-value pairs
// let kvPair = buttonObject.kv;
// console.log('App Inbox ->', 'custom key-value pair: ', kvPair);
// //_dismissAppInbox();
// break;
// }
// } else {
// //Item's body is clicked
// console.log(
// 'App Inbox ->',
// 'type/template of App Inbox item: ' + inboxMessageClicked.type,
// );
// //_dismissAppInbox();
// }
// }
// if (eventName == 'CleverTapInboxMessageButtonTapped') {
// console.log('Inbox message button tapped with customExtras:');
// for (const key of Object.keys(event)) {
// console.log('Value for key: ' + key + ' is:' + event[key]);
// }
// }
};
export const _dismissAppInbox = () => {
CleverTap.dismissInbox();
};
export const _handleCleverTapInAppEvent = (eventName, event) => {
console.log('handleCleverTapInApp', eventName, event);
showToast(`${eventName} called!`);
// Uncomment to access payload for each events.
// if (eventName == 'CleverTapInAppNotificationButtonTapped') {
// console.log('InApp button tapped with key-value pair:');
// for (const key of Object.keys(event)) {
// console.log('Value for key: '+ key + ' is:' + event[key]);
// }
// }
// if (eventName == 'CleverTapInAppNotificationDismissed') {
// let extras = event['extras'];
// let actionExtras = event['actionExtras'];
// console.log('InApp dismissed with extras: ', extras ,' and actionExtras: ', actionExtras);
// for (const key of Object.keys(extras)) {
// console.log('Value for extras key: '+ key + ' is:' + extras[key]);
// }
// for (const key of Object.keys(actionExtras)) {
// console.log('Value for actionExtras key: '+ key + ' is:' + actionExtras[key]);
// }
// }
// Following event is only applicable for the android platform
// if (eventName == 'CleverTapInAppNotificationShowed') {
// let type = event.data.type;
// console.log('Value for inApp type:', type);
// }
};
export const _handleCleverTapPushEvent = (eventName, event) => {
console.log('handleCleverTapPush', eventName, event);
showToast(`${JSON.stringify(eventName)} called!`);
// Uncomment to access payload for each events.
// if (eventName == 'CleverTapPushNotificationClicked') {
// if (event['wzrk_dl'] != null) {
// let deepLink = event['wzrk_dl'];
// console.log('Push Notification clicked with deeplink: ' + deepLink);
// }
// }
// if (eventName == 'CleverTapPushPermissionResponseReceived') {
// let accepted = event['accepted'];
// console.log('Push Permission accepted:', accepted);
// }
};
export const _handleCleverTapDisplayUnitsLoaded = (eventName, event) => {
console.log('handleCleverTapDisplayUnitsLoaded', eventName, event);
showToast(`${eventName} called!`);
let data = event['displayUnits'];
// Uncomment to access payload.
// printDisplayUnitsPayload(data);
};
export const printDisplayUnitsPayload = (data) => {
if (data != null) {
console.log('Total Display units count = ' + data.length);
data.forEach(element => {
printDisplayUnit(element);
});
}
};
export const printDisplayUnit = (element) => {
if (element != null) {
let content = element['content'];
content.forEach(contentElement => {
let title = contentElement['title'];
let message = contentElement['message'];
console.log('Title text of display unit is: ' + title['text']);
console.log('Message text of display unit is: ' + message['text']);
});
let customKV = element['custom_kv'];
if (customKV != null) {
console.log('Display units custom key-values: ', customKV);
for (const key of Object.keys(customKV)) {
console.log('Value for key: ' + key + ' is:' + customKV[key]);
}
}
}
};