Java library for Google's Measurement Protocol
https://developers.google.com/analytics/devguides/collection/protocol/v1/
Supports almost all features. 97% of lines covered by tests.
All builders and payload messages are immutable.
While aim of this library was to reduce chance of making invalid requests it is still possible to make invalid requests to GA due to not always consistent specs. If you find cases where this is true, please fill an issue and we will address it.
<dependency>
<groupId>com.github.tasubo</groupId>
<artifactId>jgmp</artifactId>
<version>1.7</version>
</dependency>
MpClient mpClient = MpClient.withTrackingId("UA-40659159-1")
.withCacheBuster()
.create();
App app = App.named("jGMP integration test").create();
ClientID clientID = ClientID.seeded("192.168.0.1", "MyString");
mpClient.send(AppView.hit("View description").with(app).with(clientId));
MpClient mpClient = MpClient.withTrackingId("UA-40659159-1")
.withCacheBuster()
.create();
ClientID clientID = ClientID.seeded("my_cookie=789");
Document document = Document.with()
.description("long document")
.hostname("localhost.com")
.path("/root")
.title("my document title")
.create();
mpClient.send(PageView.hit().with(document).with(clientID));
MpClient mpClient = MpClient.withTrackingId("UA-40659159-1")
.withClientId(clientId)
.withCacheBuster()
.create();
App app = App.named("jGMP integration test").create();
mpClient.send(Event.ofCategory("Test").withAction("testhit").labeled("Integration").create().with(app));
App app = App.named("jGMP integration test")
.version("0.1337")
.create();
SystemInfo systemInfo = SystemInfo.with()
.colorBits(24)
.screenResolution(800, 600)
.userLanguage("lt_LT")
.documentEncoding("UTF-8")
.javaEnabled()
.create();
MpClient mpClient = MpClient.withTrackingId("UA-40659159-1")
.withClientId(clientId)
.withCacheBuster()
.using(systemInfo)
.using(app)
.create();
mpClient.send(Timing.pageLoad(23));
App app = App.named("jGMP integration test")
.version("0.8008")
.create();
MpClient mpClient = MpClient.withTrackingId("UA-40659159-1")
.withClientId(clientId)
.withCacheBuster()
.using(app)
.create();
Decorating referrer = Referrer.from("http://localhost/");
UserTiming userTiming = Timing.user().name("test").time(4).create();
mpClient.send(userTiming.with(referrer));
MpClient mpClient = MpClient.withTrackingId("UA-40659159-1")
.withCacheBuster()
.noCacheBuster()
.anonymizingIp()
.noAnonymizingIp()
.usePost()
.useSsl()
.usePlainHttp()
.useGet()
.create();
App app = App.named("jGMP integration test").create();
mpClient.send(AppView.hit("View description").with(app));
// example for asynchronous, non-blocking requests
int concurrentRequests = 10;
JavaGetConnectionRequester blockingHttpRequester =
new JavaGetConnectionRequester();
AsyncHttpRequester asyncHttpRequester =
new AsyncHttpRequester(blockingHttpRequester, concurrentRequests);
MpClient asyncMpClient = MpClient.withTrackingId("UA-40659159-1")
.withClientId(clientId)
.httpRequester(asyncHttpRequester)
.create();
App app = App.named("jGMP integration test").create();
asyncMpClient.send(AppView.hit("View description").with(app));
// when your program shuts down you have to shutdown AsyncHttpRequester
asyncHttpRequester.shutdown();
For more usage examples please see tests