-
Notifications
You must be signed in to change notification settings - Fork 93
/
SPTracker.m
326 lines (276 loc) · 10.3 KB
/
SPTracker.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
//
// SPTracker.m
// Snowplow
//
// Copyright (c) 2013-2014 Snowplow Analytics Ltd. All rights reserved.
//
// This program is licensed to you under the Apache License Version 2.0,
// and you may not use this file except in compliance with the Apache License
// Version 2.0. You may obtain a copy of the Apache License Version 2.0 at
// http://www.apache.org/licenses/LICENSE-2.0.
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the Apache License Version 2.0 is distributed on
// an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
// express or implied. See the Apache License Version 2.0 for the specific
// language governing permissions and limitations there under.
//
// Authors: Jonathan Almeida, Joshua Beemster
// Copyright: Copyright (c) 2013-2015 Snowplow Analytics Ltd
// License: Apache License Version 2.0
//
#import "Snowplow.h"
#import "SPTracker.h"
#import "SPEmitter.h"
#import "SPSubject.h"
#import "SPPayload.h"
#import "SPSelfDescribingJson.h"
#import "SPUtilities.h"
#import "SPSession.h"
#import "SPEvent.h"
@interface SPTracker ()
@property (nonatomic, retain) SPEmitter * emitter;
@property (nonatomic, retain) SPSubject * subject;
@property (nonatomic, retain) NSString * appId;
@property (nonatomic, retain) NSString * trackerNamespace;
@property (nonatomic) BOOL base64Encoded;
@end
@implementation SPTracker {
NSMutableDictionary * _trackerData;
NSString * _platformContextSchema;
BOOL _dataCollection;
SPSession * _session;
BOOL _sessionContext;
NSInteger _foregroundTimeout;
NSInteger _backgroundTimeout;
NSInteger _checkInterval;
BOOL _builderFinished;
}
// SnowplowTracker Builder
+ (instancetype) build:(void(^)(id<SPTrackerBuilder>builder))buildBlock {
SPTracker* tracker = [SPTracker new];
if (buildBlock) {
buildBlock(tracker);
}
[tracker setup];
return tracker;
}
- (id) init {
self = [super init];
if (self) {
_trackerNamespace = nil;
_appId = nil;
_base64Encoded = YES;
_dataCollection = YES;
_sessionContext = NO;
_foregroundTimeout = 600;
_backgroundTimeout = 300;
_checkInterval = 15;
_builderFinished = NO;
#if SNOWPLOW_TARGET_IOS
_platformContextSchema = kSPMobileContextSchema;
#else
_platformContextSchema = kSPDesktopContextSchema;
#endif
}
return self;
}
- (void) setup {
[SPUtilities checkArgument:(_emitter != nil) withMessage:@"Emitter cannot be nil."];
[self setTrackerData];
if (_sessionContext) {
_session = [[SPSession alloc] initWithForegroundTimeout:_foregroundTimeout andBackgroundTimeout:_backgroundTimeout andCheckInterval:_checkInterval];
}
_builderFinished = YES;
}
- (void) setTrackerData {
_trackerData = [NSMutableDictionary dictionaryWithObjectsAndKeys:
kSPVersion, kSPTrackerVersion,
_trackerNamespace != nil ? _trackerNamespace : [NSNull null], kSPNamespace,
_appId != nil ? _appId : [NSNull null], kSPAppId, nil];
}
// Required
- (void) setEmitter:(SPEmitter *)emitter {
if (emitter != nil) {
_emitter = emitter;
}
}
- (void) setSubject:(SPSubject *)subject {
_subject = subject;
}
- (void) setBase64Encoded:(BOOL)encoded {
_base64Encoded = encoded;
}
- (void) setAppId:(NSString *)appId {
_appId = appId;
if (_builderFinished && _trackerData != nil) {
[self setTrackerData];
}
}
- (void) setTrackerNamespace:(NSString *)trackerNamespace {
_trackerNamespace = trackerNamespace;
if (_builderFinished && _trackerData != nil) {
[self setTrackerData];
}
}
- (void) setSessionContext:(BOOL)sessionContext {
_sessionContext = sessionContext;
if (_session != nil && !sessionContext) {
[_session stopChecker];
_session = nil;
} else if (_builderFinished && _session == nil && sessionContext) {
_session = [[SPSession alloc] initWithForegroundTimeout:_foregroundTimeout andBackgroundTimeout:_backgroundTimeout andCheckInterval:_checkInterval];
}
}
- (void) setForegroundTimeout:(NSInteger)foregroundTimeout {
_foregroundTimeout = foregroundTimeout;
if (_builderFinished && _session != nil) {
[_session setForegroundTimeout:foregroundTimeout];
}
}
- (void) setBackgroundTimeout:(NSInteger)backgroundTimeout {
_backgroundTimeout = backgroundTimeout;
if (_builderFinished && _session != nil) {
[_session setBackgroundTimeout:backgroundTimeout];
}
}
- (void) setCheckInterval:(NSInteger)checkInterval {
_checkInterval = checkInterval;
if (_builderFinished && _session != nil) {
[_session setCheckInterval:checkInterval];
}
}
// Extra Functions
- (void) pauseEventTracking {
_dataCollection = NO;
[_emitter stopTimerFlush];
[_session stopChecker];
}
- (void) resumeEventTracking {
_dataCollection = YES;
[_emitter startTimerFlush];
[_session startChecker];
}
// Getters
- (NSInteger) getSessionIndex {
return [_session getSessionIndex];
}
- (BOOL) getInBackground {
return [_session getInBackground];
}
- (BOOL) getIsTracking {
return _dataCollection;
}
// Event Tracking Functions
- (void) trackPageViewEvent:(SPPageView *)event {
if (!_dataCollection) {
return;
}
[self addEventWithPayload:[event getPayload] andContext:[event getContexts] andEventId:[event getEventId]];
}
- (void) trackStructuredEvent:(SPStructured *)event {
if (!_dataCollection) {
return;
}
[self addEventWithPayload:[event getPayload] andContext:[event getContexts] andEventId:[event getEventId]];
}
- (void) trackUnstructuredEvent:(SPUnstructured *)event {
if (!_dataCollection) {
return;
}
[self addEventWithPayload:[event getPayloadWithEncoding:_base64Encoded] andContext:[event getContexts] andEventId:[event getEventId]];
}
- (void) trackScreenViewEvent:(SPScreenView *)event {
SPUnstructured * unstruct = [SPUnstructured build:^(id<SPUnstructuredBuilder> builder) {
[builder setEventData:[event getPayload]];
[builder setTimestamp:[event getTimestamp]];
[builder setContexts:[event getContexts]];
[builder setEventId:[event getEventId]];
}];
[self trackUnstructuredEvent:unstruct];
}
- (void) trackTimingEvent:(SPTiming *)event {
SPUnstructured * unstruct = [SPUnstructured build:^(id<SPUnstructuredBuilder> builder) {
[builder setEventData:[event getPayload]];
[builder setTimestamp:[event getTimestamp]];
[builder setContexts:[event getContexts]];
[builder setEventId:[event getEventId]];
}];
[self trackUnstructuredEvent:unstruct];
}
- (void) trackEcommerceEvent:(SPEcommerce *)event {
if (!_dataCollection) {
return;
}
[self addEventWithPayload:[event getPayload] andContext:[event getContexts] andEventId:[event getEventId]];
NSNumber *tstamp = [event getTimestamp];
for (SPEcommerceItem * item in [event getItems]) {
[item setTimestamp:tstamp];
[self trackEcommerceItemEvent:item];
}
}
- (void) trackEcommerceItemEvent:(SPEcommerceItem *)event {
[self addEventWithPayload:[event getPayload] andContext:[event getContexts] andEventId:[event getEventId]];
}
- (void) trackPushNotificationEvent:(SPPushNotification *)event {
SPUnstructured * unstruct = [SPUnstructured build:^(id<SPUnstructuredBuilder> builder) {
[builder setEventData:[event getPayload]];
[builder setTimestamp:[event getTimestamp]];
[builder setContexts:[event getContexts]];
[builder setEventId:[event getEventId]];
}];
[self trackUnstructuredEvent:unstruct];
}
// Event Decoration
- (void) addEventWithPayload:(SPPayload *)pb andContext:(NSMutableArray *)contextArray andEventId:(NSString *)eventId {
[_emitter addPayloadToBuffer:[self getFinalPayloadWithPayload:pb andContext:contextArray andEventId:eventId]];
}
- (SPPayload *) getFinalPayloadWithPayload:(SPPayload *)pb andContext:(NSMutableArray *)contextArray andEventId:(NSString *)eventId {
[pb addDictionaryToPayload:_trackerData];
// Add Subject information
if (_subject != nil) {
[pb addDictionaryToPayload:[[_subject getStandardDict] getAsDictionary]];
} else {
[pb addValueToPayload:[SPUtilities getPlatform] forKey:kSPPlatform];
}
// Add the contexts
SPSelfDescribingJson * context = [self getFinalContextWithContexts:contextArray andEventId:eventId];
if (context != nil) {
[pb addDictionaryToPayload:[context getAsDictionary]
base64Encoded:_base64Encoded
typeWhenEncoded:kSPContextEncoded
typeWhenNotEncoded:kSPContext];
}
return pb;
}
- (SPSelfDescribingJson *) getFinalContextWithContexts:(NSMutableArray *)contextArray andEventId:(NSString *)eventId {
SPSelfDescribingJson * finalContext = nil;
// Add contexts if populated
if (_subject != nil) {
NSDictionary * platformDict = [[_subject getPlatformDict] getAsDictionary];
if (platformDict != nil) {
[contextArray addObject:[[SPSelfDescribingJson alloc] initWithSchema:_platformContextSchema andData:platformDict]];
}
NSDictionary * geoLocationDict = [_subject getGeoLocationDict];
if (geoLocationDict != nil) {
[contextArray addObject:[[SPSelfDescribingJson alloc] initWithSchema:kSPGeoContextSchema andData:geoLocationDict]];
}
}
// Add session if active
if (_session != nil) {
NSDictionary * sessionDict = [_session getSessionDictWithEventId:eventId];
if (sessionDict != nil) {
[contextArray addObject:[[SPSelfDescribingJson alloc] initWithSchema:kSPSessionContextSchema andData:sessionDict]];
}
}
// If some contexts are available...
if (contextArray.count > 0) {
NSMutableArray * contexts = [[NSMutableArray alloc] init];
for (SPSelfDescribingJson * context in contextArray) {
[contexts addObject:[context getAsDictionary]];
}
finalContext = [[SPSelfDescribingJson alloc] initWithSchema:kSPContextSchema andData:contexts];
}
return finalContext;
}
@end