Skip to content

Commit

Permalink
Make requests using yield-return/Coroutine
Browse files Browse the repository at this point in the history
Fixes #10 - prevents infinite loop in web player.

Also added error message if tracking code has not been set.
  • Loading branch information
emmanuellemm committed Oct 16, 2014
1 parent 20d891d commit 4a5fc3a
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 22 deletions.
Binary file modified googleanalyticsv3.unitypackage
Binary file not shown.
74 changes: 54 additions & 20 deletions source/Plugins/GoogleAnalyticsV3/GoogleAnalyticsMPV3.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,18 +40,23 @@ public class GoogleAnalyticsMPV3 {
private string clientId;
private string url;
private float timeStarted;
private Dictionary<Field, object> trackerValues;
private Dictionary<Field, object> trackerValues = new Dictionary<Field, object>();
private bool startSessionOnNextHit = false;
private bool endSessionOnNextHit = false;
private bool trackingCodeSet = true;

public void InitializeTracker() {
if(String.IsNullOrEmpty(trackingCode)){
Debug.Log("No tracking code set for 'Other' platforms - hits will not be set");
trackingCodeSet = false;
return;
}
if (GoogleAnalyticsV3.belowThreshold(logLevel, GoogleAnalyticsV3.DebugMode.INFO)) {
Debug.Log("Platform is not Android or iOS - " +
"hits will be sent using measurement protocol.");
}
screenRes = Screen.width + "x" + Screen.height;
clientId = SystemInfo.deviceUniqueIdentifier;
trackerValues = new Dictionary<Field, object>();
string language = Application.systemLanguage.ToString();
optOut = false;
#if !UNITY_WP8
Expand Down Expand Up @@ -89,6 +94,9 @@ public class GoogleAnalyticsMPV3 {
}

private string AddTrackerVals() {
if(!trackingCodeSet){
return "";
}
string vals = "";
foreach (KeyValuePair<Field, object> pair in trackerValues){
vals += AddOptionalMPParameter(pair.Key, pair.Value);
Expand All @@ -105,6 +113,12 @@ public class GoogleAnalyticsMPV3 {
}

private void SendGaHitWithMeasurementProtocol(string url) {
if (String.IsNullOrEmpty(url)) {
if (GoogleAnalyticsV3.belowThreshold(logLevel, GoogleAnalyticsV3.DebugMode.WARNING)) {
Debug.Log("No tracking code set for 'Other' platforms - hit will not be sent.");
}
return;
}
if (dryRun || optOut) {
if (GoogleAnalyticsV3.belowThreshold(logLevel, GoogleAnalyticsV3.DebugMode.WARNING)) {
Debug.Log("Dry run or opt out enabled - hits will not be sent.");
Expand All @@ -123,30 +137,41 @@ public class GoogleAnalyticsMPV3 {
if (GoogleAnalyticsV3.belowThreshold(logLevel, GoogleAnalyticsV3.DebugMode.VERBOSE)) {
Debug.Log(newUrl);
}
WWW request = new WWW(newUrl);
while (!request.isDone) {
}
if (request.responseHeaders.ContainsKey("STATUS")) {
if (request.responseHeaders["STATUS"] == "HTTP/1.1 200 OK") {
if (GoogleAnalyticsV3.belowThreshold(logLevel, GoogleAnalyticsV3.DebugMode.INFO)) {
Debug.Log("Successfully sent Google Analytics hit.");
GoogleAnalyticsV3.getInstance().StartCoroutine(this.HandleWWW(new WWW(newUrl)));
}

/*
Make request using yield and coroutine to prevent lock up waiting on request to return.
*/
public IEnumerator HandleWWW(WWW request)
{
while (!request.isDone)
{
yield return request;
if (request.responseHeaders.ContainsKey("STATUS")) {
if (request.responseHeaders["STATUS"] == "HTTP/1.1 200 OK") {
if (GoogleAnalyticsV3.belowThreshold(logLevel, GoogleAnalyticsV3.DebugMode.INFO)) {
Debug.Log("Successfully sent Google Analytics hit.");
}
} else {
if (GoogleAnalyticsV3.belowThreshold(logLevel, GoogleAnalyticsV3.DebugMode.WARNING)) {
Debug.LogWarning("Google Analytics hit request rejected with" +
"status code " + request.responseHeaders["STATUS"]);
}
}
} else {
if (GoogleAnalyticsV3.belowThreshold(logLevel, GoogleAnalyticsV3.DebugMode.WARNING)) {
Debug.LogWarning("Google Analytics hit request rejected with" +
"status code " + request.responseHeaders["STATUS"]);
Debug.LogWarning("Google Analytics hit request failed with error "
+ request.error);
}
}
} else {
if (GoogleAnalyticsV3.belowThreshold(logLevel, GoogleAnalyticsV3.DebugMode.WARNING)) {
Debug.LogWarning("Google Analytics hit request failed with error "
+ request.error);
}
}
}

private string AddRequiredMPParameter(Field parameter, object value) {
if (value == null) {
if(!trackingCodeSet){
return "";
} else if (value == null) {
if (GoogleAnalyticsV3.belowThreshold(logLevel, GoogleAnalyticsV3.DebugMode.WARNING)) {
Debug.LogWarning("Value was null for required parameter " + parameter + ". Hit cannot be sent");
}
Expand All @@ -157,7 +182,9 @@ public class GoogleAnalyticsMPV3 {
}

private string AddRequiredMPParameter(Field parameter, string value) {
if (value == null) {
if(!trackingCodeSet){
return "";
} else if (value == null) {
if (GoogleAnalyticsV3.belowThreshold(logLevel, GoogleAnalyticsV3.DebugMode.WARNING)) {
Debug.LogWarning("Value was null for required parameter " + parameter + ". Hit cannot be sent");
}
Expand All @@ -168,22 +195,25 @@ public class GoogleAnalyticsMPV3 {
}

private string AddOptionalMPParameter(Field parameter, object value) {
if (value == null) {
if (value == null || !trackingCodeSet) {
return "";
} else {
return parameter + "=" + WWW.EscapeURL(value.ToString());
}
}

private string AddOptionalMPParameter(Field parameter, string value) {
if (String.IsNullOrEmpty(value)) {
if (String.IsNullOrEmpty(value) || !trackingCodeSet) {
return "";
} else {
return parameter + "=" + WWW.EscapeURL(value);
}
}

private string AddCustomVariables<T>(HitBuilder<T> builder) {
if(!trackingCodeSet){
return "";
}
String url = "";
foreach(KeyValuePair<int, string> entry in builder.GetCustomDimensions())
{
Expand All @@ -210,6 +240,9 @@ public class GoogleAnalyticsMPV3 {


private string AddCampaignParameters<T>(HitBuilder<T> builder) {
if(!trackingCodeSet){
return "";
}
String url = "";
url += AddOptionalMPParameter(Fields.CAMPAIGN_NAME, builder.GetCampaignName());
url += AddOptionalMPParameter(Fields.CAMPAIGN_SOURCE, builder.GetCampaignSource());
Expand Down Expand Up @@ -382,4 +415,5 @@ public class GoogleAnalyticsMPV3 {
public void SetOptOut(bool optOut) {
this.optOut = optOut;
}

}
12 changes: 10 additions & 2 deletions source/Plugins/GoogleAnalyticsV3/GoogleAnalyticsV3.cs
Original file line number Diff line number Diff line change
Expand Up @@ -73,12 +73,14 @@ public enum DebugMode {
[Tooltip("If checked, hits will not be dispatched. Use for testing.")]
public bool dryRun = false;

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

public static GoogleAnalyticsV3 instance = null;

[HideInInspector]
public readonly static string currencySymbol = "USD";
public readonly static string EVENT_HIT = "createEvent";
Expand All @@ -101,9 +103,10 @@ public enum DebugMode {
private GoogleAnalyticsMPV3 mpTracker = new GoogleAnalyticsMPV3();
#endif

// TODO(emmanuellemm): Error checking on initialization parameters
// TODO: Error checking on initialization parameters
private void InitializeTracker() {
if (!initialized) {
instance = this;
Debug.Log("Initializing Google Analytics 0.1.");
#if UNITY_ANDROID && !UNITY_EDITOR
androidTracker.SetTrackingCode(androidTrackingCode);
Expand Down Expand Up @@ -441,4 +444,9 @@ public enum DebugMode {
}
return true;
}

// Instance for running Coroutines from platform specific classes
public static GoogleAnalyticsV3 getInstance() {
return instance;
}
}

0 comments on commit 4a5fc3a

Please sign in to comment.