-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathENGAManager.m
204 lines (185 loc) · 6.35 KB
/
ENGAManager.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
//
// ENGAManager.m
// ENGoogleMeasurementProtocol
//
// Created by Steve High on 1/9/14.
// Copyright (c) 2014 Evilnode Software. All rights reserved.
//
#import "ENGAManager.h"
#import "ENGAOperation.h"
@interface ENGAManager()
@property (nonatomic, strong) NSMutableDictionary *defaults;
@property (nonatomic, strong) NSDictionary *gKeys;
- (NSDictionary *) resolvedParams:(NSDictionary *)instanceParams;
@end
@implementation ENGAManager
@synthesize defaults = _defaults;
@synthesize gKeys = _gKeys;
@synthesize userAgent = _userAgent;
@synthesize userHeaders = _userHeaders;
- (id) init
{
self = [super init];
if (self) {
_userAgent = [NSString stringWithFormat:@"%@ v%@", ENGA_USERAGENT_STRING, ENGA_VERSION];
_defaults = [NSMutableDictionary dictionaryWithCapacity:5];
_gKeys = @{
@(kVersionKey): @"v",
@(kTrackingIDKey): @"tid",
@(kClientIDKey): @"cid",
@(kCampaignNameKey): @"cn",
@(kCampaignSourceKey): @"cs",
@(kCampaignMediumKey): @"cm",
@(kCampaignKeywordKey): @"ck",
@(kCampaignContentKey): @"cc",
@(kCampaignIDKey): @"ci",
@(kAdwordsIDKey): @"gclid",
@(kDisplayAdsIDKey): @"dclid",
@(kShouldAnonymizeIPKey): @"aip",
@(kShouldUseSessionControlKey): @"sc",
@(kScreenResolutionKey): @"sr",
@(kViewportSizeKey): @"vp",
@(kDocumentEncodingKey): @"de",
@(kScreenColorsKey): @"sd",
@(kUserLanguageKey): @"ul",
@(kHitTypeKey): @"t",
@(kAppNameKey): @"an",
@(kAppVersionKey): @"av",
@(kTransactionIDKey): @"ti",
@(kTransactionAffiliationKey): @"ta",
@(kTransactionRevenueKey): @"tr",
@(kTransactionShippingKey): @"ts",
@(kTransactionTaxKey): @"tt",
@(kNonInteractiveHitKey): @"ni",
@(kContentDescriptionKey): @"cd",
@(kLinkIDKey): @"linkid",
@(kEventCategoryKey): @"ec",
@(kEventActionKey): @"ea",
@(kEventLabelKey): @"el",
@(kEventValueKey): @"ev",
@(kItemNameKey): @"in",
@(kItemPriceKey): @"ip",
@(kItemQuantityKey): @"iq",
@(kItemCodeKey): @"ic",
@(kItemCategoryKey): @"iv",
@(kCurrencyCodeKey): @"cu",
@(kSocialNetworkKey): @"sn",
@(kSocialActionKey): @"sa",
@(kSocialActionTargetKey): @"st",
@(kUserTimingCategoryKey): @"utc",
@(kUserTimingVariableNameKey): @"utv",
@(kUserTimingTimeKey): @"utt",
@(kUserTimingLabelKey): @"utl",
@(kPageLoadTimeKey): @"plt",
@(kDNSTimeKey): @"dns",
@(kPageDownloadTimeKey): @"pdt",
@(kRedirectResponseTimeKey): @"rrt",
@(kTCPConnectTimeKey): @"tcp",
@(kServerResponseTimeKey): @"srt",
@(kExceptionDescriptionKey): @"exd",
@(kExceptionFatalKey): @"exf",
@(kExperimentIDKey): @"xid",
@(kExperimentVariantKey): @"xvar"
};
_userHeaders = @{};
}
return self;
}
+ (id) sharedManager
{
__strong static ENGAManager *_instance = nil;
static dispatch_once_t dot;
dispatch_once(&dot, ^{
_instance = [[ENGAManager alloc] init];
});
return _instance;
}
- (NSString *) clientToken
{
NSUserDefaults *userdefaults = [NSUserDefaults standardUserDefaults];
NSString *ret = [userdefaults valueForKey:ENGA_CID_NAME];
if (ret == nil) {
CFUUIDRef uuid = CFUUIDCreate(kCFAllocatorDefault);
ret = (__bridge_transfer NSString *)CFUUIDCreateString(kCFAllocatorDefault, uuid);
CFRelease(uuid);
[userdefaults setValue:ret forKey:ENGA_CID_NAME];
[userdefaults synchronize];
}
return ret;
}
- (void) registerDefaults:(NSDictionary *)params
{
for (NSNumber *key in params) {
self.defaults[key] = params[key];
}
}
- (NSDictionary *) resolvedParams:(NSDictionary *)instanceParams
{
NSMutableDictionary *resolved = [NSMutableDictionary
dictionaryWithCapacity:([self.defaults count] + [instanceParams count])];
for (NSNumber *key in self.defaults) {
NSString *resolvedKey = self.gKeys[key];
if (nil == resolvedKey) {
continue;
}
resolved[resolvedKey] = self.defaults[key];
}
for (NSNumber *key in instanceParams) {
NSString *resolvedKey = self.gKeys[key];
if (nil == resolvedKey) {
continue;
}
resolved[resolvedKey] = instanceParams[key];
}
return [NSDictionary dictionaryWithDictionary:resolved];
}
- (void) performActionWithType:(ENGAHitType)t hitTypeString:(NSString *)typestr params:(NSDictionary *)params
{
NSMutableDictionary *paramsWithHitType = [NSMutableDictionary dictionaryWithDictionary:params];
paramsWithHitType[@(kHitTypeKey)] = typestr;
NSDictionary *rawParams = [self resolvedParams:paramsWithHitType];
ENGAOperation *op = [ENGAOperation operation];
op.rawParams = rawParams;
op.requestParams = @{
@(kReservedUserAgentKey): self.userAgent
};
op.requestHeaders = self.userHeaders;
#ifdef ENGA_USER_PROVIDED_OPQUEUE
[self.opQueue addOperation:op];
#else
[[NSOperationQueue mainQueue] addOperation:op];
#endif
}
- (void) pageView:(NSDictionary *)params
{
[self performActionWithType:kHitTypePageView hitTypeString:@"pageview" params:params];
}
- (void) event:(NSDictionary *)params
{
[self performActionWithType:kHitTypeEvent hitTypeString:@"event" params:params];
}
- (void) appView:(NSDictionary *)params
{
[self performActionWithType:kHitTypeAppView hitTypeString:@"appview" params:params];
}
- (void) transaction:(NSDictionary *)params
{
[self performActionWithType:kHitTypeTransaction hitTypeString:@"transaction" params:params];
}
- (void) item:(NSDictionary *)params
{
[self performActionWithType:kHitTypeItem hitTypeString:@"item" params:params];
}
- (void) social:(NSDictionary *)params
{
[self performActionWithType:kHitTypeSocial hitTypeString:@"social" params:params];
}
- (void) exception:(NSDictionary *)params
{
[self performActionWithType:kHitTypeException hitTypeString:@"exception" params:params];
}
- (void) timing:(NSDictionary *)params
{
[self performActionWithType:kHitTypeTiming hitTypeString:@"timing" params:params];
}
@end