Skip to content

Commit 3fb0e7b

Browse files
committed
Changed EditorMeasurement
Updated EditorMeasurement to include the event parameters in the URL. Bug: 174959232 Change-Id: I55f2d4a9a07f2151f2008f5a44b0cd09190e4302
1 parent 62733c0 commit 3fb0e7b

File tree

5 files changed

+130
-21
lines changed

5 files changed

+130
-21
lines changed

build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ project.ext {
3535
(OperatingSystem.MAC_OSX):
3636
["/Applications/Unity/Unity.app/Contents/MacOS/Unity"] +
3737
(new FileNameFinder()).getFileNames(
38-
"/", "Applications/UnityHub/*/Unity.app/Contents/MacOS/Unity"),
38+
"/", "Applications/Unity/Hub/Editor/*/Unity.app/Contents/MacOS/Unity"),
3939
(OperatingSystem.WINDOWS):
4040
["\\Program Files\\Unity\\Editor\\Unity.exe"] +
4141
(new FileNameFinder()).getFileNames(

source/VersionHandlerImpl/src/EditorMeasurement.cs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -514,8 +514,8 @@ public void Report(string reportUrl, string reportName) {
514514
if (String.IsNullOrEmpty(cookie.Key)) continue;
515515
// See https://developers.google.com/analytics/devguides/collection/protocol/v1
516516
var status = PortableWebRequest.DefaultInstance.Post(
517-
"http://www.google-analytics.com/collect", null,
518-
new [] {
517+
"http://www.google-analytics.com/collect",
518+
new[] {
519519
// Version
520520
new KeyValuePair<string, string>("v", "1"),
521521
// Tracking ID.
@@ -525,13 +525,14 @@ public void Report(string reportUrl, string reportName) {
525525
// Hit type.
526526
new KeyValuePair<string, string>("t", "pageview"),
527527
// "URL" / string to report.
528-
new KeyValuePair<string, string>("dl",
529-
path + "?" + cookie.Value + fragment),
528+
new KeyValuePair<string, string>(
529+
"dl", path + "?" + cookie.Value + fragment),
530530
// Document title.
531531
new KeyValuePair<string, string>("dt", reportName),
532532
// Cache buster
533533
new KeyValuePair<string, string>("z", random.Next().ToString())
534-
});
534+
},
535+
null, null);
535536
if (status != null) reported = true;
536537
}
537538
if (reported) {

source/VersionHandlerImpl/src/PortableWebRequest.cs

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ namespace Google {
2020
using System.Collections.Generic;
2121
using System.Reflection;
2222
using System.Net;
23+
using System.Text;
24+
using System.Web;
2325

2426
using UnityEngine;
2527

@@ -62,6 +64,19 @@ public interface IPortableWebRequest {
6264
IPortableWebRequestStatus Post(string url, IDictionary<string, string> headers,
6365
IEnumerable<KeyValuePair<string, string>> formFields);
6466

67+
/// <summary>
68+
/// Post to a URL.
69+
/// </summary>
70+
/// <param name="path">URL path to send data to.</param>
71+
/// <param name="queryParams">Query parameters to be appended to the URL.</param>
72+
/// <param name="headers">Headers to use when performing the request.</param>
73+
/// <param name="formFields">Form fields to URL encode and send.</param>
74+
/// <returns>Web request if successfully started, null otherwise.</returns>
75+
IPortableWebRequestStatus Post(string path,
76+
IEnumerable<KeyValuePair<string, string>> queryParams,
77+
IDictionary<string, string> headers,
78+
IEnumerable<KeyValuePair<string, string>> formFields);
79+
6580
/// <summary>
6681
/// Get the contents of a URL.
6782
/// </summary>
@@ -521,6 +536,29 @@ public IPortableWebRequestStatus Post(
521536
}
522537
}
523538

539+
/// <summary>
540+
/// Post to a URL.
541+
/// </summary>
542+
/// <param name="path">URL path to send data to.</param>
543+
/// <param name="queryParams">Query parameters to be appended to the URL.</param>
544+
/// <param name="headers">Headers to use when performing the request.</param>
545+
/// <param name="formFields">Form fields to URL encode and send.</param>
546+
/// <returns>Web request if successfully started, null otherwise.</returns>
547+
public IPortableWebRequestStatus Post(string path,
548+
IEnumerable<KeyValuePair<string, string>> queryParams,
549+
IDictionary<string, string> headers,
550+
IEnumerable<KeyValuePair<string, string>> formFields) {
551+
var url = new StringBuilder(256);
552+
foreach (var param in queryParams) {
553+
url.AppendFormat("{0}{1}={2}",
554+
url.Length == 0 ? "?" : "&",
555+
HttpUtility.UrlEncode(param.Key),
556+
HttpUtility.UrlEncode(param.Value));
557+
}
558+
url.Insert(0, path);
559+
return Post(url.ToString(), headers, formFields);
560+
}
561+
524562
/// <summary>
525563
/// Get the contents of a URL.
526564
/// </summary>

source/VersionHandlerImpl/test/webrequest/Assets/PlayServicesResolver/Editor/TestPortableWebRequest.cs_DISABLED

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616

1717
using System;
1818
using System.Collections.Generic;
19-
using System.Collections;
2019
using System.Net;
2120
using Google;
2221

@@ -33,6 +32,8 @@ public class TestPortableWebRequest {
3332
private static bool postSucceeded = false;
3433
// Whether the post operation with no headers succeeded.
3534
private static bool postNoHeadersSucceeded = false;
35+
// Whether the post operation with no form fields succeeded.
36+
private static bool postNoFormFieldsSucceeded = false;
3637

3738
/// <summary>
3839
/// Register a method to call when the Version Handler has enabled all plugins in the project.
@@ -191,6 +192,43 @@ public class TestPortableWebRequest {
191192
return complete;
192193
}, synchronous: true);
193194

195+
UnityEngine.Debug.Log("Running post test with no form fields...");
196+
var postNoFormFieldsStatus = webRequest.Post(
197+
"http://localhost:8000/post?foo1=bar1&foo2=bar2",
198+
new[] {
199+
new KeyValuePair<string, string>("foo1", "bar1"),
200+
new KeyValuePair<string, string>("foo2", "bar2")
201+
},
202+
new Dictionary<string, string> {
203+
{ "Echo-Foo", "Bar" },
204+
{ "Echo-Bish", "Bosh" }
205+
},
206+
null);
207+
RunOnMainThread.PollOnUpdateUntilComplete(() => {
208+
var complete = postNoFormFieldsStatus.Complete;
209+
if (complete) {
210+
postNoFormFieldsSucceeded = CheckEqual(postNoFormFieldsStatus.Status,
211+
HttpStatusCode.OK);
212+
postNoFormFieldsSucceeded &= CheckEqual(
213+
DictionaryToString("echo-", true, postNoFormFieldsStatus.Headers),
214+
"echo-bish=Bosh\n" +
215+
"echo-foo=Bar");
216+
var result = System.Text.Encoding.Default.GetString(
217+
postNoFormFieldsStatus.Result);
218+
var expected =
219+
"{\"data\": \"Hello from a test server\", " +
220+
"\"form\": {}, " +
221+
"\"headers\": {\"Echo-Bish\": \"Bosh\", \"Echo-Foo\": \"Bar\"}, " +
222+
"\"path\": \"/post\", " +
223+
"\"query\": {\"foo1\": [\"bar1\"], \"foo2\": [\"bar2\"]}";
224+
postNoFormFieldsSucceeded &= CheckEqual(result, expected);
225+
UnityEngine.Debug.Log(String.Format(
226+
"Post with no firm fields succeeded={0}\n{1}",
227+
postNoFormFieldsSucceeded, result));
228+
}
229+
return complete;
230+
}, synchronous: true);
231+
194232
UnityEngine.Debug.Log("Running post test with no headers...");
195233
var postNoHeadersStatus = webRequest.Post(
196234
"http://localhost:8000/post/with/no/headers",

source/VersionHandlerImpl/unit_tests/src/EditorMeasurementTest.cs

Lines changed: 46 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ namespace Google.VersionHandlerImpl.Tests {
2020
using System.Collections.Generic;
2121
using System.Net;
2222
using System.IO;
23+
using System.Text;
24+
using System.Web;
2325

2426
using Google;
2527

@@ -83,17 +85,43 @@ public IPortableWebRequestStatus Post(
8385
string url, IDictionary<string, string> headers,
8486
IEnumerable<KeyValuePair<string, string>> formFields) {
8587
var formLines = new List<string>();
86-
foreach (var kv in formFields) {
87-
var value = kv.Value;
88-
// Change the random cache buster to 0.
89-
if (kv.Key == "z") value = "0";
90-
formLines.Add(String.Format("{0}={1}", kv.Key, value));
88+
if (formFields != null) {
89+
foreach (var kv in formFields) {
90+
var value = kv.Value;
91+
// Change the random cache buster to 0.
92+
if (kv.Key == "z") value = "0";
93+
formLines.Add(String.Format("{0}={1}", kv.Key, value));
94+
}
9195
}
96+
9297
PostedUrlAndForms.Add(
9398
new KeyValuePair<string, string>(url, String.Join("\n", formLines.ToArray())));
9499
return new RequestStatus();
95100
}
96101

102+
/// <summary>
103+
/// Start a post request that is immediately completed.
104+
/// </summary>
105+
public IPortableWebRequestStatus Post(
106+
string path, IEnumerable<KeyValuePair<string, string>> queryParams,
107+
IDictionary<string, string> headers,
108+
IEnumerable<KeyValuePair<string, string>> formFields)
109+
{
110+
var url = new StringBuilder(256);
111+
foreach (var param in queryParams) {
112+
var value = param.Value;
113+
// Change the random cache buster to 0.
114+
if (param.Key == "z") value = "0";
115+
116+
url.AppendFormat("{0}{1}={2}",
117+
url.Length == 0 ? "?" : "&",
118+
HttpUtility.UrlEncode(param.Key),
119+
HttpUtility.UrlEncode(value));
120+
}
121+
url.Insert(0, path);
122+
return Post(url.ToString(), headers, formFields);
123+
}
124+
97125
public IPortableWebRequestStatus Get(string url, IDictionary<string, string> headers) {
98126
throw new NotImplementedException("PortableWebRequest.Get() should not be called");
99127
}
@@ -338,15 +366,19 @@ public void ReportWhenDisabled() {
338366
private KeyValuePair<string, string> CreateMeasurementEvent(
339367
string reportUrl, string reportName, string cookie) {
340368
return new KeyValuePair<string, string>(
341-
"http://www.google-analytics.com/collect",
342-
String.Format("v=1\n" +
343-
"tid={0}\n" +
344-
"cid={1}\n" +
345-
"t=pageview\n" +
346-
"dl={2}\n" +
347-
"dt={3}\n" +
348-
"z=0",
349-
GA_TRACKING_ID, cookie, reportUrl, reportName));
369+
String.Format("http://www.google-analytics.com/collect" +
370+
"?v=1" +
371+
"&tid={0}" +
372+
"&cid={1}" +
373+
"&t=pageview" +
374+
"&dl={2}" +
375+
"&dt={3}" +
376+
"&z=0",
377+
HttpUtility.UrlEncode(GA_TRACKING_ID),
378+
HttpUtility.UrlEncode(cookie),
379+
HttpUtility.UrlEncode(reportUrl),
380+
HttpUtility.UrlEncode(reportName)),
381+
"" /* empty form fields */);
350382
}
351383

352384
/// <summary>

0 commit comments

Comments
 (0)