Reputation: 211
I am developing an application in Spring Boot and React and would like to have analytics through my Spring Boot server, as React component does not have all the necessary data. Meaning I would like to have a an endpoint that React can hit and this would gather the information needed and sent it to Google Analytics.
Point for using Google Analytics is that all our infra is in Google Cloud. We are using Google App Engine, Google Cloud Storage etc.
What I have come up with is the following RestController
@RestController
@RequestMapping("/analytics")
class AnalyticsController {
@GetMapping
fun test(){
val client: HttpClient = HttpClientBuilder.create().build()
val builder = URIBuilder()
builder
.setScheme("http")
.setHost("www.google-analytics.com")
.setPath("/collect")
.addParameter("v", "1") // API Version.
.addParameter("tid", MY_PROPERTY_ID) // Tracking ID / Property ID.
// Anonymous Client Identifier. Ideally, this should be a UUID that
// is associated with particular user, device, or browser instance.
.addParameter("cid", "555")
.addParameter("t", "event") // Event hit type.
.addParameter("ec", "example") // Event category.
.addParameter("ea", "TESTERIINO") // Event action.
var uri: URI? = null
uri = try {
builder.build()
} catch (e: URISyntaxException) {
throw ServletException("Problem building URI", e)
}
val request = HttpPost(uri)
client.execute(request)
}
}
Using this https://cloud.google.com/appengine/docs/flexible/java/integrating-with-analytics guide Im unable to get it working. Meaning the Spring boot log shows me that the request is successful, but nothing appers in Google Analytics dashboard.
Connection request: [route: {}->http://www.google-analytics.com:80][total kept alive: 0; route allocated: 0 of 2; total allocated: 0 of 20]
2020-06-02 15:12:25.878 DEBUG 47493 --- [nio-5000-exec-5] h.i.c.PoolingHttpClientConnectionManager : Connection leased: [id: 1][route: {}->http://www.google-analytics.com:80][total kept alive: 0; route allocated: 1 of 2; total allocated: 1 of 20]
2020-06-02 15:12:25.878 DEBUG 47493 --- [nio-5000-exec-5] o.a.http.impl.execchain.MainClientExec : Opening connection {}->http://www.google-analytics.com:80
2020-06-02 15:12:25.921 DEBUG 47493 --- [nio-5000-exec-5] .i.c.DefaultHttpClientConnectionOperator : Connecting to www.google-analytics.com/172.217.21.142:80
2020-06-02 15:12:25.950 DEBUG 47493 --- [nio-5000-exec-5] .i.c.DefaultHttpClientConnectionOperator : Connection established 192.168.8.129:59297<->172.217.21.142:80
2020-06-02 15:12:25.951 DEBUG 47493 --- [nio-5000-exec-5] o.a.http.impl.execchain.MainClientExec : Executing request GET /collect?v=1&tid=230236313&cid=555&t=event&ec=example&ea=TESTERIINO HTTP/1.1
2020-06-02 15:12:25.951 DEBUG 47493 --- [nio-5000-exec-5] o.a.http.impl.execchain.MainClientExec : Target auth state: UNCHALLENGED
2020-06-02 15:12:25.951 DEBUG 47493 --- [nio-5000-exec-5] o.a.http.impl.execchain.MainClientExec : Proxy auth state: UNCHALLENGED
2020-06-02 15:12:25.951 DEBUG 47493 --- [nio-5000-exec-5] org.apache.http.headers : http-outgoing-1 >> GET /collect?v=1&tid=230236313&cid=555&t=event&ec=example&ea=TESTERIINO HTTP/1.1
2020-06-02 15:12:25.951 DEBUG 47493 --- [nio-5000-exec-5] org.apache.http.headers : http-outgoing-1 >> Host: www.google-analytics.com
2020-06-02 15:12:25.951 DEBUG 47493 --- [nio-5000-exec-5] org.apache.http.headers : http-outgoing-1 >> Connection: Keep-Alive
2020-06-02 15:12:25.951 DEBUG 47493 --- [nio-5000-exec-5] org.apache.http.headers : http-outgoing-1 >> User-Agent: Apache-HttpClient/4.5.10 (Java/11.0.6)
2020-06-02 15:12:25.951 DEBUG 47493 --- [nio-5000-exec-5] org.apache.http.headers : http-outgoing-1 >> Accept-Encoding: gzip,deflate
2020-06-02 15:12:25.951 DEBUG 47493 --- [nio-5000-exec-5] org.apache.http.wire : http-outgoing-1 >> "GET /collect?v=1&tid=230236313&cid=555&t=event&ec=example&ea=TESTERIINO HTTP/1.1[\r][\n]"
2020-06-02 15:12:25.951 DEBUG 47493 --- [nio-5000-exec-5] org.apache.http.wire : http-outgoing-1 >> "Host: www.google-analytics.com[\r][\n]"
2020-06-02 15:12:25.951 DEBUG 47493 --- [nio-5000-exec-5] org.apache.http.wire : http-outgoing-1 >> "Connection: Keep-Alive[\r][\n]"
2020-06-02 15:12:25.951 DEBUG 47493 --- [nio-5000-exec-5] org.apache.http.wire : http-outgoing-1 >> "User-Agent: Apache-HttpClient/4.5.10 (Java/11.0.6)[\r][\n]"
2020-06-02 15:12:25.951 DEBUG 47493 --- [nio-5000-exec-5] org.apache.http.wire : http-outgoing-1 >> "Accept-Encoding: gzip,deflate[\r][\n]"
2020-06-02 15:12:25.951 DEBUG 47493 --- [nio-5000-exec-5] org.apache.http.wire : http-outgoing-1 >> "[\r][\n]"
2020-06-02 15:12:26.007 DEBUG 47493 --- [nio-5000-exec-5] org.apache.http.wire : http-outgoing-1 << "HTTP/1.1 200 OK[\r][\n]"
2020-06-02 15:12:26.007 DEBUG 47493 --- [nio-5000-exec-5] org.apache.http.wire : http-outgoing-1 << "Access-Control-Allow-Origin: *[\r][\n]"
2020-06-02 15:12:26.007 DEBUG 47493 --- [nio-5000-exec-5] org.apache.http.wire : http-outgoing-1 << "Date: Tue, 19 May 2020 03:16:25 GMT[\r][\n]"
2020-06-02 15:12:26.007 DEBUG 47493 --- [nio-5000-exec-5] org.apache.http.wire : http-outgoing-1 << "Pragma: no-cache[\r][\n]"
2020-06-02 15:12:26.008 DEBUG 47493 --- [nio-5000-exec-5] org.apache.http.wire : http-outgoing-1 << "Expires: Mon, 01 Jan 1990 00:00:00 GMT[\r][\n]"
2020-06-02 15:12:26.008 DEBUG 47493 --- [nio-5000-exec-5] org.apache.http.wire : http-outgoing-1 << "Last-Modified: Sun, 17 May 1998 03:00:00 GMT[\r][\n]"
2020-06-02 15:12:26.008 DEBUG 47493 --- [nio-5000-exec-5] org.apache.http.wire : http-outgoing-1 << "X-Content-Type-Options: nosniff[\r][\n]"
2020-06-02 15:12:26.008 DEBUG 47493 --- [nio-5000-exec-5] org.apache.http.wire : http-outgoing-1 << "Content-Type: image/gif[\r][\n]"
2020-06-02 15:12:26.008 DEBUG 47493 --- [nio-5000-exec-5] org.apache.http.wire : http-outgoing-1 << "Server: Golfe2[\r][\n]"
2020-06-02 15:12:26.008 DEBUG 47493 --- [nio-5000-exec-5] org.apache.http.wire : http-outgoing-1 << "Content-Length: 35[\r][\n]"
2020-06-02 15:12:26.008 DEBUG 47493 --- [nio-5000-exec-5] org.apache.http.wire : http-outgoing-1 << "Cache-Control: no-cache, no-store, must-revalidate[\r][\n]"
2020-06-02 15:12:26.008 DEBUG 47493 --- [nio-5000-exec-5] org.apache.http.wire : http-outgoing-1 << "Age: 1241760[\r][\n]"
2020-06-02 15:12:26.008 DEBUG 47493 --- [nio-5000-exec-5] org.apache.http.wire : http-outgoing-1 << "[\r][\n]"
2020-06-02 15:12:26.008 DEBUG 47493 --- [nio-5000-exec-5] org.apache.http.wire : http-outgoing-1 << "GIF89a[0x1][0x0][0x1][0x0][0x80][0xff][0x0][0xff][0xff][0xff][0x0][0x0][0x0],[0x0][0x0][0x0][0x0][0x1][0x0][0x1][0x0][0x0][0x2][0x2]D[0x1][0x0];"
2020-06-02 15:12:26.008 DEBUG 47493 --- [nio-5000-exec-5] org.apache.http.headers : http-outgoing-1 << HTTP/1.1 200 OK
2020-06-02 15:12:26.008 DEBUG 47493 --- [nio-5000-exec-5] org.apache.http.headers : http-outgoing-1 << Access-Control-Allow-Origin: *
2020-06-02 15:12:26.008 DEBUG 47493 --- [nio-5000-exec-5] org.apache.http.headers : http-outgoing-1 << Date: Tue, 19 May 2020 03:16:25 GMT
2020-06-02 15:12:26.008 DEBUG 47493 --- [nio-5000-exec-5] org.apache.http.headers : http-outgoing-1 << Pragma: no-cache
2020-06-02 15:12:26.008 DEBUG 47493 --- [nio-5000-exec-5] org.apache.http.headers : http-outgoing-1 << Expires: Mon, 01 Jan 1990 00:00:00 GMT
2020-06-02 15:12:26.008 DEBUG 47493 --- [nio-5000-exec-5] org.apache.http.headers : http-outgoing-1 << Last-Modified: Sun, 17 May 1998 03:00:00 GMT
2020-06-02 15:12:26.008 DEBUG 47493 --- [nio-5000-exec-5] org.apache.http.headers : http-outgoing-1 << X-Content-Type-Options: nosniff
2020-06-02 15:12:26.008 DEBUG 47493 --- [nio-5000-exec-5] org.apache.http.headers : http-outgoing-1 << Content-Type: image/gif
2020-06-02 15:12:26.008 DEBUG 47493 --- [nio-5000-exec-5] org.apache.http.headers : http-outgoing-1 << Server: Golfe2
2020-06-02 15:12:26.008 DEBUG 47493 --- [nio-5000-exec-5] org.apache.http.headers : http-outgoing-1 << Content-Length: 35
2020-06-02 15:12:26.008 DEBUG 47493 --- [nio-5000-exec-5] org.apache.http.headers : http-outgoing-1 << Cache-Control: no-cache, no-store, must-revalidate
2020-06-02 15:12:26.008 DEBUG 47493 --- [nio-5000-exec-5] org.apache.http.headers : http-outgoing-1 << Age: 1241760
2020-06-02 15:12:26.008 DEBUG 47493 --- [nio-5000-exec-5] o.a.http.impl.execchain.MainClientExec : Connection can be kept alive indefinitely
2020-06-02 15:12:26.009 DEBUG 47493 --- [nio-5000-exec-5] m.m.a.RequestResponseBodyMethodProcessor : Using 'application/json', given [*/*] and supported [application/json, application/*+json, application/json, application/*+json]
2020-06-02 15:12:26.009 DEBUG 47493 --- [nio-5000-exec-5] m.m.a.RequestResponseBodyMethodProcessor : Nothing to write: null body
2020-06-02 15:12:26.009 DEBUG 47493 --- [nio-5000-exec-5] o.j.s.OpenEntityManagerInViewInterceptor : Closing JPA EntityManager in OpenEntityManagerInViewInterceptor
2020-06-02 15:12:26.009 DEBUG 47493 --- [nio-5000-exec-5] o.s.web.servlet.DispatcherServlet : Completed 200 OK
2020-06-02 15:12:26.009 DEBUG 47493 --- [nio-5000-exec-5] o.s.s.w.a.ExceptionTranslationFilter : Chain processed normally
What is the thing that Im missing here ?
Upvotes: 3
Views: 4569
Reputation: 1
I have had same problem. What works for me without using RestTemplate is to add header "user-agent" to PostRequest:
HttpPost request = new HttpPost(uri);
request.addHeader("user-agent","your user agent text");
RestTemplate solution:
final HttpHeaders headers = new HttpHeaders();
headers.set("user-agent", "your user agent text");
final HttpEntity<String> entity = new HttpEntity<>(headers);
restTemplate.exchange(uri, HttpMethod.GET, entity, byte[].class);
After that I'm able to see events in GA console.
Upvotes: 0
Reputation: 11
I had the same issue. I verified that the link itself worked using postman both GET and POST so had to be something with the headers sent.
Given the same URIBuilder (with my own tracking id and values), I switched from HttpPost to spring RestTemplate and included custom user-agent into the http headers:
URI uri = builder.build();
final HttpHeaders headers = new HttpHeaders();
headers.set("User-Agent", "yourcustomuseragentname");
//Create a new HttpEntity
final HttpEntity<String> entity = new HttpEntity<>(headers);
//Execute the method writing your HttpEntity to the request
ResponseEntity<String> response = restTemplate.exchange(uri, HttpMethod.GET, entity, String.class);
This was the only way it worked and now I can see entries in the google analytics dashboard.
Upvotes: 1