forked from Countly/countly-sdk-ios
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCountlyViewTracking.m
155 lines (125 loc) · 4.24 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
// 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, strong) NSString* _Nonnull lastView;
@property (nonatomic) NSTimeInterval lastViewStartTime;
@property (nonatomic, strong) NSMutableArray* exceptionViewControllers;
@end
NSString* const kCountlyReservedEventView = @"[CLY]_view";
@implementation CountlyViewTracking
+ (instancetype)sharedInstance
{
static CountlyViewTracking* 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)
{
self.exceptionViewControllers = NSMutableArray.new;
NSArray* internalExceptionViewControllers =
@[
@"UINavigationController",
@"UIAlertController",
@"UIPageViewController",
@"UITabBarController",
@"UIReferenceLibraryViewController",
@"UISplitViewController",
@"UIInputViewController",
@"UISearchController",
@"UISearchContainerViewController",
@"UIApplicationRotationFollowingController"
];
[internalExceptionViewControllers enumerateObjectsUsingBlock:^(id _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop)
{
Class c = NSClassFromString(obj);
if(c)
[self.exceptionViewControllers addObject:c];
}];
}
return self;
}
- (void)reportView:(NSString* _Nonnull)viewName
{
[self endView];
COUNTLY_LOG(@"View tracking started: %@", viewName);
NSMutableDictionary* segmentation =
@{
@"name": viewName,
@"segment": CountlyDeviceInfo.osName,
@"visit": @1
}.mutableCopy;
if(!self.lastView)
segmentation[@"start"] = @1;
[Countly.sharedInstance recordEvent:kCountlyReservedEventView segmentation:segmentation];
self.lastView = viewName;
self.lastViewStartTime = CountlyCommon.sharedInstance.uniqueTimestamp;
}
- (void)endView
{
if(self.lastView)
{
NSDictionary* segmentation =
@{
@"name": self.lastView,
@"segment": CountlyDeviceInfo.osName,
};
NSTimeInterval duration = NSDate.date.timeIntervalSince1970 - self.lastViewStartTime;
[Countly.sharedInstance recordEvent:kCountlyReservedEventView segmentation:segmentation count:1 sum:0 duration:duration timestamp:self.lastViewStartTime];
COUNTLY_LOG(@"View tracking ended: %@ duration: %f", self.lastView, duration);
}
}
#if TARGET_OS_IOS
- (void)startAutoViewTracking
{
self.isAutoViewTrackingEnabled = YES;
Method O_method;
Method C_method;
O_method = class_getInstanceMethod(UIViewController.class, @selector(viewDidAppear:));
C_method = class_getInstanceMethod(UIViewController.class, @selector(Countly_viewDidAppear:));
method_exchangeImplementations(O_method, C_method);
}
- (void)addExceptionForAutoViewTracking:(Class _Nullable)exceptionViewControllerSubclass
{
[self.exceptionViewControllers addObject:exceptionViewControllerSubclass];
}
- (void)removeExceptionForAutoViewTracking:(Class _Nullable)exceptionViewControllerSubclass
{
[self.exceptionViewControllers removeObject:exceptionViewControllerSubclass];
}
#endif
@end
#if TARGET_OS_IOS
@implementation UIViewController (CountlyViewTracking)
- (void)Countly_viewDidAppear:(BOOL)animated
{
if(CountlyViewTracking.sharedInstance.isAutoViewTrackingEnabled)
{
__block BOOL isExceptionClass = NO;
[CountlyViewTracking.sharedInstance.exceptionViewControllers enumerateObjectsUsingBlock:^(id _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop)
{
if([self isKindOfClass:obj])
{
isExceptionClass = YES;
*stop = YES;
}
}];
if(!isExceptionClass)
{
NSString* viewTitle = self.title;
if(!viewTitle)
viewTitle = NSStringFromClass([self class]);
[CountlyViewTracking.sharedInstance reportView:viewTitle];
}
}
[self Countly_viewDidAppear:animated];
}
@end
#endif