Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add FlushRequests method #80

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 39 additions & 4 deletions source/Plugins/GoogleAnalyticsV3/GoogleAnalyticsMPV3.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ limitations under the License.
using System.Collections;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;

/*
GoogleAnalyticsMPV3 handles building hits using the Measurement Protocol.
Expand Down Expand Up @@ -47,6 +48,8 @@ public class GoogleAnalyticsMPV3 {
private bool startSessionOnNextHit = false;
private bool endSessionOnNextHit = false;
private bool trackingCodeSet = true;
private int nextCoroutineId = 0;
private Dictionary<int, IEnumerator> coroutinesById = new Dictionary<int, IEnumerator>();

public void InitializeTracker() {
if(String.IsNullOrEmpty(trackingCode)){
Expand Down Expand Up @@ -140,17 +143,22 @@ private void SendGaHitWithMeasurementProtocol(string url) {
if (GoogleAnalyticsV3.belowThreshold(logLevel, GoogleAnalyticsV3.DebugMode.VERBOSE)) {
Debug.Log(newUrl);
}
GoogleAnalyticsV3.getInstance().StartCoroutine(this.HandleWWW(new WWW(newUrl)));
var id = nextCoroutineId++;
var coroutine = this.HandleWWW(new WWW(newUrl), id);
coroutinesById.Add(id, coroutine);
GoogleAnalyticsV3.getInstance().StartCoroutine(coroutine);
}

/*
Make request using yield and coroutine to prevent lock up waiting on request to return.
*/
public IEnumerator HandleWWW(WWW request)
public IEnumerator HandleWWW(WWW request, int coroutineId)
{
while (!request.isDone)
// Normally a WWW request yielded to Unity will be done on resume. However our request
// flush mechanism behaves differently, so yield repeatedly until done.
while (!request.isDone) { yield return request; }

{
yield return request;
if (request.responseHeaders.ContainsKey("STATUS")) {
if (request.responseHeaders["STATUS"].Contains("200 OK")) {
if (GoogleAnalyticsV3.belowThreshold(logLevel, GoogleAnalyticsV3.DebugMode.INFO)) {
Expand All @@ -169,6 +177,33 @@ public IEnumerator HandleWWW(WWW request)
}
}
}
coroutinesById.Remove(coroutineId);
}

public bool FlushRequests(int timeoutMilliseconds) {
var timer = System.Diagnostics.Stopwatch.StartNew();
int initialCount = coroutinesById.Count;
while (coroutinesById.Count > 0 && timer.ElapsedMilliseconds < timeoutMilliseconds) {
// Advance each coroutine by one step. The coroutine is expected to yield the
// WWW request until done. Finally they remove themselves from the dictionary.
foreach (var coroutine in coroutinesById.Values.ToArray()) {
coroutine.MoveNext();
}
System.Threading.Thread.Sleep(1);
}
if (coroutinesById.Count > 0) {
if (GoogleAnalyticsV3.belowThreshold(logLevel, GoogleAnalyticsV3.DebugMode.WARNING)) {
Debug.LogWarningFormat("Request flush timeout exceeded, {0} requests pending.",
coroutinesById.Count);
}
return false;
} else if (initialCount > 0) {
if (GoogleAnalyticsV3.belowThreshold(logLevel, GoogleAnalyticsV3.DebugMode.INFO)) {
Debug.LogFormat("Flushed {0} requests in {1} milliseconds.",
initialCount, timer.ElapsedMilliseconds);
}
}
return true;
}

private string AddRequiredMPParameter(Field parameter, object value) {
Expand Down
13 changes: 13 additions & 0 deletions source/Plugins/GoogleAnalyticsV3/GoogleAnalyticsV3.cs
Original file line number Diff line number Diff line change
Expand Up @@ -492,4 +492,17 @@ public static bool belowThreshold(GoogleAnalyticsV3.DebugMode userLogLevel,
public static GoogleAnalyticsV3 getInstance() {
return instance;
}

// Block on completion of any pending requests, returning true if all
// requests completed (either succeeding or failing) within the given timeout.
// Intended to be called from OnApplicationQuit hook.
public bool FlushRequests(int timeoutMilliseconds) {
#if UNITY_ANDROID && !UNITY_EDITOR
return true;
#elif UNITY_IPHONE && !UNITY_EDITOR
return true;
#else
return mpTracker.FlushRequests(timeoutMilliseconds);
#endif
}
}