diff --git a/Countly.h b/Countly.h index 35271a71..fd3cd03b 100755 --- a/Countly.h +++ b/Countly.h @@ -7,14 +7,14 @@ #import -@class EventQueue; +@class CountlyEventQueue; @interface Countly : NSObject { double unsentSessionLength; NSTimer *timer; double lastTime; BOOL isSuspended; - EventQueue *eventQueue; + CountlyEventQueue *eventQueue; } + (Countly *)sharedInstance; diff --git a/Countly.m b/Countly.m index 5f6e61b5..b4d4bfc8 100755 --- a/Countly.m +++ b/Countly.m @@ -4,6 +4,7 @@ // // Please visit www.count.ly for more information. +#pragma mark - Directives #ifndef COUNTLY_DEBUG #define COUNTLY_DEBUG 0 @@ -20,6 +21,8 @@ #endif #define COUNTLY_VERSION "1.0" +#define COUNTLY_DEFAULT_UPDATE_INTERVAL 60.0 +#define COUNTLY_EVENT_SEND_THRESHOLD 10 #import "Countly.h" #import "Countly_OpenUDID.h" @@ -32,6 +35,8 @@ #include +#pragma mark - Category - GTMNSStringURLArgumentsAdditions + /// Utilities for encoding and decoding URL arguments. /// This code is from the project google-toolbox-for-mac @interface NSString (GTMNSStringURLArgumentsAdditions) @@ -84,12 +89,14 @@ - (NSString*)gtm_stringByUnescapingFromURLArgument { @end -@interface DeviceInfo : NSObject +#pragma mark - CountlyDeviceInfo + +@interface CountlyDeviceInfo : NSObject { } @end -@implementation DeviceInfo +@implementation CountlyDeviceInfo + (NSString *)udid { @@ -152,21 +159,21 @@ + (NSString *)metrics { NSString *result = @"{"; - result = [result stringByAppendingFormat:@"\"%@\":\"%@\"", @"_device", [DeviceInfo device]]; + result = [result stringByAppendingFormat:@"\"%@\":\"%@\"", @"_device", [CountlyDeviceInfo device]]; result = [result stringByAppendingFormat:@",\"%@\":\"%@\"", @"_os", @"iOS"]; - result = [result stringByAppendingFormat:@",\"%@\":\"%@\"", @"_os_version", [DeviceInfo osVersion]]; + result = [result stringByAppendingFormat:@",\"%@\":\"%@\"", @"_os_version", [CountlyDeviceInfo osVersion]]; - NSString *carrier = [DeviceInfo carrier]; + NSString *carrier = [CountlyDeviceInfo carrier]; if (carrier != nil) result = [result stringByAppendingFormat:@",\"%@\":\"%@\"", @"_carrier", carrier]; - result = [result stringByAppendingFormat:@",\"%@\":\"%@\"", @"_resolution", [DeviceInfo resolution]]; + result = [result stringByAppendingFormat:@",\"%@\":\"%@\"", @"_resolution", [CountlyDeviceInfo resolution]]; - result = [result stringByAppendingFormat:@",\"%@\":\"%@\"", @"_locale", [DeviceInfo locale]]; + result = [result stringByAppendingFormat:@",\"%@\":\"%@\"", @"_locale", [CountlyDeviceInfo locale]]; - result = [result stringByAppendingFormat:@",\"%@\":\"%@\"", @"_app_version", [DeviceInfo appVersion]]; + result = [result stringByAppendingFormat:@",\"%@\":\"%@\"", @"_app_version", [CountlyDeviceInfo appVersion]]; result = [result stringByAppendingString:@"}"]; @@ -177,6 +184,9 @@ + (NSString *)metrics @end + +#pragma mark - CountlyEvent + @interface CountlyEvent : NSObject { } @@ -219,12 +229,15 @@ - (void)dealloc @end -@interface EventQueue : NSObject + +#pragma mark - CountlyEventQueue + +@interface CountlyEventQueue : NSObject @end -@implementation EventQueue +@implementation CountlyEventQueue - (void)dealloc { @@ -462,7 +475,10 @@ - (void)recordEvent:(NSString *)key segmentation:(NSDictionary *)segmentation co @end -@interface ConnectionQueue : NSObject + +#pragma mark - CountlyConnectionQueue + +@interface CountlyConnectionQueue : NSObject { NSURLConnection *connection_; UIBackgroundTaskIdentifier bgTask_; @@ -475,19 +491,19 @@ @interface ConnectionQueue : NSObject @end -static ConnectionQueue *s_sharedConnectionQueue = nil; +static CountlyConnectionQueue *s_sharedCountlyConnectionQueue = nil; -@implementation ConnectionQueue : NSObject +@implementation CountlyConnectionQueue : NSObject @synthesize appKey; @synthesize appHost; -+ (ConnectionQueue *)sharedInstance ++ (CountlyConnectionQueue *)sharedInstance { - if (s_sharedConnectionQueue == nil) - s_sharedConnectionQueue = [[ConnectionQueue alloc] init]; + if (s_sharedCountlyConnectionQueue == nil) + s_sharedCountlyConnectionQueue = [[CountlyConnectionQueue alloc] init]; - return s_sharedConnectionQueue; + return s_sharedCountlyConnectionQueue; } - (id)init @@ -527,9 +543,9 @@ - (void)beginSession { NSString *data = [NSString stringWithFormat:@"app_key=%@&device_id=%@×tamp=%ld&sdk_version="COUNTLY_VERSION"&begin_session=1&metrics=%@", appKey, - [DeviceInfo udid], + [CountlyDeviceInfo udid], time(NULL), - [DeviceInfo metrics]]; + [CountlyDeviceInfo metrics]]; [[CountlyDB sharedInstance] addToQueue:data]; @@ -540,7 +556,7 @@ - (void)updateSessionWithDuration:(int)duration { NSString *data = [NSString stringWithFormat:@"app_key=%@&device_id=%@×tamp=%ld&session_duration=%d", appKey, - [DeviceInfo udid], + [CountlyDeviceInfo udid], time(NULL), duration]; @@ -553,7 +569,7 @@ - (void)endSessionWithDuration:(int)duration { NSString *data = [NSString stringWithFormat:@"app_key=%@&device_id=%@×tamp=%ld&end_session=1&session_duration=%d", appKey, - [DeviceInfo udid], + [CountlyDeviceInfo udid], time(NULL), duration]; @@ -566,7 +582,7 @@ - (void)recordEvents:(NSString *)events { NSString *data = [NSString stringWithFormat:@"app_key=%@&device_id=%@×tamp=%ld&events=%@", appKey, - [DeviceInfo udid], + [CountlyDeviceInfo udid], time(NULL), events]; @@ -638,6 +654,9 @@ - (void)dealloc @end + +#pragma mark - Countly Core + static Countly *s_sharedCountly = nil; @implementation Countly @@ -657,7 +676,7 @@ - (id)init timer = nil; isSuspended = NO; unsentSessionLength = 0; - eventQueue = [[EventQueue alloc] init]; + eventQueue = [[CountlyEventQueue alloc] init]; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(didEnterBackgroundCallBack:) @@ -677,47 +696,47 @@ - (id)init - (void)start:(NSString *)appKey withHost:(NSString *)appHost { - timer = [NSTimer scheduledTimerWithTimeInterval:60.0 + timer = [NSTimer scheduledTimerWithTimeInterval:COUNTLY_DEFAULT_UPDATE_INTERVAL target:self selector:@selector(onTimer:) userInfo:nil repeats:YES]; lastTime = CFAbsoluteTimeGetCurrent(); - [[ConnectionQueue sharedInstance] setAppKey:appKey]; - [[ConnectionQueue sharedInstance] setAppHost:appHost]; - [[ConnectionQueue sharedInstance] beginSession]; + [[CountlyConnectionQueue sharedInstance] setAppKey:appKey]; + [[CountlyConnectionQueue sharedInstance] setAppHost:appHost]; + [[CountlyConnectionQueue sharedInstance] beginSession]; } - (void)recordEvent:(NSString *)key count:(int)count { [eventQueue recordEvent:key count:count]; - if (eventQueue.count >= 10) - [[ConnectionQueue sharedInstance] recordEvents:[eventQueue events]]; + if (eventQueue.count >= COUNTLY_EVENT_SEND_THRESHOLD) + [[CountlyConnectionQueue sharedInstance] recordEvents:[eventQueue events]]; } - (void)recordEvent:(NSString *)key count:(int)count sum:(double)sum { [eventQueue recordEvent:key count:count sum:sum]; - if (eventQueue.count >= 10) - [[ConnectionQueue sharedInstance] recordEvents:[eventQueue events]]; + if (eventQueue.count >= COUNTLY_EVENT_SEND_THRESHOLD) + [[CountlyConnectionQueue sharedInstance] recordEvents:[eventQueue events]]; } - (void)recordEvent:(NSString *)key segmentation:(NSDictionary *)segmentation count:(int)count { [eventQueue recordEvent:key segmentation:segmentation count:count]; - if (eventQueue.count >= 10) - [[ConnectionQueue sharedInstance] recordEvents:[eventQueue events]]; + if (eventQueue.count >= COUNTLY_EVENT_SEND_THRESHOLD) + [[CountlyConnectionQueue sharedInstance] recordEvents:[eventQueue events]]; } - (void)recordEvent:(NSString *)key segmentation:(NSDictionary *)segmentation count:(int)count sum:(double)sum { [eventQueue recordEvent:key segmentation:segmentation count:count sum:sum]; - if (eventQueue.count >= 10) - [[ConnectionQueue sharedInstance] recordEvents:[eventQueue events]]; + if (eventQueue.count >= COUNTLY_EVENT_SEND_THRESHOLD) + [[CountlyConnectionQueue sharedInstance] recordEvents:[eventQueue events]]; } - (void)onTimer:(NSTimer *)timer @@ -730,11 +749,11 @@ - (void)onTimer:(NSTimer *)timer lastTime = currTime; int duration = unsentSessionLength; - [[ConnectionQueue sharedInstance] updateSessionWithDuration:duration]; + [[CountlyConnectionQueue sharedInstance] updateSessionWithDuration:duration]; unsentSessionLength -= duration; if (eventQueue.count > 0) - [[ConnectionQueue sharedInstance] recordEvents:[eventQueue events]]; + [[CountlyConnectionQueue sharedInstance] recordEvents:[eventQueue events]]; } - (void)suspend @@ -742,13 +761,13 @@ - (void)suspend isSuspended = YES; if (eventQueue.count > 0) - [[ConnectionQueue sharedInstance] recordEvents:[eventQueue events]]; + [[CountlyConnectionQueue sharedInstance] recordEvents:[eventQueue events]]; double currTime = CFAbsoluteTimeGetCurrent(); unsentSessionLength += currTime - lastTime; int duration = unsentSessionLength; - [[ConnectionQueue sharedInstance] endSessionWithDuration:duration]; + [[CountlyConnectionQueue sharedInstance] endSessionWithDuration:duration]; unsentSessionLength -= duration; } @@ -756,7 +775,7 @@ - (void)resume { lastTime = CFAbsoluteTimeGetCurrent(); - [[ConnectionQueue sharedInstance] beginSession]; + [[CountlyConnectionQueue sharedInstance] beginSession]; isSuspended = NO; }