forked from orta/ARAnalytics
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ARAnalytics.m
387 lines (307 loc) · 10.5 KB
/
ARAnalytics.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
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
//
// ARAnalytics.m
// Art.sy
//
// Created by orta therox on 18/12/2012.
// Copyright (c) 2012 Art.sy. All rights reserved.
//
#import "ARAnalytics.h"
#import "ARAnalytics+GeneratedHeader.h"
static ARAnalytics *_sharedAnalytics;
@interface ARAnalytics ()
@property (strong) NSMutableDictionary *eventsDictionary;
@end
@implementation ARAnalytics
+ (void) initialize {
static dispatch_once_t pred;
dispatch_once(&pred, ^{ _sharedAnalytics = [[ARAnalytics alloc] init]; } );
}
#pragma mark -
#pragma mark Analytics Setup
+ (void)setupWithAnalytics:(NSDictionary *)analyticsDictionary {
#ifdef AR_TESTFLIGHT_EXISTS
if (analyticsDictionary[ARTestFlightTeamToken]) {
[self setupTestFlightWithTeamToken:analyticsDictionary[ARTestFlightTeamToken]];
}
#endif
#ifdef AR_FLURRY_EXISTS
if (analyticsDictionary[ARFlurryAPIKey]) {
[self setupFlurryWithAPIKey:analyticsDictionary[ARFlurryAPIKey]];
}
#endif
#ifdef AR_GOOGLEANALYTICS_EXISTS
if (analyticsDictionary[ARGoogleAnalyticsID]) {
[self setupGoogleAnalyticsWithID:analyticsDictionary[ARGoogleAnalyticsID]];
}
#endif
#ifdef AR_KISSMETRICS_EXISTS
if (analyticsDictionary[ARKISSMetricsAPIKey]) {
[self setupKISSMetricsWithAPIKey:analyticsDictionary[ARKISSMetricsAPIKey]];
}
#endif
#ifdef AR_LOCALYTICS_EXISTS
if (analyticsDictionary[ARLocalyticsAppKey]) {
[self setupLocalyticsWithAppKey:analyticsDictionary[ARLocalyticsAppKey]];
}
#endif
#ifdef AR_MIXPANEL_EXISTS
if (analyticsDictionary[ARMixpanelToken]) {
[self setupMixpanelWithToken:analyticsDictionary[ARMixpanelToken]];
}
#endif
// Crashlytics / Crittercism should stay at the bottom of this,
// as they both need to register exceptions, and you'd only use one.
#ifdef AR_CRASHLYTICS_EXISTS
if (analyticsDictionary[ARCrashlyticsAPIKey]) {
[self setupCrashlyticsWithAPIKey:analyticsDictionary[ARCrashlyticsAPIKey]];
}
#endif
#ifdef AR_CRITTERCISM_EXISTS
if (analyticsDictionary[ARCrittercismAppID]) {
[self setupCrittercismWithAppID:analyticsDictionary[ARCrittercismAppID]];
}
#endif
}
+ (void)setupTestFlightWithTeamToken:(NSString *)token {
#ifdef AR_TESTFLIGHT_EXISTS
NSAssert([TestFlight class], @"TestFlight is not included");
// For non App store builds use a device identifier.
#ifndef RELEASE
[TestFlight setDeviceIdentifier:[self uniqueID]];
#endif
[TestFlight takeOff:token];
#endif
}
+ (void)setupCrashlyticsWithAPIKey:(NSString *)key {
#ifdef AR_CRASHLYTICS_EXISTS
NSAssert([Crashlytics class], @"Crashlytics is not included");
NSAssert([[Crashlytics class] respondsToSelector:@selector(version)], @"Crashlytics library not installed correctly.");
[Crashlytics startWithAPIKey:key];
#endif
}
+ (void)setupMixpanelWithToken:(NSString *)token {
#ifdef AR_MIXPANEL_EXISTS
NSAssert([Mixpanel class], @"Mixpanel is not included");
[Mixpanel sharedInstanceWithToken:token];
#endif
}
+ (void)setupFlurryWithAPIKey:(NSString *)key {
#ifdef AR_FLURRY_EXISTS
NSAssert([Flurry class], @"Flurry is not included");
[Flurry startSession:key];
#endif
}
+ (void)setupGoogleAnalyticsWithID:(NSString *)id {
#ifdef AR_GOOGLEANALYTICS_EXISTS
NSAssert([GAI class], @"Google Analytics SDK is not included");
[[GAI sharedInstance] trackerWithTrackingId:id];
#endif
}
+ (void)setupLocalyticsWithAppKey:(NSString *)key {
#ifdef AR_LOCALYTICS_EXISTS
NSAssert([LocalyticsSession class], @"Localytics is not included");
[[LocalyticsSession sharedLocalyticsSession] startSession:key];
#endif
}
+ (void)setupKISSMetricsWithAPIKey:(NSString *)key {
#ifdef AR_KISSMETRICS_EXISTS
NSAssert([KISSMetricsAPI class], @"KISSMetrics is not included");
[KISSMetricsAPI sharedAPIWithKey:key];
#endif
}
+ (void)setupCrittercismWithAppID:(NSString *)appID {
#ifdef AR_CRITTERCISM_EXISTS
NSAssert([Crittercism class], @"Crittercism is not included");
[Crittercism enableWithAppID:appID];
#endif
}
#pragma mark -
#pragma mark User Setup
+ (void)identifyUserwithID:(NSString *)id andEmailAddress:(NSString *)email {
#ifdef AR_MIXPANEL_EXISTS
[[[Mixpanel sharedInstance] people] identify:id];
if (email) {
[[[Mixpanel sharedInstance] people] set:@"$email" to:email];
}
#endif
#ifdef AR_TESTFLIGHT_EXISTS
[TestFlight addCustomEnvironmentInformation:@"id" forKey:id];
if (email) {
[TestFlight addCustomEnvironmentInformation:@"email" forKey:email];
}
#endif
#ifdef AR_FLURRY_EXISTS
[Flurry setUserID:id];
#endif
#ifdef AR_LOCALYTICS_EXISTS
[[LocalyticsSession sharedLocalyticsSession] setCustomerName:id];
if (email) {
[[LocalyticsSession sharedLocalyticsSession] setCustomerEmail:email];
}
#endif
#ifdef AR_GOOGLEANALYTICS_EXISTS
// Not allowed in GA
// https://developers.google.com/analytics/devguides/collection/ios/v2/customdimsmets#pii
// The Google Analytics Terms of Service prohibit sending of any personally identifiable information (PII) to Google Analytics servers. For more information, please consult the Terms of Service.
#endif
#ifdef AR_CRASHLYTICS_EXISTS
[Crashlytics setUserIdentifier:id];
if (email) {
[Crashlytics setUserName:email];
}
#endif
#ifdef AR_CRITTERCISM_EXISTS
[Crittercism setUsername:id];
if (email) {
[Crittercism setEmail:email];
}
#endif
#ifdef AR_KISSMETRICS_EXISTS
[[KISSMetricsAPI sharedAPI] identify:id];
if (email) {
[[KISSMetricsAPI sharedAPI] alias:email withIdentity:id];
}
#endif
}
+ (void)setUserProperty:(NSString *)property toValue:(NSString *)value {
if (value == nil) {
NSLog(@"ARAnalytics: Value cannot be nil ( %@ ) ", property);
return;
}
#ifdef AR_MIXPANEL_EXISTS
[[[Mixpanel sharedInstance] people] set:property to:value];
#endif
#ifdef AR_TESTFLIGHT_EXISTS
[TestFlight addCustomEnvironmentInformation:value forKey:property];
#endif
#ifdef AR_FLURRY_EXISTS
// This concept doesn't exist in Flurry
#endif
#ifdef AR_LOCALYTICS_EXISTS
// This is for enterprise only...
[[LocalyticsSession sharedLocalyticsSession] setValueForIdentifier:property value:value];
#endif
#ifdef AR_GOOGLEANALYTICS_EXISTS
[[[GAI sharedInstance] defaultTracker] set:property value:value];
#endif
#ifdef AR_CRASHLYTICS_EXISTS
[Crashlytics setObjectValue:value forKey:property];
#endif
#ifdef AR_KISSMETRICS_EXISTS
[[KISSMetricsAPI sharedAPI] setProperties:@{property: value}];
#endif
#ifdef AR_CRITTERCISM_EXISTS
[Crittercism setValue:value forKey:property];
#endif
}
+ (void)incrementUserProperty:(NSString*)counterName byInt:(int)amount {
//TODO: Reasearch if others support this
#ifdef AR_MIXPANEL_EXISTS
[[[Mixpanel sharedInstance] people] increment:counterName by:@(amount)];
#endif
}
#pragma mark -
#pragma mark Events
+ (void)event:(NSString *)event {
[self event:event withProperties:nil];
}
+ (void)event:(NSString *)event withProperties:(NSDictionary *)properties {
#ifdef AR_MIXPANEL_EXISTS
[[Mixpanel sharedInstance] track:event properties:properties];
#endif
#ifdef AR_TESTFLIGHT_EXISTS
[TestFlight passCheckpoint:event];
#endif
#ifdef AR_FLURRY_EXISTS
[Flurry logEvent:event withParameters:properties];
#endif
#ifdef AR_LOCALYTICS_EXISTS
[[LocalyticsSession sharedLocalyticsSession] tagEvent:event attributes:properties];
#endif
#ifdef AR_GOOGLEANALYTICS_EXISTS
[[[GAI sharedInstance] defaultTracker] send:event params:properties];
#endif
#ifdef AR_CRASHLYTICS_EXISTS
// This concept doesn't exist in Crashlytics
#endif
#ifdef AR_KISSMETRICS_EXISTS
[[KISSMetricsAPI sharedAPI] recordEvent:event withProperties:properties];
#endif
#ifdef AR_CRITTERCISM_EXISTS
[Crittercism leaveBreadcrumb:event];
#endif
}
#pragma mark -
#pragma mark Monitor Navigation Controller
+ (void)monitorNavigationViewController:(UINavigationController *)controller {
controller.delegate = _sharedAnalytics;
#ifdef AR_FLURRY_EXISTS
[Flurry logAllPageViews:controller];
#endif
}
- (void)navigationController:(UINavigationController *)navigationController didShowViewController:(UIViewController *)viewController animated:(BOOL)animated {
[ARAnalytics event:@"Screen view" withProperties:@{ @"screen": viewController.title }];
#ifdef AR_LOCALYTICS_EXISTS
// This is for enterprise only...
[[LocalyticsSession sharedLocalyticsSession] tagScreen:viewController.title];
#endif
#ifdef AR_GOOGLEANALYTICS_EXISTS
[[[GAI sharedInstance] defaultTracker] trackView:viewController.title];
#endif
}
#pragma mark -
#pragma mark Timing Events
+ (void)startTimingEvent:(NSString *)event {
if (!_sharedAnalytics.eventsDictionary) {
_sharedAnalytics.eventsDictionary = [NSMutableDictionary dictionary];
}
_sharedAnalytics.eventsDictionary[event] = [NSDate date];
}
+ (void)finishTimingEvent:(NSString *)event {
NSDate *startDate = _sharedAnalytics.eventsDictionary[event];
if (!startDate) {
NSLog(@"ARAnalytics: finish timing event called without a corrosponding start timing event");
return;
}
NSTimeInterval eventInterval = [[NSDate date] timeIntervalSinceDate:startDate];
[self event:event withProperties:@{ @"length": @(eventInterval) }];
}
#pragma mark -
#pragma mark Util
+ (NSString *)uniqueID {
// iOS 6 has a good API for getting a unique ID
if ([UICollectionView class] != nil) {
return [[[UIDevice currentDevice] identifierForVendor] UUIDString];
} else {
[[UIDevice currentDevice] uniqueIdentifier];
}
}
@end
NSString *const ARTestFlightTeamToken = @"ARTestFlight";
NSString *const ARCrashlyticsAPIKey = @"ARCrashlytics";
NSString *const ARMixpanelToken = @"ARMixpanel";
NSString *const ARFlurryAPIKey = @"ARFlurry";
NSString *const ARLocalyticsAppKey = @"ARLocalytics";
NSString *const ARKISSMetricsAPIKey = @"ARKISSMetrics";
NSString *const ARCrittercismAppID = @"ARCrittercism";
NSString *const ARGoogleAnalyticsID = @"ARGoogleAnalytics";
void ARLog (NSString *format, ...) {
if (format == nil) {
printf("nil \n");
return;
}
// Get a reference to the arguments that follow the format parameter
va_list argList;
va_start(argList, format);
// Perform format string argument substitution, reinstate %% escapes, then print
NSString *string = [[NSString alloc] initWithFormat:format arguments:argList];
string = [string stringByReplacingOccurrencesOfString:@"%%" withString:@"%%%%"];
printf("ARLog : %s\n", string.UTF8String);
#ifdef AR_CRASHLYTICS_EXISTS
CLSLog(@"%@", string);
#endif
#ifdef AR_TESTFLIGHT_EXISTS
TFLog(@"%@",string);
#endif
va_end(argList);
}