BrowserAndOperatingSystemAnalyticsProperties extends AdditionalAnalyticsProperties { @Override public Map<String, Object> properties(TrackRequest req) { StaplerRequest httpReq = Stapler.getCurrentRequest(); if (PARSER == null || httpReq == null) { return null; } String userAgent = httpReq.getHeader("User-Agent"); if (userAgent == null) { return null; } Client client = PARSER.parse(userAgent); String browserFamily = client.userAgent.family; if (browserFamily == null) { return null; } Map<String, Object> props = Maps.newHashMap(); props.put("browserFamily", browserFamily); String browserVersionMajor = client.userAgent.major; if (isNotEmpty(browserVersionMajor)) { props.put("browserVersionMajor", browserVersionMajor); } String browserVersionMinor = client.userAgent.minor; if (isNotEmpty(browserVersionMinor)) { props.put("browserVersionMinor", browserVersionMinor); } String operatingSystemFamily = client.os.family; if (isNotEmpty(operatingSystemFamily)) { props.put("osFamily", operatingSystemFamily); String osVersionMajor = client.os.major; if (isNotEmpty(osVersionMajor)) { props.put("osVersionMajor", osVersionMajor); } String osVersionMinor = client.os.minor; if (isNotEmpty(osVersionMinor)) { props.put("osVersionMinor", osVersionMinor); } } return props; }  @Override Map<String, Object> properties(TrackRequest req);  }
@Test public void parsesUserAgentAndCreatesPropertiesForChrome() throws Exception { BrowserAndOperatingSystemAnalyticsProperties props = setup("Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2227.1 Safari/537.36"); ImmutableMap<String, Object> actual = ImmutableMap.copyOf(props.properties(new Analytics.TrackRequest("bob", null))); Assert.assertEquals(6, actual.size()); Assert.assertEquals("41", actual.get("browserVersionMajor")); Assert.assertEquals("10", actual.get("osVersionMinor")); Assert.assertEquals("Mac OS X", actual.get("osFamily")); Assert.assertEquals("Chrome", actual.get("browserFamily")); Assert.assertEquals("0", actual.get("browserVersionMinor")); Assert.assertEquals("10", actual.get("osVersionMajor")); }
@Test public void parsesUserAgentAndCreatesPropertiesForSafari() throws Exception { BrowserAndOperatingSystemAnalyticsProperties props = setup("Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_3) AppleWebKit/537.75.14 (KHTML, like Gecko) Version/7.0.3 Safari/7046A194A"); ImmutableMap<String, Object> actual = ImmutableMap.copyOf(props.properties(new Analytics.TrackRequest("bob", null))); Assert.assertEquals(6, actual.size()); Assert.assertEquals("7", actual.get("browserVersionMajor")); Assert.assertEquals("9", actual.get("osVersionMinor")); Assert.assertEquals("Mac OS X", actual.get("osFamily")); Assert.assertEquals("Safari", actual.get("browserFamily")); Assert.assertEquals("0", actual.get("browserVersionMinor")); Assert.assertEquals("10", actual.get("osVersionMajor")); }