@snowplow/react-native-tracker 0.2.0 → 1.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,228 +1,900 @@
1
+ //
2
+ // RNSnowplowTracker.m
3
+ //
4
+ // Copyright (c) 2020-2022 Snowplow Analytics Ltd. All rights reserved.
5
+ //
6
+ // This program is licensed to you under the Apache License Version 2.0,
7
+ // and you may not use this file except in compliance with the Apache License
8
+ // Version 2.0. You may obtain a copy of the Apache License Version 2.0 at
9
+ // http://www.apache.org/licenses/LICENSE-2.0.
10
+ //
11
+ // Unless required by applicable law or agreed to in writing,
12
+ // software distributed under the Apache License Version 2.0 is distributed on
13
+ // an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
14
+ // express or implied. See the Apache License Version 2.0 for the specific
15
+ // language governing permissions and limitations there under.
16
+ //
17
+ // Copyright: Copyright (c) 2022 Snowplow Analytics Ltd
18
+ // License: Apache License Version 2.0
19
+ //
20
+
1
21
  #import "RNSnowplowTracker.h"
2
- #import <SnowplowTracker/SPTracker.h>
3
- #import <SnowplowTracker/SPEmitter.h>
4
- #import <SnowplowTracker/SPEvent.h>
22
+ #import "RNConfigUtils.h"
23
+ #import "RNUtilities.h"
24
+
25
+ #import <SnowplowTracker/SPSnowplow.h>
26
+ #import <SnowplowTracker/NSDictionary+SP_TypeMethods.h>
27
+ #import <SnowplowTracker/SPUtilities.h>
28
+ #import <SnowplowTracker/SPNetworkConfiguration.h>
29
+ #import <SnowplowTracker/SPNetworkConnection.h>
30
+ #import <SnowplowTracker/SPTrackerConfiguration.h>
31
+ #import <SnowplowTracker/SPSessionConfiguration.h>
32
+ #import <SnowplowTracker/SPEmitterConfiguration.h>
33
+ #import <SnowplowTracker/SPSubjectConfiguration.h>
34
+ #import <SnowplowTracker/SPGDPRConfiguration.h>
35
+ #import <SnowplowTracker/SPGlobalContextsConfiguration.h>
36
+ #import <SnowplowTracker/SPGlobalContext.h>
5
37
  #import <SnowplowTracker/SPSelfDescribingJson.h>
6
- #import <SnowplowTracker/SPSubject.h>
38
+ #import <SnowplowTracker/SPSelfDescribing.h>
39
+ #import <SnowplowTracker/SPScreenView.h>
40
+ #import <SnowplowTracker/SPPageView.h>
41
+ #import <SnowplowTracker/SPTiming.h>
42
+ #import <SnowplowTracker/SPConsentGranted.h>
43
+ #import <SnowplowTracker/SPConsentWithdrawn.h>
44
+ #import <SnowplowTracker/SPStructured.h>
45
+ #import <SnowplowTracker/SPEcommerceItem.h>
46
+ #import <SnowplowTracker/SPEcommerce.h>
47
+ #import <SnowplowTracker/SPDeepLinkReceived.h>
48
+ #import <SnowplowTracker/SPMessageNotification.h>
7
49
 
8
- @implementation RNSnowplowTracker
9
50
 
10
- - (dispatch_queue_t)methodQueue
11
- {
12
- return dispatch_get_main_queue();
13
- }
51
+ @implementation RNSnowplowTracker
14
52
 
15
53
  RCT_EXPORT_MODULE()
16
54
 
