forked from jaredmessenger/unity3d-google-analytics
-
Notifications
You must be signed in to change notification settings - Fork 1
/
GoogleAnalytics.cs
494 lines (410 loc) · 11.9 KB
/
GoogleAnalytics.cs
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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class GoogleAnalytics : MonoBehaviour {
public string propertyID;
public string defaultURL;
public static GoogleAnalytics instance;
private Hashtable sessionRequestParams = new Hashtable();
private List<Hashtable> eventList= new List<Hashtable>();
private string currentSessionStartTime;
private string lastSessionStartTime;
private string firstSessionStartTime;
private int sessions;
void Awake()
{
if(instance)
DestroyImmediate(gameObject);
else
{
DontDestroyOnLoad(gameObject);
instance = this;
}
}
public void Start(){
// Increment the number of times played
IncrSessions();
//Get the player prefs last time played and current time
currentSessionStartTime = GetEpochTime().ToString();
lastSessionStartTime = SavedLastSessionStartTime;
firstSessionStartTime = SavedFirstSessionStartTime;
sessions = NumSessions;
// Set the last session start time
SavedLastSessionStartTime = currentSessionStartTime;
string screenResolution = Screen.width.ToString() + "x" + Screen.height.ToString();
sessionRequestParams["utmac"] = propertyID;
sessionRequestParams["utmhn"] = SystemInfo.deviceType.ToString();
sessionRequestParams["utmfl"] = Application.unityVersion.ToString();
sessionRequestParams["utmsc"] = "24-bit";
sessionRequestParams["utmsr"] = screenResolution;
sessionRequestParams["utmwv"] = "4.9.1";
sessionRequestParams["utmul"] = "en-us";
sessionRequestParams["utmcs"] = "ISO-8859-1";
sessionRequestParams["utmcn"] = "1";
}
private Hashtable LevelSpecificRequestParams()
{
// Copy the session request params
Hashtable levelRequestParams = new Hashtable(sessionRequestParams);
levelRequestParams["utmdt"] = System.Uri.EscapeDataString( Application.loadedLevelName );
// Will be overridden if you use GALevel
levelRequestParams["utmp"] = System.Uri.EscapeDataString( Application.loadedLevelName );
return levelRequestParams;
}
public void Add(GALevel gaLevel)
{
Hashtable eventSpecificParams = (Hashtable)LevelSpecificRequestParams().Clone();
eventSpecificParams["utmcc"] = CookieData();
eventSpecificParams["utmn"] = Random.Range(1000000000,2000000000).ToString();
eventSpecificParams["utmp"] = gaLevel.ToUrlParamString();
eventList.Add(eventSpecificParams);
}
public void Add(GAEvent gaEvent)
{
Hashtable eventSpecificParams = (Hashtable)LevelSpecificRequestParams().Clone();
eventSpecificParams["utmt"] = GoogleTrackTypeToString( GoogleTrackType.GAEvent );
eventSpecificParams["utmcc"] = CookieData();
eventSpecificParams["utmn"] = Random.Range(1000000000,2000000000).ToString();
eventSpecificParams["utme"] = gaEvent.ToUrlParamString();
if (gaEvent.NonInteraction)
{
eventSpecificParams["utmni"] = 1;
}
eventList.Add(eventSpecificParams);
}
//
// For more info on user tracking https://developers.google.com/analytics/devguides/collection/gajs/gaTrackingTiming
//
public void Add(GAUserTimer gaUserTimer)
{
Hashtable eventSpecificParams = (Hashtable)LevelSpecificRequestParams().Clone();
eventSpecificParams["utmt"] = GoogleTrackTypeToString( GoogleTrackType.GATiming );
eventSpecificParams["utmn"] = Random.Range(1000000000,2000000000).ToString();
eventSpecificParams["utmcc"] = CookieData();
eventSpecificParams["utme"] = gaUserTimer.ToUrlParamString();
eventList.Add(eventSpecificParams);
}
//
// turns all the events into request urls
//
public void Dispatch()
{
for (int evtIndex=0; evtIndex<eventList.Count; evtIndex++)
{
string urlRequestParams = BuildRequestString(eventList[evtIndex]);
string url = "http://www.google-analytics.com/__utm.gif?" + urlRequestParams;
StartCoroutine( MakeRequest(url, eventList[evtIndex]) );
}
}
//
// send the request to google
//
IEnumerator MakeRequest(string url, Hashtable evt)
{
// HACK: make it think it's an image to get by crossdomain issues
#if UNITY_WEBPLAYER
if (url.Contains("?")) {
url += "&";
}
else {
url += "?";
}
url += "_fakeext=.jpg";
#endif
WWW request = new WWW(url);
yield return request;
if(request.error == null)
{
if (request.responseHeaders.ContainsKey("STATUS"))
{
if (request.responseHeaders["STATUS"].Contains("200"))
{
if (eventList.Contains(evt)) {
eventList.Remove(evt);
}
}
} else if (request.responseHeaders.ContainsKey("CONTENT-LENGTH")) {
// If the response didn't have a status in the header, check for content length (some androids return null as it's status)
foreach (string keyValue in request.responseHeaders.Values)
{
if (keyValue.Contains("200"))
{
if (eventList.Contains(evt)) {
eventList.Remove(evt);
}
break;
}
}
}else{
Debug.LogWarning("Event failed to send to Google");
}
}else{
Debug.LogWarning("GoogleAnalytics WWW failure: " + request.error.ToString());
}
}
private string GoogleTrackTypeToString(GoogleTrackType trackType)
{
switch(trackType)
{
case GoogleTrackType.GAEvent:
return "event";
case GoogleTrackType.GALevel:
return "page";
case GoogleTrackType.GATiming:
return "event";
default:
return "page";
}
}
private long DeviceIdentifier
{
get{ return Hash (SystemInfo.deviceUniqueIdentifier ); }
}
private int NumSessions
{
get{ return PlayerPrefs.GetInt("gaNumSessions"); }
}
private void IncrSessions()
{
int sessions = PlayerPrefs.GetInt("gaNumSessions");
sessions += 1;
PlayerPrefs.SetInt("gaNumSessions", sessions);
}
private string SavedFirstSessionStartTime
{
get{ if (PlayerPrefs.HasKey("gaFirstSessionStartTime"))
{
return PlayerPrefs.GetString("gaFirstSessionStartTime");
}else{
long currentTime = GetEpochTime();
PlayerPrefs.SetString("gaFirstSessionStartTime", currentTime.ToString());
return PlayerPrefs.GetString("gaFirstSessionStartTime");
}
}
}
private string SavedLastSessionStartTime
{
get{
if (PlayerPrefs.HasKey("gaLastSessionStartTime"))
{
return PlayerPrefs.GetString("gaLastSessionStartTime");
}else{
string firstSession = SavedFirstSessionStartTime;
PlayerPrefs.SetString("gaLastSessionStartTime", firstSession);
return firstSession;
}
}
set{ PlayerPrefs.SetString("gaLastSessionStartTime", value.ToString()); }
}
// Grab the cookie data for every event/pageview because it grabs the current time
private string CookieData()
{
long domainHash = Hash(defaultURL);
// __utma Identifies unique Visitors
// <Domain hash>.<visitor id>.<time of initial session>.<time of previsous session>.<time of current session>.<session number>
string _utma = domainHash + "." + DeviceIdentifier + "." + firstSessionStartTime + "." +
lastSessionStartTime + "." + currentSessionStartTime + "." + sessions + WWW.EscapeURL(";") + WWW.EscapeURL("+");
// __utmz Referral information in the cookie
string cookieUTMZstr = "utmcsr" + WWW.EscapeURL("=") + "(direct)" + WWW.EscapeURL("|") +
"utmccn" + WWW.EscapeURL("=") + "(direct)" + WWW.EscapeURL("|") +
"utmcmd" + WWW.EscapeURL("=") + "(none)" + WWW.EscapeURL(";");
string _utmz = domainHash + "." + currentSessionStartTime + "." + sessions + ".1." + cookieUTMZstr;
return "__utma" + WWW.EscapeURL("=") + _utma + "__utmz" + WWW.EscapeURL("=") + _utmz;
}
private string BuildRequestString(Hashtable urlParams)
{
List<string> args = new List<string>();
foreach( string key in urlParams.Keys )
{
args.Add( key + "=" + urlParams[key] );
}
return string.Join("&", args.ToArray());
}
private long Hash(string url)
{
if(url.Length < 3) return Random.Range(10000000,99999999);
int hash = 0;
int hashCmp = 0;
for(int urlLen=url.Length-1; urlLen>=0; urlLen--)
{
int charCode = (int)url[urlLen];
hash = (hash<<6&268435455) + charCode + (charCode<<14);
hashCmp = hash&266338304;
hash = hashCmp != 0 ? hash^hashCmp>>21 : hash;
}
return hash;
}
public long GetEpochTime()
{
System.DateTime currentTime = System.DateTime.Now;
System.DateTime epochStart = System.Convert.ToDateTime("1/1/1970 0:00:00 AM");
System.TimeSpan timeSpan = currentTime.Subtract(epochStart);
long epochTime = ((((((timeSpan.Days * 24) + timeSpan.Hours) * 60) + timeSpan.Minutes) * 60) + timeSpan.Seconds);
return epochTime;
}
}
public enum GoogleTrackType{
GALevel,
GAEvent,
GATiming,
}
public class GALevel
{
private string _page_name;
public GALevel ()
{
_page_name = Application.loadedLevelName;
}
public GALevel (string levelName)
{
_page_name = levelName;
}
public string Level
{
get{ return _page_name; }
set{ _page_name = value; }
}
public string ToUrlParamString()
{
if (Level == null)
{
throw new System.ArgumentException("GALevel: Please Specify a Level Name");
}
return System.Uri.EscapeDataString( Level );
}
}
public class GAEvent
{
private string _category;
private string _action;
private string _opt_label;
private int _opt_value = -1;
private bool _opt_noninteraction = false;
public GAEvent(string category, string action)
{
Category = category;
Action = action;
}
public GAEvent(string category, string action, string label)
{
Category = category;
Action = action;
Label = label;
}
public GAEvent(string category, string action, string label, int opt_value)
{
Category = category;
Action = action;
Label = label;
Value = opt_value;
}
public string Category
{
get{ return _category; }
set{ _category = value; }
}
public string Action
{
get{ return _action;}
set{ _action = value;}
}
public string Label
{
get{ return _opt_label; }
set{ _opt_label = value; }
}
public int Value
{
get{ return _opt_value; }
set{ _opt_value = value; }
}
public bool NonInteraction
{
get{ return _opt_noninteraction;}
set{ _opt_noninteraction = value; }
}
public string ToUrlParamString()
{
//"5(<category>*<action>*<label>*<value>)"
string utme = "5(";
utme += System.Uri.EscapeDataString( Category );
utme += "*" + System.Uri.EscapeDataString( Action );
if (Category == null || Action == null)
{
throw new System.ArgumentException("GAEvent: Category and Action must be specified");
}
if (Label != null)
{
utme += "*" + System.Uri.EscapeDataString( Label );
}
if (Value != -1)
{
utme += ")(" + Value ;
}
utme += ")";
return utme;
}
}
public class GAUserTimer
{
private string _category;
private string _variable;
private string _label;
private long _startTime;
private long _stopTime;
public GAUserTimer(string category, string variable)
{
Category = category;
Variable = variable;
}
public GAUserTimer(string category, string variable, string label)
{
Category = category;
Variable = variable;
Label = label;
}
public string Category
{
get{ return _category; }
set{ _category = value; }
}
public string Variable
{
get{ return _variable; }
set{ _variable = value; }
}
public string Label
{
get{ return _label; }
set{ _label = value; }
}
public void Start(){
_startTime = GoogleAnalytics.instance.GetEpochTime();
}
public void Stop()
{
_stopTime = GoogleAnalytics.instance.GetEpochTime();
}
private long ElapsedTime()
{
if (_startTime == 0 || _stopTime == 0)
{
throw new System.ArgumentException("To get the elapsed time please specify the start and end time");
}
return (_stopTime - _startTime) * 1000;
}
public string ToUrlParamString()
{
string utme = "14(90!";
utme += System.Uri.EscapeDataString( Variable );
utme += "*" + System.Uri.EscapeDataString( Category );
utme += "*" + ElapsedTime().ToString();
if (Label != null)
{
utme += "*" + System.Uri.EscapeDataString( Label );
}
utme += ")";
utme += "(90!" + ElapsedTime().ToString() + ")";
return utme;
}
}