Skip to content

Commit

Permalink
Reporting for OS version for Windows and OS X by overriding the user …
Browse files Browse the repository at this point in the history
…agent

- Implemented using a mixture of code from the following sources
1. googleanalytics#78
1.1 For user agent override
2. googleanalytics#94
2.1 For the user agent string for Windows and OS X.
2.2 Includes a bit of extra code to fix a bug in Unity 4.6 where it reports Windows 10 as Windows 8.1 (6.3.10XXX), where the build number is correct
  • Loading branch information
Yeo Loon Chew committed Apr 14, 2016
1 parent 2484327 commit 602f8aa
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 1 deletion.
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
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 void InitializeTracker() {
+ 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 void SetOptOut(bool optOut) {
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
}

0 comments on commit 602f8aa

Please sign in to comment.