Skip to content

Commit 602f8aa

Browse files
author
Yeo Loon Chew
committed
Reporting for OS version for Windows and OS X by overriding the user 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
1 parent 2484327 commit 602f8aa

File tree

2 files changed

+68
-1
lines changed

2 files changed

+68
-1
lines changed

source/Plugins/GoogleAnalyticsV4/Fields.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ public class Fields {
4040
public readonly static Field SCREEN_COLORS = new Field("&sd");
4141
public readonly static Field SCREEN_RESOLUTION = new Field("&sr");
4242
public readonly static Field VIEWPORT_SIZE = new Field("&vp");
43+
public readonly static Field USER_AGENT_OVERRIDE = new Field("&ua");
4344

4445
// Application
4546
public readonly static Field APP_NAME = new Field("&an");

source/Plugins/GoogleAnalyticsV4/GoogleAnalyticsMPV3.cs

Lines changed: 67 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,8 @@ public void InitializeTracker() {
7878
+ AddRequiredMPParameter(Fields.TRACKING_ID, trackingCode)
7979
+ AddRequiredMPParameter(Fields.APP_ID, bundleIdentifier)
8080
+ AddRequiredMPParameter(Fields.CLIENT_ID, clientId)
81-
+ AddRequiredMPParameter(Fields.APP_VERSION, appVersion);
81+
+ AddRequiredMPParameter(Fields.APP_VERSION, appVersion)
82+
+ AddRequiredMPParameter(Fields.USER_AGENT_OVERRIDE, GetUserAgent());
8283
if(anonymizeIP){
8384
url += AddOptionalMPParameter(Fields.ANONYMIZE_IP, 1);
8485
}
@@ -419,5 +420,70 @@ public void SetOptOut(bool optOut) {
419420
this.optOut = optOut;
420421
}
421422

423+
string GetWindowsNTVersion(string UnityOSVersionName)
424+
{
425+
//https://en.wikipedia.org/wiki/Windows_NT
426+
if (UnityOSVersionName.Contains("(5.1"))
427+
return "Windows NT 5.1";
428+
else if (UnityOSVersionName.Contains("(5.2"))
429+
return "Windows NT 5.2";
430+
else if (UnityOSVersionName.Contains("(6.0"))
431+
return "Windows NT 6.0";
432+
else if (UnityOSVersionName.Contains("(6.1"))
433+
return "Windows NT 6.1";
434+
else if (UnityOSVersionName.Contains("(6.2"))
435+
return "Windows NT 6.2";
436+
#if UNITY_4_6
437+
else if (UnityOSVersionName.Contains("(6.3"))
438+
{
439+
// 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
440+
System.Text.RegularExpressions.Match regexResult = System.Text.RegularExpressions.Regex.Match(UnityOSVersionName, @"Windows.*?\((\d{0,5}\.\d{0,5}\.(\d{0,5}))\)");
441+
if (regexResult.Success)
442+
{
443+
string buildNumberString = regexResult.Groups[2].Value;
444+
// Fix a bug in older versions of Unity where Windows 10 isn't recognised properly
445+
int buildNumber = 0;
446+
Int32.TryParse(buildNumberString, out buildNumber);
447+
if (buildNumber > 10000)
448+
{
449+
return "Windows NT 10.0";
450+
}
451+
}
452+
return "Windows NT 6.3";
453+
}
454+
#else
455+
else if (UnityOSVersionName.Contains("(6.3"))
456+
return "Windows NT 6.3";
457+
else if (UnityOSVersionName.Contains("(10.0"))
458+
return "Windows NT 10.0";
459+
#endif
460+
else
461+
return "Unknown";
462+
}
463+
464+
string GetUserAgent()
465+
{
466+
string str_userAgent;
467+
str_userAgent = "Unknown";
468+
#if UNITY_EDITOR_OSX || UNITY_STANDALONE_OSX
469+
str_userAgent = (Application.platform == RuntimePlatform.OSXPlayer ? "Unity/" : "UnityEditor/")
470+
+ Application.unityVersion
471+
+ " (Macintosh; "
472+
+ (SystemInfo.processorType.Contains("Intel") ? "Intel " : "PPC ")
473+
+ SystemInfo.operatingSystem.Replace(".", "_") + ")"
474+
+ " Unity/" + Application.unityVersion
475+
+ " Unity/" + Application.unityVersion;
476+
#endif
477+
#if UNITY_EDITOR_WIN || UNITY_STANDALONE_WIN
478+
str_userAgent =
479+
(Application.platform == RuntimePlatform.WindowsPlayer ? "Unity/" : "UnityEditor/") + Application.unityVersion
480+
+ " (" + GetWindowsNTVersion(SystemInfo.operatingSystem) + (SystemInfo.operatingSystem.Contains("64bit") ? "; WOW64)" : ")")
481+
+ " Unity/" + Application.unityVersion
482+
+ " (KHTML, like Gecko) Unity/" + Application.unityVersion
483+
+ " Unity/" + Application.unityVersion;
484+
#endif
485+
return str_userAgent;
486+
}
487+
422488
#endif
423489
}

0 commit comments

Comments
 (0)