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

Reporting for OS version for Windows and OS X by overriding the user … #121

Open
wants to merge 3 commits 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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions source/Plugins/GoogleAnalyticsV4/Fields.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ public class Fields {
public readonly static Field SCREEN_COLORS = new Field("&sd");
public readonly static Field SCREEN_RESOLUTION = new Field("&sr");
public readonly static Field VIEWPORT_SIZE = new Field("&vp");
public readonly static Field USER_AGENT_OVERRIDE = new Field("&ua");

// Application
public readonly static Field APP_NAME = new Field("&an");
Expand Down
86 changes: 86 additions & 0 deletions source/Plugins/GoogleAnalyticsV4/GoogleAnalyticsAndroidV4.cs
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,16 @@ public class GoogleAnalyticsAndroidV4 : IDisposable {
tracker.Call("setScreenName", builder.GetScreenName());

AndroidJavaObject eventBuilder = new AndroidJavaObject("com.google.android.gms.analytics.HitBuilders$ScreenViewBuilder");

foreach(KeyValuePair<int, string> i in builder.GetCustomDimensions())
{
eventBuilder.Call<AndroidJavaObject>("setCustomDimension", new object[] { i.Key, i.Value });
}
foreach(KeyValuePair<int, string> i in builder.GetCustomMetrics())
{
eventBuilder.Call<AndroidJavaObject>("setCustomMetric", new object[] { i.Key, i.Value });
}

object[] builtScreenView = new object[] { eventBuilder.Call<AndroidJavaObject>("build") };
tracker.Call("send", builtScreenView);
}
Expand All @@ -100,6 +110,16 @@ public class GoogleAnalyticsAndroidV4 : IDisposable {
eventBuilder.Call<AndroidJavaObject>("setAction", new object[] { builder.GetEventAction() });
eventBuilder.Call<AndroidJavaObject>("setLabel", new object[] { builder.GetEventLabel() });
eventBuilder.Call<AndroidJavaObject>("setValue", new object[] { builder.GetEventValue() });

foreach(KeyValuePair<int, string> i in builder.GetCustomDimensions())
{
eventBuilder.Call<AndroidJavaObject>("setCustomDimension", new object[] { i.Key, i.Value });
}
foreach(KeyValuePair<int, string> i in builder.GetCustomMetrics())
{
eventBuilder.Call<AndroidJavaObject>("setCustomMetric", new object[] { i.Key, i.Value });
}

object[] builtEvent = new object[] { eventBuilder.Call<AndroidJavaObject>("build") };
tracker.Call("send", builtEvent);
}
Expand All @@ -113,6 +133,15 @@ public class GoogleAnalyticsAndroidV4 : IDisposable {
transactionBuilder.Call<AndroidJavaObject>("setShipping", new object[] { builder.GetShipping() });
transactionBuilder.Call<AndroidJavaObject>("setCurrencyCode", new object[] { builder.GetCurrencyCode() });

foreach(KeyValuePair<int, string> i in builder.GetCustomDimensions())
{
transactionBuilder.Call<AndroidJavaObject>("setCustomDimension", new object[] { i.Key, i.Value });
}
foreach(KeyValuePair<int, string> i in builder.GetCustomMetrics())
{
transactionBuilder.Call<AndroidJavaObject>("setCustomMetric", new object[] { i.Key, i.Value });
}

object[] builtTransaction = new object[] { transactionBuilder.Call<AndroidJavaObject>("build") };
tracker.Call("send", builtTransaction);
}
Expand All @@ -127,17 +156,74 @@ public class GoogleAnalyticsAndroidV4 : IDisposable {
itemBuilder.Call<AndroidJavaObject>("setQuantity", new object[] { builder.GetQuantity() });
itemBuilder.Call<AndroidJavaObject>("setCurrencyCode", new object[] { builder.GetCurrencyCode() });

foreach(KeyValuePair<int, string> i in builder.GetCustomDimensions())
{
itemBuilder.Call<AndroidJavaObject>("setCustomDimension", new object[] { i.Key, i.Value });
}
foreach(KeyValuePair<int, string> i in builder.GetCustomMetrics())
{
itemBuilder.Call<AndroidJavaObject>("setCustomMetric", new object[] { i.Key, i.Value });
}

object[] builtItem = new object[] { itemBuilder.Call<AndroidJavaObject>("build") };
tracker.Call("send", builtItem);
}

public void LogException(ExceptionHitBuilder builder) {
AndroidJavaObject exceptionBuilder = new AndroidJavaObject("com.google.android.gms.analytics.HitBuilders$ExceptionBuilder");
exceptionBuilder.Call<AndroidJavaObject>("setDescription", new object[] { builder.GetExceptionDescription() });
exceptionBuilder.Call<AndroidJavaObject>("setFatal", new object[] { builder.IsFatal() });

foreach(KeyValuePair<int, string> i in builder.GetCustomDimensions())
{
exceptionBuilder.Call<AndroidJavaObject>("setCustomDimension", new object[] { i.Key, i.Value });
}
foreach(KeyValuePair<int, string> i in builder.GetCustomMetrics())
{
exceptionBuilder.Call<AndroidJavaObject>("setCustomMetric", new object[] { i.Key, i.Value });
}

object[] builtException = new object[] { exceptionBuilder.Call<AndroidJavaObject>("build") };
tracker.Call("send", builtException);
}

public void LogSocial(SocialHitBuilder builder) {
AndroidJavaObject socialBuilder = new AndroidJavaObject("com.google.android.gms.analytics.HitBuilders$SocialBuilder");
socialBuilder.Call<AndroidJavaObject>("setNetwork", new object[] { builder.GetSocialNetwork() });
socialBuilder.Call<AndroidJavaObject>("setAction", new object[] { builder.GetSocialAction() });
socialBuilder.Call<AndroidJavaObject>("setTarget", new object[] { builder.GetSocialTarget() });

foreach(KeyValuePair<int, string> i in builder.GetCustomDimensions())
{
socialBuilder.Call<AndroidJavaObject>("setCustomDimension", new object[] { i.Key, i.Value });
}
foreach(KeyValuePair<int, string> i in builder.GetCustomMetrics())
{
socialBuilder.Call<AndroidJavaObject>("setCustomMetric", new object[] { i.Key, i.Value });
}

object[] builtSocial = new object[] { socialBuilder.Call<AndroidJavaObject>("build") };
tracker.Call("send", builtSocial);
}

public void LogTiming(TimingHitBuilder builder) {
AndroidJavaObject timingBuilder = new AndroidJavaObject("com.google.android.gms.analytics.HitBuilders$TimingBuilder");
timingBuilder.Call<AndroidJavaObject>("setCategory", new object[] { builder.GetTimingCategory() });
timingBuilder.Call<AndroidJavaObject>("setValue", new object[] { builder.GetTimingInterval() });
timingBuilder.Call<AndroidJavaObject>("setVariable", new object[] { builder.GetTimingName() });
timingBuilder.Call<AndroidJavaObject>("setLabel", new object[] { builder.GetTimingLabel() });

foreach(KeyValuePair<int, string> i in builder.GetCustomDimensions())
{
timingBuilder.Call<AndroidJavaObject>("setCustomDimension", new object[] { i.Key, i.Value });
}
foreach(KeyValuePair<int, string> i in builder.GetCustomMetrics())
{
timingBuilder.Call<AndroidJavaObject>("setCustomMetric", new object[] { i.Key, i.Value });
}

object[] builtTiming = new object[] { timingBuilder.Call<AndroidJavaObject>("build") };
tracker.Call("send", builtTiming);
}

public void DispatchHits() {
Expand Down
68 changes: 67 additions & 1 deletion source/Plugins/GoogleAnalyticsV4/GoogleAnalyticsMPV3.cs
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,8 @@ public class GoogleAnalyticsMPV3 {
+ AddRequiredMPParameter(Fields.TRACKING_ID, trackingCode)
+ AddRequiredMPParameter(Fields.APP_ID, bundleIdentifier)
+ AddRequiredMPParameter(Fields.CLIENT_ID, clientId)
+ AddRequiredMPParameter(Fields.APP_VERSION, appVersion);
+ AddRequiredMPParameter(Fields.APP_VERSION, appVersion)
+ AddRequiredMPParameter(Fields.USER_AGENT_OVERRIDE, GetUserAgent());
if(anonymizeIP){
url += AddOptionalMPParameter(Fields.ANONYMIZE_IP, 1);
}
Expand Down Expand Up @@ -419,5 +420,70 @@ public IEnumerator HandleWWW(WWW request)
this.optOut = optOut;
}

string GetWindowsNTVersion(string UnityOSVersionName)
{
//https://en.wikipedia.org/wiki/Windows_NT
if (UnityOSVersionName.Contains("(5.1"))
return "Windows NT 5.1";
else if (UnityOSVersionName.Contains("(5.2"))
return "Windows NT 5.2";
else if (UnityOSVersionName.Contains("(6.0"))
return "Windows NT 6.0";
else if (UnityOSVersionName.Contains("(6.1"))
return "Windows NT 6.1";
else if (UnityOSVersionName.Contains("(6.2"))
return "Windows NT 6.2";
#if UNITY_4_6
else if (UnityOSVersionName.Contains("(6.3"))
{
// But on older versions of Unity on Windows 10, it returns "Windows 8.1 (6.3.10586) 64bit" for Windows 10.0.10586 64 bit
System.Text.RegularExpressions.Match regexResult = System.Text.RegularExpressions.Regex.Match(UnityOSVersionName, @"Windows.*?\((\d{0,5}\.\d{0,5}\.(\d{0,5}))\)");
if (regexResult.Success)
{
string buildNumberString = regexResult.Groups[2].Value;
// Fix a bug in older versions of Unity where Windows 10 isn't recognised properly
int buildNumber = 0;
Int32.TryParse(buildNumberString, out buildNumber);
if (buildNumber > 10000)
{
return "Windows NT 10.0";
}
}
return "Windows NT 6.3";
}
#else
else if (UnityOSVersionName.Contains("(6.3"))
return "Windows NT 6.3";
else if (UnityOSVersionName.Contains("(10.0"))
return "Windows NT 10.0";
#endif
else
return "Unknown";
}

string GetUserAgent()
{
string str_userAgent;
str_userAgent = "Unknown";
#if UNITY_EDITOR_OSX || UNITY_STANDALONE_OSX
str_userAgent = (Application.platform == RuntimePlatform.OSXPlayer ? "Unity/" : "UnityEditor/")
+ Application.unityVersion
+ " (Macintosh; "
+ (SystemInfo.processorType.Contains("Intel") ? "Intel " : "PPC ")
+ SystemInfo.operatingSystem.Replace(".", "_") + ")"
+ " Unity/" + Application.unityVersion
+ " Unity/" + Application.unityVersion;
#endif
#if UNITY_EDITOR_WIN || UNITY_STANDALONE_WIN
str_userAgent =
(Application.platform == RuntimePlatform.WindowsPlayer ? "Unity/" : "UnityEditor/") + Application.unityVersion
+ " (" + GetWindowsNTVersion(SystemInfo.operatingSystem) + (SystemInfo.operatingSystem.Contains("64bit") ? "; WOW64)" : ")")
+ " Unity/" + Application.unityVersion
+ " (KHTML, like Gecko) Unity/" + Application.unityVersion
+ " Unity/" + Application.unityVersion;
#endif
return str_userAgent;
}

#endif
}
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public class ItemHitBuilder : HitBuilder<ItemHitBuilder> {
}

public string GetSKU() {
return name;
return SKU;
}

public ItemHitBuilder SetSKU(string SKU) {
Expand Down