A quick and simple limited feature implemenetation of the Google Analytics Measurement Protocol API in Java.
This GoogleAnalytics.java implementation relies on the Lombok code generation annotation library to generate a Builder class to allow for composing the fields of a GoogleAnalytics request.
Notes:
- The
/collect
and/debug/collect
endpoints are supported. - The
/batch
endpoit is not supported. - Both
POST
andGET
http request types are availabe.POST
is the default. - To enable debug mode use
GoogleAnalytics.setDebug(true)
. It will update the endpoint to/debug/collect
and set logging level toLevel.ALL
for verbose logging. - To control the logging level, use
GoogleAnalytics.setLogLevel(Level)
. The default logging level isLevel.SEVERE
. - Invoking the
GoogleAnalytics.send()
method will perform the network I/O asynchronously by spinning up a new Thread for doing the work. - For sychronous operation, use
GoogleAnalytics.send(false)
which will perform the network I/O on the thread it was invoked from. - All non-required parameters are cleared from the Tracker irregardless of success or failure of the network I/O when
GoogleAnalytics.send()
is invoked. - The following hit types are currently supported:
- pageview
- screenview
- event
Usage example:
String trackingId = "UA-12345677-12";
UUID clientId = UUID.randomUUID();
String appName = "My Application"
GoogleAnalytics.Tracker tracker = GoogleAnalytics.buildTracker(trackingId, clientId, appName);
tracker.type(GoogleAnalytics.HitType.event)
.category("application")
.action("startup")
.build()
.send();