forked from Countly/countly-sdk-ios
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCountlyViewTracking.m
403 lines (320 loc) · 12.3 KB
/
CountlyViewTracking.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
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
// CountlyViewTracking.m
//
// This code is provided under the MIT License.
//
// Please visit www.count.ly for more information.
#import "CountlyCommon.h"
@interface CountlyViewTracking ()
@property (nonatomic) NSString* currentView;
@property (nonatomic) NSTimeInterval currentViewStartTime;
@property (nonatomic) NSTimeInterval accumulatedTime;
@property (nonatomic) NSMutableArray* exceptionViewControllers;
@end
NSString* const kCountlyReservedEventView = @"[CLY]_view";
NSString* const kCountlyVTKeyName = @"name";
NSString* const kCountlyVTKeySegment = @"segment";
NSString* const kCountlyVTKeyVisit = @"visit";
NSString* const kCountlyVTKeyStart = @"start";
NSString* const kCountlyVTKeyBounce = @"bounce";
NSString* const kCountlyVTKeyExit = @"exit";
NSString* const kCountlyVTKeyView = @"view";
NSString* const kCountlyVTKeyDomain = @"domain";
NSString* const kCountlyVTKeyDur = @"dur";
#if (TARGET_OS_IOS || TARGET_OS_TV)
@interface UIViewController (CountlyViewTracking)
- (void)Countly_viewDidAppear:(BOOL)animated;
- (void)Countly_viewDidDisappear:(BOOL)animated;
@end
#endif
@implementation CountlyViewTracking
+ (instancetype)sharedInstance
{
if (!CountlyCommon.sharedInstance.hasStarted)
return nil;
static CountlyViewTracking* s_sharedInstance = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{s_sharedInstance = self.new;});
return s_sharedInstance;
}
- (instancetype)init
{
if (self = [super init])
{
self.exceptionViewControllers =
@[
@"CLYInternalViewController",
@"UINavigationController",
@"UIAlertController",
@"UIPageViewController",
@"UITabBarController",
@"UIReferenceLibraryViewController",
@"UISplitViewController",
@"UIInputViewController",
@"UISearchController",
@"UISearchContainerViewController",
@"UIApplicationRotationFollowingController",
@"MFMailComposeInternalViewController",
@"MFMailComposeInternalViewController",
@"MFMailComposePlaceholderViewController",
@"UIInputWindowController",
@"_UIFallbackPresentationViewController",
@"UIActivityViewController",
@"UIActivityGroupViewController",
@"_UIActivityGroupListViewController",
@"_UIActivityViewControllerContentController",
@"UIKeyboardCandidateRowViewController",
@"UIKeyboardCandidateGridCollectionViewController",
@"UIPrintMoreOptionsTableViewController",
@"UIPrintPanelTableViewController",
@"UIPrintPanelViewController",
@"UIPrintPaperViewController",
@"UIPrintPreviewViewController",
@"UIPrintRangeViewController",
@"UIDocumentMenuViewController",
@"UIDocumentPickerViewController",
@"UIDocumentPickerExtensionViewController",
@"UIInterfaceActionGroupViewController",
@"UISystemInputViewController",
@"UIRecentsInputViewController",
@"UICompatibilityInputViewController",
@"UIInputViewAnimationControllerViewController",
@"UISnapshotModalViewController",
@"UIMultiColumnViewController",
@"UIKeyCommandDiscoverabilityHUDViewController"
].mutableCopy;
}
return self;
}
#pragma mark -
- (void)startView:(NSString *)viewName
{
[self startView:viewName customSegmentation:nil];
}
- (void)startView:(NSString *)viewName customSegmentation:(NSDictionary *)customSegmentation
{
if (!viewName.length)
return;
if (!CountlyConsentManager.sharedInstance.consentForViewTracking)
return;
viewName = viewName.copy;
[self endView];
CLY_LOG_D(@"View tracking started: %@", viewName);
viewName = [viewName cly_truncatedKey:@"View name"];
NSMutableDictionary* segmentation = NSMutableDictionary.new;
segmentation[kCountlyVTKeyName] = viewName;
segmentation[kCountlyVTKeySegment] = CountlyDeviceInfo.osName;
segmentation[kCountlyVTKeyVisit] = @1;
if (!self.currentView)
segmentation[kCountlyVTKeyStart] = @1;
if (customSegmentation)
{
NSMutableDictionary* mutableCustomSegmentation = customSegmentation.mutableCopy;
[mutableCustomSegmentation removeObjectsForKeys:self.reservedViewTrackingSegmentationKeys];
[segmentation addEntriesFromDictionary:mutableCustomSegmentation];
}
self.currentView = viewName;
self.previousViewID = self.currentViewID;
self.currentViewID = CountlyCommon.sharedInstance.randomEventID;
self.currentViewStartTime = CountlyCommon.sharedInstance.uniqueTimestamp;
[Countly.sharedInstance recordReservedEvent:kCountlyReservedEventView segmentation:segmentation ID:self.currentViewID];
}
- (void)endView
{
if (!CountlyConsentManager.sharedInstance.consentForViewTracking)
return;
if (self.currentView)
{
NSMutableDictionary* segmentation = NSMutableDictionary.new;
segmentation[kCountlyVTKeyName] = self.currentView;
segmentation[kCountlyVTKeySegment] = CountlyDeviceInfo.osName;
NSTimeInterval duration = NSDate.date.timeIntervalSince1970 - self.currentViewStartTime + self.accumulatedTime;
self.accumulatedTime = 0;
[Countly.sharedInstance recordReservedEvent:kCountlyReservedEventView segmentation:segmentation count:1 sum:0 duration:duration ID:self.currentViewID timestamp:self.currentViewStartTime];
CLY_LOG_D(@"View tracking ended: %@ duration: %.17g", self.currentView, duration);
}
}
- (void)pauseView
{
if (self.currentViewStartTime)
self.accumulatedTime = NSDate.date.timeIntervalSince1970 - self.currentViewStartTime;
}
- (void)resumeView
{
self.currentViewStartTime = CountlyCommon.sharedInstance.uniqueTimestamp;
}
#pragma mark -
#if (TARGET_OS_IOS || TARGET_OS_TV)
- (void)startAutoViewTracking
{
if (!self.isEnabledOnInitialConfig)
return;
if (!CountlyConsentManager.sharedInstance.consentForViewTracking)
return;
self.isAutoViewTrackingActive = YES;
[self swizzleViewTrackingMethods];
UIViewController* topVC = CountlyCommon.sharedInstance.topViewController;
NSString* viewTitle = [CountlyViewTracking.sharedInstance titleForViewController:topVC];
[self startView:viewTitle];
}
- (void)swizzleViewTrackingMethods
{
static BOOL alreadySwizzled;
if (alreadySwizzled)
return;
alreadySwizzled = YES;
Method O_method = class_getInstanceMethod(UIViewController.class, @selector(viewDidAppear:));
Method C_method = class_getInstanceMethod(UIViewController.class, @selector(Countly_viewDidAppear:));
method_exchangeImplementations(O_method, C_method);
O_method = class_getInstanceMethod(UIViewController.class, @selector(viewDidDisappear:));
C_method = class_getInstanceMethod(UIViewController.class, @selector(Countly_viewDidDisappear:));
method_exchangeImplementations(O_method, C_method);
}
- (void)stopAutoViewTracking
{
self.isAutoViewTrackingActive = NO;
self.currentView = nil;
self.currentViewID = nil;
self.currentViewStartTime = 0;
self.accumulatedTime = 0;
}
- (void)performAutoViewTrackingForViewController:(UIViewController *)viewController
{
if (!self.isAutoViewTrackingActive)
return;
if (!CountlyConsentManager.sharedInstance.consentForViewTracking)
return;
NSString* viewTitle = [self titleForViewController:viewController];
if ([self.currentView isEqualToString:viewTitle])
return;
BOOL isException = NO;
for (NSString* exception in self.exceptionViewControllers)
{
isException = [viewTitle isEqualToString:exception] ||
[viewController isKindOfClass:NSClassFromString(exception)] ||
[NSStringFromClass(viewController.class) isEqualToString:exception];
if (isException)
{
CLY_LOG_V(@"%@ is an exceptional view, so it will be ignored for view tracking.", viewTitle);
break;
}
}
if (!isException)
[self startView:viewTitle];
}
#pragma mark -
- (void)setIsAutoViewTrackingActive:(BOOL)isAutoViewTrackingActive
{
if (!self.isEnabledOnInitialConfig)
return;
if (!CountlyConsentManager.sharedInstance.consentForViewTracking)
return;
_isAutoViewTrackingActive = isAutoViewTrackingActive;
}
#pragma mark -
- (void)addExceptionForAutoViewTracking:(NSString *)exception
{
if (!exception.length)
return;
if (![self.exceptionViewControllers containsObject:exception])
[self.exceptionViewControllers addObject:exception];
}
- (void)removeExceptionForAutoViewTracking:(NSString *)exception
{
[self.exceptionViewControllers removeObject:exception];
}
#pragma mark -
- (NSString*)titleForViewController:(UIViewController *)viewController
{
if (!viewController)
return nil;
NSString* title = nil;
if ([viewController respondsToSelector:@selector(countlyAutoViewTrackingName)])
{
CLY_LOG_I(@"Viewcontroller conforms to CountlyAutoViewTrackingName protocol for custom auto view tracking name.");
title = [(id<CountlyAutoViewTrackingName>)viewController countlyAutoViewTrackingName];
}
if (!title)
title = viewController.title;
if (!title)
title = [viewController.navigationItem.titleView isKindOfClass:UILabel.class] ? ((UILabel *)viewController.navigationItem.titleView).text : nil;
if (!title)
title = viewController.navigationItem.title;
if (!title)
title = NSStringFromClass(viewController.class);
return title;
}
#endif
- (NSArray *)reservedViewTrackingSegmentationKeys
{
NSArray* reservedViewTrackingSegmentationKeys =
@[
kCountlyVTKeyName,
kCountlyVTKeySegment,
kCountlyVTKeyVisit,
kCountlyVTKeyStart,
kCountlyVTKeyBounce,
kCountlyVTKeyExit,
kCountlyVTKeyView,
kCountlyVTKeyDomain,
kCountlyVTKeyDur
];
return reservedViewTrackingSegmentationKeys;
}
@end
#pragma mark -
#if (TARGET_OS_IOS || TARGET_OS_TV)
@implementation UIViewController (CountlyViewTracking)
- (void)Countly_viewDidAppear:(BOOL)animated
{
[self Countly_viewDidAppear:animated];
[CountlyViewTracking.sharedInstance performAutoViewTrackingForViewController:self];
if (self.isPageSheetModal)
{
//NOTE: Since iOS 13, modals with PageSheet presentation style
// does not trigger `viewDidAppear` on presenting view controller when they are dismissed.
// Also, `self.presentingViewController` property is nil in both `viewWillDisappear` and `viewDidDisappear`.
// So, we store it here in modal's `viewDidAppear` to be used for view tracking later.
CLY_LOG_I(@"A modal view controller with PageSheet presentation style is presented on iOS 13+. Storing presenting view controller to be used later.");
UIViewController* presenting = self.presentingViewController;
if ([presenting isKindOfClass:UINavigationController.class])
{
presenting = ((UINavigationController *)presenting).topViewController;
}
self.presentingVC = presenting;
}
}
- (void)Countly_viewDidDisappear:(BOOL)animated
{
[self Countly_viewDidDisappear:animated];
if (self.presentingVC)
{
CLY_LOG_I(@"A modal view controller with PageSheet presentation style is dismissed on iOS 13+. Forcing auto view tracking with stored presenting view controller.");
[CountlyViewTracking.sharedInstance performAutoViewTrackingForViewController:self.presentingVC];
self.presentingVC = nil;
}
}
- (BOOL)isPageSheetModal
{
//NOTE: iOS 13 check is not related to availability of UIModalPresentationPageSheet,
// but needed due to behavioral difference in presenting logic compared to previous iOS versions.
#if (TARGET_OS_IOS)
if (@available(iOS 13.0, *))
{
if (self.modalPresentationStyle == UIModalPresentationPageSheet && self.isBeingPresented)
{
return YES;
}
}
#endif
return NO;
}
- (void)setPresentingVC:(UIViewController *)presentingVC
{
objc_setAssociatedObject(self, @selector(presentingVC), presentingVC, OBJC_ASSOCIATION_ASSIGN);
}
- (UIViewController *)presentingVC
{
return objc_getAssociatedObject(self, @selector(presentingVC));
}
@end
#endif