-
Notifications
You must be signed in to change notification settings - Fork 62
Expand file tree
/
Copy pathGoogleAnalytics.java
More file actions
462 lines (365 loc) · 16 KB
/
GoogleAnalytics.java
File metadata and controls
462 lines (365 loc) · 16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
/*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.brsanthu.googleanalytics;
import static com.brsanthu.googleanalytics.GaUtils.isEmpty;
import static com.brsanthu.googleanalytics.GaUtils.isNotEmpty;
import java.io.IOException;
import java.net.UnknownHostException;
import java.nio.charset.Charset;
import java.text.MessageFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.concurrent.Callable;
import java.util.concurrent.Future;
import java.util.concurrent.LinkedBlockingDeque;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;
import org.apache.http.HttpHost;
import org.apache.http.NameValuePair;
import org.apache.http.auth.AuthScope;
import org.apache.http.auth.UsernamePasswordCredentials;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.BasicCredentialsProvider;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* This is the main class of this library that accepts the requests from clients and
* sends the events to Google Analytics (GA).
*
* Clients needs to instantiate this object with {@link GoogleAnalyticsConfig} and {@link DefaultRequest}.
* Configuration contains sensible defaults so one could just initialize using one of the convenience constructors.
*
* This object is ThreadSafe and it is intended that clients create one instance of this for each GA Tracker Id
* and reuse each time an event needs to be posted.
*
* This object contains resources which needs to be shutdown/disposed. So {@link #close()} method is called
* to release all resources. Once close method is called, this instance cannot be reused so create new instance
* if required.
*/
public class GoogleAnalytics {
private static final Charset UTF8 = Charset.forName("UTF-8");
private static final Logger logger = LoggerFactory.getLogger(GoogleAnalytics.class);
private GoogleAnalyticsConfig config = null;
private DefaultRequest defaultRequest = null;
private CloseableHttpClient httpClient = null;
private ThreadPoolExecutor executor = null;
private GoogleAnalyticsStats stats = new GoogleAnalyticsStats();
public GoogleAnalytics(String trackingId) {
this(new GoogleAnalyticsConfig(), new DefaultRequest().trackingId(trackingId));
}
public GoogleAnalytics(GoogleAnalyticsConfig config, String trackingId) {
this(config, new DefaultRequest().trackingId(trackingId));
}
public GoogleAnalytics(String trackingId, String appName, String appVersion) {
this(new GoogleAnalyticsConfig(), trackingId, appName, appVersion);
}
public GoogleAnalytics(GoogleAnalyticsConfig config, String trackingId, String appName, String appVersion) {
this(config, new DefaultRequest().trackingId(trackingId).applicationName(appName).applicationVersion(appVersion));
}
public GoogleAnalytics(GoogleAnalyticsConfig config, DefaultRequest defaultRequest) {
if (config.isDiscoverRequestParameters() && config.getRequestParameterDiscoverer() != null) {
config.getRequestParameterDiscoverer().discoverParameters(config, defaultRequest);
}
logger.info("Initializing Google Analytics with config=" + config + " and defaultRequest=" + defaultRequest);
this.config = config;
this.defaultRequest = defaultRequest;
this.httpClient = createHttpClient(config);
}
public GoogleAnalyticsConfig getConfig() {
return config;
}
public HttpClient getHttpClient() {
return httpClient;
}
public DefaultRequest getDefaultRequest() {
return defaultRequest;
}
public void setDefaultRequest(DefaultRequest request) {
this.defaultRequest = request;
}
public void setHttpClient(CloseableHttpClient httpClient) {
this.httpClient = httpClient;
}
@SuppressWarnings({ "rawtypes", "unchecked" })
public GoogleAnalyticsResponse post(GoogleAnalyticsRequest request) {
GoogleAnalyticsResponse response = new GoogleAnalyticsResponse();
if (!config.isEnabled()) {
return response;
}
CloseableHttpResponse httpResponse = null;
try {
List<NameValuePair> postParms = new ArrayList<NameValuePair>();
logger.debug("Processing " + request);
//Process the parameters
processParameters(request, postParms);
//Process custom dimensions
processCustomParameters(request, postParms, defaultRequest.customDimensions, request.customDimensions);
//Process custom metrics
processCustomParameters(request, postParms, defaultRequest.customMetrics, request.customMetrics);
// Process productParameters
processCustomSubParameters(request, postParms);
logger.debug("Processed all parameters and sending the request " + postParms);
HttpPost httpPost = new HttpPost(config.getUrl());
httpPost.setEntity(new UrlEncodedFormEntity(postParms, UTF8));
httpResponse = (CloseableHttpResponse) httpClient.execute(httpPost);
response.setStatusCode(httpResponse.getStatusLine().getStatusCode());
response.setPostedParms(postParms);
EntityUtils.consumeQuietly(httpResponse.getEntity());
if (config.isGatherStats()) {
gatherStats(request);
}
} catch (Exception e) {
if (e instanceof UnknownHostException) {
logger.warn("Coudln't connect to Google Analytics. Internet may not be available. " + e.toString());
} else {
logger.warn("Exception while sending the Google Analytics tracker request " + request, e);
}
} finally {
try {
httpResponse.close();
} catch (Exception e2) {
//ignore
}
}
return response;
}
@SuppressWarnings({ "rawtypes", "unchecked" })
private void processParameters(GoogleAnalyticsRequest request, List<NameValuePair> postParms) {
Map<GoogleAnalyticsParameter, String> requestParms = request.getParameters();
Map<GoogleAnalyticsParameter, String> defaultParms = defaultRequest.getParameters();
for (GoogleAnalyticsParameter parm : defaultParms.keySet()) {
String value = requestParms.get(parm);
String defaultValue = defaultParms.get(parm);
if (isEmpty(value) && !isEmpty(defaultValue)) {
requestParms.put(parm, defaultValue);
}
}
for (GoogleAnalyticsParameter key : requestParms.keySet()) {
postParms.add(new BasicNameValuePair(key.getParameterName(), requestParms.get(key)));
}
}
/**
* Processes the custom dimensions/metrics and adds the values to list of parameters, which would be posted to GA.
*
* @param request
* @param postParms
* @param defaultCustomElements
* @param requestCustomElements
*/
private void processCustomParameters(@SuppressWarnings("rawtypes") GoogleAnalyticsRequest request, List<NameValuePair> postParms,
Map<String, String> defaultCustomElements, Map<String, String> requestCustomElements) {
Map<String, String> customParms = new HashMap<String, String>();
customParms.putAll(defaultCustomElements);
customParms.putAll(requestCustomElements);
for (Entry<String, String> e : customParms.entrySet()) {
postParms.add(new BasicNameValuePair(e.getKey(), e.getValue()));
}
}
private void processCustomSubParameters(@SuppressWarnings("rawtypes") GoogleAnalyticsRequest request, List<NameValuePair> postParms) {
int productIndex = 0;
@SuppressWarnings("unchecked")
Map<Integer, SubParameters> productParametersMap = request.getProductParameters();
for (Entry<Integer, SubParameters> prodocutParametersEntry : productParametersMap.entrySet()) {
productIndex = prodocutParametersEntry.getKey();
for (Entry<GoogleAnalyticsParameter, String> productParametersValueEntry
: prodocutParametersEntry.getValue().getParameters().entrySet()) {
String key = productParametersValueEntry.getKey().getParameterName()
.replace("#", Integer.toString(productIndex));
postParms.add(new BasicNameValuePair(key, productParametersValueEntry.getValue()));
}
customDimensionAndMetrics(prodocutParametersEntry.getValue(), postParms, 0, productIndex);
}
productIndex = 0;
@SuppressWarnings("unchecked")
Map<Integer, SubParameters> productImpressionParametersMap = request.getProductImpressionParameters();
for (Entry<Integer, SubParameters> prodocutImpressionParametersEntry : productImpressionParametersMap.entrySet()) {
int listIndex = prodocutImpressionParametersEntry.getKey();
Map<Integer, SubParameters> productImpressionProductParametersMap = prodocutImpressionParametersEntry.getValue().subParameters();
for (Entry<Integer, SubParameters> prodocutParametersEntry : productImpressionProductParametersMap.entrySet()) {
productIndex = prodocutParametersEntry.getKey();
for (Entry<GoogleAnalyticsParameter, String> productParametersValueEntry
: prodocutParametersEntry.getValue().getParameters().entrySet()) {
String key = productParametersValueEntry.getKey().getParameterName()
.replace("#", Integer.toString(productIndex))
.replace("$", Integer.toString(listIndex));
postParms.add(new BasicNameValuePair(key, productParametersValueEntry.getValue()));
}
customDimensionAndMetrics(prodocutParametersEntry.getValue(), postParms, listIndex, productIndex);
}
}
int promoIndex = 0;
@SuppressWarnings("unchecked")
Map<Integer, SubParameters> promotionParametersMap = request.getPromotionParameters();
for (Entry<Integer, SubParameters> promotionParametersEntry : promotionParametersMap.entrySet()) {
promoIndex = promotionParametersEntry.getKey();
for (Entry<GoogleAnalyticsParameter, String> productParametersValueEntry
: promotionParametersEntry.getValue().getParameters().entrySet()) {
String key = productParametersValueEntry.getKey().getParameterName()
.replace("&", Integer.toString(promoIndex));
postParms.add(new BasicNameValuePair(key, productParametersValueEntry.getValue()));
}
}
}
private void customDimensionAndMetrics(SubParameters parameterSet, List<NameValuePair> postParms,
int listIndex, int productIndex) {
// custom dimensions
for (Entry<Integer, String> customDimensionEntry : parameterSet.customDimensions().entrySet()) {
int customIndex = customDimensionEntry.getKey();
String key = GoogleAnalyticsParameter.PRODUCT_IMPRESSION_CUSTOM_DIMENSION.getParameterName()
.replace("#", Integer.toString(productIndex))
.replace("$", Integer.toString(listIndex))
.replace("§", Integer.toString(customIndex))
+ customDimensionEntry.getKey();
postParms.add(new BasicNameValuePair(key, customDimensionEntry.getValue()));
}
// custom metrics
for (Entry<Integer, Integer> customMetricsEntry : parameterSet.customMetrics().entrySet()) {
int customIndex = customMetricsEntry.getKey();
String key = GoogleAnalyticsParameter.PRODUCT_IMPRESSION_CUSTOM_METRIC.getParameterName()
.replace("#", Integer.toString(productIndex))
.replace("$", Integer.toString(listIndex))
.replace("§", Integer.toString(customIndex))
+ customMetricsEntry.getKey();
postParms.add(new BasicNameValuePair(key, Integer.toString(customMetricsEntry.getValue())));
}
}
private void gatherStats(@SuppressWarnings("rawtypes") GoogleAnalyticsRequest request) {
String hitType = request.hitType();
if ("pageview".equalsIgnoreCase(hitType)) {
stats.pageViewHit();
} else if ("appview".equalsIgnoreCase(hitType)) {
stats.appViewHit();
} else if ("event".equalsIgnoreCase(hitType)) {
stats.eventHit();
} else if ("item".equalsIgnoreCase(hitType)) {
stats.itemHit();
} else if ("transaction".equalsIgnoreCase(hitType)) {
stats.transactionHit();
} else if ("social".equalsIgnoreCase(hitType)) {
stats.socialHit();
} else if ("timing".equalsIgnoreCase(hitType)) {
stats.timingHit();
}
}
public Future<GoogleAnalyticsResponse> postAsync(final RequestProvider requestProvider) {
if (!config.isEnabled()) {
return null;
}
Future<GoogleAnalyticsResponse> future = getExecutor().submit(new Callable<GoogleAnalyticsResponse>() {
public GoogleAnalyticsResponse call() throws Exception {
try {
@SuppressWarnings("rawtypes")
GoogleAnalyticsRequest request = requestProvider.getRequest();
if (request != null) {
return post(request);
}
} catch (Exception e) {
logger.warn("Request Provider (" + requestProvider +") thrown exception " + e.toString() + " and hence nothing is posted to GA.");
}
return null;
}
});
return future;
}
@SuppressWarnings("rawtypes")
public Future<GoogleAnalyticsResponse> postAsync(final GoogleAnalyticsRequest request) {
if (!config.isEnabled()) {
return null;
}
Future<GoogleAnalyticsResponse> future = getExecutor().submit(new Callable<GoogleAnalyticsResponse>() {
public GoogleAnalyticsResponse call() throws Exception {
return post(request);
}
});
return future;
}
public void close() {
try {
executor.shutdown();
} catch (Exception e) {
//ignore
}
try {
httpClient.close();
} catch (IOException e) {
//ignore
}
}
protected CloseableHttpClient createHttpClient(GoogleAnalyticsConfig config) {
PoolingHttpClientConnectionManager connManager = new PoolingHttpClientConnectionManager();
connManager.setDefaultMaxPerRoute(getDefaultMaxPerRoute(config));
HttpClientBuilder builder = HttpClients.custom().setConnectionManager(connManager);
if (isNotEmpty(config.getUserAgent())) {
builder.setUserAgent(config.getUserAgent());
}
if (isNotEmpty(config.getProxyHost())) {
builder.setProxy(new HttpHost(config.getProxyHost(), config.getProxyPort()));
if (isNotEmpty(config.getProxyUserName())) {
BasicCredentialsProvider credentialsProvider = new BasicCredentialsProvider();
credentialsProvider.setCredentials(new AuthScope(config.getProxyHost(), config.getProxyPort()),
new UsernamePasswordCredentials(config.getProxyUserName(), config.getProxyPassword()));
builder.setDefaultCredentialsProvider(credentialsProvider);
}
}
return builder.build();
}
protected int getDefaultMaxPerRoute(GoogleAnalyticsConfig config) {
return Math.max(config.getMaxThreads(), 1);
}
protected ThreadPoolExecutor getExecutor() {
if (executor == null) {
executor = createExecutor(config);
}
return executor;
}
protected synchronized ThreadPoolExecutor createExecutor(GoogleAnalyticsConfig config) {
return new ThreadPoolExecutor(0, config.getMaxThreads(), 5, TimeUnit.MINUTES, new LinkedBlockingDeque<Runnable>(), createThreadFactory());
}
protected ThreadFactory createThreadFactory() {
return new GoogleAnalyticsThreadFactory(config.getThreadNameFormat());
}
public GoogleAnalyticsStats getStats() {
return stats;
}
public void resetStats() {
stats = new GoogleAnalyticsStats();
}
}
class GoogleAnalyticsThreadFactory implements ThreadFactory {
private final AtomicInteger threadNumber = new AtomicInteger(1);
private String threadNameFormat = null;
public GoogleAnalyticsThreadFactory(String threadNameFormat) {
this.threadNameFormat = threadNameFormat;
}
public Thread newThread(Runnable r) {
Thread thread = new Thread(Thread.currentThread().getThreadGroup(), r, MessageFormat.format(threadNameFormat, threadNumber.getAndIncrement()), 0);
thread.setDaemon(true);
thread.setPriority(Thread.MIN_PRIORITY);
return thread;
}
}