Skip to content

Commit 4a5fc3a

Browse files
committed
Make requests using yield-return/Coroutine
Fixes googleanalytics#10 - prevents infinite loop in web player. Also added error message if tracking code has not been set.
1 parent 20d891d commit 4a5fc3a

File tree

3 files changed

+64
-22
lines changed

3 files changed

+64
-22
lines changed

googleanalyticsv3.unitypackage

-133 Bytes
Binary file not shown.

source/Plugins/GoogleAnalyticsV3/GoogleAnalyticsMPV3.cs

Lines changed: 54 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -40,18 +40,23 @@ public class GoogleAnalyticsMPV3 {
4040
private string clientId;
4141
private string url;
4242
private float timeStarted;
43-
private Dictionary<Field, object> trackerValues;
43+
private Dictionary<Field, object> trackerValues = new Dictionary<Field, object>();
4444
private bool startSessionOnNextHit = false;
4545
private bool endSessionOnNextHit = false;
46+
private bool trackingCodeSet = true;
4647

4748
public void InitializeTracker() {
49+
if(String.IsNullOrEmpty(trackingCode)){
50+
Debug.Log("No tracking code set for 'Other' platforms - hits will not be set");
51+
trackingCodeSet = false;
52+
return;
53+
}
4854
if (GoogleAnalyticsV3.belowThreshold(logLevel, GoogleAnalyticsV3.DebugMode.INFO)) {
4955
Debug.Log("Platform is not Android or iOS - " +
5056
"hits will be sent using measurement protocol.");
5157
}
5258
screenRes = Screen.width + "x" + Screen.height;
5359
clientId = SystemInfo.deviceUniqueIdentifier;
54-
trackerValues = new Dictionary<Field, object>();
5560
string language = Application.systemLanguage.ToString();
5661
optOut = false;
5762
#if !UNITY_WP8
@@ -89,6 +94,9 @@ public void SetTrackerVal(Field field, object value) {
8994
}
9095

9196
private string AddTrackerVals() {
97+
if(!trackingCodeSet){
98+
return "";
99+
}
92100
string vals = "";
93101
foreach (KeyValuePair<Field, object> pair in trackerValues){
94102
vals += AddOptionalMPParameter(pair.Key, pair.Value);
@@ -105,6 +113,12 @@ internal void StopSession() {
105113
}
106114

107115
private void SendGaHitWithMeasurementProtocol(string url) {
116+
if (String.IsNullOrEmpty(url)) {
117+
if (GoogleAnalyticsV3.belowThreshold(logLevel, GoogleAnalyticsV3.DebugMode.WARNING)) {
118+
Debug.Log("No tracking code set for 'Other' platforms - hit will not be sent.");
119+
}
120+
return;
121+
}
108122
if (dryRun || optOut) {
109123
if (GoogleAnalyticsV3.belowThreshold(logLevel, GoogleAnalyticsV3.DebugMode.WARNING)) {
110124
Debug.Log("Dry run or opt out enabled - hits will not be sent.");
@@ -123,30 +137,41 @@ private void SendGaHitWithMeasurementProtocol(string url) {
123137
if (GoogleAnalyticsV3.belowThreshold(logLevel, GoogleAnalyticsV3.DebugMode.VERBOSE)) {
124138
Debug.Log(newUrl);
125139
}
126-
WWW request = new WWW(newUrl);
127-
while (!request.isDone) {
128-
}
129-
if (request.responseHeaders.ContainsKey("STATUS")) {
130-
if (request.responseHeaders["STATUS"] == "HTTP/1.1 200 OK") {
131-
if (GoogleAnalyticsV3.belowThreshold(logLevel, GoogleAnalyticsV3.DebugMode.INFO)) {
132-
Debug.Log("Successfully sent Google Analytics hit.");
140+
GoogleAnalyticsV3.getInstance().StartCoroutine(this.HandleWWW(new WWW(newUrl)));
141+
}
142+
143+
/*
144+
Make request using yield and coroutine to prevent lock up waiting on request to return.
145+
*/
146+
public IEnumerator HandleWWW(WWW request)
147+
{
148+
while (!request.isDone)
149+
{
150+
yield return request;
151+
if (request.responseHeaders.ContainsKey("STATUS")) {
152+
if (request.responseHeaders["STATUS"] == "HTTP/1.1 200 OK") {
153+
if (GoogleAnalyticsV3.belowThreshold(logLevel, GoogleAnalyticsV3.DebugMode.INFO)) {
154+
Debug.Log("Successfully sent Google Analytics hit.");
155+
}
156+
} else {
157+
if (GoogleAnalyticsV3.belowThreshold(logLevel, GoogleAnalyticsV3.DebugMode.WARNING)) {
158+
Debug.LogWarning("Google Analytics hit request rejected with" +
159+
"status code " + request.responseHeaders["STATUS"]);
160+
}
133161
}
134162
} else {
135163
if (GoogleAnalyticsV3.belowThreshold(logLevel, GoogleAnalyticsV3.DebugMode.WARNING)) {
136-
Debug.LogWarning("Google Analytics hit request rejected with" +
137-
"status code " + request.responseHeaders["STATUS"]);
164+
Debug.LogWarning("Google Analytics hit request failed with error "
165+
+ request.error);
138166
}
139167
}
140-
} else {
141-
if (GoogleAnalyticsV3.belowThreshold(logLevel, GoogleAnalyticsV3.DebugMode.WARNING)) {
142-
Debug.LogWarning("Google Analytics hit request failed with error "
143-
+ request.error);
144-
}
145168
}
146169
}
147170

148171
private string AddRequiredMPParameter(Field parameter, object value) {
149-
if (value == null) {
172+
if(!trackingCodeSet){
173+
return "";
174+
} else if (value == null) {
150175
if (GoogleAnalyticsV3.belowThreshold(logLevel, GoogleAnalyticsV3.DebugMode.WARNING)) {
151176
Debug.LogWarning("Value was null for required parameter " + parameter + ". Hit cannot be sent");
152177
}
@@ -157,7 +182,9 @@ private string AddRequiredMPParameter(Field parameter, object value) {
157182
}
158183

159184
private string AddRequiredMPParameter(Field parameter, string value) {
160-
if (value == null) {
185+
if(!trackingCodeSet){
186+
return "";
187+
} else if (value == null) {
161188
if (GoogleAnalyticsV3.belowThreshold(logLevel, GoogleAnalyticsV3.DebugMode.WARNING)) {
162189
Debug.LogWarning("Value was null for required parameter " + parameter + ". Hit cannot be sent");
163190
}
@@ -168,22 +195,25 @@ private string AddRequiredMPParameter(Field parameter, string value) {
168195
}
169196

170197
private string AddOptionalMPParameter(Field parameter, object value) {
171-
if (value == null) {
198+
if (value == null || !trackingCodeSet) {
172199
return "";
173200
} else {
174201
return parameter + "=" + WWW.EscapeURL(value.ToString());
175202
}
176203
}
177204

178205
private string AddOptionalMPParameter(Field parameter, string value) {
179-
if (String.IsNullOrEmpty(value)) {
206+
if (String.IsNullOrEmpty(value) || !trackingCodeSet) {
180207
return "";
181208
} else {
182209
return parameter + "=" + WWW.EscapeURL(value);
183210
}
184211
}
185212

186213
private string AddCustomVariables<T>(HitBuilder<T> builder) {
214+
if(!trackingCodeSet){
215+
return "";
216+
}
187217
String url = "";
188218
foreach(KeyValuePair<int, string> entry in builder.GetCustomDimensions())
189219
{
@@ -210,6 +240,9 @@ private string AddCustomVariables<T>(HitBuilder<T> builder) {
210240

211241

212242
private string AddCampaignParameters<T>(HitBuilder<T> builder) {
243+
if(!trackingCodeSet){
244+
return "";
245+
}
213246
String url = "";
214247
url += AddOptionalMPParameter(Fields.CAMPAIGN_NAME, builder.GetCampaignName());
215248
url += AddOptionalMPParameter(Fields.CAMPAIGN_SOURCE, builder.GetCampaignSource());
@@ -382,4 +415,5 @@ public void SetDryRun(bool dryRun) {
382415
public void SetOptOut(bool optOut) {
383416
this.optOut = optOut;
384417
}
418+
385419
}

source/Plugins/GoogleAnalyticsV3/GoogleAnalyticsV3.cs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,12 +73,14 @@ public enum DebugMode {
7373
[Tooltip("If checked, hits will not be dispatched. Use for testing.")]
7474
public bool dryRun = false;
7575

76-
// TODO(emmanuellemm): Create conditional textbox attribute
76+
// TODO: Create conditional textbox attribute
7777
[Tooltip("The amount of time in seconds your application can stay in" +
7878
"the background before the session is ended. Default is 30 minutes" +
7979
" (1800 seconds). A value of -1 will disable session management.")]
8080
public int sessionTimeout = 1800;
8181

82+
public static GoogleAnalyticsV3 instance = null;
83+
8284
[HideInInspector]
8385
public readonly static string currencySymbol = "USD";
8486
public readonly static string EVENT_HIT = "createEvent";
@@ -101,9 +103,10 @@ public enum DebugMode {
101103
private GoogleAnalyticsMPV3 mpTracker = new GoogleAnalyticsMPV3();
102104
#endif
103105

104-
// TODO(emmanuellemm): Error checking on initialization parameters
106+
// TODO: Error checking on initialization parameters
105107
private void InitializeTracker() {
106108
if (!initialized) {
109+
instance = this;
107110
Debug.Log("Initializing Google Analytics 0.1.");
108111
#if UNITY_ANDROID && !UNITY_EDITOR
109112
androidTracker.SetTrackingCode(androidTrackingCode);
@@ -441,4 +444,9 @@ public static bool belowThreshold(GoogleAnalyticsV3.DebugMode userLogLevel,
441444
}
442445
return true;
443446
}
447+
448+
// Instance for running Coroutines from platform specific classes
449+
public static GoogleAnalyticsV3 getInstance() {
450+
return instance;
451+
}
444452
}

0 commit comments

Comments
 (0)