-
Notifications
You must be signed in to change notification settings - Fork 242
Expand file tree
/
Copy pathCountlyServerConfig.m
More file actions
532 lines (452 loc) · 18.7 KB
/
CountlyServerConfig.m
File metadata and controls
532 lines (452 loc) · 18.7 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
// CountlyServerConfig.m
//
// This code is provided under the MIT License.
//
// Please visit www.count.ly for more information.
#import "CountlyCommon.h"
@interface CountlyServerConfig () {
NSTimer *_requestTimer;
}
@property (nonatomic) BOOL trackingEnabled;
@property (nonatomic) BOOL networkingEnabled;
@property (nonatomic) BOOL crashReportingEnabled;
@property (nonatomic) BOOL loggingEnabled;
@property (nonatomic) BOOL customEventTrackingEnabled;
@property (nonatomic) BOOL viewTrackingEnabled;
@property (nonatomic) BOOL sessionTrackingEnabled;
@property (nonatomic) BOOL enterContentZone;
@property (nonatomic) BOOL consentRequired;
@property (nonatomic) BOOL locationTracking;
@property (nonatomic) BOOL refreshContentZone;
@property (nonatomic) NSInteger limitKeyLength;
@property (nonatomic) NSInteger limitValueSize;
@property (nonatomic) NSInteger limitSegValues;
@property (nonatomic) NSInteger limitBreadcrumb;
@property (nonatomic) NSInteger limitTraceLine;
@property (nonatomic) NSInteger limitTraceLength;
@property (nonatomic) NSInteger sessionInterval;
@property (nonatomic) NSInteger eventQueueSize;
@property (nonatomic) NSInteger requestQueueSize;
@property (nonatomic) NSInteger contentZoneInterval;
@property (nonatomic) NSInteger dropOldRequestTime;
@property (nonatomic) NSInteger serverConfigUpdateInterval;
@property (nonatomic) NSInteger currentServerConfigUpdateInterval;
@property (nonatomic) NSInteger version;
@property (nonatomic) long long timestamp;
@property (nonatomic) long long lastFetchTimestamp;
@property (nonatomic) BOOL serverConfigDisabled;
@end
NSString *const kCountlySCKeySC = @"sc";
NSString *const kTracking = @"tracking";
NSString *const kNetworking = @"networking";
// request keys
NSString *const kRTimestamp = @"t";
NSString *const kRVersion = @"v";
NSString *const kRConfig = @"c";
NSString *const kRReqQueueSize = @"rqs";
NSString *const kREventQueueSize = @"eqs";
NSString *const kRLogging = @"log";
NSString *const kRSessionUpdateInterval = @"sui";
NSString *const kRSessionTracking = @"st";
NSString *const kRViewTracking = @"vt";
NSString *const kRLocationTracking = @"lt";
NSString *const kRRefreshContentZone = @"rcz";
NSString *const kRLimitKeyLength = @"lkl";
NSString *const kRLimitValueSize = @"lvs";
NSString *const kRLimitSegValues = @"lsv";
NSString *const kRLimitBreadcrumb = @"lbc";
NSString *const kRLimitTraceLine = @"ltlpt";
NSString *const kRLimitTraceLength = @"ltl";
NSString *const kRCustomEventTracking = @"cet";
NSString *const kREnterContentZone = @"ecz";
NSString *const kRContentZoneInterval = @"czi";
NSString *const kRConsentRequired = @"cr";
NSString *const kRDropOldRequestTime = @"dort";
NSString *const kRCrashReporting = @"crt";
NSString *const kRServerConfigUpdateInterval = @"scui";
@implementation CountlyServerConfig
+ (instancetype)sharedInstance
{
if (!CountlyCommon.sharedInstance.hasStarted)
return nil;
static CountlyServerConfig *s_sharedInstance = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
s_sharedInstance = self.new;
});
return s_sharedInstance;
}
- (instancetype)init
{
self = [super init];
if (self)
{
// Set default values
_trackingEnabled = YES;
_networkingEnabled = YES;
_crashReportingEnabled = YES;
_customEventTrackingEnabled = YES;
_enterContentZone = NO;
_locationTracking = YES;
_viewTrackingEnabled = YES;
_sessionTrackingEnabled = YES;
_loggingEnabled = NO;
_refreshContentZone = YES;
_timestamp = 0;
_version = 0;
_currentServerConfigUpdateInterval = 4;
_requestTimer = nil;
_serverConfigDisabled = NO;
}
return self;
}
- (void)retrieveServerConfigFromStorage:(NSString *)sdkBehaviorSettings
{
if (_serverConfigDisabled) {
return;
}
NSError *error = nil;
NSDictionary *serverConfigObject = [CountlyPersistency.sharedInstance retrieveServerConfig];
if (serverConfigObject.count == 0 && sdkBehaviorSettings)
{
serverConfigObject = [NSJSONSerialization JSONObjectWithData:[sdkBehaviorSettings cly_dataUTF8] options:0 error:&error];
}
if (serverConfigObject.count > 0 && !error)
{
[self populateServerConfig:serverConfigObject];
}
}
- (void)setBoolProperty:(BOOL *)property fromDictionary:(NSDictionary *)dictionary key:(NSString *)key logString:(NSMutableString *)logString
{
NSNumber *value = dictionary[key];
if (value)
{
*property = value.boolValue;
[logString appendFormat:@"%@: %@, ", key, *property ? @"YES" : @"NO"];
}
}
- (void)setIntegerProperty:(NSInteger *)property fromDictionary:(NSDictionary *)dictionary key:(NSString *)key logString:(NSMutableString *)logString
{
NSNumber *value = dictionary[key];
if (value)
{
*property = value.integerValue;
[logString appendFormat:@"%@: %ld, ", key, (long)*property];
}
}
- (void)populateServerConfig:(NSDictionary *)serverConfig
{
if (!serverConfig[kRConfig])
{
CLY_LOG_D(@"%s, config key is missing in the server configuration omitting", __FUNCTION__);
return;
}
NSDictionary *dictionary = serverConfig[kRConfig];
if (!serverConfig[kRVersion] || !serverConfig[kRTimestamp])
{
CLY_LOG_D(@"%s, version or timestamp is missing in the server configuration omitting", __FUNCTION__);
return;
}
_version = [serverConfig[kRVersion] integerValue];
_timestamp = [serverConfig[kRTimestamp] longLongValue];
NSMutableString *logString = [NSMutableString stringWithString:@"Server Config: "];
[self setBoolProperty:&_trackingEnabled fromDictionary:dictionary key:kTracking logString:logString];
[self setBoolProperty:&_networkingEnabled fromDictionary:dictionary key:kNetworking logString:logString];
[self setIntegerProperty:&_sessionInterval fromDictionary:dictionary key:kRSessionUpdateInterval logString:logString];
[self setIntegerProperty:&_requestQueueSize fromDictionary:dictionary key:kRReqQueueSize logString:logString];
[self setIntegerProperty:&_eventQueueSize fromDictionary:dictionary key:kREventQueueSize logString:logString];
[self setBoolProperty:&_crashReportingEnabled fromDictionary:dictionary key:kRCrashReporting logString:logString];
[self setBoolProperty:&_sessionTrackingEnabled fromDictionary:dictionary key:kRSessionTracking logString:logString];
[self setBoolProperty:&_loggingEnabled fromDictionary:dictionary key:kRLogging logString:logString];
[self setIntegerProperty:&_limitKeyLength fromDictionary:dictionary key:kRLimitKeyLength logString:logString];
[self setIntegerProperty:&_limitValueSize fromDictionary:dictionary key:kRLimitValueSize logString:logString];
[self setIntegerProperty:&_limitSegValues fromDictionary:dictionary key:kRLimitSegValues logString:logString];
[self setIntegerProperty:&_limitBreadcrumb fromDictionary:dictionary key:kRLimitBreadcrumb logString:logString];
[self setIntegerProperty:&_limitTraceLine fromDictionary:dictionary key:kRLimitTraceLine logString:logString];
[self setIntegerProperty:&_limitTraceLength fromDictionary:dictionary key:kRLimitTraceLength logString:logString];
[self setBoolProperty:&_customEventTrackingEnabled fromDictionary:dictionary key:kRCustomEventTracking logString:logString];
[self setBoolProperty:&_viewTrackingEnabled fromDictionary:dictionary key:kRViewTracking logString:logString];
[self setBoolProperty:&_enterContentZone fromDictionary:dictionary key:kREnterContentZone logString:logString];
[self setIntegerProperty:&_contentZoneInterval fromDictionary:dictionary key:kRContentZoneInterval logString:logString];
[self setBoolProperty:&_consentRequired fromDictionary:dictionary key:kRConsentRequired logString:logString];
[self setIntegerProperty:&_dropOldRequestTime fromDictionary:dictionary key:kRDropOldRequestTime logString:logString];
[self setIntegerProperty:&_serverConfigUpdateInterval fromDictionary:dictionary key:kRServerConfigUpdateInterval logString:logString];
[self setBoolProperty:&_locationTracking fromDictionary:dictionary key:kRLocationTracking logString:logString];
[self setBoolProperty:&_refreshContentZone fromDictionary:dictionary key:kRRefreshContentZone logString:logString];
CLY_LOG_D(@"%s, version:[%li], timestamp:[%lli], %@", __FUNCTION__, _version, _timestamp, logString);
}
- (void)notifySdkConfigChange:(CountlyConfig *)config
{
if (_serverConfigDisabled) {
return;
}
config.enableDebug = _loggingEnabled || config.enableDebug;
CountlyCommon.sharedInstance.enableDebug = config.enableDebug;
// Limits could be moved to another function, but letting them stay here serves us a monopolized view of notify
if (config.maxKeyLength != kCountlyMaxKeyLength)
{
[config.sdkInternalLimits setMaxKeyLength:config.maxKeyLength];
}
if (config.maxValueLength != kCountlyMaxValueSize)
{
[config.sdkInternalLimits setMaxValueSize:config.maxValueLength];
}
if (config.maxSegmentationValues != kCountlyMaxSegmentationValues)
{
[config.sdkInternalLimits setMaxSegmentationValues:config.maxSegmentationValues];
}
if (config.crashLogLimit != kCountlyMaxBreadcrumbCount)
{
[config.sdkInternalLimits setMaxBreadcrumbCount:config.crashLogLimit];
}
[config.sdkInternalLimits setMaxKeyLength:_limitKeyLength ?: config.sdkInternalLimits.getMaxKeyLength];
[config.sdkInternalLimits setMaxValueSize:_limitValueSize ?: config.sdkInternalLimits.getMaxValueSize];
[config.sdkInternalLimits setMaxSegmentationValues:_limitSegValues ?: config.sdkInternalLimits.getMaxSegmentationValues];
[config.sdkInternalLimits setMaxBreadcrumbCount:_limitBreadcrumb ?: config.sdkInternalLimits.getMaxBreadcrumbCount];
[config.sdkInternalLimits setMaxStackTraceLineLength:_limitTraceLength ?: config.sdkInternalLimits.getMaxStackTraceLineLength];
[config.sdkInternalLimits setMaxStackTraceLinesPerThread:_limitTraceLine ?: config.sdkInternalLimits.getMaxStackTraceLinesPerThread];
CountlyCommon.sharedInstance.maxKeyLength = config.sdkInternalLimits.getMaxKeyLength;
CountlyCommon.sharedInstance.maxValueLength = config.sdkInternalLimits.getMaxValueSize;
CountlyCommon.sharedInstance.maxSegmentationValues = config.sdkInternalLimits.getMaxSegmentationValues;
config.requiresConsent = _consentRequired ?: config.requiresConsent;
CountlyConsentManager.sharedInstance.requiresConsent = config.requiresConsent;
if (_consentRequired)
{
[CountlyConsentManager.sharedInstance cancelConsentForAllFeatures];
}
config.eventSendThreshold = _eventQueueSize ?: config.eventSendThreshold;
config.requestDropAgeHours = _dropOldRequestTime ?: config.requestDropAgeHours;
config.storedRequestsLimit = _requestQueueSize ?: config.storedRequestsLimit;
CountlyPersistency.sharedInstance.eventSendThreshold = config.eventSendThreshold;
CountlyPersistency.sharedInstance.requestDropAgeHours = config.requestDropAgeHours;
CountlyPersistency.sharedInstance.storedRequestsLimit = MAX(1, config.storedRequestsLimit);
config.updateSessionPeriod = _sessionInterval ?: config.updateSessionPeriod;
_sessionInterval = config.updateSessionPeriod;
#if (TARGET_OS_IOS)
[config.content setZoneTimerInterval:_contentZoneInterval ?: config.content.getZoneTimerInterval];
if (config.content.getZoneTimerInterval)
{
CountlyContentBuilderInternal.sharedInstance.zoneTimerInterval = config.content.getZoneTimerInterval;
}
if (!_enterContentZone)
{
dispatch_async(dispatch_get_main_queue(), ^{
[CountlyContentBuilderInternal.sharedInstance exitContentZone];
});
}
else
{
dispatch_async(dispatch_get_main_queue(), ^{
[CountlyContentBuilderInternal.sharedInstance exitContentZone];
[CountlyContentBuilderInternal.sharedInstance enterContentZone:@[]];
});
}
#endif
CountlyCrashReporter.sharedInstance.crashLogLimit = config.sdkInternalLimits.getMaxBreadcrumbCount;
if (_serverConfigUpdateInterval && _serverConfigUpdateInterval != _currentServerConfigUpdateInterval && _requestTimer)
{
_currentServerConfigUpdateInterval = _serverConfigUpdateInterval;
[_requestTimer invalidate];
_requestTimer = nil;
_requestTimer = [NSTimer timerWithTimeInterval:_currentServerConfigUpdateInterval * 60 * 60 target:self selector:@selector(fetchServerConfigTimer:) userInfo:config repeats:YES];
[NSRunLoop.mainRunLoop addTimer:_requestTimer forMode:NSRunLoopCommonModes];
}
if (!_locationTracking && !CountlyLocationManager.sharedInstance.isLocationInfoDisabled)
{
[CountlyLocationManager.sharedInstance disableLocationInfo];
}
}
- (void)fetchServerConfigTimer:(NSTimer *)timer
{
CountlyConfig *config = (CountlyConfig *)timer.userInfo; // Retrieve CountlyConfig from userInfo
if (config)
{
[self fetchServerConfig:config];
}
}
- (void)fetchServerConfigIfTimeIsUp
{
if (_serverConfigDisabled) {
return;
}
if (_lastFetchTimestamp)
{
long long currentTime = NSDate.date.timeIntervalSince1970 * 1000;
long long timePassed = currentTime - _lastFetchTimestamp;
if (timePassed > _currentServerConfigUpdateInterval * 60 * 60 * 1000)
{
[self fetchServerConfig:CountlyConfig.new];
}
}
}
- (void)fetchServerConfig:(CountlyConfig *)config
{
if (_serverConfigDisabled) {
return;
}
CLY_LOG_D(@"Fetching server configs...");
if (CountlyDeviceInfo.sharedInstance.isDeviceIDTemporary)
return;
_lastFetchTimestamp = NSDate.date.timeIntervalSince1970 * 1000;
if (!_requestTimer)
{
_requestTimer = [NSTimer timerWithTimeInterval:_currentServerConfigUpdateInterval * 60 * 60 target:self selector:@selector(fetchServerConfigTimer:) userInfo:config repeats:YES];
[NSRunLoop.mainRunLoop addTimer:_requestTimer forMode:NSRunLoopCommonModes];
}
id handler = ^(NSData *data, NSURLResponse *response, NSError *error) {
NSDictionary *serverConfigResponse = nil;
if (!error)
{
serverConfigResponse = [NSJSONSerialization JSONObjectWithData:data options:0 error:&error];
CLY_LOG_D(@"Server Config Fetched: %@", serverConfigResponse.description);
}
if (!error)
{
if (((NSHTTPURLResponse *)response).statusCode != 200)
{
NSMutableDictionary *serverConfig = serverConfigResponse.mutableCopy;
serverConfig[NSLocalizedDescriptionKey] = @"Server configuration general API error";
error = [NSError errorWithDomain:kCountlyErrorDomain code:CLYErrorServerConfigGeneralAPIError userInfo:serverConfig];
}
}
if (error)
{
CLY_LOG_E(@"Error while fetching server configs: %@", error.description);
}
if (serverConfigResponse[kRConfig] != nil)
{
[CountlyPersistency.sharedInstance storeServerConfig:serverConfigResponse];
self->_trackingEnabled = YES;
self->_networkingEnabled = YES;
self->_crashReportingEnabled = YES;
self->_customEventTrackingEnabled = YES;
self->_enterContentZone = NO;
self->_locationTracking = YES;
self->_viewTrackingEnabled = YES;
self->_sessionTrackingEnabled = YES;
self->_loggingEnabled = NO;
self->_refreshContentZone = YES;
[self populateServerConfig:serverConfigResponse];
}
[self notifySdkConfigChange:config]; // if no config let stored ones to be set
};
// Set default values
NSURLSessionTask *task = [CountlyCommon.sharedInstance.URLSession dataTaskWithRequest:[self serverConfigRequest] completionHandler:handler];
[task resume];
}
- (NSURLRequest *)serverConfigRequest
{
NSString *queryString = [NSString stringWithFormat:@"%@=%@&%@=%@&%@=%@&%@=%@&%@=%@", kCountlyQSKeyMethod, kCountlySCKeySC, kCountlyQSKeyAppKey, CountlyConnectionManager.sharedInstance.appKey.cly_URLEscaped, kCountlyQSKeyDeviceID, CountlyDeviceInfo.sharedInstance.deviceID.cly_URLEscaped,
kCountlyQSKeySDKName, CountlyCommon.sharedInstance.SDKName, kCountlyQSKeySDKVersion, CountlyCommon.sharedInstance.SDKVersion];
queryString = [queryString stringByAppendingFormat:@"&%@=%@", kCountlyAppVersionKey, CountlyDeviceInfo.appVersion];
queryString = [CountlyConnectionManager.sharedInstance appendChecksum:queryString];
NSMutableString *URL = CountlyConnectionManager.sharedInstance.host.mutableCopy;
[URL appendString:kCountlyEndpointO];
[URL appendString:kCountlyEndpointSDK];
if (queryString.length > kCountlyGETRequestMaxLength || CountlyConnectionManager.sharedInstance.alwaysUsePOST)
{
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:URL]];
request.HTTPMethod = @"POST";
request.HTTPBody = [queryString cly_dataUTF8];
return request.copy;
}
else
{
[URL appendFormat:@"?%@", queryString];
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:URL]];
return request;
}
CLY_LOG_D(@"serverConfigRequest URL :%@", URL);
}
- (void)disableSDKBehaviourSettings {
_serverConfigDisabled = YES;
}
- (BOOL)trackingEnabled
{
return _trackingEnabled;
}
- (BOOL)networkingEnabled
{
return _networkingEnabled;
}
- (NSInteger)sessionInterval
{
return _sessionInterval;
}
- (NSInteger)requestQueueSize
{
return _requestQueueSize;
}
- (NSInteger)eventQueueSize
{
return _eventQueueSize;
}
- (BOOL)crashReportingEnabled
{
return _crashReportingEnabled;
}
- (BOOL)sessionTrackingEnabled
{
return _sessionTrackingEnabled;
}
- (BOOL)loggingEnabled
{
return _loggingEnabled;
}
- (NSInteger)limitKeyLength
{
return _limitKeyLength;
}
- (NSInteger)limitValueSize
{
return _limitValueSize;
}
- (NSInteger)limitSegValues
{
return _limitSegValues;
}
- (NSInteger)limitBreadcrumb
{
return _limitBreadcrumb;
}
- (NSInteger)limitTraceLine
{
return _limitTraceLine;
}
- (NSInteger)limitTraceLength
{
return _limitTraceLength;
}
- (BOOL)customEventTrackingEnabled
{
return _customEventTrackingEnabled;
}
- (BOOL)viewTrackingEnabled
{
return _viewTrackingEnabled;
}
- (BOOL)enterContentZone
{
return _enterContentZone;
}
- (NSInteger)contentZoneInterval
{
return _contentZoneInterval;
}
- (BOOL)consentRequired
{
return _consentRequired;
}
- (NSInteger)dropOldRequestTime
{
return _dropOldRequestTime;
}
- (BOOL)locationTrackingEnabled
{
return _locationTracking;
}
- (BOOL)refreshContentZoneEnabled
{
return _refreshContentZone;
}
@end