-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPublisherAnalytics.cs
270 lines (233 loc) · 9.18 KB
/
PublisherAnalytics.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
//
// PublisherAnalytics.cs
//
// Publisher Analytics
// /~https://github.com/SpaceMadness/publisher-analytics
//
// Copyright 2017 Alex Lementuev, SpaceMadness.
//
// 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.
//
using System;
using System.Collections;
using System.Collections.Generic;
using System.Net;
using System.Text;
using UnityEngine;
using UnityEditor;
#if UNITY_EDITOR_WIN
using System.Net.Security;
using System.Security.Cryptography.X509Certificates;
#endif
namespace SpaceMadness
{
public static class PublisherAnalytics
{
public static readonly string kDisableApplicationTracking = "DisableApplicationTracking";
/// <summary>
/// Basic raw GET request URL for Google Analytics
/// </summary>
private static readonly string kTrackingURL = "https://www.google-analytics.com/collect";
private const int kUndefinedValue = int.MinValue;
/// <summary>
/// Basic app specific payload for the GET request
/// </summary>
private static string defaultPayload;
public static void Initialize(string trackingId, string packageVersion, IDictionary<string, bool> configuration = null)
{
if (string.IsNullOrEmpty(trackingId))
{
throw new ArgumentException("Tracking id is null or empty");
}
if (string.IsNullOrEmpty(packageVersion))
{
throw new ArgumentException("Package version is null or empty");
}
// Create shared package specific payload
defaultPayload = CreateDefaultPayload(trackingId, packageVersion, configuration ?? new Dictionary<string, bool>());
// Track package version update (if any)
TrackPackageVersionUpdate(trackingId, packageVersion);
}
/// <summary>
/// Notifies the server about plugin update.
/// </summary>
private static void TrackPackageVersionUpdate(string trackingId, string version)
{
var prefsKey = "Com.SpaceMadness.PublisherAnalytics." + trackingId + ".LastKnownPackageVersion";
var lastKnownVersion = EditorPrefs.GetString(prefsKey);
if (lastKnownVersion != version)
{
EditorPrefs.SetString(prefsKey, version);
TrackEvent("Version", "updated_version");
}
}
public static void TrackEvent(string category, string action, int value = kUndefinedValue)
{
if (defaultPayload == null)
{
Debug.LogWarningFormat("Can't track event '{0}': instance is not initialized", action);
return;
}
var payloadStr = CreatePayload(category, action, value);
if (payloadStr != null)
{
Log.d("Event track payload: " + payloadStr);
PublisherHttpClient downloader = new PublisherHttpClient(kTrackingURL);
downloader.UploadData(payloadStr, delegate(string result, Exception error)
{
if (error != null)
{
Log.e("Event track failed: " + error);
}
else
{
Log.d("Event track result: " + result);
}
});
}
}
private static string CreatePayload(string category, string action, int value)
{
var payload = new StringBuilder(defaultPayload);
payload.AppendFormat("&ec={0}", WWW.EscapeURL(category));
payload.AppendFormat("&ea={0}", WWW.EscapeURL(action));
if (value != kUndefinedValue)
{
payload.AppendFormat("&ev={0}", value.ToString());
}
return payload.ToString();
}
private static string CreateDefaultPayload(string trackingId, string packageVersion, IDictionary<string, bool> configuration)
{
var payload = new StringBuilder("v=1&t=event");
payload.AppendFormat("&tid={0}", trackingId);
payload.AppendFormat("&cid={0}", WWW.EscapeURL(SystemInfo.deviceUniqueIdentifier));
payload.AppendFormat("&ua={0}", WWW.EscapeURL(SystemInfo.operatingSystem));
payload.AppendFormat("&av={0}", WWW.EscapeURL(packageVersion));
#if UNITY_EDITOR
payload.AppendFormat("&ds={0}", "editor");
#else
payload.AppendFormat("&ds={0}", "player");
#endif
bool disableAppTracking;
configuration.TryGetValue(kDisableApplicationTracking, out disableAppTracking);
if (!disableAppTracking)
{
if (!string.IsNullOrEmpty(Application.productName))
{
var productName = WWW.EscapeURL(Application.productName);
if (productName.Length <= 100)
{
payload.AppendFormat("&an={0}", productName);
}
}
#if UNITY_5_6_OR_NEWER
var identifier = Application.identifier;
#else
var identifier = Application.bundleIdentifier;
#endif
if (!string.IsNullOrEmpty(identifier))
{
var bundleIdentifier = WWW.EscapeURL(identifier);
if (bundleIdentifier.Length <= 150)
{
payload.AppendFormat("&aid={0}", bundleIdentifier);
}
}
if (!string.IsNullOrEmpty(Application.companyName))
{
var companyName = WWW.EscapeURL(Application.companyName);
if (companyName.Length <= 150)
{
payload.AppendFormat("&aiid={0}", companyName);
}
}
}
return payload.ToString();
}
}
delegate void LunarConsoleHttpDownloaderCallback(string result,Exception error);
class PublisherHttpClient
{
private Uri m_uri;
private WebClient m_client;
#if UNITY_EDITOR_WIN
static PublisherHttpClient()
{
ServicePointManager.ServerCertificateValidationCallback = MyRemoteCertificateValidationCallback;
}
private static bool MyRemoteCertificateValidationCallback(System.Object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors)
{
if (sslPolicyErrors != SslPolicyErrors.None)
{
for (int i = 0; i < chain.ChainStatus.Length; i++)
{
if (chain.ChainStatus[i].Status != X509ChainStatusFlags.RevocationStatusUnknown)
{
chain.ChainPolicy.RevocationFlag = X509RevocationFlag.EntireChain;
chain.ChainPolicy.RevocationMode = X509RevocationMode.Online;
chain.ChainPolicy.UrlRetrievalTimeout = new TimeSpan(0, 1, 0);
chain.ChainPolicy.VerificationFlags = X509VerificationFlags.AllFlags;
bool chainIsValid = chain.Build((X509Certificate2)certificate);
if (!chainIsValid)
{
return false;
}
}
}
}
return true;
}
#endif // UNITY_EDITOR_WIN
public PublisherHttpClient(string uri)
: this(new Uri(uri))
{
}
public PublisherHttpClient(Uri uri)
{
if (uri == null)
{
throw new ArgumentNullException("Uri is null");
}
m_uri = uri;
m_client = new WebClient();
}
public void UploadData(string data, LunarConsoleHttpDownloaderCallback callback)
{
if (callback == null)
{
throw new ArgumentNullException("Callback is null");
}
if (m_client == null)
{
throw new InvalidOperationException("Already downloading something");
}
if (callback != null)
{
m_client.UploadStringCompleted += (object sender, UploadStringCompletedEventArgs e) => callback(e.Result, e.Error);
}
m_client.UploadStringAsync(m_uri, data);
}
}
static class Log
{
public static void d(string message)
{
Debug.Log(message);
}
public static void e(string message)
{
Debug.LogError(message);
}
}
}