forked from adswerve/Swift-GA-Tracker-for-Apple-tvOS
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGATracker.swift
169 lines (150 loc) · 5.5 KB
/
GATracker.swift
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
//
// AnalyticObject.swift
// TVSwiftMeasurementProtocol
//
// Created by Vincent Lee and Luka Cempre on 11/23/15.
// Copyright © 2015 Analytics Pros. All rights reserved.
//
import Foundation
private var _analyticsTracker: GATracker!
class GATracker {
/*
Define properties
@tid = Google Analytics property id
@cid = Google Analytics client id
@appName = Application Name
@appVersion = Application Version
@MPVersion = Measurement Protocol version
@ua = User Agent string
*/
private var tid : String
var cid: String
var appName : String
var appVersion : String
var MPVersion : String
var ua : String
var ul : String
//Set up singleton object for the tracker
class func setup(tid: String) -> GATracker {
struct Static {
static var onceToken: dispatch_once_t = 0
}
dispatch_once(&Static.onceToken) {
_analyticsTracker = GATracker(tid: tid)
}
return _analyticsTracker
}
class var sharedInstance: GATracker! {
if _analyticsTracker == nil {
#if DEBUG
print("Analytics Tracker not set up")
#endif
}
return _analyticsTracker
}
init(tid: String) {
/*
Initialize Tracker with Property Id
Set up all attributes
*/
#if DEBUG
print("Google Analytics Tracker Initialized")
#endif
self.tid = tid
self.appName = NSBundle.mainBundle().infoDictionary!["CFBundleName"] as! String
let nsObject: AnyObject? = NSBundle.mainBundle().infoDictionary!["CFBundleShortVersionString"]
self.appVersion = nsObject as! String
self.ua = "Mozilla/5.0 (Apple TV; CPU iPhone OS 9_0 like Mac OS X) AppleWebKit/601.1.46 (KHTML, like Gecko) Mobile/13T534YI"
self.MPVersion = "1"
let defaults = NSUserDefaults.standardUserDefaults()
if let cid = defaults.stringForKey("cid") {
self.cid = cid
}
else {
self.cid = NSUUID().UUIDString
defaults.setObject(self.cid, forKey: "cid")
}
let language = NSLocale.preferredLanguages().first
if language?.characters.count > 0 {
self.ul = language!
} else {
self.ul = "(not set)"
}
}
func send(type: String, params: Dictionary<String, String>) {
/*
Generic hit sender to Measurement Protocol
Consists out of hit type and a dictionary of other parameters
*/
let endpoint = "https://www.google-analytics.com/collect?"
var parameters = "v=" + self.MPVersion + "&an=" + self.appName + "&tid=" + self.tid + "&av=" + self.appVersion + "&cid=" + self.cid + "&t=" + type + "&ua=" + self.ua + "&ul=" + self.ul
for (key, value) in params {
parameters += "&" + key + "=" + value
}
//Encoding all the parameters
if let paramEndcode = parameters.stringByAddingPercentEncodingWithAllowedCharacters(NSCharacterSet.URLPathAllowedCharacterSet()){
let urlString = endpoint + paramEndcode;
let url = NSURL(string: urlString);
#if DEBUG
print(urlString)
#endif
let task = NSURLSession.sharedSession().dataTaskWithURL(url!) { (data, response, error) -> Void in
if let httpReponse = response as? NSHTTPURLResponse {
let statusCode = httpReponse.statusCode
#if DEBUG
print(statusCode)
#endif
}
else {
if (error != nil) {
#if DEBUG
print(error!.description)
#endif
}
}
}
task.resume()
}
}
func screenView(screenName: String, customParameters: Dictionary<String, String>?) {
/*
A screenview hit, use screenname
*/
var params = ["cd" : screenName]
if (customParameters != nil) {
for (key, value) in customParameters! {
params.updateValue(value, forKey: key)
}
}
self.send("screenview", params: params)
}
func event(category: String, action: String, label: String?, customParameters: Dictionary<String, String>?) {
/*
An event hit with category, action, label
*/
//event parameters category, action and label
var params = ["ec" : category, "ea" : action, "el" : label ?? ""]
if (customParameters != nil) {
for (key, value) in customParameters! {
params.updateValue(value, forKey: key)
}
}
self.send("event", params: params)
}
func exception(description: String, isFatal:Bool, customParameters: Dictionary<String, String>?) {
/*
An exception hit with exception description (exd) and "fatality" (Crashed or not) (exf)
*/
var fatal="0";
if (isFatal){
fatal = "1";
}
var params = ["exd":description, "exf":fatal]
if (customParameters != nil) {
for (key, value) in customParameters! {
params.updateValue(value, forKey: key)
}
}
self.send("exception", params: params)
}
}