forked from google/GTXiLib
-
Notifications
You must be signed in to change notification settings - Fork 0
/
GTXAnalyticsUtils.m
105 lines (91 loc) · 4.16 KB
/
GTXAnalyticsUtils.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
//
// Copyright 2018 Google Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
#import "GTXAnalyticsUtils.h"
#import "GTXLogging.h"
#import <CommonCrypto/CommonDigest.h>
/**
The endpoint that receives GTXiLib usage data.
*/
static NSString *const kGTXTrackingEndPoint = @"https://ssl.google-analytics.com/collect";
#pragma mark - Implementation
@implementation GTXAnalyticsUtils
/**
@return The clientID to be used by GTXiLib analytics, this is a hash of App's bundle ID.
*/
+ (NSString *)clientID {
static NSString *clientID;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
NSString *bundleIDMD5 = [[NSBundle mainBundle] bundleIdentifier];
if (!bundleIDMD5) {
// If bundle ID is not available we use a placeholder.
bundleIDMD5 = @"<Missing Bundle ID>";
}
// Get MD5 value of the given string.
unsigned char md5Value[CC_MD5_DIGEST_LENGTH];
const char *stringCPtr = [bundleIDMD5 UTF8String];
CC_MD5(stringCPtr, (CC_LONG)strlen(stringCPtr), md5Value);
// Parse MD5 value into individual hex values.
NSMutableString *stringWithHexMd5Values = [[NSMutableString alloc] init];
for (int i = 0; i < CC_MD5_DIGEST_LENGTH; i++) {
[stringWithHexMd5Values appendFormat:@"%02x", md5Value[i]];
}
clientID = [stringWithHexMd5Values copy];
});
return clientID;
}
/**
Util method to send an analytics event over to Google Analytics.
@param trackingID Google Analytics account tracking ID
@param clientID Client ID to use in the event.
@param category Event category to use in the event.
@param action Event action to use in the event.
@param value Event value to use in the event.
*/
+ (void)sendEventHitWithTrackingID:(NSString *)trackingID
clientID:(NSString *)clientID
category:(NSString *)category
action:(NSString *)action
value:(NSString *)value {
// Initialize the payload with version(=1), tracking ID, client ID, category, action, and value.
NSMutableString *payload = [[NSMutableString alloc] initWithFormat:@"v=1"
@"&t=event"
@"&tid=%@"
@"&cid=%@"
@"&ec=%@"
@"&ea=%@"
@"&ev=%@",
trackingID,
clientID,
category,
action,
value];
NSURLComponents *components = [[NSURLComponents alloc] initWithString:kGTXTrackingEndPoint];
[components setQuery:payload];
NSURL *url = [components URL];
[[[NSURLSession sharedSession] dataTaskWithURL:url
completionHandler:^(NSData *data,
NSURLResponse *response,
NSError *error) {
if (error) {
// Failed to send analytics data, but since the test might be running in a sandboxed
// environment it's not a good idea to freeze or throw assertions, let's just log and
// move on.
GTX_LOG(@"Failed to send analytics data due to: %@", error);
}
}] resume];
}
@end