google-analytics-java icon indicating copy to clipboard operation
google-analytics-java copied to clipboard

Operating System on User Agent

Open IvanRF opened this issue 8 years ago • 12 comments

I started testing the stable version (v1.1.2) but I think this issue is still present on 2.0.

When sending a post to GA the userAgent in my case is:

java/1.8.0_112-b15/Oracle Corporation/Java HotSpot(TM) 64-Bit Server VM/Windows 10/10.0/amd64

It includes the OS data but I guess the format is not correct or at least is not the one that GA expects.

User Agent Override

Example value: Opera/9.80 (Windows NT 6.0) Presto/2.12.388 Version/12.14
Example usage: ua=Opera%2F9.80%20%28Windows%20NT%206.0%29%20Presto%2F2.12.388%20Version%2F12.14

The first part is correctly gathered by GA: Browser: java Browser Version: 1.8.0_112-b15

but then the rest is empty: Operating System: (not set)

I just made a test overriding the user agent:

defaultRequest.userAgent("Java/1.8.0_112-b15 (Windows NT 10.0; Win64; x64)");

Now I have to wait until GA audience reports are available, but I will share if that format works.

IvanRF avatar Sep 01 '17 20:09 IvanRF

thank you. Please check if that new format works and we can make that as default. Btw, are you using version2? Version 1 is not going to be updated going forward.

brsanthu avatar Sep 01 '17 20:09 brsanthu

I can confirm it works: Operating System: Windows Operating System Version: 10

To test it faster I used the version on Central Repository. Do you plan to upload v2.0?

IvanRF avatar Sep 01 '17 20:09 IvanRF

In order to dynamically build the user agent string, I'm testing this code now:

OperatingSystemMXBean os = ManagementFactory.getOperatingSystemMXBean();
defaultRequest.userAgent("Java/" + System.getProperty("java.version") + " (" + os.getName() + "; " + os.getArch()+ ")");

which in my case sets Java/1.8.0_112 (Windows 10; amd64), not equal but similar 😃

IvanRF avatar Sep 01 '17 20:09 IvanRF

I will try to upload v2 with beta label over the weekend.

brsanthu avatar Sep 01 '17 20:09 brsanthu

Excelent! another question: why screenResolution is not set by default? I'm currently using config.setRequestParameterDiscoverer(new AwtRequestParameterDiscoverer()); but the JavaScript library for GA always send that info and the screenColors.

By the way, comparing with the Google's library, screenColors sends '32, 32' but it should be '32-bit'. screenResolution sends '1366x768, 96 dpi' and should be '1366x768' (although dpi info could be useful). Screen Resolution Screen Colors

IvanRF avatar Sep 01 '17 20:09 IvanRF

Since many times, the env where this library is used may not have access to display to get the stats and that's why it is not set by default.

I will look into the screen resolution and colors info.

brsanthu avatar Sep 01 '17 20:09 brsanthu

oh, you're right, I was thinking only on desktop apps but the library could be used on a server.

IvanRF avatar Sep 01 '17 20:09 IvanRF

Finally, the second report is available. The OS value didn't worked. I sent Windows 10 and apparently they expect Windows NT 10.0 (since that value worked).

So, a simple logic could be applied there to replace Java property value to a valid value.

IvanRF avatar Sep 01 '17 21:09 IvanRF

Yes, that change worked (I don't know if there is any correlation but when the OS is valid the pageview appears faster on the audience report).

I applied this simple logic, but more work is needed to support other OS:

OperatingSystemMXBean os = ManagementFactory.getOperatingSystemMXBean();
String osVersion = (isWindows()) ? "Windows NT " + os.getVersion() : os.getName();
config.setUserAgent("Java/" + System.getProperty("java.version") + " (" + osVersion + "; " + os.getArch()+ ")");

IvanRF avatar Sep 01 '17 22:09 IvanRF

Any news on this? I'm experiencing the same issue, will experiment with your previous suggestions!

nicolabeghin avatar Apr 28 '19 13:04 nicolabeghin

Using like this at the moment:

public class WMRequestParameterDiscoverer extends AwtRequestParameterDiscoverer {
    protected String getUserAgentString() {
        return String.format("Java/%s (%s %s; %s)", System.getProperty("java.version"), System.getProperty("os.name"), System.getProperty("os.version"), System.getProperty("os.arch"));
    }
}

nicolabeghin avatar Apr 28 '19 13:04 nicolabeghin

Yes ran into the same issue, took me quite a while to find out why my requests are not working.

kaikun213 avatar Jun 13 '19 16:06 kaikun213