This repository has been archived by the owner on Apr 4, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 24
/
CountlyReactNative.m
369 lines (259 loc) · 9.66 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
#import <React/RCTBridge.h>
#import <React/RCTLog.h>
#import <React/RCTUtils.h>
#import <React/RCTEventDispatcher.h>
#import "Countly.h"
#import "CountlyReactNative.h"
#import "CountlyConfig.h"
#import "CountlyPushNotifications.h"
CountlyConfig* config = nil;
@implementation CountlyReactNative
RCT_EXPORT_MODULE();
RCT_EXPORT_METHOD(init:(NSArray*)arguments)
{
NSString* serverurl = [arguments objectAtIndex:0];
NSString* appkey = [arguments objectAtIndex:1];
if (config == nil){
config = CountlyConfig.new;
}
config.appKey = appkey;
config.host = serverurl;
if (serverurl != nil && [serverurl length] > 0) {
[[Countly sharedInstance] startWithConfig:config];
} else {
}
}
RCT_EXPORT_METHOD(event:(NSArray*)arguments)
{
NSString* eventType = [arguments objectAtIndex:0];
if (eventType != nil && [eventType length] > 0) {
if ([eventType isEqual: @"event"]) {
NSString* eventName = [arguments objectAtIndex:1];
NSString* countString = [arguments objectAtIndex:2];
int countInt = [countString intValue];
[[Countly sharedInstance] recordEvent:eventName count:countInt];
}
else if ([eventType isEqual: @"eventWithSum"]){
NSString* eventName = [arguments objectAtIndex:1];
NSString* countString = [arguments objectAtIndex:2];
int countInt = [countString intValue];
NSString* sumString = [arguments objectAtIndex:3];
float sumFloat = [sumString floatValue];
[[Countly sharedInstance] recordEvent:eventName count:countInt sum:sumFloat];
}
else if ([eventType isEqual: @"eventWithSegment"]){
NSString* eventName = [arguments objectAtIndex:1];
NSString* countString = [arguments objectAtIndex:2];
int countInt = [countString intValue];
NSMutableDictionary *dict = [[NSMutableDictionary alloc] init];
for(int i=3,il=(int)arguments.count;i<il;i+=2){
dict[[arguments objectAtIndex:i]] = [arguments objectAtIndex:i+1];
}
[[Countly sharedInstance] recordEvent:eventName segmentation:dict count:countInt];
}
else if ([eventType isEqual: @"eventWithSumSegment"]){
NSString* eventName = [arguments objectAtIndex:1];
NSString* countString = [arguments objectAtIndex:2];
int countInt = [countString intValue];
NSString* sumString = [arguments objectAtIndex:3];
float sumFloat = [sumString floatValue];
NSMutableDictionary *dict = [[NSMutableDictionary alloc] init];
for(int i=4,il=(int)arguments.count;i<il;i+=2){
dict[[arguments objectAtIndex:i]] = [arguments objectAtIndex:i+1];
}
[[Countly sharedInstance] recordEvent:eventName segmentation:dict count:countInt sum:sumFloat];
}
else{
}
} else {
}
}
RCT_EXPORT_METHOD(recordView:(NSArray*)arguments)
{
NSString* recordView = [arguments objectAtIndex:0];
[Countly.sharedInstance reportView:recordView];
}
RCT_EXPORT_METHOD(setloggingenabled:(NSArray*)arguments)
{
}
RCT_EXPORT_METHOD(setuserdata:(NSArray*)arguments)
{
NSString* name = [arguments objectAtIndex:0];
NSString* username = [arguments objectAtIndex:1];
NSString* email = [arguments objectAtIndex:2];
NSString* org = [arguments objectAtIndex:3];
NSString* phone = [arguments objectAtIndex:4];
NSString* picture = [arguments objectAtIndex:5];
//NSString* picturePath = [arguments objectAtIndex:6];
NSString* gender = [arguments objectAtIndex:7];
NSString* byear = [arguments objectAtIndex:8];
Countly.user.name = name;
Countly.user.username = username;
Countly.user.email = email;
Countly.user.organization = org;
Countly.user.phone = phone;
Countly.user.pictureURL = picture;
Countly.user.gender = gender;
Countly.user.birthYear = @([byear integerValue]);
//NSInteger byearint = [byear intValue];
[Countly.user recordUserDetails];
[Countly.user save];
// [Countly.sharedInstance recordUserDetails: @{
// kCLYUserName: name,
// kCLYUserEmail: email,
// kCLYUserBirthYear: byear,
// kCLYUserGender: gender,
// kCLYUserOrganization: org,
// kCLYUserPhone: phone,
// kCLYUserUsername: username,
// kCLYUserPicture: picture
// }];
}
RCT_EXPORT_METHOD(onregistrationid:(NSArray*)arguments)
{
NSString* token = [arguments objectAtIndex:0];
NSString* messagingMode = [arguments objectAtIndex:1];
int mode = [messagingMode intValue];
NSData *tokenByte = [token dataUsingEncoding:NSUTF8StringEncoding];
if(mode == 1){
// [[CountlyConnectionQueue sharedInstance] setStartedWithTest:YES];
}
// CountlyPushNotifications.sharedInstance.token = token;
// [CountlyPushNotifications.sharedInstance sendToken];
// [Countly.sharedInstance didRegisterForRemoteNotificationsWithDeviceToken:tokenByte];
// [[CountlyConnectionQueue sharedInstance] tokenSession:token];
}
RCT_EXPORT_METHOD(start)
{
[Countly.sharedInstance resume];
}
RCT_EXPORT_METHOD(stop)
{
[Countly.sharedInstance suspend];
}
RCT_EXPORT_METHOD(changeDeviceId:(NSArray*)arguments)
{
NSString* newDeviceID = [arguments objectAtIndex:0];
[Countly.sharedInstance setNewDeviceID:newDeviceID onServer:YES];
}
RCT_EXPORT_METHOD(setHttpPostForced:(NSArray*)arguments)
{
}
RCT_EXPORT_METHOD(enableParameterTamperingProtection:(NSArray*)arguments)
{
NSString* salt = [arguments objectAtIndex:0];
if (config == nil){
config = CountlyConfig.new;
}
config.secretSalt = salt;
}
RCT_EXPORT_METHOD(startEvent:(NSArray*)arguments)
{
NSString* eventName = [arguments objectAtIndex:0];
[Countly.sharedInstance startEvent:eventName];
}
RCT_EXPORT_METHOD(endEvent:(NSArray*)arguments)
{
NSString* eventType = [arguments objectAtIndex:0];
if ([eventType isEqual: @"event"]) {
NSString* eventName = [arguments objectAtIndex:1];
[Countly.sharedInstance endEvent:eventName];
}
else if ([eventType isEqual: @"eventWithSegment"]){
NSString* eventName = [arguments objectAtIndex:1];
NSString* countString = [arguments objectAtIndex:2];
int countInt = [countString intValue];
NSMutableDictionary *dict = [[NSMutableDictionary alloc] init];
for(int i=3,il=(int)arguments.count;i<il;i+=2){
dict[[arguments objectAtIndex:i]] = [arguments objectAtIndex:i+1];
}
[Countly.sharedInstance endEvent:eventName segmentation:dict count:countInt sum:0];
}
else{
}
}
RCT_EXPORT_METHOD(setLocation:(NSArray*)arguments)
{
NSString* latitudeString = [arguments objectAtIndex:0];
NSString* longitudeString = [arguments objectAtIndex:1];
double latitudeDouble = [latitudeString doubleValue];
double longitudeDouble = [longitudeString doubleValue];
if (config == nil){
config = CountlyConfig.new;
}
config.location = (CLLocationCoordinate2D){latitudeDouble,longitudeDouble};
}
RCT_EXPORT_METHOD(enableCrashReporting:(NSArray*)arguments)
{
if (config == nil){
config = CountlyConfig.new;
}
config.features = @[CLYCrashReporting];
}
RCT_EXPORT_METHOD(addCrashLog:(NSArray*)arguments)
{
NSString* token = [arguments objectAtIndex:0];
NSString* messagingMode = [arguments objectAtIndex:1];
int mode = [messagingMode intValue];
NSData *tokenByte = [token dataUsingEncoding:NSUTF8StringEncoding];
}
RCT_EXPORT_METHOD(userData_setProperty:(NSArray*)arguments)
{
NSString* keyName = [arguments objectAtIndex:0];
NSString* keyValue = [arguments objectAtIndex:1];
[Countly.user set:keyName value:keyValue];
[Countly.user save];
}
RCT_EXPORT_METHOD(userData_increment:(NSArray*)arguments)
{
NSString* keyName = [arguments objectAtIndex:0];
[Countly.user increment:keyName];
[Countly.user save];
}
RCT_EXPORT_METHOD(userData_incrementBy:(NSArray*)arguments)
{
NSString* keyName = [arguments objectAtIndex:0];
NSString* keyValue = [arguments objectAtIndex:1];
int keyValueInteger = [keyValue intValue];
[Countly.user incrementBy:keyName value:keyValueInteger];
[Countly.user save];
}
RCT_EXPORT_METHOD(userData_multiply:(NSArray*)arguments)
{
NSString* keyName = [arguments objectAtIndex:0];
NSString* keyValue = [arguments objectAtIndex:1];
int keyValueInteger = [keyValue intValue];
[Countly.user multiply:keyName value:keyValueInteger];
[Countly.user save];
}
RCT_EXPORT_METHOD(userData_saveMax:(NSArray*)arguments)
{
NSString* keyName = [arguments objectAtIndex:0];
NSString* keyValue = [arguments objectAtIndex:1];
int keyValueInteger = [keyValue intValue];
[Countly.user max:keyName value:keyValueInteger];
[Countly.user save];
}
RCT_EXPORT_METHOD(userData_saveMin:(NSArray*)arguments)
{
NSString* keyName = [arguments objectAtIndex:0];
NSString* keyValue = [arguments objectAtIndex:1];
int keyValueInteger = [keyValue intValue];
[Countly.user min:keyName value:keyValueInteger];
[Countly.user save];
}
RCT_EXPORT_METHOD(userData_setOnce:(NSArray*)arguments)
{
NSString* keyName = [arguments objectAtIndex:0];
NSString* keyValue = [arguments objectAtIndex:1];
[Countly.user setOnce:keyName value:keyValue];
[Countly.user save];
}
RCT_EXPORT_METHOD(demo:(NSArray*)arguments)
{
NSString* token = [arguments objectAtIndex:0];
NSString* messagingMode = [arguments objectAtIndex:1];
int mode = [messagingMode intValue];
NSData *tokenByte = [token dataUsingEncoding:NSUTF8StringEncoding];
}
@end