Google Analytics tracking with Java

After you start using Google Analytics, it’s pretty hard to stop. The ability to see stats and trends from users is invaluable to successfully maintaining software. Working with Adobe Flex, it was easy to set up google analytics tracking, as libraries were provided. But I was surprised to find that there was no library provided for sending tracking data in Java. The data export api is there, but nothing to send the tracking data. After doing some quick research, I decided to make my own.

The documentation provided by google here was pretty helpful, but not quite from complete (and not completely correct, I think it’s a bit outdated).  So anyways, I present JGoogleAnalyticsTracker!

JGoogleAnalytics tracker is lightweight (19kb, 6 classes) library designed to be simple an unobtrusive. Using the tracker is dead simple. You start my making your configuration data, which represents the machine making the request. This has stuff like the tracking code, java version, screen resolution, etc. Most of the stuff is populated automatically, the only thing it can’t figure out is the flash version.

AnalyticsConfigData config = new AnalyticsConfigData("MyTrackingCode");
// if you want to set your own config parameters:
config.setFlashVesion("9.0 r24");
// etc

Next, just create the tracker with your configuration and you’re good to go:

JGoogleAnalyticsTracker tracker = new JGoogleAnalyticsTracker(config, GoogleAnalyticsVersion.V_4_7_2);

And that’s it. You can track page visits, referrals, searches, events, and even make your own custom request and fill out whatever data you want for the tracking parameters. The tracker can out these requests in three different modes, synchronous, asynchronous, and queued (default). See the project page for more info.

For complete documentation, see the javadocs, or just view the source, it’s very straightforward.