Skip to content

Commit

Permalink
Add batching support for GA
Browse files Browse the repository at this point in the history
  • Loading branch information
brsanthu committed Aug 15, 2017
1 parent afd90db commit 8ad1c1d
Show file tree
Hide file tree
Showing 9 changed files with 239 additions and 83 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,8 @@ public class GoogleAnalyticsConfig {
private int maxHttpConnectionsPerRoute = 10;
private boolean useHttps = true;
private boolean validate = true;
private boolean batchingEnabled = true;
private int batchSize = 1000;
private boolean batchingEnabled = false;
private int batchSize = 500;
private String httpUrl = "http://www.google-analytics.com/collect";
private String httpsUrl = "https://www.google-analytics.com/collect";
private String batchUrl = "https://www.google-analytics.com/batch";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,14 @@
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

import org.apache.http.HttpEntity;
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.ClientProtocolException;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
Expand All @@ -36,41 +39,6 @@ public ApacheHttpClientImpl(GoogleAnalyticsConfig config) {
apacheHttpClient = createHttpClient(config);
}

@Override
public HttpResponse post(HttpRequest req) {

HttpResponse resp = new HttpResponse();

CloseableHttpResponse httpResp = null;
try {
HttpPost httpPost = new HttpPost(req.getUrl());
List<NameValuePair> parmas = new ArrayList<>();
req.getBodyParams().forEach((key, value) -> parmas.add(new BasicNameValuePair(key, value)));

httpPost.setEntity(new UrlEncodedFormEntity(parmas, StandardCharsets.UTF_8));

httpResp = apacheHttpClient.execute(httpPost);
resp.setStatusCode(httpResp.getStatusLine().getStatusCode());

} catch (Exception e) {
if (e instanceof UnknownHostException) {
logger.warn("Couldn't connect to Google Analytics. Internet may not be available. " + e.toString());
} else {
logger.warn("Exception while sending the Google Analytics tracker request " + req, e);
}

} finally {
EntityUtils.consumeQuietly(httpResp.getEntity());
try {
httpResp.close();
} catch (Exception e2) {
// ignore
}
}

return resp;
}

@Override
public void close() {
try {
Expand Down Expand Up @@ -109,8 +77,76 @@ public boolean isBatchSupported() {
return true;
}

protected CloseableHttpResponse execute(String url, HttpEntity entity) throws ClientProtocolException, IOException {

HttpPost httpPost = new HttpPost(url);

httpPost.setEntity(entity);

return apacheHttpClient.execute(httpPost);
}

protected List<NameValuePair> createNameValuePairs(HttpRequest req) {
List<NameValuePair> parmas = new ArrayList<>();
req.getBodyParams().forEach((key, value) -> parmas.add(new BasicNameValuePair(key, value)));
return parmas;
}

@Override
public HttpResponse post(HttpRequest req) {
HttpResponse resp = new HttpResponse();
CloseableHttpResponse httpResp = null;

try {

httpResp = execute(req.getUrl(), new UrlEncodedFormEntity(createNameValuePairs(req), StandardCharsets.UTF_8));
resp.setStatusCode(httpResp.getStatusLine().getStatusCode());

} catch (Exception e) {
if (e instanceof UnknownHostException) {
logger.warn("Couldn't connect to Google Analytics. Internet may not be available. " + e.toString());
} else {
logger.warn("Exception while sending the Google Analytics tracker request " + req, e);
}

} finally {
EntityUtils.consumeQuietly(httpResp.getEntity());
try {
httpResp.close();
} catch (Exception e2) {
// ignore
}
}

return resp;
}

@Override
public HttpBatchResponse postBatch(HttpBatchRequest req) {
return null;
HttpBatchResponse resp = new HttpBatchResponse();
CloseableHttpResponse httpResp = null;

try {
List<List<NameValuePair>> listOfReqPairs = req.getRequests().stream().map(this::createNameValuePairs).collect(Collectors.toList());
httpResp = execute(req.getUrl(), new BatchUrlEncodedFormEntity(listOfReqPairs));
resp.setStatusCode(httpResp.getStatusLine().getStatusCode());

} catch (Exception e) {
if (e instanceof UnknownHostException) {
logger.warn("Couldn't connect to Google Analytics. Internet may not be available. " + e.toString());
} else {
logger.warn("Exception while sending the Google Analytics tracker request " + req, e);
}

} finally {
EntityUtils.consumeQuietly(httpResp.getEntity());
try {
httpResp.close();
} catch (Exception e2) {
// ignore
}
}

return resp;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package com.brsanthu.googleanalytics.httpclient;

import java.nio.charset.StandardCharsets;
import java.util.List;

import org.apache.http.NameValuePair;
import org.apache.http.client.utils.URLEncodedUtils;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.StringEntity;

public class BatchUrlEncodedFormEntity extends StringEntity {

public BatchUrlEncodedFormEntity(List<List<NameValuePair>> parameters) {
super(constructCombinedEntityString(parameters), ContentType.create(URLEncodedUtils.CONTENT_TYPE));
}

private static String constructCombinedEntityString(final List<List<NameValuePair>> parameters) {
StringBuilder builder = new StringBuilder();

for (List<? extends NameValuePair> param : parameters) {
builder.append(URLEncodedUtils.format(param, StandardCharsets.UTF_8));
builder.append("\r\n");
}

return builder.toString();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import java.util.List;

public class HttpBatchRequest {
private String url;
private List<HttpRequest> requests = new ArrayList<>();

public HttpBatchRequest addRequest(HttpRequest request) {
Expand All @@ -20,4 +21,13 @@ public HttpBatchRequest setRequests(List<HttpRequest> requests) {
return this;
}

public String getUrl() {
return url;
}

public HttpBatchRequest setUrl(String url) {
this.url = url;
return this;
}

}
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
package com.brsanthu.googleanalytics.httpclient;

public class HttpBatchResponse {
private int statusCode;

public int getStatusCode() {
return statusCode;
}

public HttpBatchResponse setStatusCode(int statusCode) {
this.statusCode = statusCode;
return this;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,6 @@ public interface Constants {
String TYPE_TEXT = "text";
String TYPE_BOOLEAN = "boolean";
String TYPE_CURRENCY = "currency";

String TEST_TRACKING_ID = "UA-612100-12";
}
Loading

0 comments on commit 8ad1c1d

Please sign in to comment.