forked from orta/ARAnalytics
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ARDSL.m
210 lines (160 loc) · 7.63 KB
/
ARDSL.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
//
// ARDSL.m
// Artsy
//
// Created by Ash Furrow on 04/29/14.
// Copyright (c) 2014 Artsy. All rights reserved.
//
#import "ARDSL.h"
#import <Aspects/Aspects.h>
NSString * const ARAnalyticsTrackedEvents = @"trackedEvents";
NSString * const ARAnalyticsTrackedScreens = @"trackedScreens";
NSString * const ARAnalyticsClass = @"class";
NSString * const ARAnalyticsDetails = @"details";
NSString * const ARAnalyticsPageName = @"pageName";
NSString * const ARAnalyticsPageNameKeyPath = @"keypath";
NSString * const ARAnalyticsEventName = @"event";
NSString * const ARAnalyticsSelectorName = @"selector";
NSString * const ARAnalyticsEventProperties = @"properties";
NSString * const ARAnalyticsShouldFire = @"shouldFire";;
static NSMutableDictionary *ARAnalyticsHooksStore;
static BOOL ar_shouldFireForInstance (NSDictionary *dictionary, id instance, NSArray *arguments) {
ARAnalyticsEventShouldFireBlock shouldFireBlock = dictionary[ARAnalyticsShouldFire];
BOOL shouldFire;
if (shouldFireBlock) {
shouldFire = shouldFireBlock(instance, arguments);
} else {
shouldFire = YES;
}
return shouldFire;
}
@implementation ARAnalytics (DSL)
+ (void)setupWithAnalytics:(NSDictionary *)analyticsDictionary configuration:(NSDictionary *)configurationDictionary {
[self setupWithAnalytics:analyticsDictionary];
NSArray *trackedScreenClasses = configurationDictionary[ARAnalyticsTrackedScreens];
[trackedScreenClasses enumerateObjectsUsingBlock:^(NSDictionary *screenDictionary, NSUInteger idx, BOOL *stop) {
[self addScreenMonitoringAnalyticsHook:screenDictionary];
}];
NSArray *trackedEventClasses = configurationDictionary[ARAnalyticsTrackedEvents];
[trackedEventClasses enumerateObjectsUsingBlock:^(NSDictionary *eventDictionary, NSUInteger idx, BOOL *stop) {
[self addEventAnalyticsHooks:eventDictionary];
}];
}
+ (void)addEventAnalyticsHooks:(NSDictionary *)eventDictionary {
Class klass = eventDictionary[ARAnalyticsClass];
NSArray *analyticsDetails = eventDictionary[ARAnalyticsDetails];
[analyticsDetails enumerateObjectsUsingBlock:^(NSDictionary *detailsDictionary, NSUInteger idx, BOOL *stop) {
NSError *error = nil;
SEL selector = [self selectorForEventAnalyticsDetails:detailsDictionary];
NSString *event = detailsDictionary[ARAnalyticsEventName];
id aspectRef = [klass aspect_hookSelector:selector withOptions:AspectPositionAfter usingBlock:^(__unsafe_unretained id instance, NSArray *arguments) {
BOOL shouldFire = ar_shouldFireForInstance(detailsDictionary, instance, arguments);
if (shouldFire) {
NSDictionary *properties = nil;
ARAnalyticsEventPropertiesBlock propertiesBlock = detailsDictionary[ARAnalyticsEventProperties];
if (propertiesBlock) {
properties = propertiesBlock(instance, arguments);
}
[ARAnalytics event:event withProperties:properties];
}
} error:&error];
if (error) {
NSLog(@"ARAnalytics DSL: Error setting up hook for %@ on %@ - %@", klass, NSStringFromSelector(selector), error.localizedDescription);
} else {
[self storeAspectRef:aspectRef forClass:klass andSelector:selector];
}
}];
}
+ (void)addScreenMonitoringAnalyticsHook:(NSDictionary *)screenDictionary {
Class klass = screenDictionary[ARAnalyticsClass];
NSArray *analyticsDetails = screenDictionary[ARAnalyticsDetails];
[analyticsDetails enumerateObjectsUsingBlock:^(id analyticsDictionary, NSUInteger idx, BOOL *stop) {
NSError *error = nil;
SEL selector = [self selectorForScreenAnalyticsDetails:analyticsDictionary class:klass];
// Try to grab the page name from the dictionary.
NSString *dictionaryPageName = analyticsDictionary[ARAnalyticsPageName];
// If there wasn't one, then try to invoke keypath.
NSString *pageNameKeypath = analyticsDictionary[ARAnalyticsPageNameKeyPath];
id aspectRef = [klass aspect_hookSelector:selector withOptions:AspectPositionAfter usingBlock:^(__unsafe_unretained id instance, NSArray *arguments) {
BOOL shouldFire = ar_shouldFireForInstance(analyticsDictionary, instance, arguments);
if (shouldFire) {
NSString *pageName;
if (dictionaryPageName) {
pageName = dictionaryPageName;
} else {
pageName = [instance valueForKeyPath:pageNameKeypath];
NSAssert(pageName, @"Value for Key on `%@` returned nil from instance of class: `%@`", pageNameKeypath, NSStringFromClass([instance class]));
}
[ARAnalytics pageView:pageName];
}
} error:&error];
if (error) {
NSLog(@"ARAnalytics DSL: Error setting up hook for %@ on %@ - %@", klass, NSStringFromSelector(selector), error.localizedDescription);
} else {
[self storeAspectRef:aspectRef forClass:klass andSelector:selector];
}
}];
}
+ (SEL)selectorForEventAnalyticsDetails:(NSDictionary *)detailsDictionary
{
NSString *selectorName = detailsDictionary[ARAnalyticsSelectorName];
SEL selector = NSSelectorFromString(selectorName);
NSAssert(selector != NULL, @"Event selector lookup failed.");
return selector;
}
+ (SEL)selectorForScreenAnalyticsDetails:(NSDictionary *)dictionary class:(Class)klass
{
SEL selector;
NSString *selectorName = dictionary[ARAnalyticsSelectorName];
if (selectorName) {
selector = NSSelectorFromString(selectorName);
} else {
#if TARGET_OS_IPHONE
selector = @selector(viewDidAppear:);
NSAssert([[klass new] isKindOfClass:[UIViewController class]], @"Default selector of viewDidAppear: must only be used on classes extending UIViewController. ");
#else
@throw [NSException exceptionWithName:NSInternalInconsistencyException
reason:@"You must specify a selector name for page views on OS X."
userInfo:nil];
#endif
}
return selector;
}
NSString *ARKeyForClassAndSelector(Class klass, SEL selector) {
return [NSStringFromClass(klass) stringByAppendingFormat:@"-%@", NSStringFromSelector(selector)];
}
+ (void)storeAspectRef:(id)ref forClass:(Class)klass andSelector:(SEL)selector {
if(!ARAnalyticsHooksStore){
ARAnalyticsHooksStore = [NSMutableDictionary dictionary];
}
NSString *key = ARKeyForClassAndSelector(klass, selector);
ARAnalyticsHooksStore[key] = ref;
}
+ (BOOL)removeEventsAnalyticsHooks:(NSDictionary *)analyticsDictionary
{
SEL selector = [self selectorForEventAnalyticsDetails:analyticsDictionary];
Class klass = analyticsDictionary[ARAnalyticsClass];
NSString *key = ARKeyForClassAndSelector(klass, selector);
id <Aspect> aspectRef = ARAnalyticsHooksStore[key];
return [aspectRef remove];
}
+ (BOOL)removeScreenMonitoringAnalyticsHooks:(NSDictionary *)screenDictionary
{
Class klass = screenDictionary[ARAnalyticsClass];
SEL selector = [self selectorForScreenAnalyticsDetails:screenDictionary[ARAnalyticsDetails] class:klass];
NSString *key = ARKeyForClassAndSelector(klass, selector);
id <Aspect> aspectRef = ARAnalyticsHooksStore[key];
return [aspectRef remove];
}
+ (BOOL)removeAllAnalyticsHooks
{
__block BOOL success = YES;
[ARAnalyticsHooksStore enumerateKeysAndObjectsUsingBlock:^(NSString *key, id <Aspect> aspectRef, BOOL *stop) {
if ([aspectRef remove] == NO){
success = NO;
}
}];
[ARAnalyticsHooksStore removeAllObjects];
return success;
}
@end