├── .github
└── dependabot.yml
├── .gitignore
├── .travis.yml
├── README.md
├── pom.xml
└── src
├── main
└── java
│ └── com
│ └── brsanthu
│ └── googleanalytics
│ ├── GoogleAnalytics.java
│ ├── GoogleAnalyticsBuilder.java
│ ├── GoogleAnalyticsConfig.java
│ ├── GoogleAnalyticsException.java
│ ├── GoogleAnalyticsExecutor.java
│ ├── GoogleAnalyticsStats.java
│ ├── discovery
│ ├── AwtRequestParameterDiscoverer.java
│ ├── DefaultRequestParameterDiscoverer.java
│ └── RequestParameterDiscoverer.java
│ ├── httpclient
│ ├── ApacheHttpClientImpl.java
│ ├── BatchUrlEncodedFormEntity.java
│ ├── GoogleAnalyticsThreadFactory.java
│ ├── HttpBatchRequest.java
│ ├── HttpBatchResponse.java
│ ├── HttpClient.java
│ ├── HttpRequest.java
│ ├── HttpResponse.java
│ ├── NameValuePair.java
│ └── OkHttpClientImpl.java
│ ├── internal
│ ├── Constants.java
│ ├── GaUtils.java
│ ├── GoogleAnalyticsImpl.java
│ ├── GoogleAnalyticsStatsImpl.java
│ └── ParameterGetterSetterGenerator.java
│ ├── logger
│ ├── DefaultLoggerFactory.java
│ ├── Logger.java
│ └── LoggerFactory.java
│ └── request
│ ├── DefaultRequest.java
│ ├── EventHit.java
│ ├── ExceptionHit.java
│ ├── GoogleAnalyticsParameter.java
│ ├── GoogleAnalyticsRequest.java
│ ├── GoogleAnalyticsResponse.java
│ ├── ItemHit.java
│ ├── PageViewHit.java
│ ├── ScreenViewHit.java
│ ├── SocialHit.java
│ ├── TimingHit.java
│ └── TransactionHit.java
└── test
└── java
└── com
└── brsanthu
└── googleanalytics
├── AwtRequestParameterDiscovererTest.java
├── DefaultRequestParameterDiscovererTest.java
├── EventHitTest.java
├── GaUtilsTest.java
├── GoogleAnalyticsApacheHttpTest.java
├── GoogleAnalyticsBatchTest.java
├── GoogleAnalyticsConfigTest.java
├── GoogleAnalyticsOkHttpTest.java
├── GoogleAnalyticsParameterTest.java
├── GoogleAnalyticsRequestTest.java
├── HitTypesTest.java
└── SamplingTest.java
/.github/dependabot.yml:
--------------------------------------------------------------------------------
1 | version: 2
2 | updates:
3 | - package-ecosystem: maven
4 | directory: "/"
5 | schedule:
6 | interval: daily
7 | time: "10:00"
8 | open-pull-requests-limit: 10
9 | - package-ecosystem: maven
10 | directory: "/"
11 | schedule:
12 | interval: daily
13 | time: "10:00"
14 | open-pull-requests-limit: 10
15 | target-branch: java7
16 | ignore:
17 | - dependency-name: com.squareup.okhttp3:okhttp
18 | versions:
19 | - ">= 3.13.a, < 3.14"
20 | - dependency-name: com.squareup.okhttp3:okhttp
21 | versions:
22 | - ">= 3.14.a, < 3.15"
23 | - dependency-name: com.squareup.okhttp3:okhttp
24 | versions:
25 | - ">= 4.a, < 5"
26 |
--------------------------------------------------------------------------------
/.gitignore:
--------------------------------------------------------------------------------
1 | *.class
2 |
3 | # Maven publish settings
4 | settings.xml
5 |
6 | # Package Files #
7 | *.jar
8 | *.war
9 | *.ear
10 |
11 | META-INF/
12 |
13 | # virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml
14 | hs_err_pid*
15 |
16 | # Eclipse Core
17 |
18 | .classpath
19 | .project
20 | .settings/
21 | .metadata/
22 | .recommenders/
23 | run-*
24 |
25 | # Ignore build artfacts at all sub folder level
26 | target/
27 | bin/
28 | src/test/resources/run-*
29 |
30 | # Locally stored "Eclipse launch configurations"
31 | *.launch
32 |
33 | # Eclipse CDT-specific
34 | .cproject
35 |
36 | ## IntelliJ
37 | *.iml
38 | .idea/
39 |
40 | # Java annotation processor (APT)
41 | .factorypath
42 |
43 | # PDT-specific
44 | .buildpath
45 |
46 | # sbteclipse plugin
47 | .target
48 |
49 | # TeXlipse plugin
50 | .texlipse
51 |
52 | # STS (Spring Tool Suite)
53 | .springBeans
54 |
55 | #Netbeans
56 | nbactions.xml
57 |
58 | #Mac finder files
59 | .DS_Store
60 |
61 | #node js
62 | node_modules/
63 |
64 | #JRebel files
65 | rebel.xml
66 | .rebel.xml.bak
67 |
68 | nohup.out
69 |
70 | core-search-service
71 | core-dns-service
72 |
--------------------------------------------------------------------------------
/.travis.yml:
--------------------------------------------------------------------------------
1 | language: java
2 | sudo: false
3 |
4 | jdk:
5 | - openjdk8
6 |
7 | env:
8 | - DISPLAY=:99.0
9 |
10 | services:
11 | - xvfb
12 |
13 | install: true
14 |
15 | script:
16 | - mvn package -Dgpg.skip=true
17 |
18 | before_cache:
19 | - rm -fr $HOME/.m2/repository/net/mikehardy/google-analytics-java
20 |
21 | cache:
22 | directories:
23 | - $HOME/.m2
24 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | Overview
2 | ==
3 | Java API for [Google Analytics Measurement Protocol](https://developers.google.com/analytics/devguides/collection/protocol/v1/), with the Open Source compatible Apache 2.0 license
4 |
5 | The library is available from Maven Central. Add the following dependency, replacing `$google-analytics-version` with the current stable version number:
6 |
7 | Maven:
8 |
9 |
10 | net.mikehardy
11 | google-analytics-java
12 | $google-analytics-version
13 |
14 |
15 | Gradle:
16 |
17 | implementation 'net.mikehardy:google-analytics-java:$google-analytics-version'
18 |
19 | Others: [Check Here](https://search.maven.org/#artifactdetails%7Cnet.mikehardy)
20 |
21 | To get a local build, do
22 |
23 | git clone https://github.com/mikehardy/google-analytics-java.git
24 | mvn install
25 |
26 | View Javadocs [here](https://www.javadoc.io/doc/net.mikehardy/google-analytics-java)
27 |
28 | The fluent API is very easy to use, with sensible default options.
29 |
30 | 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](https://github.com/stefan-zobel/streamsupport). If you need that, you'll just want to alter the dependency to use the "google-analytics-java7" artifact instead of "google-analytics-java"
31 |
32 |
33 | Features
34 | ==
35 | This library implements the measurement protocol with following features.
36 |
37 | * Supports all parameters and hit types.
38 | * Able to configure default parameters, which would be used for each request.
39 | * Type-safe data types as appropriate (String, Integer, Double and Boolean)
40 | * Convenient hit specific request types for easy construction.
41 | * Synchronous or Asynchronous Event Processing.
42 | * Support for delayed request construction.
43 | * Asynchronous processing uses Java Concurrent Executor Service.
44 | * Uses the latest Apache Http or OkHttp client for high performing event posting.
45 | * Event posting can be enabled/disabled at runtime at configuration level.
46 | * Supports connections via Proxy
47 | * Gathers some basic information from the underlying Jvm (File Encoding, User Language, Screen Size, Color Depth etc)
48 | * Validates the request and can throw exception or log warning if validation fails (still wip)
49 | * Logging uses SLF4J api
50 | * Gathers basic stats (number of events posted for each hit type) if requested in the configuration.
51 | * Implementation is Thread Safe
52 | * Jar files are OSGi ready, so could be used with Eclipse
53 | * Build against Java 1.8
54 | * Supports batching of requests
55 | * Complete Measurement Protocol parameter information is made available as Javadocs
56 |
57 | Usage
58 | ==
59 |
60 | Init
61 | --
62 | Before using the library to post events, `GoogleAnalytics` instance needs to be initialized. Once it is initialized, same instance can be used
63 | to post events across multiple threads and instance is designed to be thread-safe.
64 |
65 | 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.
66 |
67 | Builder also provides typed methods to set most-relevant attributes of default request for readability.
68 |
69 | Simplified initialization with all defaults is as follows.
70 |
71 | ga = GoogleAnalytics.builder()
72 | .withTrackingId("UA-00000000")
73 | .build();
74 |
75 | To build with custom configuration:
76 |
77 | ga = GoogleAnalytics.builder()
78 | .withConfig(new GoogleAnalyticsConfig().setBatchingEnabled(true).setBatchSize(10))
79 | .withTrackingId("UA-00000000")
80 | .build();
81 |
82 | To build with custom configuration and some default request attributes:
83 |
84 | ga = GoogleAnalytics.builder()
85 | .withConfig(new GoogleAnalyticsConfig().setBatchingEnabled(true).setBatchSize(10))
86 | .withDefaultRequest(new DefaultRequest().userIp("127.0.0.1").trackingId("UA-00000000"))
87 | .build();
88 |
89 | 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.
90 |
91 | Sending Events
92 | --
93 | To send requests, create one of the event type requests, configure the values for that event and call `send()`.
94 |
95 | Here are some examples:
96 |
97 | ga.screenView()
98 | .sessionControl("start")
99 | .send();
100 |
101 | ga.pageView()
102 | .documentTitle(entry.getPage())
103 | .documentPath("/" + entry.getPage())
104 | .clientId("Some Id")
105 | .customDimension(1, "Product")
106 | .customDimension(1, "Version")
107 | .userIp("198.165.0.1")
108 | .send();
109 |
110 | ga.exception()
111 | .exceptionDescription(e.getMessage())
112 | .send();
113 |
114 | ga.screenView()
115 | .sessionControl("end")
116 | .send();
117 |
118 | Async Posting
119 | --
120 | 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
121 | to send requests asynchronously.
122 |
123 | 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`.
124 |
125 | 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.
126 |
127 | To send request async, call `.sendAsync()` instead of `.send()` as follows
128 |
129 | ga.screenView()
130 | .sessionControl("end")
131 | .sendAsync();
132 |
133 | Batching
134 | --
135 | 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.
136 |
137 | 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.
138 |
139 | Max batch size is 20 requests and that is the default, which can be changed using config `batchSize`
140 |
141 | Master Switch
142 | --
143 | 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.
144 |
145 | Discovering Request Parameters
146 | --
147 | 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.
148 |
149 | It discovers following parameters:
150 |
151 | * user agent
152 | * user language
153 | * document encoding
154 | * screen resolution
155 | * screen colors
156 |
157 | 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`
158 |
159 | Http Client
160 | --
161 | 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`.
162 |
163 |
164 | Release Notes
165 | ==
166 | Version 2.0.11 - Mar 3 2020
167 | --
168 | * Dependency updates
169 |
170 | Version 2.0.10 - Jan 6 2019
171 | --
172 | * fixed ConcurrentModificationException in ApacheClient batch post
173 | * dependency updates
174 | * logging during test
175 |
176 | Version 2.0.9 - Dec 19 2019
177 | --
178 | * dependency updates
179 | * update CI
180 |
181 | Version 2.0.8 - Jun 27 2019
182 | --
183 | * Various dependency updates, notably OkHTTP 3 -> 4
184 |
185 | Version 2.0.7 - Feb 19 2019
186 | --
187 | * Various dependency updates
188 | * Fix APIs to return CompletableFuture instead of Future (thanks @jtjeferreira!)
189 |
190 | Version 2.0.6 - Dec 12 2018
191 | --
192 | * Compatibility - An evolution of 2.0.5, but "Java8-clean", Java7 compatibility split into separate artifact
193 |
194 | Version 2.0.5 - Dec 12 2018
195 | --
196 | * Enhancement - HTTP calls are Java-async now, still Android/Java7 compatible (thanks @jtjeferreira!)
197 |
198 | Version 2.0.4 - Oct 14 2018
199 | --
200 | * Compatibility - removed slf4j-simple from test to prevent dependency pollution (#20)
201 | * Enhancement - added more API abilities to manipulate sampling, and added sampling test
202 |
203 | Version 2.0.3 - Oct 12 2018
204 | --
205 | * Compatibility - Altered Core and OkHttpClientImpl so it worked with minSDK / API15 on Android
206 |
207 | Version 2.0.2 - Oct 12 2018
208 | --
209 | * Error - Fixed #11 - not closing OkHttp response body in postBatch()
210 | * Enhancement - Fixed #16 - implemented basic sampling strategy with GoogleAnalyticsConfig.setSamplePercentage(int)
211 | * Enhancement - request parameters are alphabetically ordered so they are predictable now
212 | * Build - fix javadoc generation on JDK10+
213 |
214 | Version 2.0.1 - Oct 02 2018
215 | --
216 | * Implement OkHttp transport as an option
217 |
218 | Version 2.0.0 - Jan 24 2018
219 | --
220 | * API redesign based on builder and fluent pattern
221 | * Added support for batching requests
222 |
223 | Version 1.1.2 - Apr 29 2015
224 | --
225 |
226 |
227 | Version 1.1.1 - May 21 2014
228 | --
229 | * Fixed the issue #14. Https Collection url has been updated to latest one.
230 | * Fixed the issue #15. Added new parameter User Id (uid). As part of this, another change was made to move initializing the default ClientId parameter from GoogleAnalyticsRequest to DefaultRequest. This way, whatever the default clientid you use, will be used for all requests. Previously, default client id wasn't referred.
231 |
232 | Version 1.1.0 - Apr 22 2014
233 | --
234 | * Fixed the issue #5. Fix changes some of the existing behavior. If you are using discover system parameters, then by default Screen Colors and Screen Resolution will not be populated. If you would like, you need to set AwtRequestParameterDiscoverer in the GoogleAnalyticsConfig before initializing the GoogleAnalytics. This change is to ensure that it can be used in a environment where JVM has no access to java.awt.* classes.
235 |
236 | Version 1.0.5 - Apr 09 2014
237 | --
238 | * Fixed the issue #12
239 |
240 | Version 1.0.4 - Mar 3 2014
241 | --
242 | * Fixed the issue #8
243 |
244 | Version 1.0.3 - Jan 20 2014
245 | --
246 | * Fixed the issue #6
247 |
248 |
249 | Development Notes
250 | ==
251 | * To release (assuming you have maven / sonatype permission), `mvn release:prepare` then `mvn release:perform`
252 |
253 |
254 | Other Implementations
255 | ==
256 |
257 | This is a fork of what I still consider the "upstream" version here: https://github.com/brsanthu/google-analytics-java
258 |
259 | Santosh Kumar created what I believe is the best open-source Java google analytics client. My only reason for forking was a desire for a large number of changes rapidly and I didn't see PRs being accepted in the main repo - no other reason and an eventual merge would be fine. In the same manner: please fork this repo and move forward if I don't respond to you :-)
260 |
261 | There are few Java implementation of Google Analytics api, but found some issues (or protocol mismatch) with each of them.
262 |
263 | https://github.com/nhnopensource/universal-analytics-java
264 | * Doesn't implement all parameters of Measurement Protocol.
265 | * Cannot specify default parameters
266 | * Only one unit test case and coverage is very minimal
267 | * Uses Legacy Apache Http Client (3.x)
268 |
269 | https://code.google.com/p/jgoogleanalyticstracker/
270 | * Implements Legacy Google Analytics protocol
271 |
272 | https://github.com/siddii/jgoogleanalytics
273 | * Implements Legacy Google Analytics protocol
274 |
--------------------------------------------------------------------------------
/pom.xml:
--------------------------------------------------------------------------------
1 |
2 | 4.0.0
3 | net.mikehardy
4 | google-analytics-java
5 | 2.0.12-SNAPSHOT
6 | jar
7 |
8 |
9 | 1.8
10 | 2.0.7
11 | 4.13.2
12 | 5.2.0
13 | 4.5.14
14 | 4.10.0
15 | UTF-8
16 |
17 |
18 |
19 |
20 |
21 |
22 | org.sonatype.plugins
23 | nexus-staging-maven-plugin
24 | 1.6.13
25 | true
26 |
27 | ossrh
28 | https://oss.sonatype.org/
29 | true
30 |
31 |
32 |
33 |
34 | maven-compiler-plugin
35 | 3.11.0
36 |
37 | ${java-version}
38 | ${java-version}
39 | true
40 | true
41 |
42 |
43 |
44 | maven-source-plugin
45 | 3.2.1
46 |
47 |
48 | attach-sources
49 |
50 | jar-no-fork
51 |
52 |
53 |
54 |
55 |
56 |
57 | maven-javadoc-plugin
58 | 3.5.0
59 |
60 |
61 | false
62 | none
63 |
64 |
65 |
66 | attach-javadocs
67 |
68 | jar
69 |
70 | true
71 |
72 |
73 |
74 |
75 |
76 | maven-jar-plugin
77 | 3.3.0
78 |
79 |
80 | true
81 |
82 | true
83 | true
84 |
85 |
86 |
87 |
88 |
89 |
90 | maven-surefire-plugin
91 | 3.0.0
92 |
93 | false
94 |
95 |
96 |
97 |
98 | org.apache.felix
99 | maven-bundle-plugin
100 | 5.1.8
101 |
102 | META-INF
103 |
104 | ${project.groupId}.${project.artifactId}
105 | *;version=!
106 |
107 |
108 |
109 |
110 | bundle-manifest
111 | process-classes
112 |
113 | manifest
114 |
115 |
116 |
117 | bundle
118 | package
119 |
120 | bundle
121 |
122 |
123 |
124 |
125 |
126 |
127 | org.apache.maven.plugins
128 | maven-release-plugin
129 | 3.0.0
130 |
131 | true
132 | true
133 | release
134 | deploy
135 |
136 |
137 |
138 | org.apache.maven.plugins
139 | maven-gpg-plugin
140 | 3.0.1
141 |
142 |
143 | sign-artifacts
144 | verify
145 |
146 | sign
147 |
148 |
149 | ${gpg.keyname}
150 | ${gpg.keyname}
151 |
152 |
153 |
154 |
155 |
156 |
157 |
158 |
159 |
160 | org.slf4j
161 | slf4j-api
162 | ${slf4j-version}
163 |
164 |
165 | org.slf4j
166 | slf4j-simple
167 | ${slf4j-version}
168 | test
169 |
170 |
171 | org.slf4j
172 | jcl-over-slf4j
173 | ${slf4j-version}
174 |
175 |
176 | org.apache.httpcomponents
177 | httpclient
178 | ${httpclient-version}
179 |
180 |
181 | commons-logging
182 | commons-logging
183 |
184 |
185 |
186 |
187 | com.squareup.okhttp3
188 | okhttp
189 | ${okhttp-version}
190 |
191 |
192 | junit
193 | junit
194 | ${junit.version}
195 | test
196 |
197 |
198 | org.mockito
199 | mockito-core
200 | ${mockito.version}
201 | test
202 |
203 |
204 | Google Analytics Java API
205 | https://github.com/mikehardy/google-analytics-java
206 | Open Source Java API for Google Analytics.
207 |
208 | More information about the protocol is available at https://developers.google.com/analytics/devguides/collection/protocol/v1/
209 | Sep 2013
210 |
211 | http://mikehardy.net
212 | Mike Hardy
213 |
214 |
215 | https://github.com:mikehardy/google-analytics-java
216 | scm:git:git@github.com:mikehardy/google-analytics-java.git
217 | scm:git:git@github.com:mikehardy/google-analytics-java.git
218 | HEAD
219 |
220 |
221 | github.com
222 | https://github.com/mikehardy/google-analytics-java/issues
223 |
224 |
225 |
226 | Santhosh Kumar
227 | http://www.brsanthu.com
228 | http://www.brsanthu.com
229 |
230 | Initiator
231 | Commiter
232 |
233 | brsanthu at gmail
234 |
235 |
236 | Mike Hardy
237 | http://mikehardy.net
238 | http://mikehardy.net
239 |
240 | Commiter
241 |
242 | mikehardy
243 | mike at mikehardy and a dot net
244 |
245 |
246 |
247 |
248 | The Apache Software License, Version 2.0
249 | http://www.apache.org/licenses/LICENSE-2.0.txt
250 | repo
251 |
252 |
253 |
254 |
255 | Travis CI
256 | https://travis-ci.com/mikehardy/google-analytics-java/
257 |
258 |
259 |
260 | ossrh
261 | https://oss.sonatype.org/content/repositories/snapshots
262 |
263 |
264 | ossrh
265 | https://oss.sonatype.org/service/local/staging/deploy/maven2/
266 |
267 |
268 |
269 |
--------------------------------------------------------------------------------
/src/main/java/com/brsanthu/googleanalytics/GoogleAnalytics.java:
--------------------------------------------------------------------------------
1 | package com.brsanthu.googleanalytics;
2 |
3 | import com.brsanthu.googleanalytics.request.EventHit;
4 | import com.brsanthu.googleanalytics.request.ExceptionHit;
5 | import com.brsanthu.googleanalytics.request.ItemHit;
6 | import com.brsanthu.googleanalytics.request.PageViewHit;
7 | import com.brsanthu.googleanalytics.request.ScreenViewHit;
8 | import com.brsanthu.googleanalytics.request.SocialHit;
9 | import com.brsanthu.googleanalytics.request.TimingHit;
10 | import com.brsanthu.googleanalytics.request.TransactionHit;
11 |
12 | public interface GoogleAnalytics extends AutoCloseable {
13 |
14 | EventHit event();
15 |
16 | ExceptionHit exception();
17 |
18 | ItemHit item();
19 |
20 | PageViewHit pageView();
21 |
22 | PageViewHit pageView(String url, String title);
23 |
24 | PageViewHit pageView(String url, String title, String description);
25 |
26 | ScreenViewHit screenView();
27 |
28 | ScreenViewHit screenView(String appName, String screenName);
29 |
30 | SocialHit social();
31 |
32 | SocialHit social(String socialNetwork, String socialAction, String socialTarget);
33 |
34 | TimingHit timing();
35 |
36 | TransactionHit transaction();
37 |
38 | GoogleAnalyticsStats getStats();
39 |
40 | GoogleAnalyticsConfig getConfig();
41 |
42 | boolean inSample();
43 |
44 | boolean performSamplingElection();
45 |
46 | void ifEnabled(Runnable runnable);
47 |
48 | void resetStats();
49 |
50 | static GoogleAnalyticsBuilder builder() {
51 | return new GoogleAnalyticsBuilder();
52 | }
53 |
54 | void flush();
55 | }
--------------------------------------------------------------------------------
/src/main/java/com/brsanthu/googleanalytics/GoogleAnalyticsBuilder.java:
--------------------------------------------------------------------------------
1 | package com.brsanthu.googleanalytics;
2 |
3 | import com.brsanthu.googleanalytics.discovery.DefaultRequestParameterDiscoverer;
4 | import com.brsanthu.googleanalytics.discovery.RequestParameterDiscoverer;
5 | import com.brsanthu.googleanalytics.httpclient.ApacheHttpClientImpl;
6 | import com.brsanthu.googleanalytics.httpclient.HttpClient;
7 | import com.brsanthu.googleanalytics.internal.GaUtils;
8 | import com.brsanthu.googleanalytics.internal.GoogleAnalyticsImpl;
9 | import com.brsanthu.googleanalytics.request.DefaultRequest;
10 |
11 | public class GoogleAnalyticsBuilder {
12 | private GoogleAnalyticsConfig config = new GoogleAnalyticsConfig();
13 | private DefaultRequest defaultRequest = new DefaultRequest();
14 | private HttpClient httpClient;
15 |
16 | public GoogleAnalyticsBuilder withConfig(GoogleAnalyticsConfig config) {
17 | this.config = GaUtils.firstNotNull(config, new GoogleAnalyticsConfig());
18 | return this;
19 | }
20 |
21 | public GoogleAnalyticsBuilder withTrackingId(String trackingId) {
22 | defaultRequest.trackingId(trackingId);
23 | return this;
24 | }
25 |
26 | public GoogleAnalyticsBuilder withAppName(String value) {
27 | defaultRequest.applicationName(value);
28 | return this;
29 | }
30 |
31 | public GoogleAnalyticsBuilder withAppVersion(String value) {
32 | defaultRequest.applicationVersion(value);
33 | return this;
34 | }
35 |
36 | public GoogleAnalyticsBuilder withDefaultRequest(DefaultRequest defaultRequest) {
37 | this.defaultRequest = GaUtils.firstNotNull(defaultRequest, new DefaultRequest());
38 | return this;
39 | }
40 |
41 | public GoogleAnalyticsBuilder withHttpClient(HttpClient httpClient) {
42 | this.httpClient = httpClient;
43 | return this;
44 | }
45 |
46 | public GoogleAnalytics build() {
47 | if (config.isDiscoverRequestParameters()) {
48 | RequestParameterDiscoverer discoverer = GaUtils.firstNotNull(config.getRequestParameterDiscoverer(),
49 | DefaultRequestParameterDiscoverer.INSTANCE);
50 |
51 | discoverer.discoverParameters(config, defaultRequest);
52 | }
53 |
54 | return new GoogleAnalyticsImpl(config, defaultRequest, createHttpClient());
55 | }
56 |
57 | protected HttpClient createHttpClient() {
58 | if (httpClient != null) {
59 | return httpClient;
60 | }
61 |
62 | return new ApacheHttpClientImpl(config);
63 | }
64 |
65 | }
66 |
--------------------------------------------------------------------------------
/src/main/java/com/brsanthu/googleanalytics/GoogleAnalyticsConfig.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
3 | * the License. You may obtain a copy of the License at
4 | *
5 | * http://www.apache.org/licenses/LICENSE-2.0
6 | *
7 | * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
8 | * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
9 | * specific language governing permissions and limitations under the License.
10 | */
11 | package com.brsanthu.googleanalytics;
12 |
13 | import com.brsanthu.googleanalytics.discovery.AwtRequestParameterDiscoverer;
14 | import com.brsanthu.googleanalytics.discovery.DefaultRequestParameterDiscoverer;
15 | import com.brsanthu.googleanalytics.discovery.RequestParameterDiscoverer;
16 | import com.brsanthu.googleanalytics.internal.GoogleAnalyticsImpl;
17 | import com.brsanthu.googleanalytics.internal.GoogleAnalyticsStatsImpl;
18 |
19 | /**
20 | * Properties that can be configured in this library. These would include any properties that are required to process
21 | * the tracking request or enhance the tracking request (but not specified in measurement protocol like User agent).
22 | *
23 | * Most of the properties are initialization level and request level. If a property is a initialization level property,
24 | * it should be set at the time of GoogleAnalytics object initialization. If a property is a request level property, it
25 | * can be set any time and it will be effective.
26 | *
27 | * All properties of this config object supports method chaining. So for example, you could do,
28 | * new GoogleAnalyticsConfig().setMaxThreads(2).setThreadNameFormat("name");
29 | *
30 | * @author Santhosh Kumar
31 | */
32 | public class GoogleAnalyticsConfig {
33 |
34 | public static final int DEFAULT_MAX_HTTP_CONNECTIONS_PER_ROUTE = 10;
35 |
36 | private String threadNameFormat = "googleanalyticsjava-thread-{0}";
37 | private boolean enabled = true;
38 | private int minThreads = 0;
39 | private int maxThreads = 5;
40 | private int threadTimeoutSecs = 300;
41 | private int threadQueueSize = 1000;
42 | private int maxHttpConnectionsPerRoute = DEFAULT_MAX_HTTP_CONNECTIONS_PER_ROUTE;
43 | private int samplePercentage = 100;
44 | private boolean useHttps = true;
45 | private boolean validate = true;
46 | private boolean batchingEnabled = false;
47 | private int batchSize = 20;
48 | private String httpUrl = "http://www.google-analytics.com/collect";
49 | private String httpsUrl = "https://www.google-analytics.com/collect";
50 | private String batchUrl = "https://www.google-analytics.com/batch";
51 | private String userAgent = null;
52 | private String proxyHost = null;
53 | private int proxyPort = 80;
54 | private String proxyUserName = null;
55 | private String proxyPassword = null;
56 | private boolean discoverRequestParameters = true;
57 | private boolean gatherStats = false;
58 | private RequestParameterDiscoverer requestParameterDiscoverer = new DefaultRequestParameterDiscoverer();
59 |
60 | public RequestParameterDiscoverer getRequestParameterDiscoverer() {
61 | return requestParameterDiscoverer;
62 | }
63 |
64 | /**
65 | * Sets the appropriate request parameter discoverer. Default is {@link DefaultRequestParameterDiscoverer} but can
66 | * be changed to {@link AwtRequestParameterDiscoverer} if you want to use Toolkit to derive the screen resolution
67 | * etc.
68 | *
69 | * Please make sure you also enable the discovery using {@link #setDiscoverRequestParameters(boolean)}
70 | *
71 | * @param requestParameterDiscoverer can be null and is so, parameters will not be discovered.
72 | * @return GoogleAnalyticsConfig with requestParameterDiscoverer set
73 | */
74 | public GoogleAnalyticsConfig setRequestParameterDiscoverer(RequestParameterDiscoverer requestParameterDiscoverer) {
75 | this.requestParameterDiscoverer = requestParameterDiscoverer;
76 | return this;
77 | }
78 |
79 | public boolean isGatherStats() {
80 | return gatherStats;
81 | }
82 |
83 | /**
84 | * If set to true, {@link GoogleAnalyticsImpl} will collect the basic stats about successful event postings for
85 | * various hit types and keeps a copy of {@link GoogleAnalyticsStatsImpl}, which can be retrieved using
86 | * {@link GoogleAnalyticsImpl#getStats()}
87 | *
88 | * @param gatherStats
89 | * @return GoogleAnalyticsConfig with gatherStats set
90 | */
91 | public GoogleAnalyticsConfig setGatherStats(boolean gatherStats) {
92 | this.gatherStats = gatherStats;
93 | return this;
94 | }
95 |
96 | /**
97 | * Sets the thread name format that should be while creating the threads.
98 | *
99 | * Default is "googleanalytics-thread-{0}" where {0} is the thread counter. If you specify a custom format, make
100 | * sure {0} is there somewhere otherwise all threads will be nameed same and can be an issue for troubleshooting.
101 | *
102 | * @param threadNameFormat non-null string for thread name.
103 | */
104 | public GoogleAnalyticsConfig setThreadNameFormat(String threadNameFormat) {
105 | this.threadNameFormat = threadNameFormat;
106 | return this;
107 | }
108 |
109 | public String getThreadNameFormat() {
110 | return threadNameFormat;
111 | }
112 |
113 | /**
114 | * Deprecated since 1.0.6
115 | *
116 | * @deprecated Use {@link #setDiscoverRequestParameters(boolean)} instead
117 | */
118 | @Deprecated
119 | public GoogleAnalyticsConfig setDeriveSystemParameters(boolean deriveSystemProperties) {
120 | return setDiscoverRequestParameters(deriveSystemProperties);
121 | }
122 |
123 | /**
124 | * If true, derives the system properties (User Language, Region, Country, Screen Size, Color Depth, and File
125 | * encoding) and adds to the default request.
126 | *
127 | *
128 | * This is initialization level configuration (must be set while creating GoogleAnalytics object).
129 | *
130 | */
131 | public GoogleAnalyticsConfig setDiscoverRequestParameters(boolean discoverSystemParameters) {
132 | this.discoverRequestParameters = discoverSystemParameters;
133 | return this;
134 | }
135 |
136 | public boolean isDiscoverRequestParameters() {
137 | return discoverRequestParameters;
138 | }
139 |
140 | /**
141 | * Sets the user name which should be used to authenticate to the proxy server. This is applicable only if
142 | * {@link #setProxyHost(String)} is not empty.
143 | *
144 | *
145 | * This is initialization level configuration (must be set while creating GoogleAnalytics object).
146 | *
147 | */
148 | public GoogleAnalyticsConfig setProxyUserName(String proxyUserName) {
149 | this.proxyUserName = proxyUserName;
150 | return this;
151 | }
152 |
153 | public String getProxyUserName() {
154 | return proxyUserName;
155 | }
156 |
157 | public String getProxyPassword() {
158 | return proxyPassword;
159 | }
160 |
161 | /**
162 | * Sets the password which should be used to authenticate to the proxy server. This is applicable only if
163 | * {@link #setProxyHost(String)} and {@link #setProxyUserName(String)} is not empty.
164 | *
165 | *
166 | * This is initialization level configuration (must be set while creating GoogleAnalytics object).
167 | *
168 | */
169 | public GoogleAnalyticsConfig setProxyPassword(String proxyPassword) {
170 | this.proxyPassword = proxyPassword;
171 | return this;
172 | }
173 |
174 | public String getProxyHost() {
175 | return proxyHost;
176 | }
177 |
178 | /**
179 | * Sets the host name of the proxy server, to connect to Google analytics.
180 | *
181 | *
182 | * This is initialization level configuration (must be set while creating GoogleAnalytics object).
183 | *
184 | */
185 | public GoogleAnalyticsConfig setProxyHost(String proxyHost) {
186 | this.proxyHost = proxyHost;
187 | return this;
188 | }
189 |
190 | public int getProxyPort() {
191 | return proxyPort;
192 | }
193 |
194 | /**
195 | * Sets the host name of the proxy server, to connect to Google analytics.
196 | *
197 | *
198 | * This is initialization level configuration (must be set while creating GoogleAnalytics object).
199 | *
200 | */
201 | public GoogleAnalyticsConfig setProxyPort(int proxyPort) {
202 | this.proxyPort = proxyPort;
203 | return this;
204 | }
205 |
206 | public String getUserAgent() {
207 | return userAgent;
208 | }
209 |
210 | /**
211 | * Sets the user agent string that should be sent while making the http request. Default is Apache Http Client's
212 | * user agent, which looks something similar to this. Apache-HttpClient/release (java 1.5)
213 | *
214 | *
215 | * This is initialization level configuration (must be set while creating GoogleAnalytics object).
216 | *
217 | */
218 | public GoogleAnalyticsConfig setUserAgent(String userAgent) {
219 | this.userAgent = userAgent;
220 | return this;
221 | }
222 |
223 | public boolean isEnabled() {
224 | return enabled;
225 | }
226 |
227 | /**
228 | * Enables or disables the GoogleAnalytics posting. If disabled, library will continue to accept the send/post
229 | * requests but silently skips sending the event and returns successful response. Default is false.
230 | *
231 | *
232 | * This is request level configuration (can be changed any time).
233 | *
234 | */
235 | public GoogleAnalyticsConfig setEnabled(boolean enabled) {
236 | this.enabled = enabled;
237 | return this;
238 | }
239 |
240 | /**
241 | * Maximum threads to use to process the asynchronous event posting and Http client connection pooling. Default is
242 | *
243 | *
244 | * This is initialization level configuration (must be set while creating GoogleAnalytics object).
245 | *
246 | */
247 | public int getMaxThreads() {
248 | return maxThreads;
249 | }
250 |
251 | public GoogleAnalyticsConfig setMaxThreads(int maxThreads) {
252 | this.maxThreads = maxThreads;
253 | return this;
254 | }
255 |
256 | public int getMinThreads() {
257 | return minThreads;
258 | }
259 |
260 | public GoogleAnalyticsConfig setMinThreads(int minThreads) {
261 | this.minThreads = minThreads;
262 | return this;
263 | }
264 |
265 | public boolean isUseHttps() {
266 | return useHttps;
267 | }
268 |
269 | /**
270 | * Instructs to use https url to send the events. Default is true.
271 | *
272 | *
273 | * This is request level configuration (can be changed any time).
274 | *
275 | */
276 | public GoogleAnalyticsConfig setUseHttps(boolean useHttps) {
277 | this.useHttps = useHttps;
278 | return this;
279 | }
280 |
281 | public boolean isValidate() {
282 | return validate;
283 | }
284 |
285 | /**
286 | * If set, validates the request before sending to Google Analytics. If any errors found, GoogleAnalyticsException
287 | * will be thrown with details. Default is false. Note that, if you are sending the event in async mode, then
288 | * request is always validated and logged to log file as warnings irrespective of this flag.
289 | *
290 | *
291 | * This is request level configuration (can be changed any time).
292 | *
293 | */
294 | public GoogleAnalyticsConfig setValidate(boolean validate) {
295 | this.validate = validate;
296 | return this;
297 | }
298 |
299 | public String getHttpUrl() {
300 | return httpUrl;
301 | }
302 |
303 | /**
304 | * URL to use when posting the event in http mode. This url is Google Analytics service url and usually not updated
305 | * by the clients.
306 | *
307 | *
308 | * Default value is http://www.google-analytics.com/collect
309 | *
310 | *
311 | *
312 | * This is request level configuration (can be changed any time).
313 | *
314 | */
315 | public GoogleAnalyticsConfig setHttpUrl(String httpUrl) {
316 | this.httpUrl = httpUrl;
317 | return this;
318 | }
319 |
320 | public String getHttpsUrl() {
321 | return httpsUrl;
322 | }
323 |
324 | /**
325 | * URL to use when posting the event in https mode. This url is Google Analytics service url and usually not updated
326 | * by the clients.
327 | *
328 | * Default value is https://www.google-analytics.com/collect
329 | *
330 | *
331 | * This is request level configuration (can be changed any time).
332 | *
333 | */
334 | public GoogleAnalyticsConfig setHttpsUrl(String httpsUrl) {
335 | this.httpsUrl = httpsUrl;
336 | return this;
337 | }
338 |
339 | public String getUrl() {
340 | return useHttps ? httpsUrl : httpUrl;
341 | }
342 |
343 | public int getMaxHttpConnectionsPerRoute() {
344 | return maxHttpConnectionsPerRoute;
345 | }
346 |
347 | public GoogleAnalyticsConfig setMaxHttpConnectionsPerRoute(int maxHttpConnectionsPerRoute) {
348 | this.maxHttpConnectionsPerRoute = maxHttpConnectionsPerRoute;
349 | return this;
350 | }
351 |
352 | /**
353 | * The sample percentage to apply to all analytics. Integer between 0 and 100, every time an analytics implementation
354 | * is initialized, a random number will be compared to the sample percentage, allowing or disallowing analytics
355 | * from the initialized instance
356 | * @return int between 1 and 100
357 | */
358 | public int getSamplePercentage() {
359 | return samplePercentage;
360 | }
361 |
362 | public GoogleAnalyticsConfig setSamplePercentage(int samplePercentage) {
363 | this.samplePercentage = samplePercentage;
364 | return this;
365 | }
366 |
367 | @Override
368 | public String toString() {
369 | return "GoogleAnalyticsConfig [threadNameFormat=" + threadNameFormat + ", enabled=" + enabled + ", minThreads=" + minThreads + ", maxThreads="
370 | + maxThreads + ", threadTimeoutSecs=" + threadTimeoutSecs + ", threadQueueSize=" + threadQueueSize + ", maxHttpConnectionsPerRoute="
371 | + maxHttpConnectionsPerRoute + ", samplePercentage=" + samplePercentage + ", useHttps=" + useHttps + ", validate=" + validate + ", httpUrl=" + httpUrl + ", httpsUrl=" + httpsUrl
372 | + ", userAgent=" + userAgent + ", proxyHost=" + proxyHost + ", proxyPort=" + proxyPort + ", proxyUserName=" + proxyUserName
373 | + ", proxyPassword=" + mask(proxyPassword) + ", discoverRequestParameters=" + discoverRequestParameters + ", gatherStats="
374 | + gatherStats + ", requestParameterDiscoverer=" + requestParameterDiscoverer + "]";
375 | }
376 |
377 | public static String mask(String value) {
378 | return value == null ? null : "********";
379 | }
380 |
381 | public int getThreadQueueSize() {
382 | return threadQueueSize;
383 | }
384 |
385 | public GoogleAnalyticsConfig setThreadQueueSize(int threadQueueSize) {
386 | this.threadQueueSize = threadQueueSize;
387 | return this;
388 | }
389 |
390 | public int getThreadTimeoutSecs() {
391 | return threadTimeoutSecs;
392 | }
393 |
394 | public GoogleAnalyticsConfig setThreadTimeoutSecs(int threadTimeoutSecs) {
395 | this.threadTimeoutSecs = threadTimeoutSecs;
396 | return this;
397 | }
398 |
399 | public String getBatchUrl() {
400 | return batchUrl;
401 | }
402 |
403 | public GoogleAnalyticsConfig setBatchUrl(String batchUrl) {
404 | this.batchUrl = batchUrl;
405 | return this;
406 | }
407 |
408 | public boolean isBatchingEnabled() {
409 | return batchingEnabled;
410 | }
411 |
412 | public GoogleAnalyticsConfig setBatchingEnabled(boolean batchingEnabled) {
413 | this.batchingEnabled = batchingEnabled;
414 | return this;
415 | }
416 |
417 | public int getBatchSize() {
418 | return batchSize;
419 | }
420 |
421 | public GoogleAnalyticsConfig setBatchSize(int batchSize) {
422 | this.batchSize = batchSize;
423 | return this;
424 | }
425 |
426 | }
427 |
--------------------------------------------------------------------------------
/src/main/java/com/brsanthu/googleanalytics/GoogleAnalyticsException.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Licensed under the Apache License, Version 2.0 (the "License");
3 | * you may not use this file except in compliance with the License.
4 | * You may obtain a copy of the License at
5 | *
6 | * http://www.apache.org/licenses/LICENSE-2.0
7 | *
8 | * Unless required by applicable law or agreed to in writing, software
9 | * distributed under the License is distributed on an "AS IS" BASIS,
10 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11 | * See the License for the specific language governing permissions and
12 | * limitations under the License.
13 | */
14 |
15 | package com.brsanthu.googleanalytics;
16 |
17 | /**
18 | * Any exception thrown (usually due to validation), it would be of this type.
19 | *
20 | * @author Santhosh Kumar
21 | */
22 | public class GoogleAnalyticsException extends RuntimeException {
23 |
24 | private static final long serialVersionUID = 1L;
25 |
26 | public GoogleAnalyticsException() {
27 | super();
28 | }
29 |
30 | public GoogleAnalyticsException(String message, Throwable cause) {
31 | super(message, cause);
32 | }
33 |
34 | public GoogleAnalyticsException(String message) {
35 | super(message);
36 | }
37 |
38 | public GoogleAnalyticsException(Throwable cause) {
39 | super(cause);
40 | }
41 | }
42 |
--------------------------------------------------------------------------------
/src/main/java/com/brsanthu/googleanalytics/GoogleAnalyticsExecutor.java:
--------------------------------------------------------------------------------
1 | package com.brsanthu.googleanalytics;
2 |
3 | import java.util.concurrent.CompletableFuture;
4 |
5 | import com.brsanthu.googleanalytics.request.GoogleAnalyticsRequest;
6 | import com.brsanthu.googleanalytics.request.GoogleAnalyticsResponse;
7 |
8 | public interface GoogleAnalyticsExecutor {
9 | GoogleAnalyticsResponse post(GoogleAnalyticsRequest> request);
10 |
11 | CompletableFuture postAsync(GoogleAnalyticsRequest> request);
12 | }
13 |
--------------------------------------------------------------------------------
/src/main/java/com/brsanthu/googleanalytics/GoogleAnalyticsStats.java:
--------------------------------------------------------------------------------
1 | package com.brsanthu.googleanalytics;
2 |
3 | public interface GoogleAnalyticsStats {
4 |
5 | long getPageViewHits();
6 |
7 | long getEventHits();
8 |
9 | long getScreenViewHits();
10 |
11 | long getItemHits();
12 |
13 | long getTransactionHits();
14 |
15 | long getTimingHits();
16 |
17 | long getSocialHits();
18 |
19 | long getExceptionHits();
20 | }
--------------------------------------------------------------------------------
/src/main/java/com/brsanthu/googleanalytics/discovery/AwtRequestParameterDiscoverer.java:
--------------------------------------------------------------------------------
1 | package com.brsanthu.googleanalytics.discovery;
2 |
3 | import static com.brsanthu.googleanalytics.internal.GaUtils.isEmpty;
4 |
5 | import java.awt.Dimension;
6 | import java.awt.GraphicsDevice;
7 | import java.awt.GraphicsEnvironment;
8 | import java.awt.Toolkit;
9 |
10 | import com.brsanthu.googleanalytics.GoogleAnalyticsConfig;
11 | import com.brsanthu.googleanalytics.request.DefaultRequest;
12 |
13 | /**
14 | * Clases uses AWT classes to discover following properties.
15 | *
171 | * Specifies the event value. Values must be non-negative.
172 | *
173 | *
174 | *
175 | *
176 | *
Parameter
177 | *
Value Type
178 | *
Default Value
179 | *
Max Length
180 | *
Supported Hit Types
181 | *
182 | *
183 | *
ev
184 | *
integer
185 | *
None
186 | *
None
187 | *
event
188 | *
189 | *
190 | *
191 | *
Example value: 55
192 | * Example usage: ev=55
193 | */
194 | public EventHit eventValue(Integer value) {
195 | setInteger(EVENT_VALUE, value);
196 | return this;
197 | }
198 |
199 | public Integer eventValue() {
200 | return getInteger(EVENT_VALUE);
201 | }
202 | }
203 |
--------------------------------------------------------------------------------
/src/main/java/com/brsanthu/googleanalytics/request/ExceptionHit.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Licensed under the Apache License, Version 2.0 (the "License");
3 | * you may not use this file except in compliance with the License.
4 | * You may obtain a copy of the License at
5 | *
6 | * http://www.apache.org/licenses/LICENSE-2.0
7 | *
8 | * Unless required by applicable law or agreed to in writing, software
9 | * distributed under the License is distributed on an "AS IS" BASIS,
10 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11 | * See the License for the specific language governing permissions and
12 | * limitations under the License.
13 | */
14 | package com.brsanthu.googleanalytics.request;
15 |
16 | import static com.brsanthu.googleanalytics.internal.Constants.HIT_EXCEPTION;
17 | import static com.brsanthu.googleanalytics.request.GoogleAnalyticsParameter.EXCEPTION_DESCRIPTION;
18 | import static com.brsanthu.googleanalytics.request.GoogleAnalyticsParameter.EXCEPTION_FATAL;
19 |
20 | /**
21 | * GA request to track exceptions.
22 | *
23 | *
45 | * Required for transaction hit type.
46 | * Required for item hit type.
47 | *
48 | *
49 | * A unique identifier for the transaction. This value should be the same for both the Transaction hit and Items
50 | * hits associated to the particular transaction.
51 | *
52 | *
53 | *
54 | *
55 | *
Parameter
56 | *
Value Type
57 | *
Default Value
58 | *
Max Length
59 | *
Supported Hit Types
60 | *
61 | *
62 | *
ti
63 | *
text
64 | *
None
65 | *
500 Bytes
66 | *
transaction, item
67 | *
68 | *
69 | *
70 | *
Example value: OD564
71 | * Example usage: ti=OD564
278 | * When present indicates the local currency for all transaction currency values. Value should be a valid ISO 4217
279 | * currency code.
280 | *
281 | *
282 | *
283 | *
284 | *
Parameter
285 | *
Value Type
286 | *
Default Value
287 | *
Max Length
288 | *
Supported Hit Types
289 | *
290 | *
291 | *
cu
292 | *
text
293 | *
None
294 | *
10 Bytes
295 | *
transaction, item
296 | *
297 | *
298 | *
299 | *
Example value: EUR
300 | * Example usage: cu=EUR
301 | */
302 | public ItemHit currencyCode(String value) {
303 | setString(CURRENCY_CODE, value);
304 | return this;
305 | }
306 |
307 | public String currencyCode() {
308 | return getString(CURRENCY_CODE);
309 | }
310 |
311 | }
312 |
--------------------------------------------------------------------------------
/src/main/java/com/brsanthu/googleanalytics/request/PageViewHit.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Licensed under the Apache License, Version 2.0 (the "License");
3 | * you may not use this file except in compliance with the License.
4 | * You may obtain a copy of the License at
5 | *
6 | * http://www.apache.org/licenses/LICENSE-2.0
7 | *
8 | * Unless required by applicable law or agreed to in writing, software
9 | * distributed under the License is distributed on an "AS IS" BASIS,
10 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11 | * See the License for the specific language governing permissions and
12 | * limitations under the License.
13 | */
14 | package com.brsanthu.googleanalytics.request;
15 |
16 | import static com.brsanthu.googleanalytics.internal.Constants.HIT_PAGEVIEW;
17 |
18 | /**
19 | * GA request to track a typical web page view
20 | *
21 | *
26 | *
27 | * @author Santhosh Kumar
28 | */
29 | public class PageViewHit extends GoogleAnalyticsRequest {
30 | public PageViewHit() {
31 | this(null, null, null);
32 | }
33 |
34 | public PageViewHit(String url, String title) {
35 | this(url, title, null);
36 | }
37 |
38 | public PageViewHit(String url, String title, String description) {
39 | super(HIT_PAGEVIEW);
40 | documentUrl(url);
41 | documentTitle(title);
42 | contentDescription(description);
43 | }
44 | }
45 |
--------------------------------------------------------------------------------
/src/main/java/com/brsanthu/googleanalytics/request/ScreenViewHit.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Licensed under the Apache License, Version 2.0 (the "License");
3 | * you may not use this file except in compliance with the License.
4 | * You may obtain a copy of the License at
5 | *
6 | * http://www.apache.org/licenses/LICENSE-2.0
7 | *
8 | * Unless required by applicable law or agreed to in writing, software
9 | * distributed under the License is distributed on an "AS IS" BASIS,
10 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11 | * See the License for the specific language governing permissions and
12 | * limitations under the License.
13 | */
14 | package com.brsanthu.googleanalytics.request;
15 |
16 | import static com.brsanthu.googleanalytics.internal.Constants.HIT_SCREENVIEW;
17 | import static com.brsanthu.googleanalytics.request.GoogleAnalyticsParameter.SCREEN_NAME;
18 |
19 | /**
20 | * GA request to track a typical web page view
21 | *
22 | *
89 | * Specifies the social interaction action. For example on Google Plus when a user clicks the +1 button, the social
90 | * action is 'plus'.
91 | *
92 | *
93 | *
94 | *
95 | *
Parameter
96 | *
Value Type
97 | *
Default Value
98 | *
Max Length
99 | *
Supported Hit Types
100 | *
101 | *
102 | *
sa
103 | *
text
104 | *
None
105 | *
50 Bytes
106 | *
social
107 | *
108 | *
109 | *
110 | *
Example value: like
111 | * Example usage: sa=like
128 | * Specifies the target of a social interaction. This value is typically a URL but can be any text.
129 | *
130 | *
131 | *
132 | *
133 | *
Parameter
134 | *
Value Type
135 | *
Default Value
136 | *
Max Length
137 | *
Supported Hit Types
138 | *
139 | *
140 | *
st
141 | *
text
142 | *
None
143 | *
2048 Bytes
144 | *
social
145 | *
146 | *
147 | *
148 | *
Example value: http://foo.com
149 | * Example usage: st=http%3A%2F%2Ffoo.com
150 | */
151 | public SocialHit socialActionTarget(String value) {
152 | setString(SOCIAL_ACTION_TARGET, value);
153 | return this;
154 | }
155 |
156 | public String socialActionTarget() {
157 | return getString(SOCIAL_ACTION_TARGET);
158 | }
159 | }
160 |
--------------------------------------------------------------------------------
/src/main/java/com/brsanthu/googleanalytics/request/TimingHit.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Licensed under the Apache License, Version 2.0 (the "License");
3 | * you may not use this file except in compliance with the License.
4 | * You may obtain a copy of the License at
5 | *
6 | * http://www.apache.org/licenses/LICENSE-2.0
7 | *
8 | * Unless required by applicable law or agreed to in writing, software
9 | * distributed under the License is distributed on an "AS IS" BASIS,
10 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11 | * See the License for the specific language governing permissions and
12 | * limitations under the License.
13 | */
14 | package com.brsanthu.googleanalytics.request;
15 |
16 | import static com.brsanthu.googleanalytics.internal.Constants.HIT_TIMING;
17 | import static com.brsanthu.googleanalytics.request.GoogleAnalyticsParameter.DNS_TIME;
18 | import static com.brsanthu.googleanalytics.request.GoogleAnalyticsParameter.PAGE_DOWNLOAD_TIME;
19 | import static com.brsanthu.googleanalytics.request.GoogleAnalyticsParameter.PAGE_LOAD_TIME;
20 | import static com.brsanthu.googleanalytics.request.GoogleAnalyticsParameter.REDIRECT_RESPONSE_TIME;
21 | import static com.brsanthu.googleanalytics.request.GoogleAnalyticsParameter.SERVER_RESPONSE_TIME;
22 | import static com.brsanthu.googleanalytics.request.GoogleAnalyticsParameter.TCP_CONNECT_TIME;
23 | import static com.brsanthu.googleanalytics.request.GoogleAnalyticsParameter.USER_TIMING_CATEGORY;
24 | import static com.brsanthu.googleanalytics.request.GoogleAnalyticsParameter.USER_TIMING_LABEL;
25 | import static com.brsanthu.googleanalytics.request.GoogleAnalyticsParameter.USER_TIMING_TIME;
26 | import static com.brsanthu.googleanalytics.request.GoogleAnalyticsParameter.USER_TIMING_VARIABLE_NAME;
27 |
28 | /**
29 | * GA request to track performance timings like page load time, server response time etc.
30 | *
31 | *
392 | * Specifies the time it took for the server to respond after the connect time. The value is in milliseconds.
393 | *
394 | *
395 | *
396 | *
397 | *
Parameter
398 | *
Value Type
399 | *
Default Value
400 | *
Max Length
401 | *
Supported Hit Types
402 | *
403 | *
404 | *
srt
405 | *
integer
406 | *
None
407 | *
None
408 | *
timing
409 | *
410 | *
411 | *
412 | *
Example value: 500
413 | * Example usage: srt=500
414 | */
415 | public TimingHit serverResponseTime(Integer value) {
416 | setInteger(SERVER_RESPONSE_TIME, value);
417 | return this;
418 | }
419 |
420 | public Integer serverResponseTime() {
421 | return getInteger(SERVER_RESPONSE_TIME);
422 | }
423 | }
424 |
--------------------------------------------------------------------------------
/src/main/java/com/brsanthu/googleanalytics/request/TransactionHit.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Licensed under the Apache License, Version 2.0 (the "License");
3 | * you may not use this file except in compliance with the License.
4 | * You may obtain a copy of the License at
5 | *
6 | * http://www.apache.org/licenses/LICENSE-2.0
7 | *
8 | * Unless required by applicable law or agreed to in writing, software
9 | * distributed under the License is distributed on an "AS IS" BASIS,
10 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11 | * See the License for the specific language governing permissions and
12 | * limitations under the License.
13 | */
14 | package com.brsanthu.googleanalytics.request;
15 |
16 | import static com.brsanthu.googleanalytics.internal.Constants.HIT_TXN;
17 | import static com.brsanthu.googleanalytics.request.GoogleAnalyticsParameter.CURRENCY_CODE;
18 | import static com.brsanthu.googleanalytics.request.GoogleAnalyticsParameter.TRANSACTION_AFFILIATION;
19 | import static com.brsanthu.googleanalytics.request.GoogleAnalyticsParameter.TRANSACTION_ID;
20 | import static com.brsanthu.googleanalytics.request.GoogleAnalyticsParameter.TRANSACTION_REVENUE;
21 | import static com.brsanthu.googleanalytics.request.GoogleAnalyticsParameter.TRANSACTION_SHIPPING;
22 | import static com.brsanthu.googleanalytics.request.GoogleAnalyticsParameter.TRANSACTION_TAX;
23 |
24 | /**
25 | * GA request to track ecommerce transaction.
26 | *
27 | *
70 | * Required for transaction hit type.
71 | * Required for item hit type.
72 | *
73 | *
74 | * A unique identifier for the transaction. This value should be the same for both the Transaction hit and Items
75 | * hits associated to the particular transaction.
76 | *
77 | *
78 | *
79 | *
80 | *
Parameter
81 | *
Value Type
82 | *
Default Value
83 | *
Max Length
84 | *
Supported Hit Types
85 | *
86 | *
87 | *
ti
88 | *
text
89 | *
None
90 | *
500 Bytes
91 | *
transaction, item
92 | *
93 | *
94 | *
95 | *
Example value: OD564
96 | * Example usage: ti=OD564