*
* Optional.
- *
+ *
*
*
* This parameter specifies that this visitor has been exposed to a particular variation of an experiment. It should
@@ -1880,4 +1897,58 @@ public GoogleAnalyticsRequest setExecutor(GoogleAnalyticsExecutor delegateExe
this.delegateExecutor = delegateExecutor;
return this;
}
+
+ /**
+ * Indicates the datetime at which this event occurred. This is used to report the qt
parameter, if one
+ * is not set. The occurredAt
defaults to datetime when this request was instantiated.
+ */
+ public T occurredAt(ZonedDateTime value) {
+ this.occurredAt = value;
+ return (T) this;
+ }
+
+ public ZonedDateTime occurredAt() {
+ return occurredAt;
+ }
+
+ /**
+ * Deep clones this hit and returns new request of same type. Any changes made to this request after this call, will
+ * not be reflected in the returned instance.
+ */
+ @Override
+ public T clone() {
+ try {
+ GoogleAnalyticsRequest clonedReq = this.getClass().newInstance();
+ clonedReq.occurredAt = ZonedDateTime.now();
+ clonedReq.parms = cloneMap(parms);
+ clonedReq.customDimensions = cloneMap(customDimensions);
+ clonedReq.customMetrics = cloneMap(customMetrics);
+ clonedReq.delegateExecutor = delegateExecutor;
+
+ return (T) clonedReq;
+ } catch (Exception e) {
+ throw new RuntimeException("Exception while deep cloning " + this, e);
+ }
+ }
+
+ private Map cloneMap(Map input) {
+ Map output = new HashMap<>();
+ output.putAll(input);
+ return output;
+ }
+
+ /**
+ * Creates a anyhit wrapper for current request with state shared between this instance and returned {@link AnyHit}.
+ * If you want a copy, then call {@link #deepClone()} first and then this method.
+ */
+ public AnyHit asAnyHit() {
+ AnyHit anyHit = new AnyHit();
+ anyHit.customDimensions = customDimensions;
+ anyHit.customMetrics = customMetrics;
+ anyHit.delegateExecutor = delegateExecutor;
+ anyHit.parms = parms;
+ anyHit.occurredAt = occurredAt;
+
+ return anyHit;
+ }
}
diff --git a/src/main/java/com/brsanthu/googleanalytics/request/GoogleAnalyticsResponse.java b/src/main/java/com/brsanthu/googleanalytics/request/GoogleAnalyticsResponse.java
index eba245f..291be2b 100644
--- a/src/main/java/com/brsanthu/googleanalytics/request/GoogleAnalyticsResponse.java
+++ b/src/main/java/com/brsanthu/googleanalytics/request/GoogleAnalyticsResponse.java
@@ -1,20 +1,20 @@
/*
- * 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
+ * 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
+ * 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.
+ * 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.request;
import java.util.Map;
+import com.brsanthu.googleanalytics.httpclient.HttpRequest;
+import com.brsanthu.googleanalytics.httpclient.HttpResponse;
+
/**
* Response for GA tracking request.
*
@@ -22,18 +22,23 @@
*/
public class GoogleAnalyticsResponse {
private int statusCode = 200;
+ private GoogleAnalyticsRequest> googleAnalyticsRequest;
private Map requestParams = null;
+ private HttpRequest httpRequest;
+ private HttpResponse httpResponse;
public Map getRequestParams() {
return requestParams;
}
- public void setRequestParams(Map postedParms) {
- this.requestParams = postedParms;
+ public GoogleAnalyticsResponse setRequestParams(Map postedParms) {
+ requestParams = postedParms;
+ return this;
}
- public void setStatusCode(int statusCode) {
+ public GoogleAnalyticsResponse setStatusCode(int statusCode) {
this.statusCode = statusCode;
+ return this;
}
public int getStatusCode() {
@@ -48,4 +53,31 @@ public String toString() {
builder.append("]");
return builder.toString();
}
+
+ public GoogleAnalyticsRequest> getGoogleAnalyticsRequest() {
+ return googleAnalyticsRequest;
+ }
+
+ public GoogleAnalyticsResponse setGoogleAnalyticsRequest(GoogleAnalyticsRequest> 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;
+ }
}
diff --git a/src/main/java/com/brsanthu/googleanalytics/request/ItemHit.java b/src/main/java/com/brsanthu/googleanalytics/request/ItemHit.java
index c894d85..29887f3 100644
--- a/src/main/java/com/brsanthu/googleanalytics/request/ItemHit.java
+++ b/src/main/java/com/brsanthu/googleanalytics/request/ItemHit.java
@@ -1,19 +1,15 @@
/*
- * 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
+ * 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
+ * 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.
+ * 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.request;
-import static com.brsanthu.googleanalytics.internal.Constants.HIT_ITEM;
import static com.brsanthu.googleanalytics.request.GoogleAnalyticsParameter.CURRENCY_CODE;
import static com.brsanthu.googleanalytics.request.GoogleAnalyticsParameter.ITEM_CATEGORY;
import static com.brsanthu.googleanalytics.request.GoogleAnalyticsParameter.ITEM_CODE;
diff --git a/src/main/java/com/brsanthu/googleanalytics/request/PageViewHit.java b/src/main/java/com/brsanthu/googleanalytics/request/PageViewHit.java
index 566b2ea..1a76e7f 100644
--- a/src/main/java/com/brsanthu/googleanalytics/request/PageViewHit.java
+++ b/src/main/java/com/brsanthu/googleanalytics/request/PageViewHit.java
@@ -1,20 +1,15 @@
/*
- * 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
+ * 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
+ * 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.
+ * 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.request;
-import static com.brsanthu.googleanalytics.internal.Constants.HIT_PAGEVIEW;
-
/**
* GA request to track a typical web page view
*
diff --git a/src/main/java/com/brsanthu/googleanalytics/request/ScreenViewHit.java b/src/main/java/com/brsanthu/googleanalytics/request/ScreenViewHit.java
index a27e0a1..6f38fe3 100644
--- a/src/main/java/com/brsanthu/googleanalytics/request/ScreenViewHit.java
+++ b/src/main/java/com/brsanthu/googleanalytics/request/ScreenViewHit.java
@@ -1,19 +1,15 @@
/*
- * 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
+ * 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
+ * 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.
+ * 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.request;
-import static com.brsanthu.googleanalytics.internal.Constants.HIT_SCREENVIEW;
import static com.brsanthu.googleanalytics.request.GoogleAnalyticsParameter.SCREEN_NAME;
/**
diff --git a/src/main/java/com/brsanthu/googleanalytics/request/SocialHit.java b/src/main/java/com/brsanthu/googleanalytics/request/SocialHit.java
index 9d7afb8..a77dce8 100644
--- a/src/main/java/com/brsanthu/googleanalytics/request/SocialHit.java
+++ b/src/main/java/com/brsanthu/googleanalytics/request/SocialHit.java
@@ -1,19 +1,15 @@
/*
- * 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
+ * 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
+ * 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.
+ * 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.request;
-import static com.brsanthu.googleanalytics.internal.Constants.HIT_SOCIAL;
import static com.brsanthu.googleanalytics.request.GoogleAnalyticsParameter.SOCIAL_ACTION;
import static com.brsanthu.googleanalytics.request.GoogleAnalyticsParameter.SOCIAL_ACTION_TARGET;
import static com.brsanthu.googleanalytics.request.GoogleAnalyticsParameter.SOCIAL_NETWORK;
diff --git a/src/main/java/com/brsanthu/googleanalytics/request/TimingHit.java b/src/main/java/com/brsanthu/googleanalytics/request/TimingHit.java
index ed2b5d7..79e62fd 100644
--- a/src/main/java/com/brsanthu/googleanalytics/request/TimingHit.java
+++ b/src/main/java/com/brsanthu/googleanalytics/request/TimingHit.java
@@ -1,19 +1,15 @@
/*
- * 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
+ * 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
+ * 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.
+ * 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.request;
-import static com.brsanthu.googleanalytics.internal.Constants.HIT_TIMING;
import static com.brsanthu.googleanalytics.request.GoogleAnalyticsParameter.DNS_TIME;
import static com.brsanthu.googleanalytics.request.GoogleAnalyticsParameter.PAGE_DOWNLOAD_TIME;
import static com.brsanthu.googleanalytics.request.GoogleAnalyticsParameter.PAGE_LOAD_TIME;
diff --git a/src/main/java/com/brsanthu/googleanalytics/request/TransactionHit.java b/src/main/java/com/brsanthu/googleanalytics/request/TransactionHit.java
index f257fab..6d1ebe1 100644
--- a/src/main/java/com/brsanthu/googleanalytics/request/TransactionHit.java
+++ b/src/main/java/com/brsanthu/googleanalytics/request/TransactionHit.java
@@ -1,19 +1,15 @@
/*
- * 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
+ * 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
+ * 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.
+ * 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.request;
-import static com.brsanthu.googleanalytics.internal.Constants.HIT_TXN;
import static com.brsanthu.googleanalytics.request.GoogleAnalyticsParameter.CURRENCY_CODE;
import static com.brsanthu.googleanalytics.request.GoogleAnalyticsParameter.TRANSACTION_AFFILIATION;
import static com.brsanthu.googleanalytics.request.GoogleAnalyticsParameter.TRANSACTION_ID;
diff --git a/src/test/java/com/brsanthu/googleanalytics/AwtRequestParameterDiscovererTest.java b/src/test/java/com/brsanthu/googleanalytics/AwtRequestParameterDiscovererTest.java
index 9a5c73e..6b016b9 100644
--- a/src/test/java/com/brsanthu/googleanalytics/AwtRequestParameterDiscovererTest.java
+++ b/src/test/java/com/brsanthu/googleanalytics/AwtRequestParameterDiscovererTest.java
@@ -3,18 +3,18 @@
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
-import org.junit.Test;
+import org.junit.jupiter.api.Test;
import com.brsanthu.googleanalytics.discovery.AwtRequestParameterDiscoverer;
import com.brsanthu.googleanalytics.request.DefaultRequest;
public class AwtRequestParameterDiscovererTest {
-
+
@Test
public void testDiscoverParameters() throws Exception {
AwtRequestParameterDiscoverer discoverer = new AwtRequestParameterDiscoverer();
DefaultRequest request = new DefaultRequest();
-
+
GoogleAnalyticsConfig config = new GoogleAnalyticsConfig();
assertNull(config.getUserAgent());
@@ -22,14 +22,14 @@ public void testDiscoverParameters() throws Exception {
assertNull(request.documentEncoding());
assertNull(request.screenColors());
assertNull(request.screenResolution());
-
+
discoverer.discoverParameters(config, request);
-
+
assertNotNull(config.getUserAgent());
assertNotNull(request.userLanguage());
assertNotNull(request.documentEncoding());
assertNotNull(request.screenColors());
assertNotNull(request.screenResolution());
}
-
+
}
diff --git a/src/test/java/com/brsanthu/googleanalytics/DefaultRequestParameterDiscovererTest.java b/src/test/java/com/brsanthu/googleanalytics/DefaultRequestParameterDiscovererTest.java
index 00d320a..a19258c 100644
--- a/src/test/java/com/brsanthu/googleanalytics/DefaultRequestParameterDiscovererTest.java
+++ b/src/test/java/com/brsanthu/googleanalytics/DefaultRequestParameterDiscovererTest.java
@@ -3,29 +3,29 @@
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
-import org.junit.Test;
+import org.junit.jupiter.api.Test;
import com.brsanthu.googleanalytics.discovery.DefaultRequestParameterDiscoverer;
import com.brsanthu.googleanalytics.request.DefaultRequest;
public class DefaultRequestParameterDiscovererTest {
-
+
@Test
public void testDiscoverParameters() throws Exception {
DefaultRequestParameterDiscoverer discoverer = new DefaultRequestParameterDiscoverer();
DefaultRequest request = new DefaultRequest();
-
+
GoogleAnalyticsConfig config = new GoogleAnalyticsConfig();
assertNull(config.getUserAgent());
assertNull(request.userLanguage());
assertNull(request.documentEncoding());
-
+
discoverer.discoverParameters(config, request);
-
+
assertNotNull(config.getUserAgent());
assertNotNull(request.userLanguage());
assertNotNull(request.documentEncoding());
}
-
+
}
diff --git a/src/test/java/com/brsanthu/googleanalytics/EventHitTest.java b/src/test/java/com/brsanthu/googleanalytics/EventHitTest.java
index a2afe65..3b758ca 100644
--- a/src/test/java/com/brsanthu/googleanalytics/EventHitTest.java
+++ b/src/test/java/com/brsanthu/googleanalytics/EventHitTest.java
@@ -2,12 +2,12 @@
import static org.junit.Assert.assertEquals;
-import org.junit.Test;
+import org.junit.jupiter.api.Test;
import com.brsanthu.googleanalytics.request.EventHit;
public class EventHitTest {
-
+
@Test
public void testEventHit() throws Exception {
EventHit eventHit = new EventHit("eventCategory", "eventAction", "eventLabel", 10);
diff --git a/src/test/java/com/brsanthu/googleanalytics/GaUtilsTest.java b/src/test/java/com/brsanthu/googleanalytics/GaUtilsTest.java
index 0447c70..2cf936a 100644
--- a/src/test/java/com/brsanthu/googleanalytics/GaUtilsTest.java
+++ b/src/test/java/com/brsanthu/googleanalytics/GaUtilsTest.java
@@ -1,13 +1,13 @@
package com.brsanthu.googleanalytics;
-import static org.junit.Assert.*;
+import static org.junit.Assert.assertEquals;
-import org.junit.Test;
+import org.junit.jupiter.api.Test;
import com.brsanthu.googleanalytics.internal.GaUtils;
public class GaUtilsTest {
-
+
@Test
public void testIsEmpty() throws Exception {
assertEquals(true, GaUtils.isEmpty(null));
@@ -16,7 +16,7 @@ public void testIsEmpty() throws Exception {
assertEquals(false, GaUtils.isEmpty("value"));
assertEquals(false, GaUtils.isEmpty(" value "));
}
-
+
@Test
public void isNotEmpty() throws Exception {
assertEquals(false, GaUtils.isNotEmpty(null));
@@ -25,7 +25,7 @@ public void isNotEmpty() throws Exception {
assertEquals(true, GaUtils.isNotEmpty("value"));
assertEquals(true, GaUtils.isNotEmpty(" value "));
}
-
+
@Test
public void testAppendSystemProperty() throws Exception {
System.setProperty("test", "test");
diff --git a/src/test/java/com/brsanthu/googleanalytics/GoogleAnalyticsBatchTest.java b/src/test/java/com/brsanthu/googleanalytics/GoogleAnalyticsBatchTest.java
index 2ff0d2a..d35094b 100644
--- a/src/test/java/com/brsanthu/googleanalytics/GoogleAnalyticsBatchTest.java
+++ b/src/test/java/com/brsanthu/googleanalytics/GoogleAnalyticsBatchTest.java
@@ -4,17 +4,17 @@
import java.util.stream.IntStream;
-import org.junit.BeforeClass;
-import org.junit.Test;
+import org.junit.jupiter.api.BeforeAll;
+import org.junit.jupiter.api.Test;
public class GoogleAnalyticsBatchTest {
private static GoogleAnalytics ga = null;
- @BeforeClass
+ @BeforeAll
public static void setup() {
- ga = GoogleAnalytics.builder().withTrackingId(TEST_TRACKING_ID).withAppName("Junit Test").withAppVersion("1.0.0")
- .withConfig(new GoogleAnalyticsConfig().setBatchingEnabled(true).setBatchSize(10)).build();
+ ga = GoogleAnalytics.builder().withTrackingId(TEST_TRACKING_ID).withAppName("Junit Test").withAppVersion("1.0.0").withConfig(
+ new GoogleAnalyticsConfig().setBatchingEnabled(true).setBatchSize(10)).build();
}
@Test
@@ -23,4 +23,4 @@ public void testPageView() throws Exception {
ga.pageView("http://www.google.com", "Search").send();
});
}
-}
\ No newline at end of file
+}
diff --git a/src/test/java/com/brsanthu/googleanalytics/GoogleAnalyticsConfigTest.java b/src/test/java/com/brsanthu/googleanalytics/GoogleAnalyticsConfigTest.java
index e40b26f..c28019a 100644
--- a/src/test/java/com/brsanthu/googleanalytics/GoogleAnalyticsConfigTest.java
+++ b/src/test/java/com/brsanthu/googleanalytics/GoogleAnalyticsConfigTest.java
@@ -1,8 +1,9 @@
package com.brsanthu.googleanalytics;
+import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.Assert.assertEquals;
-import org.junit.Test;
+import org.junit.jupiter.api.Test;
public class GoogleAnalyticsConfigTest {
@@ -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");
+ }
+
}
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..c49023b
--- /dev/null
+++ b/src/test/java/com/brsanthu/googleanalytics/GoogleAnalyticsDebugTest.java
@@ -0,0 +1,53 @@
+package com.brsanthu.googleanalytics;
+
+import static com.brsanthu.googleanalytics.internal.Constants.TEST_TRACKING_ID;
+import static org.junit.Assert.assertEquals;
+
+import java.util.Map;
+
+import org.json.simple.JSONArray;
+import org.json.simple.JSONObject;
+import org.json.simple.parser.JSONParser;
+import org.junit.jupiter.api.BeforeAll;
+import org.junit.jupiter.api.Test;
+
+import com.brsanthu.googleanalytics.request.GoogleAnalyticsResponse;
+
+public class GoogleAnalyticsDebugTest {
+
+ private static GoogleAnalytics ga = null;
+
+ @BeforeAll
+ public static void setup() {
+ ga = GoogleAnalytics.builder().withTrackingId(TEST_TRACKING_ID).withAppName("Junit Test").withAppVersion("1.0.0").withConfig(
+ new GoogleAnalyticsConfig().setHitDebug(true)).build();
+ }
+
+ @SuppressWarnings("unchecked")
+ @Test
+ public void testPageViewDebug() throws Exception {
+ GoogleAnalyticsResponse response = ga.pageView("http://www.google.com", "Search").send();
+
+ // Sample response from GA
+ // @formatter:off
+ /*
+ {
+ "hitParsingResult": [ {
+ "valid": true,
+ "parserMessage": [ ],
+ "hit": "/debug/collect?dt=Search\u0026de=UTF-8\u0026qt=1\u0026t=pageview\u0026av=1.0.0\u0026v=1\u0026ul=en-US\u0026dl=http://www.google.com\u0026an=Junit Test\u0026tid=UA-612100-12\u0026cid=7beb1a85-2a9e-440f-8e2d-5e91f8a7e708"
+ } ],
+ "parserMessage": [ {
+ "messageType": "INFO",
+ "description": "Found 1 hit in the request."
+ } ]
+ }
+ */
+ // @formatter:on
+ String originalResponse = response.getHttpResponse().getBody();
+ 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"));
+ }
+}
diff --git a/src/test/java/com/brsanthu/googleanalytics/GoogleAnalyticsParameterTest.java b/src/test/java/com/brsanthu/googleanalytics/GoogleAnalyticsParameterTest.java
index 7510dcc..510402f 100644
--- a/src/test/java/com/brsanthu/googleanalytics/GoogleAnalyticsParameterTest.java
+++ b/src/test/java/com/brsanthu/googleanalytics/GoogleAnalyticsParameterTest.java
@@ -4,7 +4,7 @@
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;
-import org.junit.Test;
+import org.junit.jupiter.api.Test;
import com.brsanthu.googleanalytics.request.GoogleAnalyticsParameter;
diff --git a/src/test/java/com/brsanthu/googleanalytics/GoogleAnalyticsRequestTest.java b/src/test/java/com/brsanthu/googleanalytics/GoogleAnalyticsRequestTest.java
index ccd6cf1..37b49ce 100644
--- a/src/test/java/com/brsanthu/googleanalytics/GoogleAnalyticsRequestTest.java
+++ b/src/test/java/com/brsanthu/googleanalytics/GoogleAnalyticsRequestTest.java
@@ -2,7 +2,7 @@
import static org.junit.Assert.assertEquals;
-import org.junit.Test;
+import org.junit.jupiter.api.Test;
import com.brsanthu.googleanalytics.request.PageViewHit;
diff --git a/src/test/java/com/brsanthu/googleanalytics/GoogleAnalyticsTest.java b/src/test/java/com/brsanthu/googleanalytics/GoogleAnalyticsTest.java
index 2eab362..11fd188 100644
--- a/src/test/java/com/brsanthu/googleanalytics/GoogleAnalyticsTest.java
+++ b/src/test/java/com/brsanthu/googleanalytics/GoogleAnalyticsTest.java
@@ -1,22 +1,42 @@
package com.brsanthu.googleanalytics;
import static com.brsanthu.googleanalytics.internal.Constants.TEST_TRACKING_ID;
+import static com.brsanthu.googleanalytics.request.GoogleAnalyticsParameter.QUEUE_TIME;
+import static org.assertj.core.api.Assertions.assertThat;
+import static org.assertj.core.api.Assertions.assertThatThrownBy;
+import static org.assertj.core.api.Assertions.within;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.when;
-import org.junit.BeforeClass;
-import org.junit.Test;
+import java.time.ZonedDateTime;
+import java.util.concurrent.Future;
+import java.util.stream.IntStream;
+import org.junit.jupiter.api.BeforeAll;
+import org.junit.jupiter.api.Test;
+import org.mockito.ArgumentMatchers;
+
+import com.brsanthu.googleanalytics.httpclient.HttpClient;
+import com.brsanthu.googleanalytics.httpclient.HttpResponse;
+import com.brsanthu.googleanalytics.request.AnyHit;
import com.brsanthu.googleanalytics.request.DefaultRequest;
+import com.brsanthu.googleanalytics.request.GoogleAnalyticsParameter;
+import com.brsanthu.googleanalytics.request.GoogleAnalyticsRequest;
import com.brsanthu.googleanalytics.request.GoogleAnalyticsResponse;
+import com.brsanthu.googleanalytics.request.ScreenViewHit;
public class GoogleAnalyticsTest {
private static GoogleAnalytics ga = null;
+ private static HttpClient client;
- @BeforeClass
+ @BeforeAll
public static void setup() {
ga = GoogleAnalytics.builder().withTrackingId(TEST_TRACKING_ID).withAppName("Junit Test").withAppVersion("1.0.0").build();
+ client = mock(HttpClient.class);
+ when(client.post(ArgumentMatchers.any())).thenReturn(new HttpResponse().setStatusCode(200));
}
@Test
@@ -70,8 +90,8 @@ public void testCustomDimensions() throws Exception {
// Local ga
GoogleAnalytics lga = GoogleAnalytics.builder().withDefaultRequest(defaultRequest).withTrackingId(TEST_TRACKING_ID).build();
- GoogleAnalyticsResponse response = lga.pageView("http://www.google.com", "Search").customDimension(2, "bob").customDimension(5, "alice")
- .send();
+ GoogleAnalyticsResponse response = lga.pageView("http://www.google.com", "Search").customDimension(2, "bob").customDimension(5,
+ "alice").send();
assertEquals("foo", response.getRequestParams().get("cd1"));
assertEquals("bob", response.getRequestParams().get("cd2"));
@@ -101,8 +121,8 @@ public void testUserIpAndAgent() throws Exception {
GoogleAnalytics lga = GoogleAnalytics.builder().withDefaultRequest(defaultRequest).withTrackingId(TEST_TRACKING_ID).build();
- GoogleAnalyticsResponse response = lga.pageView("http://www.google.com", "Search").userIp("1.2.3.5")
- .userAgent("Chrome/9.80 (Windows NT 6.0) Presto/2.12.388 Version/12.14").send();
+ GoogleAnalyticsResponse response = lga.pageView("http://www.google.com", "Search").userIp("1.2.3.5").userAgent(
+ "Chrome/9.80 (Windows NT 6.0) Presto/2.12.388 Version/12.14").send();
assertEquals("1.2.3.5", response.getRequestParams().get("uip"));
assertEquals("Chrome/9.80 (Windows NT 6.0) Presto/2.12.388 Version/12.14", response.getRequestParams().get("ua"));
@@ -127,4 +147,154 @@ public void testUserDetails() throws Exception {
assertEquals("12345", response.getRequestParams().get("cid"));
assertEquals("user2", response.getRequestParams().get("uid"));
}
+
+ @Test
+ void testExceptionHandler() throws Exception {
+ HttpClient client = mock(HttpClient.class);
+ when(client.post(ArgumentMatchers.any())).thenThrow(new RuntimeException("Testing Exception"));
+
+ GoogleAnalytics ga = GoogleAnalytics.builder().withHttpClient(client).withConfig(new GoogleAnalyticsConfig()).build();
+
+ // Since default behavior is to log exception, this function call should work fine.
+ ga.screenView().send();
+
+ GoogleAnalytics propagatingGa = GoogleAnalytics.builder().withHttpClient(client).withConfig(
+ new GoogleAnalyticsConfig().setExceptionHandler(new PropagatingExceptionHandler())).build();
+
+ assertThatThrownBy(() -> propagatingGa.screenView().send()).hasMessage("Testing Exception");
+
+ assertThatThrownBy(() -> propagatingGa.screenView().sendAsync().get()).hasMessageContaining("Testing Exception");
+ }
+
+ @Test
+ void testAutoQueueTime() throws Exception {
+ // By default queue time is added based on when hit was created and posted. In this test case, it should be
+ // close to 0
+ GoogleAnalytics gaAutoTimeEnabled = GoogleAnalytics.builder().withHttpClient(client).withConfig(new GoogleAnalyticsConfig()).build();
+ GoogleAnalyticsResponse respEnabled = gaAutoTimeEnabled.screenView().send();
+ assertThat(Integer.parseInt(respEnabled.getRequestParams().get(GoogleAnalyticsParameter.QUEUE_TIME.getParameterName()))).isCloseTo(0,
+ within(100));
+
+ // We can set occurred at to a past value and if so, it will be used to calcualte queue time.
+ GoogleAnalytics gaAutoTimeEnabledOccurredAt = GoogleAnalytics.builder().withHttpClient(client).withConfig(
+ new GoogleAnalyticsConfig()).build();
+ GoogleAnalyticsResponse respEnabledOccurredAt = gaAutoTimeEnabledOccurredAt.screenView().occurredAt(
+ ZonedDateTime.now().minusSeconds(5)).send();
+ assertThat(Integer.parseInt(respEnabledOccurredAt.getRequestParams().get(GoogleAnalyticsParameter.QUEUE_TIME.getParameterName()))).isCloseTo(
+ 5000, within(100));
+
+ // We can set both occurredAt and queue time, then time based on occurred at will be added to set queue time.
+ GoogleAnalyticsResponse respEnabledWithSetQueueTime = gaAutoTimeEnabled.screenView().occurredAt(
+ ZonedDateTime.now().minusSeconds(5)).queueTime(1000).send();
+ assertThat(Integer.parseInt(
+ respEnabledWithSetQueueTime.getRequestParams().get(GoogleAnalyticsParameter.QUEUE_TIME.getParameterName()))).isCloseTo(6000,
+ within(100));
+
+ // If we disable auto queue time, then queue time is not calculated
+ GoogleAnalytics gaAutoTimeDisabled = GoogleAnalytics.builder().withHttpClient(client).withConfig(
+ new GoogleAnalyticsConfig().setAutoQueueTimeEnabled(false)).build();
+ GoogleAnalyticsResponse respDisabled = gaAutoTimeDisabled.screenView().occurredAt(ZonedDateTime.now().minusSeconds(5)).send();
+ assertThat(respDisabled.getRequestParams().get(GoogleAnalyticsParameter.QUEUE_TIME.getParameterName())).isNull();
+ }
+
+ @Test
+ void testAutoQueueTimeBatch() throws Exception {
+ GoogleAnalytics gaAutoTimeEnabled = GoogleAnalytics.builder().withHttpClient(client).withConfig(
+ new GoogleAnalyticsConfig().setBatchingEnabled(true).setBatchSize(2)).build();
+
+ // First request will add to batch.
+ GoogleAnalyticsResponse resp1 = gaAutoTimeEnabled.screenView().send();
+
+ Thread.sleep(500);
+
+ GoogleAnalyticsResponse resp2 = gaAutoTimeEnabled.screenView().send();
+
+ assertThat(Integer.parseInt(resp1.getRequestParams().get(QUEUE_TIME.getParameterName()))).isCloseTo(500, within(100));
+
+ assertThat(Integer.parseInt(resp2.getRequestParams().get(QUEUE_TIME.getParameterName()))).isCloseTo(0, within(100));
+ }
+
+ @Test
+ void testDeepClone() throws Exception {
+ ScreenViewHit screenhit1 = ga.screenView().adwordsId("test1");
+ ScreenViewHit screenhit2 = screenhit1.clone();
+
+ screenhit1.adwordsId("test1updated");
+
+ assertThat(screenhit1).isNotSameAs(screenhit2);
+ assertThat(screenhit1.adwordsId()).isEqualTo("test1updated");
+ assertThat(screenhit2.adwordsId()).isEqualTo("test1");
+ }
+
+ @Test
+ void testStats() throws Exception {
+ GoogleAnalytics ga = GoogleAnalytics.builder().withHttpClient(client).withConfig(
+ new GoogleAnalyticsConfig().setGatherStats(true).setBatchingEnabled(true).setBatchSize(2)).build();
+
+ IntStream.rangeClosed(1, 1).forEach(val -> ga.pageView().send());
+ IntStream.rangeClosed(1, 2).forEach(val -> ga.event().send());
+ IntStream.rangeClosed(1, 3).forEach(val -> ga.screenView().send());
+ IntStream.rangeClosed(1, 4).forEach(val -> ga.item().send());
+ IntStream.rangeClosed(1, 5).forEach(val -> ga.transaction().send());
+ IntStream.rangeClosed(1, 6).forEach(val -> ga.timing().send());
+ IntStream.rangeClosed(1, 7).forEach(val -> ga.social().send());
+ IntStream.rangeClosed(1, 8).forEach(val -> ga.exception().send());
+
+ assertThat(ga.getStats().getPageViewHits()).isEqualTo(1);
+ assertThat(ga.getStats().getEventHits()).isEqualTo(2);
+ assertThat(ga.getStats().getScreenViewHits()).isEqualTo(3);
+ assertThat(ga.getStats().getItemHits()).isEqualTo(4);
+ assertThat(ga.getStats().getTransactionHits()).isEqualTo(5);
+ assertThat(ga.getStats().getTimingHits()).isEqualTo(6);
+ assertThat(ga.getStats().getSocialHits()).isEqualTo(7);
+ assertThat(ga.getStats().getExceptionHits()).isEqualTo(8);
+ assertThat(ga.getStats().getTotalHits()).isEqualTo(36);
+ }
+
+ @Test
+ void testAnyHit() throws Exception {
+ ScreenViewHit sv = ga.screenView().adwordsId("adsid123");
+
+ AnyHit anyhit1 = sv.asAnyHit();
+ anyhit1.adwordsId("adsid456");
+ assertThat(sv.adwordsId()).isEqualTo(anyhit1.adwordsId());
+
+ AnyHit anyhit2 = sv.clone().asAnyHit();
+ anyhit2.adwordsId("adsid789");
+ assertThat(sv.adwordsId()).isNotEqualTo(anyhit2.adwordsId());
+ }
+
+ @Test
+ void testAnonymizeUserIp() throws Exception {
+ String userIp = "176.134.201.004";
+
+ GoogleAnalytics ga = GoogleAnalytics.builder().withHttpClient(client).withConfig(new GoogleAnalyticsConfig()).build();
+ GoogleAnalyticsResponse resp = ga.screenView().userIp(userIp).send();
+ assertThat(resp.getRequestParams().get(GoogleAnalyticsParameter.USER_IP.getParameterName())).isEqualTo(userIp);
+
+ GoogleAnalytics ga2 = GoogleAnalytics.builder().withHttpClient(client).withConfig(
+ new GoogleAnalyticsConfig().setAnonymizeUserIp(true)).build();
+ GoogleAnalyticsResponse resp2 = ga2.screenView().userIp(userIp).send();
+ assertThat(resp2.getRequestParams().get(GoogleAnalyticsParameter.USER_IP.getParameterName())).isEqualTo("176.134.201.0");
+ }
+
+ @Test
+ void testCustomExecutor() throws Exception {
+ GoogleAnalyticsExecutor exector = new GoogleAnalyticsExecutor() {
+ @Override
+ public Future postAsync(GoogleAnalyticsRequest> request) {
+ return null;
+ }
+
+ @Override
+ public GoogleAnalyticsResponse post(GoogleAnalyticsRequest> request) {
+ return new GoogleAnalyticsResponse().setStatusCode(400);
+ }
+ };
+
+ GoogleAnalytics ga = GoogleAnalytics.builder().withGoogleAnalyticsExecutor(exector).build();
+ GoogleAnalyticsResponse resp = ga.screenView().send();
+ assertThat(resp.getStatusCode()).isEqualTo(400);
+
+ }
}
diff --git a/src/test/java/com/brsanthu/googleanalytics/HitTypesTest.java b/src/test/java/com/brsanthu/googleanalytics/HitTypesTest.java
index ea522dc..0de5b77 100644
--- a/src/test/java/com/brsanthu/googleanalytics/HitTypesTest.java
+++ b/src/test/java/com/brsanthu/googleanalytics/HitTypesTest.java
@@ -1,9 +1,11 @@
package com.brsanthu.googleanalytics;
+import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.Assert.assertEquals;
-import org.junit.Test;
+import org.junit.jupiter.api.Test;
+import com.brsanthu.googleanalytics.request.AnyHit;
import com.brsanthu.googleanalytics.request.EventHit;
import com.brsanthu.googleanalytics.request.ExceptionHit;
import com.brsanthu.googleanalytics.request.ItemHit;
@@ -25,5 +27,6 @@ public void testHitTypes() throws Exception {
assertEquals("social", new SocialHit().hitType());
assertEquals("timing", new TimingHit().hitType());
assertEquals("transaction", new TransactionHit().hitType());
+ assertThat(new AnyHit().hitType()).isEqualTo("pageview");
}
}