@@ -104,9 +104,9 @@ - (id)initWithConfiguration:(SEGAnalyticsConfiguration *)configuration {
104
104
105
105
- (NSDictionary *)staticContext {
106
106
NSMutableDictionary *dict = [[NSMutableDictionary alloc ] init ];
107
-
107
+
108
108
dict[@" library" ] = @{ @" name" : @" analytics-ios" , @" version" : SEGStringize (ANALYTICS_VERSION) };
109
-
109
+
110
110
NSDictionary *infoDictionary = [[NSBundle mainBundle ] infoDictionary ];
111
111
if (infoDictionary.count ) {
112
112
dict[@" app" ] = @{
@@ -116,9 +116,9 @@ - (NSDictionary *)staticContext {
116
116
@" namespace" : [[NSBundle mainBundle ] bundleIdentifier ] ?: @" " ,
117
117
};
118
118
}
119
-
119
+
120
120
UIDevice *device = [UIDevice currentDevice ];
121
-
121
+
122
122
dict[@" device" ] = ({
123
123
NSMutableDictionary *dict = [[NSMutableDictionary alloc ] init ];
124
124
dict[@" manufacturer" ] = @" Apple" ;
@@ -133,22 +133,22 @@ - (NSDictionary *)staticContext {
133
133
}
134
134
dict;
135
135
});
136
-
136
+
137
137
dict[@" os" ] = @{
138
138
@" name" : device.systemName ,
139
139
@" version" : device.systemVersion
140
140
};
141
-
141
+
142
142
CTCarrier *carrier = [[[CTTelephonyNetworkInfo alloc ] init ] subscriberCellularProvider ];
143
143
if (carrier.carrierName .length )
144
144
dict[@" network" ] = @{ @" carrier" : carrier.carrierName };
145
-
145
+
146
146
CGSize screenSize = [UIScreen mainScreen ].bounds .size ;
147
147
dict[@" screen" ] = @{
148
148
@" width" : @(screenSize.width ),
149
149
@" height" : @(screenSize.height )
150
150
};
151
-
151
+
152
152
#if !(TARGET_IPHONE_SIMULATOR)
153
153
Class adClient = NSClassFromString (SEGADClientClass);
154
154
if (adClient) {
@@ -167,48 +167,48 @@ - (NSDictionary *)staticContext {
167
167
#pragma clang diagnostic pop
168
168
}
169
169
#endif
170
-
170
+
171
171
return dict;
172
172
}
173
173
174
174
- (NSMutableDictionary *)liveContext {
175
175
NSMutableDictionary *context = [[NSMutableDictionary alloc ] init ];
176
-
176
+
177
177
[context addEntriesFromDictionary: self .context];
178
-
178
+
179
179
context[@" locale" ] = [NSString stringWithFormat:
180
180
@" %@ -%@ " ,
181
181
[NSLocale .currentLocale objectForKey: NSLocaleLanguageCode ],
182
182
[NSLocale .currentLocale objectForKey: NSLocaleCountryCode ]];
183
-
183
+
184
184
context[@" timezone" ] = [[NSTimeZone localTimeZone ] name ];
185
-
185
+
186
186
context[@" network" ] = ({
187
187
NSMutableDictionary *network = [[NSMutableDictionary alloc ] init ];
188
-
188
+
189
189
if (self.bluetooth .hasKnownState )
190
190
network[@" bluetooth" ] = @(self.bluetooth .isEnabled );
191
-
191
+
192
192
if (self.reachability .isReachable ){
193
193
network[@" wifi" ] = @(self.reachability .isReachableViaWiFi );
194
194
network[@" cellular" ] = @(self.reachability .isReachableViaWWAN );
195
195
}
196
-
196
+
197
197
network;
198
198
});
199
-
199
+
200
200
if (self.location .hasKnownLocation )
201
201
context[@" location" ] = self.location .locationDictionary ;
202
-
202
+
203
203
context[@" traits" ] = ({
204
204
NSMutableDictionary *traits = [[NSMutableDictionary alloc ] initWithDictionary: [self traits ]];
205
-
205
+
206
206
if (self.location .hasKnownLocation )
207
207
traits[@" address" ] = self.location .addressDictionary ;
208
-
208
+
209
209
traits;
210
210
});
211
-
211
+
212
212
return context;
213
213
}
214
214
@@ -222,7 +222,7 @@ - (void)dispatchBackgroundAndWait:(void(^)(void))block {
222
222
223
223
- (void )beginBackgroundTask {
224
224
[self endBackgroundTask ];
225
-
225
+
226
226
self.flushTaskID = [[UIApplication sharedApplication ] beginBackgroundTaskWithExpirationHandler: ^{
227
227
[self endBackgroundTask ];
228
228
}];
@@ -266,13 +266,13 @@ - (void)identify:(NSString *)userId traits:(NSDictionary *)traits options:(NSDic
266
266
[self saveUserId: userId];
267
267
[self addTraits: traits];
268
268
}];
269
-
269
+
270
270
[self enqueueAction: @" identify" dictionary: @{ @" traits" : traits } options: options];
271
271
}
272
272
273
273
- (void )track : (NSString *)event properties : (NSDictionary *)properties options : (NSDictionary *)options {
274
274
NSCParameterAssert (event.length > 0 );
275
-
275
+
276
276
NSMutableDictionary *dictionary = [NSMutableDictionary dictionary ];
277
277
[dictionary setValue: event forKey: @" event" ];
278
278
[dictionary setValue: properties forKey: @" properties" ];
@@ -282,21 +282,21 @@ - (void)track:(NSString *)event properties:(NSDictionary *)properties options:(N
282
282
283
283
- (void )screen : (NSString *)screenTitle properties : (NSDictionary *)properties options : (NSDictionary *)options {
284
284
NSCParameterAssert (screenTitle.length > 0 );
285
-
285
+
286
286
NSMutableDictionary *dictionary = [NSMutableDictionary dictionary ];
287
287
[dictionary setValue: screenTitle forKey: @" name" ];
288
288
[dictionary setValue: properties forKey: @" properties" ];
289
-
289
+
290
290
[self enqueueAction: @" screen" dictionary: dictionary options: options];
291
291
}
292
292
293
293
- (void )group : (NSString *)groupId traits : (NSDictionary *)traits options : (NSDictionary *)options {
294
294
NSCParameterAssert (groupId.length > 0 );
295
-
295
+
296
296
NSMutableDictionary *dictionary = [NSMutableDictionary dictionary ];
297
297
[dictionary setValue: groupId forKey: @" groupId" ];
298
298
[dictionary setValue: traits forKey: @" traits" ];
299
-
299
+
300
300
[self enqueueAction: @" group" dictionary: dictionary options: options];
301
301
}
302
302
@@ -312,7 +312,7 @@ - (void)alias:(NSString *)newId options:(NSDictionary *)options {
312
312
313
313
- (void )registerForRemoteNotificationsWithDeviceToken : (NSData *)deviceToken options : (NSDictionary *)options {
314
314
NSCParameterAssert (deviceToken != nil );
315
-
315
+
316
316
const unsigned char *buffer = (const unsigned char *)[deviceToken bytes ];
317
317
if (!buffer) {
318
318
return ;
@@ -343,7 +343,7 @@ - (void)enqueueAction:(NSString *)action dictionary:(NSDictionary *)dictionary o
343
343
payload[@" type" ] = action;
344
344
payload[@" timestamp" ] = [[NSDate date ] description ];
345
345
payload[@" messageId" ] = GenerateUUIDString ();
346
-
346
+
347
347
[self dispatchBackground: ^{
348
348
// attach userId and anonymousId inside the dispatch_async in case
349
349
// they've changed (see identify function)
@@ -358,7 +358,7 @@ - (void)enqueueAction:(NSString *)action dictionary:(NSDictionary *)dictionary o
358
358
NSMutableDictionary *context = [NSMutableDictionary dictionaryWithCapacity: capacity];
359
359
[context addEntriesFromDictionary: defaultContext];
360
360
[context addEntriesFromDictionary: customContext]; // let the custom context override ours
361
- [payload setValue: [self liveContext ] forKey: @" context" ];
361
+ [payload setValue: [self context ] forKey: @" context" ];
362
362
363
363
SEGLog (@" %@ Enqueueing action: %@ " , self, payload);
364
364
[self queuePayload: payload];
@@ -388,17 +388,17 @@ - (void)flushWithMaxSize:(NSUInteger)maxBatchSize {
388
388
} else {
389
389
self.batch = [NSArray arrayWithArray: self .queue];
390
390
}
391
-
391
+
392
392
SEGLog (@" %@ Flushing %lu of %lu queued API calls." , self, (unsigned long )self.batch .count , (unsigned long )self.queue .count );
393
-
393
+
394
394
NSMutableDictionary *payloadDictionary = [NSMutableDictionary dictionary ];
395
395
[payloadDictionary setObject: self .configuration.writeKey forKey: @" writeKey" ];
396
396
[payloadDictionary setObject: [[NSDate date ] description ] forKey: @" sentAt" ];
397
397
[payloadDictionary setObject: self .context forKey: @" context" ];
398
398
[payloadDictionary setObject: self .batch forKey: @" batch" ];
399
-
399
+
400
400
SEGLog (@" Flushing payload %@ " , payloadDictionary);
401
-
401
+
402
402
NSError *error = nil ;
403
403
NSException *exception = nil ;
404
404
NSData *payload = nil ;
@@ -419,7 +419,7 @@ - (void)flushWithMaxSize:(NSUInteger)maxBatchSize {
419
419
- (void )flushQueueByLength {
420
420
[self dispatchBackground: ^{
421
421
SEGLog (@" %@ Length is %lu ." , self, (unsigned long )self.queue .count );
422
-
422
+
423
423
if (self.request == nil && [self .queue count ] >= self.configuration .flushAt ) {
424
424
[self flush ];
425
425
}
@@ -452,7 +452,7 @@ - (void)sendData:(NSData *)data {
452
452
[urlRequest setValue: @" application/json" forHTTPHeaderField: @" Content-Type" ];
453
453
[urlRequest setHTTPMethod: @" POST" ];
454
454
[urlRequest setHTTPBody: data];
455
-
455
+
456
456
SEGLog (@" %@ Sending batch API request." , self);
457
457
self.request = [SEGAnalyticsRequest startWithURLRequest: urlRequest completion: ^{
458
458
[self dispatchBackground: ^{
@@ -466,7 +466,7 @@ - (void)sendData:(NSData *)data {
466
466
[self .queue writeToURL: [self queueURL ] atomically: YES ];
467
467
[self notifyForName: SEGSegmentioRequestDidSucceedNotification userInfo: self .batch];
468
468
}
469
-
469
+
470
470
self.batch = nil ;
471
471
self.request = nil ;
472
472
[self endBackgroundTask ];
@@ -532,7 +532,7 @@ - (void)setConfiguration:(SEGAnalyticsConfiguration *)configuration {
532
532
[self .configuration removeObserver: self forKeyPath: @" shouldUseLocationServices" ];
533
533
[self .configuration removeObserver: self forKeyPath: @" enableAdvertisingTracking" ];
534
534
}
535
-
535
+
536
536
[super setConfiguration: configuration];
537
537
[self .configuration addObserver: self forKeyPath: @" shouldUseLocationServices" options: NSKeyValueObservingOptionInitial|NSKeyValueObservingOptionNew context: NULL ];
538
538
[self .configuration addObserver: self forKeyPath: @" enableAdvertisingTracking" options: NSKeyValueObservingOptionInitial|NSKeyValueObservingOptionNew context: NULL ];
0 commit comments