4

I'm trying to post to Google Measurement Protocol using Ruby:

uri = URI.parse("http://www.google-analytics.com/collect")
params = {"v"=>"1", "tid"=>"UA-XXXXXXXX-X", "cid"=>"XXXXXXXXX.XXXXXXXXX", "t"=>"event", "ec"=>"200", "ea"=>"John"}
result = Net::HTTP.post_form(uri, params) #<Net::HTTPOK 200 OK readbody=true> 
result.body # "GIF89a\x01\x00\x01\x00\x80\xFF\x00\xFF\xFF\xFF\x00\x00\x00,\x00\x00\x00\x00\x01\x00\x01\x00\x00\x02\x02D\x01\x00;" 

Also tried to send a GET request instead of POST:

RestClient.get("http://www.google-analytics.com/collect", params: params, timeout: 4, open_timeout: 4) # "GIF89a\x01\x00\x01\x00\x80\xFF\x00\xFF\xFF\xFF\x00\x00\x00,\x00\x00\x00\x00\x01\x00\x01\x00\x00\x02\x02D\x01\x00;" 

And the event is not being tracked: enter image description here

They I tried to send the same parameters by simply typing them into Chrome:

enter image description here

It worked:

enter image description here

I guess I'm missing a header or something like this when I'm sending the request to Management Protocol using Ruby. But I can't figure out what I'm missing.

Sergey
  • 47,222
  • 25
  • 87
  • 129

3 Answers3

2

Had a similar issue with python. Worked after setting User-Agent header

A curl example would look like this

curl -H "user-agent: Some user agent" "https://ssl.google-analytics.com/collect?v=1&<other query params>"
Murali KG
  • 134
  • 1
  • 7
  • For some reason data is not collected without User-Agent header. Even if I want to track offline transactions that have no User-Agent. – YankovskyAndrey Mar 22 '19 at 09:08
2

Try changing:

uri = URI.parse("http://www.google-analytics.com/collect")

to:

uri = URI.parse("https://www.google-analytics.com/debug/collect")

This way you will test that request against Measurement Protocol Validation Server and instead of getting "GIF89a\x01\x00\x01\x00\x80\xFF\x00\xFF\xFF\xFF\x00\x00\x00,\x00\x00\x00\x00\x01\x00\x01\x00\x00\x02\x02D\x01\x00;" in response body you will get something along the lines

{
  "hitParsingResult": [
    {
      "valid": false,
      "hit": "GET /debug/collect?tid=fake\u0026v=1 HTTP/1.1",
      "parserMessage": [
        {
          "messageType": "ERROR",
          "description": "The value provided for parameter 'tid' is invalid. Please see xxx for details.",
          "parameter": "tid"
        },
        {
          "messageType": "ERROR",
          "description": "Tracking Id is a required field for this hit. Please see xxx for details.",
          "parameter": "tid"
        }
      ]
    }
  ]
}

You can read more here: https://developers.google.com/analytics/devguides/collection/protocol/v1/validating-hits

zwolin
  • 974
  • 3
  • 9
  • 19
0

You don't need user agent, but simply follow the rules of event tracking & rest-client. Using Post method and fill all parameter.

You can use rest-client with post method like this :

RestClient.post('https://www.google-analytics.com/collect?v=1&tid=UA-XXXXXXXXX-1&cid=XXXX-XXX&t=event&ec=Event%20Category&ea=Event%20Action&ev=1&el=Event%20Label', {}, {})

Format rest client with post method :

RestClient.post(url, payload, headers={})

Source :

rails_id
  • 8,120
  • 4
  • 46
  • 84