17
- RCT_EXPORT_METHOD(initialize:
18
- (NSDictionary *)options
55
+ RCT_EXPORT_METHOD(createTracker:
56
+ (NSDictionary *)argmap
19
57
  resolver:(RCTPromiseResolveBlock)resolve
20
58
  rejecter:(RCTPromiseRejectBlock)reject) {
21
59
 
22
- // throw if index.js has failed to pass a complete options object
23
- if (!(options[@"endpoint"] != nil &&
24
- options[@"namespace"] != nil &&
25
- options[@"appId"] != nil &&
26
- options[@"method"] != nil &&
27
- options[@"protocol"] != nil &&
28
- options[@"base64Encoded"] != nil &&
29
- options[@"platformContext"] != nil &&
30
- options[@"applicationContext"] != nil &&
31
- options[@"lifecycleEvents"] != nil &&
32
- options[@"screenContext"] != nil &&
33
- options[@"sessionContext"] != nil &&
34
- options[@"foregroundTimeout"] != nil &&
35
- options[@"backgroundTimeout"] != nil &&
36
- options[@"checkInterval"] != nil &&
37
- options[@"installTracking"] != nil)) {
60
+ NSString *trackerNs = [argmap objectForKey:@"namespace"];
61
+ NSDictionary *networkConfig =[argmap objectForKey:@"networkConfig"];
62
+
63
+ // NetworkConfiguration
64
+ NSString *method = [networkConfig sp_stringForKey:@"method" defaultValue:nil];
65
+ SPHttpMethod httpMethod = [@"get" isEqualToString:method] ? SPHttpMethodGet : SPHttpMethodPost;
66
+ SPNetworkConfiguration *networkConfiguration = [[SPNetworkConfiguration alloc] initWithEndpoint:networkConfig[@"endpoint"] method:httpMethod];
67
+
68
+ // Configurations
69
+ NSMutableArray *controllers = [NSMutableArray array];
70
+
71
+ // TrackerConfiguration
72
+ NSObject *trackerArg = [argmap objectForKey:@"trackerConfig"];
73
+ if (trackerArg !=nil && [trackerArg isKindOfClass:NSDictionary.class]) {
74
+ SPTrackerConfiguration *trackerConfiguration = [RNConfigUtils mkTrackerConfig:(NSDictionary *)trackerArg];
75
+ [controllers addObject:trackerConfiguration];
76
+ }
77
+
78
+ // SessionConfiguration
79
+ NSObject *sessionArg = [argmap objectForKey:@"sessionConfig"];
80
+ if (sessionArg !=nil && [sessionArg isKindOfClass:NSDictionary.class]) {
81
+ SPSessionConfiguration *sessionConfiguration = [RNConfigUtils mkSessionConfig:(NSDictionary *)sessionArg];
82
+ [controllers addObject:sessionConfiguration];
83
+ }
84
+
85
+ // EmitterConfiguration
86
+ NSObject *emitterArg = [argmap objectForKey:@"emitterConfig"];
87
+ if (emitterArg !=nil && [emitterArg isKindOfClass:NSDictionary.class]) {
88
+ SPEmitterConfiguration *emitterConfiguration = [RNConfigUtils mkEmitterConfig:(NSDictionary *)emitterArg];
89
+ [controllers addObject:emitterConfiguration];
90
+ }
38
91
 
39
- NSError * error = [NSError errorWithDomain:@"SnowplowTracker" code:100 userInfo:nil];
40
- return reject(@"ERROR", @"SnowplowTracker: initialize() method - missing parameter with no default found", error);
92
+ // SubjectConfiguration
93
+ NSObject *subjectArg = [argmap objectForKey:@"subjectConfig"];
94
+ if (subjectArg != nil && [subjectArg isKindOfClass:NSDictionary.class]) {
95
+ SPSubjectConfiguration *subjectConfiguration = [RNConfigUtils mkSubjectConfig:(NSDictionary *)subjectArg];
96
+ [controllers addObject:subjectConfiguration];
41
97
  }
42
98
 
43
- SPSubject *subject = [[SPSubject alloc] initWithPlatformContext:[options[@"platformContext"] boolValue] andGeoContext:NO];
99
+ // GdprConfiguration
100
+ NSObject *gdprArg = [argmap objectForKey:@"gdprConfig"];
101
+ if (gdprArg != nil && [gdprArg isKindOfClass:NSDictionary.class]) {
102
+ SPGDPRConfiguration *gdprConfiguration = [RNConfigUtils mkGdprConfig:(NSDictionary *)gdprArg];
103
+ [controllers addObject:gdprConfiguration];
104
+ }
44
105
 
45
- SPEmitter *emitter = [SPEmitter build:^(id<SPEmitterBuilder> builder) {
46
- [builder setUrlEndpoint:options[@"endpoint"]];
47
- [builder setHttpMethod:([@"post" caseInsensitiveCompare:options[@"method"]] == NSOrderedSame) ? SPRequestPost : SPRequestGet];
48
- [builder setProtocol:([@"https" caseInsensitiveCompare:options[@"protocol"]] == NSOrderedSame) ? SPHttps : SPHttp];
49
- }];
106
+ // GConfiguration
107
+ NSObject *gcArg = [argmap objectForKey:@"gcConfig"];
108
+ if (gcArg != nil && [gcArg isKindOfClass:NSArray.class]) {
109
+ SPGlobalContextsConfiguration *gcConfiguration = [RNConfigUtils mkGCConfig:(NSArray *)gcArg];
110
+ [controllers addObject:gcConfiguration];
111
+ }
50
112
 
51
- self.tracker = [SPTracker build:^(id<SPTrackerBuilder> builder) {
52
- [builder setEmitter:emitter];
53
- [builder setAppId:options[@"appId"]];
54
- [builder setBase64Encoded:[options[@"base64Encoded"] boolValue]];
55
- [builder setTrackerNamespace:options[@"namespace"]];
56
- [builder setApplicationContext:[options[@"applicationContext"] boolValue]];
57
- [builder setLifecycleEvents:[options[@"lifecycleEvents"] boolValue]];
58
- [builder setScreenContext:[options[@"screenContext"] boolValue]];
59
- [builder setInstallEvent:[options[@"installTracking"] boolValue]];
60
- [builder setSubject:subject];
61
- [builder setSessionContext:[options[@"sessionContext"] boolValue]];
62
- [builder setCheckInterval:[options[@"checkInterval"] integerValue]];
63
- [builder setForegroundTimeout:[options[@"foregroundTimeout"] integerValue]];
64
- [builder setBackgroundTimeout:[options[@"backgroundTimeout"] integerValue]];
65
- }];
113
+ id<SPTrackerController> tracker = [SPSnowplow createTrackerWithNamespace:trackerNs network:networkConfiguration configurations:controllers];
66
114
 
67
- if (self.tracker) {
115
+ if (tracker) {
68
116
  resolve(@YES);
69
117
  } else {
70
- NSError * error = [NSError errorWithDomain:@"SnowplowTracker" code:200 userInfo:nil];
71
- return reject(@"ERROR", @"SnowplowTracker: initialize() method - tracker initialisation failed", error);
118
+ NSError* error = [NSError errorWithDomain:@"SnowplowTracker" code:200 userInfo:nil];
119
+ reject(@"ERROR", @"tracker initialization failed", error);
72
120
  }
73
121
  }
74
122
 
75
- RCT_EXPORT_METHOD(setSubjectData :(NSDictionary *)options
76
- :rejecter:(RCTPromiseRejectBlock)reject) {
123
+ RCT_EXPORT_METHOD(removeTracker: (NSDictionary *)details
124
+ resolver:(RCTPromiseResolveBlock)resolve
125
+ rejecter:(RCTPromiseRejectBlock)reject) {
126
+ NSString *namespace = [details objectForKey:@"tracker"];
127
+ id<SPTrackerController> trackerController = [SPSnowplow trackerByNamespace:namespace];
128
+ BOOL removed = [SPSnowplow removeTracker:trackerController];
129
+ resolve(@(removed));
130
+ }
77
131
 
78
- // the readability we achieved elsewere by using similar patterns to android is not possible here.
79
- NSString *userId = options[@"userId"];
80
- NSString *timezone = options[@"timezone"];
81
- NSString *language = options[@"language"];
82
- NSString *ipAddress = options[@"ipAddress"];
83
- NSString *useragent = options[@"useragent"];
84
- NSString *networkUserId = options[@"networkUserId"];
85
- NSString *domainUserId = options[@"domainUserId"];
132
+ RCT_EXPORT_METHOD(removeAllTrackers: (RCTPromiseResolveBlock)resolve rejecter:(RCTPromiseRejectBlock)reject) {
133
+ [SPSnowplow removeAllTrackers];
134
+ resolve(@YES);
135
+ }
86
136
 
87
- NSNumber *screenWidth = options[@"screenWidth"];
88
- NSNumber *screenHeight = options[@"screenHeight"];
89
- NSNumber *viewportWidth = options[@"viewportWidth"];
90
- NSNumber *viewportHeight = options[@"viewportHeight"];
91
- NSNumber *colorDepth = options[@"colorDepth"];
137
+ RCT_EXPORT_METHOD(trackSelfDescribingEvent:
138
+ (NSDictionary *)details
139
+ resolver:(RCTPromiseResolveBlock)resolve
140
+ rejecter:(RCTPromiseRejectBlock)reject) {
141
+ NSString *namespace = [details objectForKey:@"tracker"];
142
+ id<SPTrackerController> trackerController = [SPSnowplow trackerByNamespace:namespace];
92
143
 
93
- if (userId) {
94
- NSString *newUserId = [[NSNull null] isEqual:userId] ? nil : userId;
95
- [self.tracker.subject setUserId:newUserId];
96
- }
144
+ if (trackerController != nil) {
145
+ NSDictionary *argmap = [details objectForKey:@"eventData"];
146
+ NSArray<NSDictionary *> *contexts = [details objectForKey:@"contexts"];
97
147
 
98
- if (timezone) {
99
- NSString *newTimezone = [[NSNull null] isEqual:timezone] ? nil : timezone;
100
- [self.tracker.subject setTimezone:newTimezone];
101
- }
148
+ SPSelfDescribingJson *eventData = [[SPSelfDescribingJson alloc] initWithSchema:(NSString *)[argmap objectForKey:@"schema"]
149
+ andDictionary:(NSDictionary *)[argmap objectForKey:@"data"]];
102
150
 
103
- if (ipAddress) {
104
- NSString *newIpAddress = [[NSNull null] isEqual:ipAddress] ? nil : ipAddress;
105
- [self.tracker.subject setIpAddress:newIpAddress];
151
+ SPSelfDescribing *event = [[SPSelfDescribing alloc] initWithEventData:eventData];
152
+ [event contexts:[RNUtilities mkSDJArray:contexts]];
153
+ [trackerController track:event];
154
+ resolve(@YES);
155
+ } else {
156
+ NSError* error = [NSError errorWithDomain:@"SnowplowTracker" code:200 userInfo:nil];
157
+ reject(@"ERROR", @"tracker with given namespace not found", error);
106
158
  }
159
+ }
160
+
161
+ RCT_EXPORT_METHOD(trackStructuredEvent:
162
+ (NSDictionary *)details
163
+ resolver:(RCTPromiseResolveBlock)resolve
164
+ rejecter:(RCTPromiseRejectBlock)reject) {
165
+ NSString *namespace = [details objectForKey:@"tracker"];
166
+ id<SPTrackerController> trackerController = [SPSnowplow trackerByNamespace:namespace];
167
+
168
+ if (trackerController != nil) {
169
+ NSDictionary *argmap = [details objectForKey:@"eventData"];
170
+ NSArray<NSDictionary *> *contexts = [details objectForKey:@"contexts"];
171
+
172
+ NSString *category = [argmap sp_stringForKey:@"category" defaultValue:nil];
173
+ NSString *action = [argmap sp_stringForKey:@"action" defaultValue:nil];
174
+ SPStructured *event = [[SPStructured alloc] initWithCategory:category
175
+ action:action];
176
+ NSString *label = [argmap sp_stringForKey:@"label" defaultValue:nil];
177
+ if (label) {
178
+ event.label = label;
179
+ }
180
+ NSString *property = [argmap sp_stringForKey:@"property" defaultValue:nil];
181
+ if (property) {
182
+ event.property = property;
183
+ }
184
+ NSNumber *value = [argmap sp_numberForKey:@"value" defaultValue:nil];
185
+ if (label) {
186
+ event.value = value;
187
+ }
107
188
 
108
- if (language) {
109
- NSString *newLanguage = [[NSNull null] isEqual:language] ? nil : language;
110
- [self.tracker.subject setLanguage:newLanguage];
189
+ [event contexts:[RNUtilities mkSDJArray:contexts]];
190
+ [trackerController track:event];
191
+ resolve(@YES);
192
+ } else {
193
+ NSError* error = [NSError errorWithDomain:@"SnowplowTracker" code:200 userInfo:nil];
194
+ reject(@"ERROR", @"tracker with given namespace not found", error);
111
195
  }
196
+ }
197
+
198
+ RCT_EXPORT_METHOD(trackScreenViewEvent:
199
+ (NSDictionary *)details
200
+ resolver:(RCTPromiseResolveBlock)resolve
201
+ rejecter:(RCTPromiseRejectBlock)reject) {
202
+ NSString *namespace = [details objectForKey:@"tracker"];
203
+ id<SPTrackerController> trackerController = [SPSnowplow trackerByNamespace:namespace];
204
+
205
+ if (trackerController != nil) {
206
+ NSDictionary *argmap = [details objectForKey:@"eventData"];
207
+ NSArray<NSDictionary *> *contexts = [details objectForKey:@"contexts"];
208
+
209
+ NSString *screenName = [argmap sp_stringForKey:@"name" defaultValue:nil];
210
+ NSString *screenId = [argmap sp_stringForKey:@"id" defaultValue:nil];
211
+ NSUUID *screenUuid = [[NSUUID alloc] initWithUUIDString:screenId];
212
+ if (screenId != nil && screenUuid == nil) {
213
+ NSError* error = [NSError errorWithDomain:@"SnowplowTracker" code:200 userInfo:nil];
214
+ reject(@"ERROR", @"screenId has to be a valid UUID string", error);
215
+ }
216
+ SPScreenView *event = [[SPScreenView alloc] initWithName:screenName
217
+ screenId:screenUuid];
218
+
219
+ NSString *type = [argmap sp_stringForKey:@"type" defaultValue:nil];
220
+ if (type) {
221
+ event.type = type;
222
+ }
223
+ NSString *previousName = [argmap sp_stringForKey:@"previousName" defaultValue:nil];
224
+ if (previousName) {
225
+ event.previousName = previousName;
226
+ }
227
+ NSString *previousId = [argmap sp_stringForKey:@"previousId" defaultValue:nil];
228
+ if (previousId) {
229
+ event.previousId = previousId;
230
+ }
231
+ NSString *previousType = [argmap sp_stringForKey:@"previousType" defaultValue:nil];
232
+ if (previousType) {
233
+ event.previousType = previousType;
234
+ }
235
+ NSString *transitionType = [argmap sp_stringForKey:@"transitionType" defaultValue:nil];
236
+ if (transitionType) {
237
+ event.transitionType = transitionType;
238
+ }
112
239
 
113
- if (useragent) {
114
- NSString *newUseragent = [[NSNull null] isEqual:useragent] ? nil : useragent;
115
- [self.tracker.subject setUseragent:newUseragent];
240
+ [event contexts:[RNUtilities mkSDJArray:contexts]];
241
+ [trackerController track:event];
242
+ resolve(@YES);
243
+ } else {
244
+ NSError* error = [NSError errorWithDomain:@"SnowplowTracker" code:200 userInfo:nil];
245
+ reject(@"ERROR", @"tracker with given namespace not found", error);
116
246
  }
247
+ }
117
248
 
118
- if (networkUserId) {
119
- NSString *newNetworkUserId = [[NSNull null] isEqual:networkUserId] ? nil : networkUserId;
120
- [self.tracker.subject setNetworkUserId:newNetworkUserId];
249
+ RCT_EXPORT_METHOD(trackPageViewEvent:
250
+ (NSDictionary *)details
251
+ resolver:(RCTPromiseResolveBlock)resolve
252
+ rejecter:(RCTPromiseRejectBlock)reject) {
253
+ NSString *namespace = [details objectForKey:@"tracker"];
254
+ id<SPTrackerController> trackerController = [SPSnowplow trackerByNamespace:namespace];
255
+
256
+ if (trackerController != nil) {
257
+ NSDictionary *argmap = [details objectForKey:@"eventData"];
258
+ NSArray<NSDictionary *> *contexts = [details objectForKey:@"contexts"];
259
+
260
+ NSString *pageUrl = [argmap sp_stringForKey:@"pageUrl" defaultValue:nil];
261
+ SPPageView *event = [[SPPageView alloc] initWithPageUrl:pageUrl];
262
+
263
+ NSString *pageTitle = [argmap sp_stringForKey:@"pageTitle" defaultValue:nil];
264
+ if (pageTitle) {
265
+ event.pageTitle = pageTitle;
266
+ }
267
+ NSString *referrer = [argmap sp_stringForKey:@"referrer" defaultValue:nil];
268
+ if (referrer) {
269
+ event.referrer = referrer;
270
+ }
271
+
272
+ [event contexts:[RNUtilities mkSDJArray:contexts]];
273
+ [trackerController track:event];
274
+ resolve(@YES);
275
+ } else {
276
+ NSError* error = [NSError errorWithDomain:@"SnowplowTracker" code:200 userInfo:nil];
277
+ reject(@"ERROR", @"tracker with given namespace not found", error);
121
278
  }
279
+ }
280
+
281
+ RCT_EXPORT_METHOD(trackTimingEvent:
282
+ (NSDictionary *)details
283
+ resolver:(RCTPromiseResolveBlock)resolve
284
+ rejecter:(RCTPromiseRejectBlock)reject) {
285
+ NSString *namespace = [details objectForKey:@"tracker"];
286
+ id<SPTrackerController> trackerController = [SPSnowplow trackerByNamespace:namespace];
287
+
288
+ if (trackerController != nil) {
289
+ NSDictionary *argmap = [details objectForKey:@"eventData"];
290
+ NSArray<NSDictionary *> *contexts = [details objectForKey:@"contexts"];
291
+
292
+ NSString *category = [argmap sp_stringForKey:@"category" defaultValue:nil];
293
+ NSString *variable = [argmap sp_stringForKey:@"variable" defaultValue:nil];
294
+ NSNumber *timing = [argmap sp_numberForKey:@"timing" defaultValue:nil];
295
+ SPTiming *event = [[SPTiming alloc] initWithCategory:category
296
+ variable:variable
297
+ timing:timing];
298
+ NSString *label = [argmap sp_stringForKey:@"label" defaultValue:nil];
299
+ if (label) {
300
+ event.label = label;
301
+ }
122
302
 
123
- if (domainUserId) {
124
- NSString *newDomainUserId = [[NSNull null] isEqual:domainUserId] ? nil : domainUserId;
125
- [self.tracker.subject setDomainUserId:newDomainUserId];
303
+ [event contexts:[RNUtilities mkSDJArray:contexts]];
304
+ [trackerController track:event];
305
+ resolve(@YES);
306
+ } else {
307
+ NSError* error = [NSError errorWithDomain:@"SnowplowTracker" code:200 userInfo:nil];
308
+ reject(@"ERROR", @"tracker with given namespace not found", error);
126
309
  }
310
+ }
127
311
 
128
- if (screenWidth && screenHeight) {
129
- if ([[NSNull null] isEqual:screenWidth] || [[NSNull null] isEqual:screenHeight]) {
130
- NSError * error = [NSError errorWithDomain:@"SnowplowTracker" code:100 userInfo:nil];
131
- return reject(@"ERROR", @"SnowplowTracker: setSubjectData() method - screenWidth and screenHeight cannot be null", error);
132
- } else {
133
- [self.tracker.subject setResolutionWithWidth:[screenWidth integerValue] andHeight:[screenHeight integerValue]];
312
+ RCT_EXPORT_METHOD(trackConsentGrantedEvent:
313
+ (NSDictionary *)details
314
+ resolver:(RCTPromiseResolveBlock)resolve
315
+ rejecter:(RCTPromiseRejectBlock)reject) {
316
+ NSString *namespace = [details objectForKey:@"tracker"];
317
+ id<SPTrackerController> trackerController = [SPSnowplow trackerByNamespace:namespace];
318
+
319
+ if (trackerController != nil) {
320
+ NSDictionary *argmap = [details objectForKey:@"eventData"];
321
+ NSArray<NSDictionary *> *contexts = [details objectForKey:@"contexts"];
322
+
323
+ NSString *expiry = [argmap sp_stringForKey:@"expiry" defaultValue:nil];
324
+ NSString *documentId = [argmap sp_stringForKey:@"documentId" defaultValue:nil];
325
+ NSString *version = [argmap sp_stringForKey:@"version" defaultValue:nil];
326
+ SPConsentGranted *event = [[SPConsentGranted alloc] initWithExpiry:expiry
327
+ documentId:documentId
328
+ version:version];
329
+
330
+ NSString *name = [argmap sp_stringForKey:@"name" defaultValue:nil];
331
+ if (name) {
332
+ event.name = name;
134
333
  }
334
+ NSString *documentDescription = [argmap sp_stringForKey:@"documentDescription" defaultValue:nil];
335
+ if (documentDescription) {
336
+ event.documentDescription = documentDescription;
337
+ }
338
+
339
+ [event contexts:[RNUtilities mkSDJArray:contexts]];
340
+ [trackerController track:event];
341
+ resolve(@YES);
342
+ } else {
343
+ NSError* error = [NSError errorWithDomain:@"SnowplowTracker" code:200 userInfo:nil];
344
+ reject(@"ERROR", @"tracker with given namespace not found", error);
135
345
  }
346
+ }
136
347
 
137
- if (viewportWidth && viewportHeight) {
138
- if ([[NSNull null] isEqual:viewportWidth] || [[NSNull null] isEqual:viewportHeight]) {
139
- NSError * error = [NSError errorWithDomain:@"SnowplowTracker" code:100 userInfo:nil];
140
- return reject(@"ERROR", @"SnowplowTracker: setSubjectData() method - viewportWidth and viewportHeight cannot be null", error);
141
- } else {
142
- [self.tracker.subject setViewPortWithWidth:[viewportWidth integerValue] andHeight:[viewportHeight integerValue]];
348
+ RCT_EXPORT_METHOD(trackConsentWithdrawnEvent:
349
+ (NSDictionary *)details
350
+ resolver:(RCTPromiseResolveBlock)resolve
351
+ rejecter:(RCTPromiseRejectBlock)reject) {
352
+ NSString *namespace = [details objectForKey:@"tracker"];
353
+ id<SPTrackerController> trackerController = [SPSnowplow trackerByNamespace:namespace];
354
+
355
+ if (trackerController != nil) {
356
+ NSDictionary *argmap = [details objectForKey:@"eventData"];
357
+ NSArray<NSDictionary *> *contexts = [details objectForKey:@"contexts"];
358
+
359
+ SPConsentWithdrawn *event = [SPConsentWithdrawn new];
360
+
361
+ BOOL all = [argmap sp_boolForKey:@"all" defaultValue:nil];
362
+ event.all = all;
363
+ NSString *documentId = [argmap sp_stringForKey:@"documentId" defaultValue:nil];
364
+ event.documentId = documentId;
365
+ NSString *version = [argmap sp_stringForKey:@"version" defaultValue:nil];
366
+ event.version = version;
367
+ NSString *name = [argmap sp_stringForKey:@"name" defaultValue:nil];
368
+ if (name) {
369
+ event.name = name;
143
370
  }
371
+ NSString *documentDescription = [argmap sp_stringForKey:@"documentDescription" defaultValue:nil];
372
+ if (documentDescription) {
373
+ event.documentDescription = documentDescription;
374
+ }
375
+
376
+ [event contexts:[RNUtilities mkSDJArray:contexts]];
377
+ [trackerController track:event];
378
+ resolve(@YES);
379
+ } else {
380
+ NSError* error = [NSError errorWithDomain:@"SnowplowTracker" code:200 userInfo:nil];
381
+ reject(@"ERROR", @"tracker with given namespace not found", error);
144
382
  }
383
+ }
145
384
 
146
- if (colorDepth != nil) {
147
- if ([[NSNull null] isEqual:colorDepth]) {
148
- NSError * error = [NSError errorWithDomain:@"SnowplowTracker" code:100 userInfo:nil];
149
- return reject(@"ERROR", @"SnowplowTracker: setSubjectData() method - colorDepth cannot be null", error);
150
- } else {
151
- [self.tracker.subject setColorDepth:[colorDepth integerValue]];
385
+ RCT_EXPORT_METHOD(trackEcommerceTransactionEvent:
386
+ (NSDictionary *)details
387
+ resolver:(RCTPromiseResolveBlock)resolve
388
+ rejecter:(RCTPromiseRejectBlock)reject) {
389
+ NSString *namespace = [details objectForKey:@"tracker"];
390
+ id<SPTrackerController> trackerController = [SPSnowplow trackerByNamespace:namespace];
391
+
392
+ if (trackerController != nil) {
393
+ NSDictionary *argmap = [details objectForKey:@"eventData"];
394
+ NSArray<NSDictionary *> *contexts = [details objectForKey:@"contexts"];
395
+
396
+ NSString *orderId = [argmap sp_stringForKey:@"orderId" defaultValue:nil];
397
+ NSNumber *totalValue = [argmap sp_numberForKey:@"totalValue" defaultValue:nil];
398
+ NSArray *items = [argmap objectForKey:@"items"];
399
+
400
+ NSMutableArray *transItems = [NSMutableArray new];
401
+ for (NSDictionary* item in items) {
402
+ NSString *sku = [item sp_stringForKey:@"sku" defaultValue:nil];
403
+ NSNumber *price = [item sp_numberForKey:@"price" defaultValue:nil];
404
+ NSNumber *quantity = [item sp_numberForKey:@"quantity" defaultValue:nil];
405
+ SPEcommerceItem *ecomItem = [[SPEcommerceItem alloc] initWithSku:sku
406
+ price:price
407
+ quantity:quantity];
408
+
409
+ NSString *name = [argmap sp_stringForKey:@"name" defaultValue:nil];
410
+ if (name) {
411
+ ecomItem.name = name;
412
+ }
413
+ NSString *category = [argmap sp_stringForKey:@"category" defaultValue:nil];
414
+ if (category) {
415
+ ecomItem.category = category;
416
+ }
417
+ NSString *currency = [argmap sp_stringForKey:@"currency" defaultValue:nil];
418
+ if (currency) {
419
+ ecomItem.currency = currency;
420
+ }
421
+
422
+ [transItems addObject:ecomItem];
152
423
  }
424
+
425
+ SPEcommerce *event = [[SPEcommerce alloc] initWithOrderId:orderId
426
+ totalValue:totalValue
427
+ items:(NSArray<SPEcommerceItem *> *)transItems];
428
+ [event contexts:[RNUtilities mkSDJArray:contexts]];
429
+ [trackerController track:event];
430
+ resolve(@YES);
431
+
432
+ } else {
433
+ NSError* error = [NSError errorWithDomain:@"SnowplowTracker" code:200 userInfo:nil];
434
+ reject(@"ERROR", @"tracker with given namespace not found", error);
153
435
  }
154
436
  }
155
437
 
156
- RCT_EXPORT_METHOD(trackSelfDescribingEvent
157
- :(nonnull SPSelfDescribingJson *)event
158
- :(NSArray<SPSelfDescribingJson *> *)contexts) {
438
+ RCT_EXPORT_METHOD(trackDeepLinkReceivedEvent:
439
+ (NSDictionary *)details
440
+ resolver:(RCTPromiseResolveBlock)resolve
441
+ rejecter:(RCTPromiseRejectBlock)reject) {
442
+ NSString *namespace = [details objectForKey:@"tracker"];
443
+ id<SPTrackerController> trackerController = [SPSnowplow trackerByNamespace:namespace];
444
+
445
+ if (trackerController != nil) {
446
+ NSDictionary *argmap = [details objectForKey:@"eventData"];
447
+ NSArray<NSDictionary *> *contexts = [details objectForKey:@"contexts"];
448
+
449
+ NSString *url = [argmap sp_stringForKey:@"url" defaultValue:nil];
450
+ SPDeepLinkReceived *event = [[SPDeepLinkReceived alloc] initWithUrl:url];
159
451
 
160
- SPUnstructured * unstructEvent = [SPUnstructured build:^(id<SPUnstructuredBuilder> builder) {
161
- [builder setEventData:event];
162
- if (contexts) {
163
- [builder setContexts:[[NSMutableArray alloc] initWithArray:contexts]];
452
+ NSString *referrer = [argmap sp_stringForKey:@"referrer" defaultValue:nil];
453
+ if (referrer) {
454
+ event.referrer = referrer;
164
455
  }
165
- }];
166
456
 
167
- [self.tracker trackUnstructuredEvent:unstructEvent];
457
+ [event contexts:[RNUtilities mkSDJArray:contexts]];
458
+ [trackerController track:event];
459
+ resolve(@YES);
460
+ } else {
461
+ NSError* error = [NSError errorWithDomain:@"SnowplowTracker" code:200 userInfo:nil];
462
+ reject(@"ERROR", @"tracker with given namespace not found", error);
463
+ }
168
464
  }
169
465
 
170
- RCT_EXPORT_METHOD(trackStructuredEvent
171
- :(NSDictionary *)details
172
- :(NSArray<SPSelfDescribingJson *> *)contexts) {
466
+ RCT_EXPORT_METHOD(trackMessageNotificationEvent:
467
+ (NSDictionary *)details
468
+ resolver:(RCTPromiseResolveBlock)resolve
469
+ rejecter:(RCTPromiseRejectBlock)reject) {
470
+ NSString *namespace = [details objectForKey:@"tracker"];
471
+ id<SPTrackerController> trackerController = [SPSnowplow trackerByNamespace:namespace];
472
+
473
+ if (trackerController != nil) {
474
+ NSDictionary *argmap = [details objectForKey:@"eventData"];
475
+ NSArray<NSDictionary *> *contexts = [details objectForKey:@"contexts"];
476
+
477
+ NSString *title = [argmap sp_stringForKey:@"title" defaultValue:nil];
478
+ NSString *body = [argmap sp_stringForKey:@"body" defaultValue:nil];
479
+ NSString *triggerStr = [argmap sp_stringForKey:@"trigger" defaultValue:nil];
480
+ SPMessageNotificationTrigger trigger;
481
+ if ([triggerStr isEqualToString:@"push"]) {
482
+ trigger = SPMessageNotificationTriggerPush;
483
+ } else if ([triggerStr isEqualToString:@"location"]) {
484
+ trigger = SPMessageNotificationTriggerLocation;
485
+ } else if ([triggerStr isEqualToString:@"calendar"]) {
486
+ trigger = SPMessageNotificationTriggerCalendar;
487
+ } else if ([triggerStr isEqualToString:@"timeInterval"]) {
488
+ trigger = SPMessageNotificationTriggerTimeInterval;
489
+ } else {
490
+ trigger = SPMessageNotificationTriggerOther;
491
+ }
492
+ SPMessageNotification *event = [[SPMessageNotification alloc] initWithTitle: title
493
+ body: body
494
+ trigger: trigger];
173
495
 
174
- SPStructured * structuredEvent = [SPStructured build:^(id<SPStructuredBuilder> builder) {
175
- [builder setCategory:details[@"category"]];
176
- [builder setAction:details[@"action"]];
177
- if (details[@"label"] != nil) {
178
- [builder setLabel:details[@"label"]];
496
+ NSString *action = [argmap sp_stringForKey:@"action" defaultValue:nil];
497
+ if (action) {
498
+ event.action = action;
499
+ }
500
+ NSArray *attachmentsMap = [argmap objectForKey:@"attachments"];
501
+ if (attachmentsMap) {
502
+ NSMutableArray *attachments = [NSMutableArray new];
503
+ for (NSDictionary* attachmentMap in attachmentsMap) {
504
+ NSString *identifier = [attachmentMap sp_stringForKey:@"identifier" defaultValue:nil];
505
+ NSString *type = [attachmentMap sp_stringForKey:@"type" defaultValue:nil];
506
+ NSString *url = [attachmentMap sp_stringForKey:@"url" defaultValue:nil];
507
+ SPMessageNotificationAttachment *attachment = [[SPMessageNotificationAttachment alloc] initWithIdentifier:identifier
508
+ type:type
509
+ url:url];
510
+ [attachments addObject:attachment];
511
+ }
512
+ event.attachments = attachments;
513
+ }
514
+ NSArray<NSString *> *bodyLocArgs = [argmap objectForKey:@"bodyLocArgs"];
515
+ if (bodyLocArgs) {
516
+ event.bodyLocArgs = bodyLocArgs;
517
+ }
518
+ NSString *bodyLocKey = [argmap sp_stringForKey:@"bodyLocKey" defaultValue:nil];
519
+ if (bodyLocKey) {
520
+ event.bodyLocKey = bodyLocKey;
521
+ }
522
+ NSString *category = [argmap sp_stringForKey:@"category" defaultValue:nil];
523
+ if (category) {
524
+ event.category = category;
525
+ }
526
+ NSNumber *contentAvailable = [argmap sp_numberForKey:@"contentAvailable" defaultValue:nil];
527
+ if (contentAvailable != nil) {
528
+ event.contentAvailable = contentAvailable;
529
+ }
530
+ NSString *group = [argmap sp_stringForKey:@"group" defaultValue:nil];
531
+ if (group) {
532
+ event.group = group;
179
533
  }
180
- if (details[@"property"] != nil) {
181
- [builder setProperty:details[@"property"]];
534
+ NSString *icon = [argmap sp_stringForKey:@"icon" defaultValue:nil];
535
+ if (icon) {
536
+ event.icon = icon;
182
537
  }
183
- // doubleValue cannot be NSNull, and falsey value evaluates to 0 in objective-c. Only set 'value' parameter where neither are the case.
184
- if (details[@"value"] != (id)[NSNull null] && details[@"value"] != nil) {
185
- [builder setValue:[details[@"value"] doubleValue]];
538
+ NSNumber *notificationCount = [argmap sp_numberForKey:@"notificationCount" defaultValue:nil];
539
+ if (notificationCount) {
540
+ event.notificationCount = notificationCount;
186
541
  }
187
- if (contexts) {
188
- [builder setContexts:[[NSMutableArray alloc] initWithArray:contexts]];
542
+ NSString *notificationTimestamp = [argmap sp_stringForKey:@"notificationTimestamp" defaultValue:nil];
543
+ if (notificationTimestamp) {
544
+ event.notificationTimestamp = notificationTimestamp;
189
545
  }
190
- }];
546
+ NSString *sound = [argmap sp_stringForKey:@"sound" defaultValue:nil];
547
+ if (sound) {
548
+ event.sound = sound;
549
+ }
550
+ NSString *subtitle = [argmap sp_stringForKey:@"subtitle" defaultValue:nil];
551
+ if (subtitle) {
552
+ event.subtitle = subtitle;
553
+ }
554
+ NSString *tag = [argmap sp_stringForKey:@"tag" defaultValue:nil];
555
+ if (tag) {
556
+ event.tag = tag;
557
+ }
558
+ NSString *threadIdentifier = [argmap sp_stringForKey:@"threadIdentifier" defaultValue:nil];
559
+ if (threadIdentifier) {
560
+ event.threadIdentifier = threadIdentifier;
561
+ }
562
+ NSArray<NSString *> *titleLocArgs = [argmap objectForKey:@"titleLocArgs"];
563
+ if (titleLocArgs) {
564
+ event.titleLocArgs = titleLocArgs;
565
+ }
566
+ NSString *titleLocKey = [argmap sp_stringForKey:@"titleLocKey" defaultValue:nil];
567
+ if (titleLocKey) {
568
+ event.titleLocKey = titleLocKey;
569
+ }
570
+
571
+ [event contexts:[RNUtilities mkSDJArray:contexts]];
572
+ [trackerController track:event];
573
+ resolve(@YES);
574
+ } else {
575
+ NSError* error = [NSError errorWithDomain:@"SnowplowTracker" code:200 userInfo:nil];
576
+ reject(@"ERROR", @"tracker with given namespace not found", error);
577
+ }
578
+ }
191
579
 
192
- [self.tracker trackStructuredEvent:structuredEvent];
580
+ RCT_EXPORT_METHOD(removeGlobalContexts:
581
+ (NSDictionary *)details
582
+ resolver:(RCTPromiseResolveBlock)resolve
583
+ rejecter:(RCTPromiseRejectBlock)reject) {
584
+ NSString *namespace = [details objectForKey:@"tracker"];
585
+ id<SPTrackerController> trackerController = [SPSnowplow trackerByNamespace:namespace];
586
+
587
+ if (trackerController != nil) {
588
+ NSString *tag = [details objectForKey:@"removeTag"];
589
+ [[trackerController globalContexts] removeWithTag:tag];
590
+ resolve(@YES);
591
+ } else {
592
+ NSError* error = [NSError errorWithDomain:@"SnowplowTracker" code:200 userInfo:nil];
593
+ reject(@"ERROR", @"tracker with given namespace not found", error);
594
+ }
193
595
  }
194
596
 
195
- RCT_EXPORT_METHOD(trackScreenViewEvent
196
- :(NSDictionary *)details
197
- :(NSArray<SPSelfDescribingJson *> *)contexts) {
597
+ RCT_EXPORT_METHOD(addGlobalContexts:
598
+ (NSDictionary *)details
599
+ resolver:(RCTPromiseResolveBlock)resolve
600
+ rejecter:(RCTPromiseRejectBlock)reject) {
601
+ NSString *namespace = [details objectForKey:@"tracker"];
602
+ id<SPTrackerController> trackerController = [SPSnowplow trackerByNamespace:namespace];
198
603
 
199
- SPScreenView * screenViewEvent = [SPScreenView build:^(id<SPScreenViewBuilder> builder) {
200
- [builder setName:details[@"screenName"]];
604
+ if (trackerController != nil) {
605
+ NSDictionary *gcArg = [details objectForKey:@"addGlobalContext"];
606
+ NSString *tag = [gcArg objectForKey:@"tag"];
607
+ NSArray *globalContexts = [gcArg objectForKey:@"globalContexts"];
201
608
 
202
- // screenType must not be NSNull.
203
- if (details[@"screenType"] != (id)[NSNull null] && details[@"screenType"] != nil) [builder setType:details[@"screenType"]];
204
- if (details[@"transitionType"] != (id)[NSNull null] && details[@"transitionType"] != nil) [builder setTransitionType:details[@"transitionType"]];
205
- if (contexts) {
206
- [builder setContexts:[[NSMutableArray alloc] initWithArray:contexts]];
207
- }
208
- }];
609
+ NSArray *staticContexts = [[RNUtilities mkSDJArray:globalContexts] mutableCopy];
610
+ SPGlobalContext *gcStatic = [[SPGlobalContext alloc] initWithStaticContexts:staticContexts];
611
+
612
+ [[trackerController globalContexts] addWithTag:tag contextGenerator:gcStatic];
613
+ resolve(@YES);
614
+ } else {
615
+ NSError* error = [NSError errorWithDomain:@"SnowplowTracker" code:200 userInfo:nil];
616
+ reject(@"ERROR", @"tracker with given namespace not found", error);
617
+ }
618
+ }
619
+
620
+ RCT_EXPORT_METHOD(setUserId:
621
+ (NSDictionary *)details
622
+ resolver:(RCTPromiseResolveBlock)resolve
623
+ rejecter:(RCTPromiseRejectBlock)reject) {
624
+ NSString *namespace = [details objectForKey:@"tracker"];
625
+ id<SPTrackerController> trackerController = [SPSnowplow trackerByNamespace:namespace];
626
+
627
+ if (trackerController != nil) {
628
+ NSString *newUid = [details sp_stringForKey:@"userId" defaultValue:nil];
629
+ [trackerController.subject setUserId:newUid];
630
+ resolve(@YES);
631
+ } else {
632
+ NSError* error = [NSError errorWithDomain:@"SnowplowTracker" code:200 userInfo:nil];
633
+ reject(@"ERROR", @"tracker with given namespace not found", error);
634
+ }
635
+ }
636
+
637
+ RCT_EXPORT_METHOD(setNetworkUserId:
638
+ (NSDictionary *)details
639
+ resolver:(RCTPromiseResolveBlock)resolve
640
+ rejecter:(RCTPromiseRejectBlock)reject) {
641
+ NSString *namespace = [details objectForKey:@"tracker"];
642
+ id<SPTrackerController> trackerController = [SPSnowplow trackerByNamespace:namespace];
643
+
644
+ if (trackerController != nil) {
645
+ NSString *newNuid = [details sp_stringForKey:@"networkUserId" defaultValue:nil];
646
+ [trackerController.subject setNetworkUserId:newNuid];
647
+ resolve(@YES);
648
+ } else {
649
+ NSError* error = [NSError errorWithDomain:@"SnowplowTracker" code:200 userInfo:nil];
650
+ reject(@"ERROR", @"tracker with given namespace not found", error);
651
+ }
652
+ }
653
+
654
+ RCT_EXPORT_METHOD(setDomainUserId:
655
+ (NSDictionary *)details
656
+ resolver:(RCTPromiseResolveBlock)resolve
657
+ rejecter:(RCTPromiseRejectBlock)reject) {
658
+ NSString *namespace = [details objectForKey:@"tracker"];
659
+ id<SPTrackerController> trackerController = [SPSnowplow trackerByNamespace:namespace];
660
+
661
+ if (trackerController != nil) {
662
+ NSString *newDuid = [details sp_stringForKey:@"domainUserId" defaultValue:nil];
663
+ [trackerController.subject setDomainUserId:newDuid];
664
+ resolve(@YES);
665
+ } else {
666
+ NSError* error = [NSError errorWithDomain:@"SnowplowTracker" code:200 userInfo:nil];
667
+ reject(@"ERROR", @"tracker with given namespace not found", error);
668
+ }
669
+ }
670
+
671
+ RCT_EXPORT_METHOD(setIpAddress:
672
+ (NSDictionary *)details
673
+ resolver:(RCTPromiseResolveBlock)resolve
674
+ rejecter:(RCTPromiseRejectBlock)reject) {
675
+ NSString *namespace = [details objectForKey:@"tracker"];
676
+ id<SPTrackerController> trackerController = [SPSnowplow trackerByNamespace:namespace];
677
+
678
+ if (trackerController != nil) {
679
+ NSString *newIp = [details sp_stringForKey:@"ipAddress" defaultValue:nil];
680
+ [trackerController.subject setIpAddress:newIp];
681
+ resolve(@YES);
682
+ } else {
683
+ NSError* error = [NSError errorWithDomain:@"SnowplowTracker" code:200 userInfo:nil];
684
+ reject(@"ERROR", @"tracker with given namespace not found", error);
685
+ }
686
+ }
209
687
 
210
- [self.tracker trackScreenViewEvent:screenViewEvent];
688
+ RCT_EXPORT_METHOD(setUseragent:
689
+ (NSDictionary *)details
690
+ resolver:(RCTPromiseResolveBlock)resolve
691
+ rejecter:(RCTPromiseRejectBlock)reject) {
692
+ NSString *namespace = [details objectForKey:@"tracker"];
693
+ id<SPTrackerController> trackerController = [SPSnowplow trackerByNamespace:namespace];
694
+
695
+ if (trackerController != nil) {
696
+ NSString *newUagent = [details sp_stringForKey:@"useragent" defaultValue:nil];
697
+ [trackerController.subject setUseragent:newUagent];
698
+ resolve(@YES);
699
+ } else {
700
+ NSError* error = [NSError errorWithDomain:@"SnowplowTracker" code:200 userInfo:nil];
701
+ reject(@"ERROR", @"tracker with given namespace not found", error);
702
+ }
211
703
  }
212
704
 
213
- RCT_EXPORT_METHOD(trackPageViewEvent
214
- :(NSDictionary *)details
215
- :(NSArray<SPSelfDescribingJson *> *)contexts) {
705
+ RCT_EXPORT_METHOD(setTimezone:
706
+ (NSDictionary *)details
707
+ resolver:(RCTPromiseResolveBlock)resolve
708
+ rejecter:(RCTPromiseRejectBlock)reject) {
709
+ NSString *namespace = [details objectForKey:@"tracker"];
710
+ id<SPTrackerController> trackerController = [SPSnowplow trackerByNamespace:namespace];
216
711
 
217
- SPPageView * pageViewEvent = [SPPageView build:^(id<SPPageViewBuilder> builder) {
218
- [builder setPageUrl:details[@"pageUrl"]];
219
- if (details[@"pageTitle"] != nil) [builder setPageTitle:details[@"pageTitle"]];
220
- if (details[@"pageReferrer"] != nil) [builder setReferrer:details[@"pageReferrer"]];
221
- if (contexts) {
222
- [builder setContexts:[[NSMutableArray alloc] initWithArray:contexts]];
712
+ if (trackerController != nil) {
713
+ NSString *newTz = [details sp_stringForKey:@"timezone" defaultValue:nil];
714
+ [trackerController.subject setTimezone:newTz];
715
+ resolve(@YES);
716
+ } else {
717
+ NSError* error = [NSError errorWithDomain:@"SnowplowTracker" code:200 userInfo:nil];
718
+ reject(@"ERROR", @"tracker with given namespace not found", error);
719
+ }
720
+ }
721
+
722
+ RCT_EXPORT_METHOD(setLanguage:
723
+ (NSDictionary *)details
724
+ resolver:(RCTPromiseResolveBlock)resolve
725
+ rejecter:(RCTPromiseRejectBlock)reject) {
726
+ NSString *namespace = [details objectForKey:@"tracker"];
727
+ id<SPTrackerController> trackerController = [SPSnowplow trackerByNamespace:namespace];
728
+
729
+ if (trackerController != nil) {
730
+ NSString *newLang = [details sp_stringForKey:@"language" defaultValue:nil];
731
+ [trackerController.subject setLanguage:newLang];
732
+ resolve(@YES);
733
+ } else {
734
+ NSError* error = [NSError errorWithDomain:@"SnowplowTracker" code:200 userInfo:nil];
735
+ reject(@"ERROR", @"tracker with given namespace not found", error);
736
+ }
737
+ }
738
+
739
+ RCT_EXPORT_METHOD(setScreenResolution:
740
+ (NSDictionary *)details
741
+ resolver:(RCTPromiseResolveBlock)resolve
742
+ rejecter:(RCTPromiseRejectBlock)reject) {
743
+ NSString *namespace = [details objectForKey:@"tracker"];
744
+ id<SPTrackerController> trackerController = [SPSnowplow trackerByNamespace:namespace];
745
+
746
+ if (trackerController != nil) {
747
+ NSObject *newRes = [details objectForKey:@"screenResolution"];
748
+ if (newRes == [NSNull null]) {
749
+ [trackerController.subject setScreenResolution:nil];
750
+ } else {
751
+ NSNumber *resWidth = [(NSArray *)newRes objectAtIndex:0];
752
+ NSNumber *resHeight = [(NSArray *)newRes objectAtIndex:1];
753
+ SPSize *resSize = [[SPSize alloc] initWithWidth:[resWidth integerValue] height:[resHeight integerValue]];
754
+ [trackerController.subject setScreenResolution:resSize];
223
755
  }
224
- }];
225
- [self.tracker trackPageViewEvent:pageViewEvent];
756
+ resolve(@YES);
757
+ } else {
758
+ NSError* error = [NSError errorWithDomain:@"SnowplowTracker" code:200 userInfo:nil];
759
+ reject(@"ERROR", @"tracker with given namespace not found", error);
760
+ }
761
+ }
762
+
763
+ RCT_EXPORT_METHOD(setScreenViewport:
764
+ (NSDictionary *)details
765
+ resolver:(RCTPromiseResolveBlock)resolve
766
+ rejecter:(RCTPromiseRejectBlock)reject) {
767
+ NSString *namespace = [details objectForKey:@"tracker"];
768
+ id<SPTrackerController> trackerController = [SPSnowplow trackerByNamespace:namespace];
769
+
770
+ if (trackerController != nil) {
771
+ NSObject *newView = [details objectForKey:@"screenViewport"];
772
+ if (newView == [NSNull null]) {
773
+ [trackerController.subject setScreenViewPort:nil];
774
+ } else {
775
+ NSNumber *vpWidth = [(NSArray *)newView objectAtIndex:0];
776
+ NSNumber *vpHeight = [(NSArray *)newView objectAtIndex:1];
777
+ SPSize *vpSize = [[SPSize alloc] initWithWidth:[vpWidth integerValue] height:[vpHeight integerValue]];
778
+ [trackerController.subject setScreenViewPort:vpSize];
779
+ }
780
+ resolve(@YES);
781
+ } else {
782
+ NSError* error = [NSError errorWithDomain:@"SnowplowTracker" code:200 userInfo:nil];
783
+ reject(@"ERROR", @"tracker with given namespace not found", error);
784
+ }
785
+ }
786
+
787
+ RCT_EXPORT_METHOD(setColorDepth:
788
+ (NSDictionary *)details
789
+ resolver:(RCTPromiseResolveBlock)resolve
790
+ rejecter:(RCTPromiseRejectBlock)reject) {
791
+ NSString *namespace = [details objectForKey:@"tracker"];
792
+ id<SPTrackerController> trackerController = [SPSnowplow trackerByNamespace:namespace];
793
+
794
+ if (trackerController != nil) {
795
+ NSNumber *newColorD = [details sp_numberForKey:@"colorDepth" defaultValue:nil];
796
+ [trackerController.subject setColorDepth:newColorD];
797
+ resolve(@YES);
798
+ } else {
799
+ NSError* error = [NSError errorWithDomain:@"SnowplowTracker" code:200 userInfo:nil];
800
+ reject(@"ERROR", @"tracker with given namespace not found", error);
801
+ }
802
+ }
803
+
804
+ RCT_EXPORT_METHOD(getSessionUserId:
805
+ (NSDictionary *)details
806
+ resolver:(RCTPromiseResolveBlock)resolve
807
+ rejecter:(RCTPromiseRejectBlock)reject) {
808
+ NSString *namespace = [details objectForKey:@"tracker"];
809
+ id<SPTrackerController> trackerController = [SPSnowplow trackerByNamespace:namespace];
810
+
811
+ if (trackerController != nil) {
812
+ NSString *suid = [trackerController.session userId];
813
+ resolve(suid);
814
+ } else {
815
+ NSError* error = [NSError errorWithDomain:@"SnowplowTracker" code:200 userInfo:nil];
816
+ reject(@"ERROR", @"tracker with given namespace not found", error);
817
+ }
818
+ }
819
+
820
+ RCT_EXPORT_METHOD(getSessionId:
821
+ (NSDictionary *)details
822
+ resolver:(RCTPromiseResolveBlock)resolve
823
+ rejecter:(RCTPromiseRejectBlock)reject) {
824
+ NSString *namespace = [details objectForKey:@"tracker"];
825
+ id<SPTrackerController> trackerController = [SPSnowplow trackerByNamespace:namespace];
826
+
827
+ if (trackerController != nil) {
828
+ NSString *sid = [trackerController.session sessionId];
829
+ resolve(sid);
830
+ } else {
831
+ NSError* error = [NSError errorWithDomain:@"SnowplowTracker" code:200 userInfo:nil];
832
+ reject(@"ERROR", @"tracker with given namespace not found", error);
833
+ }
834
+ }
835
+
836
+ RCT_EXPORT_METHOD(getSessionIndex:
837
+ (NSDictionary *)details
838
+ resolver:(RCTPromiseResolveBlock)resolve
839
+ rejecter:(RCTPromiseRejectBlock)reject) {
840
+ NSString *namespace = [details objectForKey:@"tracker"];
841
+ id<SPTrackerController> trackerController = [SPSnowplow trackerByNamespace:namespace];
842
+
843
+ if (trackerController != nil) {
844
+ NSInteger sidx = [trackerController.session sessionIndex];
845
+ resolve(@(sidx));
846
+ } else {
847
+ NSError* error = [NSError errorWithDomain:@"SnowplowTracker" code:200 userInfo:nil];
848
+ reject(@"ERROR", @"tracker with given namespace not found", error);
849
+ }
850
+ }
851
+
852
+ RCT_EXPORT_METHOD(getIsInBackground:
853
+ (NSDictionary *)details
854
+ resolver:(RCTPromiseResolveBlock)resolve
855
+ rejecter:(RCTPromiseRejectBlock)reject) {
856
+ NSString *namespace = [details objectForKey:@"tracker"];
857
+ id<SPTrackerController> trackerController = [SPSnowplow trackerByNamespace:namespace];
858
+
859
+ if (trackerController != nil) {
860
+ BOOL isInBg = [trackerController.session isInBackground];
861
+ resolve(@(isInBg));
862
+ } else {
863
+ NSError* error = [NSError errorWithDomain:@"SnowplowTracker" code:200 userInfo:nil];
864
+ reject(@"ERROR", @"tracker with given namespace not found", error);
865
+ }
866
+ }
867
+
868
+ RCT_EXPORT_METHOD(getBackgroundIndex:
869
+ (NSDictionary *)details
870
+ resolver:(RCTPromiseResolveBlock)resolve
871
+ rejecter:(RCTPromiseRejectBlock)reject) {
872
+ NSString *namespace = [details objectForKey:@"tracker"];
873
+ id<SPTrackerController> trackerController = [SPSnowplow trackerByNamespace:namespace];
874
+
875
+ if (trackerController != nil) {
876
+ NSInteger bgIdx = [trackerController.session backgroundIndex];
877
+ resolve(@(bgIdx));
878
+ } else {
879
+ NSError* error = [NSError errorWithDomain:@"SnowplowTracker" code:200 userInfo:nil];
880
+ reject(@"ERROR", @"tracker with given namespace not found", error);
881
+ }
882
+ }
883
+
884
+ RCT_EXPORT_METHOD(getForegroundIndex:
885
+ (NSDictionary *)details
886
+ resolver:(RCTPromiseResolveBlock)resolve
887
+ rejecter:(RCTPromiseRejectBlock)reject) {
888
+ NSString *namespace = [details objectForKey:@"tracker"];
889
+ id<SPTrackerController> trackerController = [SPSnowplow trackerByNamespace:namespace];
890
+
891
+ if (trackerController != nil) {
892
+ NSInteger fgIdx = [trackerController.session foregroundIndex];
893
+ resolve(@(fgIdx));
894
+ } else {
895
+ NSError* error = [NSError errorWithDomain:@"SnowplowTracker" code:200 userInfo:nil];
896
+ reject(@"ERROR", @"tracker with given namespace not found", error);
897
+ }
226
898
  }
227
899
 
228
900
  @end