google-analytics-java icon indicating copy to clipboard operation
google-analytics-java copied to clipboard

Unable to make v 2.0.0 working

Open AleksNFedorov opened this issue 5 years ago • 6 comments

I can`t get events to appear in google analytics. Everything works fine when I send requests from Postman.

Here is my configuration No error logs nor exceptions

	ga = GoogleAnalytics.builder()
			.withTrackingId("UA-XXXXXXXX-1")
			.withAppName("S******r")
			.withAppVersion("0.1.1")
			.build();
ga.event()
	.eventAction(event.getAction().name())
	.eventCategory(event.getCategory().name())
	.eventLabel(event.getValue())
	.clientId(clientId)
	.send();

Maven dependency

<dependency>
  <groupId>com.brsanthu</groupId>
  <artifactId>google-analytics-java</artifactId>
  <version>2.0.0</version>
</dependency>

AleksNFedorov avatar Oct 02 '19 21:10 AleksNFedorov

Hi. I had a similar problem. It was a missing parameter in the resquest that prevent the hit to be processed by GA.

To debug this, i logged all request parameters.

GoogleAnalyticsResponse response = ga.event().---.send();
Map<String, String> parameters = response.getRequestParams();

Then, i replayed the request with exact same parameters and values on GA debug endpoint https://www.google-analytics.com/debug/collect. This will show you a debug message with details of the problem. Validating hits on Measurement API

Work done on PR #61 will introduce new features to validate and debug hits.

DropSnorz avatar Oct 05 '19 09:10 DropSnorz

That is exactly an issue. Request with the same parameters goes ok if send from the Postman.

Debug endpoint also returns no issues

AleksNFedorov avatar Oct 07 '19 16:10 AleksNFedorov

User-Agent header is mandatory for the measurement protocol, must be set explicitly with config (.setUserAgent)

AleksNFedorov avatar Oct 11 '19 02:10 AleksNFedorov

Is there any documentation about the expected User-Agent header?

Adding the header solved the issue to me too, but I can't find documentation about it, not even the https://www.google-analytics.com/debug/collect endpoint provides this information.

nagyzsolthun avatar Jan 07 '20 13:01 nagyzsolthun

My observations so far:

  • User-Agent is required indeed.

  • The library will generate and include the header if its value was not specified explicitly in the configuration.

  • Requests made with the generated header (for me, it starts with java/1.8.0_212 and includes OS details) do not appear in Google Analytics.

  • Setting the header to something shorter (e.g., java/1.8.0_212) does not seem to help. In fact, requests didn't appear in Google Analytics until I removed the slash altogether.

  • Requests made from Postman (which sets User-Agent to PostmanRuntime/7.20.1) do work.

The following is the cURL command I used for testing:

curl -X POST \
  https://www.google-analytics.com/collect \
  -H 'Content-Type: application/x-www-form-urlencoded' \
  -H 'User-Agent: PostmanRuntime/7.20.1' \
  -d 'de=UTF-8&t=event&v=1&ea=VERIFY_EMAIL&ec=Tests&tid=UA-XXXXXXXXX-1&cid=97b964f2-4a72-47cd-ad19-4ce7d8f24784'

bkhablenko avatar Apr 22 '20 14:04 bkhablenko

It seems like GA filters events which it thinks is coming from bots. And for some reason, calls coming from this library is assumed to be from a bot. There seems to be 2 ways to solve this:

  1. Uncheck this checkbox for your view in GA - Exclude all hits from known bots and spiders.
  2. Explicitly override the User Agent via the measurement protocol's ua option.

I did option 2 to get this to work for me. Something like this fixed it for me - ga.event().userAgent("custom").send().

Based on this SO question.

DerrylThomas avatar Aug 11 '20 10:08 DerrylThomas