Skip to content

Commit

Permalink
Add support for validating hits #48
Browse files Browse the repository at this point in the history
  • Loading branch information
brsanthu committed May 20, 2019
1 parent e73a989 commit 1b9273f
Show file tree
Hide file tree
Showing 11 changed files with 302 additions and 1 deletion.
8 changes: 7 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,13 @@
</exclusions>
</dependency>

<dependency>
<groupId>com.googlecode.json-simple</groupId>
<artifactId>json-simple</artifactId>
<version>1.1.1</version>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
Expand Down Expand Up @@ -190,7 +197,6 @@
<version>${mockito.version}</version>
<scope>test</scope>
</dependency>

</dependencies>
<name>Google Analytics Java API</name>
<url>https://github.com/brsanthu/google-analytics-java</url>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ public class GoogleAnalyticsConfig {
private int batchSize = 20;
private String httpUrl = "http://www.google-analytics.com/collect";
private String httpsUrl = "https://www.google-analytics.com/collect";
private String httpDebugUrl = "http://www.google-analytics.com/debug/collect";
private String httpsDebugUrl = "https://www.google-analytics.com/debug/collect";
private String batchUrl = "https://www.google-analytics.com/batch";
private String userAgent = null;
private String proxyHost = null;
Expand All @@ -55,6 +57,7 @@ public class GoogleAnalyticsConfig {
private GoogleAnalyticsExceptionHandler exceptionHandler;
private boolean autoQueueTimeEnabled = true;
private boolean anonymizeUserIp = false;
private boolean hitDebug = false;

public RequestParameterDiscoverer getRequestParameterDiscoverer() {
return requestParameterDiscoverer;
Expand Down Expand Up @@ -277,6 +280,9 @@ public GoogleAnalyticsConfig setUseHttps(boolean useHttps) {
return this;
}

/**
* Future use and not used at the moment.
*/
public boolean isValidate() {
return validate;
}
Expand Down Expand Up @@ -335,7 +341,15 @@ public GoogleAnalyticsConfig setHttpsUrl(String httpsUrl) {
return this;
}

/**
* Returns the effective url to which we need to post the GA request to based on if hit debug is enabled and if
* https is enabled.
*/
public String getUrl() {
if (isHitDebug()) {
return useHttps ? httpsDebugUrl : httpDebugUrl;
}

return useHttps ? httpsUrl : httpUrl;
}

Expand Down Expand Up @@ -456,4 +470,46 @@ public GoogleAnalyticsConfig setAnonymizeUserIp(boolean anonymizeUserIp) {
return this;
}

public String getHttpDebugUrl() {
return httpDebugUrl;
}

public GoogleAnalyticsConfig setHttpDebugUrl(String httpDebugUrl) {
this.httpDebugUrl = httpDebugUrl;
return this;
}

public String getHttpsDebugUrl() {
return httpsDebugUrl;
}

public GoogleAnalyticsConfig setHttpsDebugUrl(String httpsDebugUrl) {
this.httpsDebugUrl = httpsDebugUrl;
return this;
}

public boolean isHitDebug() {
return hitDebug;
}

/**
* If set to true, then library will use debug urls instead of normal GA urls (<code>debugHttpUrl</code> and
* <code>debugHttpsUrl</code>) which should return the hit validation response as part of http response. This would
* be captured as part of {@link GoogleAnalyticsResponse#getHttpResponse().getBody()}.
* <p>
* Library provides convenient validation response models at <code>com.brsanthu.googleanalytics.debug</code> package
* to deserialize http response (which will be in json format) into.
* <p>
* Note that to hit debug, disable batching as hit debug is not supported in batch mode.
* <p>
* Deserialization itself is not part of the library to avoid the dependency.
* <p>
* See <a href="https://developers.google.com/analytics/devguides/collection/protocol/v1/validating-hits">this</a>
* for more info.
*/
public GoogleAnalyticsConfig setHitDebug(boolean validateHits) {
hitDebug = validateHits;
return this;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package com.brsanthu.googleanalytics.hitdebug;

import java.util.ArrayList;
import java.util.List;

public class HitParsingResult {
private boolean valid;
private String hit;
private List<ParserMessage> parserMessage = new ArrayList<>();

public boolean isValid() {
return valid;
}

public HitParsingResult setValid(boolean valid) {
this.valid = valid;
return this;
}

public String getHit() {
return hit;
}

public HitParsingResult setHit(String hit) {
this.hit = hit;
return this;
}

public List<ParserMessage> getParserMessage() {
return parserMessage;
}

public HitParsingResult setParserMessage(List<ParserMessage> parserMessage) {
this.parserMessage = parserMessage;
return this;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package com.brsanthu.googleanalytics.hitdebug;

import java.util.ArrayList;
import java.util.List;

public class HitValidationResponse {
private List<HitParsingResult> hitParsingResult = new ArrayList<>();
private List<ParserMessage> parserMessage = new ArrayList<>();

public HitValidationResponse() {
// default
}

public List<HitParsingResult> getHitParsingResult() {
return hitParsingResult;
}

public HitValidationResponse setHitParsingResult(List<HitParsingResult> hitParsingResult) {
this.hitParsingResult = hitParsingResult;
return this;
}

public List<ParserMessage> getParserMessage() {
return parserMessage;
}

public HitValidationResponse setParserMessage(List<ParserMessage> parserMessage) {
this.parserMessage = parserMessage;
return this;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package com.brsanthu.googleanalytics.hitdebug;

public class ParserMessage {
private String messageType;
private String description;
private String parameter;

public String getMessageType() {
return messageType;
}

public ParserMessage setMessageType(String messageType) {
this.messageType = messageType;
return this;
}

public String getDescription() {
return description;
}

public ParserMessage setDescription(String description) {
this.description = description;
return this;
}

public String getParameter() {
return parameter;
}

public ParserMessage setParameter(String parameter) {
this.parameter = parameter;
return this;
}

public boolean isInfo() {
return "info".equalsIgnoreCase(messageType);
}

public boolean isWarn() {
return "warn".equalsIgnoreCase(messageType);
}

public boolean isError() {
return "error".equalsIgnoreCase(messageType);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ public HttpResponse post(HttpRequest req) {

httpResp = execute(req.getUrl(), new UrlEncodedFormEntity(createNameValuePairs(req), StandardCharsets.UTF_8));
resp.setStatusCode(httpResp.getStatusLine().getStatusCode());
resp.setBody(EntityUtils.toString(httpResp.getEntity(), "UTF-8"));

} catch (Exception e) {
if (e instanceof UnknownHostException) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
package com.brsanthu.googleanalytics.httpclient;

import java.util.HashMap;
import java.util.Map;

public class HttpResponse {

private int statusCode;
private String body;
private Map<String, String> headers = new HashMap<>();

public int getStatusCode() {
return statusCode;
Expand All @@ -13,4 +18,22 @@ public HttpResponse setStatusCode(int statusCode) {
return this;
}

public String getBody() {
return body;
}

public HttpResponse setBody(String body) {
this.body = body;
return this;
}

public Map<String, String> getHeaders() {
return headers;
}

public HttpResponse setHeaders(Map<String, String> headers) {
this.headers = headers;
return this;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -219,8 +219,11 @@ protected GoogleAnalyticsResponse postSingle(GoogleAnalyticsRequest<?> gaReq) {
HttpResponse httpResp = httpClient.post(httpReq);

GoogleAnalyticsResponse response = new GoogleAnalyticsResponse();
response.setGoogleAnalyticsRequest(gaReq);
response.setStatusCode(httpResp.getStatusCode());
response.setRequestParams(httpReq.getBodyParams());
response.setHttpRequest(httpReq);
response.setHttpResponse(httpResp);

if (config.isGatherStats()) {
gatherStats(gaReq);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@

import java.util.Map;

import com.brsanthu.googleanalytics.httpclient.HttpRequest;
import com.brsanthu.googleanalytics.httpclient.HttpResponse;

/**
* Response for GA tracking request.
*
Expand All @@ -21,6 +24,8 @@ public class GoogleAnalyticsResponse {
private int statusCode = 200;
private GoogleAnalyticsRequest<?> googleAnalyticsRequest;
private Map<String, String> requestParams = null;
private HttpRequest httpRequest;
private HttpResponse httpResponse;

public Map<String, String> getRequestParams() {
return requestParams;
Expand Down Expand Up @@ -55,4 +60,22 @@ public GoogleAnalyticsResponse setGoogleAnalyticsRequest(GoogleAnalyticsRequest<
this.googleAnalyticsRequest = googleAnalyticsRequest;
return this;
}

public HttpResponse getHttpResponse() {
return httpResponse;
}

public GoogleAnalyticsResponse setHttpResponse(HttpResponse httpResponse) {
this.httpResponse = httpResponse;
return this;
}

public HttpRequest getHttpRequest() {
return httpRequest;
}

public GoogleAnalyticsResponse setHttpRequest(HttpRequest httpRequest) {
this.httpRequest = httpRequest;
return this;
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.brsanthu.googleanalytics;

import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.Assert.assertEquals;

import org.junit.jupiter.api.Test;
Expand All @@ -14,8 +15,28 @@ public void testDefaultConfig() throws Exception {
assertEquals(5, config.getMaxThreads());
assertEquals("http://www.google-analytics.com/collect", config.getHttpUrl());
assertEquals("https://www.google-analytics.com/collect", config.getHttpsUrl());
assertEquals("http://www.google-analytics.com/debug/collect", config.getHttpDebugUrl());
assertEquals("https://www.google-analytics.com/debug/collect", config.getHttpsDebugUrl());
assertEquals(80, config.getProxyPort());
assertEquals(true, config.isDiscoverRequestParameters());
assertEquals(false, config.isGatherStats());
}

@Test
void testGetUrl() throws Exception {

GoogleAnalyticsConfig config = new GoogleAnalyticsConfig();

assertThat(config.getUrl()).isEqualTo("https://www.google-analytics.com/collect");
config.setHitDebug(true);
assertThat(config.getUrl()).isEqualTo("https://www.google-analytics.com/debug/collect");

config.setHitDebug(false);
config.setUseHttps(false);

assertThat(config.getUrl()).isEqualTo("http://www.google-analytics.com/collect");
config.setHitDebug(true);
assertThat(config.getUrl()).isEqualTo("http://www.google-analytics.com/debug/collect");
}

}
Loading

0 comments on commit 1b9273f

Please sign in to comment.