2020年6月4日

Tracking task using R + Measurement Protocol

This article introduces a key point solving the issue of google analytics data pushing, of course, I assumed that you have read the official document of Google and then you still got your program not working. I hope you can solve the issue soon, that is the reason I keep this hint here.




The tracking task flow:

What's App conversation content -> Data reformat(R) -> GA(Measurement Protocol)

R program

Just like other languages, it is able to do Post/Get call, handling return JSON data very easily, the following R functions are used in this task.

POST(), GET(), rawToChar(), fromJSON(), rev(), convert.time.unixtime(), paste(), as.POSIXct

E-Mail notification

Using sendgrid.com as the mail server, SendGrid offers APIs as well as variable code example that the developer is able to implement quickly. The E-mail notification will be sent when error occurred, such as null from API or data format incorrect.

Measurement Protocol:

You can find plenty of information in the official document of Google

The point of this article is to say what is the tricky part of the OFFICIAL document. The following is a standard payload you should include it in a POST call : 

v=1&tid=UA-123456-1&cid=5555&t=pageview&dp=%2FpageA

With this payload push via browser or Postman, you can see the reaction from Google analytics, in the realtime page, the number of the active user will increase in 5 seconds after the endpoint is called. 

However, this is not working for a calling via the program. I had spent a couple days on this issue investigation, finally found the answer in StackOverflow.

If you are going call measurement protocol via the program, you must add "ua" in the payload. The tricky part is this attribute is optional in the document, even without it, you are still able to pass the hint validation. 

Optional.
The User-Agent of the browser. Note that Google has libraries to identify real user agents. Handcrafting your own agent could break at any time. 
Example value: Opera/9.80 (Windows NT 6.0) Presto/2.12.388 Version/12.14
Example usage: ua=Opera%2F9.80%20%28Windows%20NT%206.0%29%20Presto%2F2.12.388%20Version%2F12.14
Reference
https://developers.google.com/analytics/devguides/collection/protocol/v1/parameters#ua


So if you call the measurement protocol with the following payload, you will get a positive reaction 

v=1&tid=UA-123456-1&cid=5555&t=pageview&dp=%2FpageA&ua=somethinghere


Do not forget "us" attribute in your payload if you call measurement protocol via the program.






沒有留言:

張貼留言

<Javascript> How to uncompressed GZIP at front-end using Javascript

It's been a while I haven't share my coding work. In this article I would like to share how to receive a Gzip file via stream, unzip...