diff --git a/pom.xml b/pom.xml index b26b95d..78ed294 100644 --- a/pom.xml +++ b/pom.xml @@ -162,6 +162,12 @@ ${junit.version} test + + com.googlecode.json-simple + json-simple + 1.1.1 + test + Google Analytics Java API https://github.com/brsanthu/google-analytics-java diff --git a/src/main/java/com/brsanthu/googleanalytics/httpclient/ApacheHttpClientImpl.java b/src/main/java/com/brsanthu/googleanalytics/httpclient/ApacheHttpClientImpl.java index 75ed8cf..3721669 100644 --- a/src/main/java/com/brsanthu/googleanalytics/httpclient/ApacheHttpClientImpl.java +++ b/src/main/java/com/brsanthu/googleanalytics/httpclient/ApacheHttpClientImpl.java @@ -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.setOriginalResponse(EntityUtils.toString(httpResp.getEntity(), "UTF-8")); } catch (Exception e) { if (e instanceof UnknownHostException) { diff --git a/src/main/java/com/brsanthu/googleanalytics/httpclient/HttpResponse.java b/src/main/java/com/brsanthu/googleanalytics/httpclient/HttpResponse.java index ca337c3..6bf62ea 100644 --- a/src/main/java/com/brsanthu/googleanalytics/httpclient/HttpResponse.java +++ b/src/main/java/com/brsanthu/googleanalytics/httpclient/HttpResponse.java @@ -3,6 +3,7 @@ public class HttpResponse { private int statusCode; + private String originalResponse = null; public int getStatusCode() { return statusCode; @@ -13,4 +14,13 @@ public HttpResponse setStatusCode(int statusCode) { return this; } + public String getOriginalResponse() { + return originalResponse; + } + + public HttpResponse setOriginalResponse(String originalResponse) { + this.originalResponse = originalResponse; + return this; + } + } diff --git a/src/main/java/com/brsanthu/googleanalytics/internal/GoogleAnalyticsImpl.java b/src/main/java/com/brsanthu/googleanalytics/internal/GoogleAnalyticsImpl.java index bd3b0f8..3087b87 100644 --- a/src/main/java/com/brsanthu/googleanalytics/internal/GoogleAnalyticsImpl.java +++ b/src/main/java/com/brsanthu/googleanalytics/internal/GoogleAnalyticsImpl.java @@ -167,6 +167,7 @@ protected GoogleAnalyticsResponse postSingle(GoogleAnalyticsRequest gaReq) { GoogleAnalyticsResponse response = new GoogleAnalyticsResponse(); response.setStatusCode(httpResp.getStatusCode()); response.setRequestParams(httpReq.getBodyParams()); + response.setHttpResponse(httpResp); if (config.isGatherStats()) { gatherStats(gaReq); diff --git a/src/main/java/com/brsanthu/googleanalytics/request/GoogleAnalyticsResponse.java b/src/main/java/com/brsanthu/googleanalytics/request/GoogleAnalyticsResponse.java index eba245f..3fec597 100644 --- a/src/main/java/com/brsanthu/googleanalytics/request/GoogleAnalyticsResponse.java +++ b/src/main/java/com/brsanthu/googleanalytics/request/GoogleAnalyticsResponse.java @@ -13,6 +13,8 @@ */ package com.brsanthu.googleanalytics.request; +import com.brsanthu.googleanalytics.httpclient.HttpResponse; + import java.util.Map; /** @@ -23,6 +25,7 @@ public class GoogleAnalyticsResponse { private int statusCode = 200; private Map requestParams = null; + private HttpResponse httpResponse = null; public Map getRequestParams() { return requestParams; @@ -40,6 +43,14 @@ public int getStatusCode() { return statusCode; } + public HttpResponse getHttpResponse() { + return httpResponse; + } + + public void setHttpResponse(HttpResponse httpResponse) { + this.httpResponse = httpResponse; + } + @Override public String toString() { StringBuilder builder = new StringBuilder(); diff --git a/src/test/java/com/brsanthu/googleanalytics/GoogleAnalyticsDebugTest.java b/src/test/java/com/brsanthu/googleanalytics/GoogleAnalyticsDebugTest.java new file mode 100644 index 0000000..cace52c --- /dev/null +++ b/src/test/java/com/brsanthu/googleanalytics/GoogleAnalyticsDebugTest.java @@ -0,0 +1,37 @@ +package com.brsanthu.googleanalytics; + +import com.brsanthu.googleanalytics.request.GoogleAnalyticsResponse; +import org.json.simple.JSONArray; +import org.json.simple.JSONObject; +import org.json.simple.parser.JSONParser; +import org.junit.BeforeClass; +import org.junit.Test; + +import java.util.Map; + +import static com.brsanthu.googleanalytics.internal.Constants.TEST_TRACKING_ID; +import static org.junit.Assert.assertEquals; + +public class GoogleAnalyticsDebugTest { + + private static GoogleAnalytics ga = null; + + @BeforeClass + public static void setup() { + ga = GoogleAnalytics.builder() + .withTrackingId(TEST_TRACKING_ID).withAppName("Junit Test").withAppVersion("1.0.0") + .withConfig(new GoogleAnalyticsConfig().setHttpsUrl("https://www.google-analytics.com/debug/collect")) + .build(); + } + + @Test + public void testPageViewDebug() throws Exception { + GoogleAnalyticsResponse response = ga.pageView("http://www.google.com", "Search").send(); + String originalResponse = response.getHttpResponse().getOriginalResponse(); + JSONParser parser = new JSONParser(); + JSONObject json = (JSONObject) parser.parse(originalResponse); + assertEquals(200, response.getStatusCode()); + assertEquals(true, ((Map) ((JSONArray) json.get("hitParsingResult")).get(0)).get("valid")); + } + +}