Java API for Google Analytics Measurement Protocol, with the Open Source compatible Apache 2.0 license
The library is available from Maven Central. Add the following dependency, replacing $google-analytics-version
with the current stable version number:
Maven:
<dependency>
<groupId>net.mikehardy</groupId>
<artifactId>google-analytics-java</artifactId>
<version>$google-analytics-version</version>
</dependency>
Gradle:
implementation 'net.mikehardy:google-analytics-java:$google-analytics-version'
Others: Check Here
To get a local build, do
git clone https://github.com/mikehardy/google-analytics-java.git
mvn install
View Javadocs here
The fluent API is very easy to use, with sensible default options.
The library uses Java 1.8 features only available in Android7/API24/Nougat or higher, but there is a supported version compatible with Java 1.7 (and Android down to at least API 15) using the fairly amazing Java streamsupport / completable-futures compatibility library. If you need that, you'll just want to alter the dependency to use the "google-analytics-java7" artifact instead of "google-analytics-java"
This library implements the measurement protocol with following features.
Before using the library to post events, GoogleAnalytics
instance needs to be initialized. Once it is initialized, same instance can be used
to post events across multiple threads and instance is designed to be thread-safe.
It can be initialized with two types of information. Set of information called configuration (via GoogleAnalyticsConfig
), which is used by the library and default request settings (DefaultRequest
), which defines the default attributes for all subsequent requests.
Builder also provides typed methods to set most-relevant attributes of default request for readability.
Simplified initialization with all defaults is as follows.
ga = GoogleAnalytics.builder()
.withTrackingId("UA-00000000")
.build();
To build with custom configuration:
ga = GoogleAnalytics.builder()
.withConfig(new GoogleAnalyticsConfig().setBatchingEnabled(true).setBatchSize(10))
.withTrackingId("UA-00000000")
.build();
To build with custom configuration and some default request attributes:
ga = GoogleAnalytics.builder()
.withConfig(new GoogleAnalyticsConfig().setBatchingEnabled(true).setBatchSize(10))
.withDefaultRequest(new DefaultRequest().userIp("127.0.0.1").trackingId("UA-00000000"))
.build();
Note that tracking id can be set to one value for all requests (using default request attributes) or it can be set on per request basis.
To send requests, create one of the event type requests, configure the values for that event and call send()
.
Here are some examples:
ga.screenView()
.sessionControl("start")
.send();
ga.pageView()
.documentTitle(entry.getPage())
.documentPath("/" + entry.getPage())
.clientId("Some Id")
.customDimension(1, "Product")
.customDimension(1, "Version")
.userIp("198.165.0.1")
.send();
ga.exception()
.exceptionDescription(e.getMessage())
.send();
ga.screenView()
.sessionControl("end")
.send();
Sending request to Google Analytics is network call and hence it may take a little bit of time. If you would like to avoid this overhead, you can opt in to send requests asynchronously.
Executor is created to process the requests async with default config of minThreads=0, maxThreads=5, threadFormat=googleanalyticsjava-thread-{0}, threadTimeoutSecs=300, queueSize=1000. rejectExecutor=CallerRunsPolicy
.
If you want to change these values, configure them before building GoogleAnalytics
instance. You can also set your own executor in the config, in that case that executor will be used.
To send request async, call .sendAsync()
instead of .send()
as follows
ga.screenView()
.sessionControl("end")
.sendAsync();
Google Analytics api supports sending events in batch to reduce the network overhead. Batching is disabled by default but it can be enabled using batchingEnabled
config. This needs to be set before Google Analytics is built.
Once batching is enabled, usage is same as non-batching. Upon submission, request will be held in a internal list and upon reaching the batch limit, it will be posted to Google api. Note that batching can be used along with Async posting and it work in the same way.
Max batch size is 20 requests and that is the default, which can be changed using config batchSize
Library provides a master switch with config enabled
. If set to false
then requests will be accepted and silently dropped. This config variable can be changed before or after building the ga
instance.
Library tries to discover some default request parameters, which is controlled via config discoverRequestParameters
with default value of true
. Parameters are discovered during the building process so it is one time activity.
It discovers following parameters:
To discover screen resolution and colors, it needs access to java.awt
. Since not all environments have access to awt, it is not enabled by default. If would like to use it, set config requestParameterDiscoverer
to instance of AwtRequestParameterDiscoverer
Library abstracts http client interaction via HttpClient
interface with default implementation based on Apache HttpClient. If you want to use your own version of http client, set config httpClient
.