-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathBatchProfileBridge.m
274 lines (240 loc) · 10.6 KB
/
BatchProfileBridge.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
#import "BatchProfileBridge.h"
#import "BatchBridgeUtils.h"
#import <Batch/BatchUser.h>
#define INVALID_PARAMETER @"Invalid parameter."
@implementation BatchProfileBridge
+ (void)identify:(NSDictionary*)params
{
[BatchProfile identify:[BatchBridgeUtils nullableString:params forKey:@"custom_user_id"]];
}
+ (void)editAttributes:(NSDictionary*)params
{
NSArray *operations = [params objectForKey:@"operations"];
if (!operations)
{
[NSException raise:INVALID_PARAMETER format:@"Couldn't find operations for user.edit"];
return;
}
BatchProfileEditor *editor = [BatchProfile editor];
for (NSDictionary *operationDescription in operations)
{
if (![operationDescription isKindOfClass:[NSDictionary class]])
{
continue;
}
NSString *operationName = [operationDescription objectForKey:@"operation"];
if (![operationName isKindOfClass:[NSString class]])
{
continue;
}
if ([@"SET_LANGUAGE" isEqualToString:operationName])
{
[editor setLanguage:[BatchBridgeUtils nullableString:operationDescription forKey:@"value"] error:nil];
}
else if ([@"SET_REGION" isEqualToString:operationName])
{
[editor setRegion:[BatchBridgeUtils nullableString:operationDescription forKey:@"value"] error:nil];
}
else if([@"SET_EMAIL_ADDRESS" isEqualToString:operationName])
{
[editor setEmailAddress:[BatchBridgeUtils nullableString:operationDescription forKey:@"value"] error:nil];
}
else if([@"SET_EMAIL_MARKETING_SUB" isEqualToString:operationName]) {
NSString* value = [operationDescription objectForKey:@"value"];
if([[value uppercaseString] isEqualToString:@"SUBSCRIBED"]) {
[editor setEmailMarketingSubscriptionState:BatchEmailSubscriptionStateSubscribed];
} else if ([[value uppercaseString] isEqualToString:@"UNSUBSCRIBED"]) {
[editor setEmailMarketingSubscriptionState: BatchEmailSubscriptionStateUnsubscribed];
} else {
NSLog(@"Batch Bridge - Invalid value for email marketing subscription state. Must be `subscribed` or `unsubscribed`.");
}
}
else if([@"SET_PHONE_NUMBER" isEqualToString:operationName])
{
[editor setPhoneNumber:[BatchBridgeUtils nullableString:operationDescription forKey:@"value"] error:nil];
}
else if([@"SET_SMS_MARKETING_SUB" isEqualToString:operationName]) {
NSString* value = [operationDescription objectForKey:@"value"];
if([[value uppercaseString] isEqualToString:@"SUBSCRIBED"]) {
[editor setSMSMarketingSubscriptionState:BatchSMSSubscriptionStateSubscribed];
} else if ([[value uppercaseString] isEqualToString:@"UNSUBSCRIBED"]) {
[editor setSMSMarketingSubscriptionState: BatchSMSSubscriptionStateUnsubscribed];
} else {
NSLog(@"Batch Bridge - Invalid value for SMS marketing subscription state. Must be `subscribed` or `unsubscribed`.");
}
}
else if ([@"SET_ATTRIBUTE" isEqualToString:operationName])
{
NSString *type = operationDescription[@"type"];
if (![type isKindOfClass:[NSString class]])
{
continue;
}
NSString *key = operationDescription[@"key"];
if ([@"string" isEqualToString:type])
{
[editor setStringAttribute:operationDescription[@"value"] forKey:key error:nil];
}
else if ([@"url" isEqualToString:type])
{
NSString *absoluteURL = operationDescription[@"value"];
if (![absoluteURL isKindOfClass:[NSString class]]) {
NSLog(@"Batch Bridge - %@", @"Error while setting URL attribute: invalid internal value");
continue;
}
NSURL *urlValue = [NSURL URLWithString:absoluteURL];
if (urlValue == nil) {
NSLog(@"Batch Bridge - Could not set URL for attribute '%@': the URL is not valid", key);
continue;
}
[editor setURLAttribute:urlValue forKey:key error:nil];
}
else if ([@"date" isEqualToString:type])
{
double dateValue = [operationDescription[@"value"] doubleValue]/1000;
[editor setDateAttribute:[NSDate dateWithTimeIntervalSince1970:dateValue] forKey:key error:nil];
}
else if ([@"integer" isEqualToString:type] || [@"float" isEqualToString:type] || [@"boolean" isEqualToString:type])
{
NSNumber *numberValue = operationDescription[@"value"];
if (!numberValue)
{
// NaN or Infinity
continue;
}
if ([numberValue isKindOfClass:[NSNumber class]] || [numberValue isKindOfClass:[NSString class]])
{
if ([@"float" isEqualToString:type])
{
[editor setFloatAttribute:[numberValue doubleValue] forKey:key error:nil];
}
else if ([@"boolean" isEqualToString:type])
{
[editor setBooleanAttribute:[numberValue boolValue] forKey:key error:nil];
}
else
{
[editor setLongLongAttribute:[numberValue longLongValue] forKey:key error:nil];
}
}
else
{
NSLog(@"Batch Bridge - %@", @"Error while reading SET_ATTRIBUTE integer value: must be a string or a number");
continue;
}
}
else if ([@"array" isEqualToString:type])
{
[editor setStringArrayAttribute:operationDescription[@"value"] forKey:key error:nil];
}
else
{
NSLog(@"Batch Bridge - %@", @"Error while reading SET_ATTRIBUTE value: unknown type");
}
}
else if ([@"REMOVE_ATTRIBUTE" isEqualToString:operationName])
{
[editor removeAttributeForKey:operationDescription[@"key"] error:nil];
}
else if ([@"ADD_TO_ARRAY" isEqualToString:operationName])
{
NSString* key = operationDescription[@"key"] ;
id value = operationDescription[@"value"];
if ([value isKindOfClass:[NSString class]]) {
[editor addItemToStringArrayAttribute:value forKey:key error:nil];
} else if ([value isKindOfClass:[NSArray class]]) {
for (NSString *item in value) {
[editor addItemToStringArrayAttribute:item forKey:key error:nil];
}
}
}
else if ([@"REMOVE_FROM_ARRAY" isEqualToString:operationName])
{
NSString* key = operationDescription[@"key"] ;
id value = operationDescription[@"value"];
if ([value isKindOfClass:[NSString class]]) {
[editor removeItemFromStringArrayAttribute:value forKey:key error:nil];
} else if ([value isKindOfClass:[NSArray class]]) {
for (NSString *item in value) {
[editor removeItemFromStringArrayAttribute:item forKey:key error:nil];
}
}
}
}
[editor save];
}
+ (NSString*)trackEvent:(NSDictionary*)params
{
BACSimplePromise<NSObject*>* result = [BACSimplePromise new];
if (!params || [params count]==0)
{
[NSException raise:INVALID_PARAMETER format:@"Empty or null parameters for user.track.event"];
}
NSString *name = params[@"name"];
NSDictionary *data = params[@"event_data"];
if (![name isKindOfClass:[NSString class]])
{
[NSException raise:INVALID_PARAMETER format:@"name should be a string"];
}
BatchEventAttributes *batchEventAttributes = nil;
if ([data isKindOfClass:[NSDictionary class]]) {
batchEventAttributes = [BatchBridgeUtils convertSerializedEventDataToEventAttributes:data];
NSError *err;
[batchEventAttributes validateWithError:&err];
if (batchEventAttributes != nil && err == nil) {
[BatchProfile trackEventWithName:name attributes:batchEventAttributes];
return nil;
} else {
NSLog(@"Batch Bridge - Event attributes validation failed with errors: %@", err);
return err.description;
}
}
[BatchProfile trackEventWithName:name attributes:batchEventAttributes];
return nil;
}
+ (void)trackLocation:(NSDictionary*)params
{
if (!params || [params count]==0)
{
[NSException raise:INVALID_PARAMETER format:@"Empty or null parameters for action user.track.location"];
}
NSNumber *latitude = params[@"latitude"];
NSNumber *longitude = params[@"longitude"];
NSNumber *date = params[@"date"]; // MS
NSNumber *precision = params[@"precision"];
if (![latitude isKindOfClass:[NSNumber class]])
{
[NSException raise:INVALID_PARAMETER format:@"latitude should be a string"];
}
if (![longitude isKindOfClass:[NSNumber class]])
{
[NSException raise:INVALID_PARAMETER format:@"longitude should be a string"];
}
NSTimeInterval ts = 0;
if (date)
{
if ([date isKindOfClass:[NSNumber class]]) {
ts = [date doubleValue] / 1000.0;
} else {
[NSException raise:INVALID_PARAMETER format:@"date should be an object or undefined"];
}
}
NSDate *parsedDate = ts != 0 ? [NSDate dateWithTimeIntervalSince1970:ts] : [NSDate date];
NSInteger parsedPrecision = 0;
if (precision)
{
if ([precision isKindOfClass:[NSNumber class]]) {
parsedPrecision = [precision integerValue];
} else {
[NSException raise:INVALID_PARAMETER format:@"precision should be an object or undefined"];
}
}
[BatchProfile trackLocation:[[CLLocation alloc] initWithCoordinate:CLLocationCoordinate2DMake([latitude doubleValue], [longitude doubleValue])
altitude:0
horizontalAccuracy:parsedPrecision
verticalAccuracy:-1
course:0
speed:0
timestamp:parsedDate]];
}
@end