Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

exposed original response body string in GoogleAnalyticsResponse #49

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,12 @@
<version>${junit.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.googlecode.json-simple</groupId>
<artifactId>json-simple</artifactId>
<version>1.1.1</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 @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
public class HttpResponse {

private int statusCode;
private String originalResponse = null;

public int getStatusCode() {
return statusCode;
Expand All @@ -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;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
*/
package com.brsanthu.googleanalytics.request;

import com.brsanthu.googleanalytics.httpclient.HttpResponse;

import java.util.Map;

/**
Expand All @@ -23,6 +25,7 @@
public class GoogleAnalyticsResponse {
private int statusCode = 200;
private Map<String, String> requestParams = null;
private HttpResponse httpResponse = null;

public Map<String, String> getRequestParams() {
return requestParams;
Expand All @@ -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();
Expand Down
Original file line number Diff line number Diff line change
@@ -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"));
}

}