简体   繁体   中英

How to post to Google Measurement protocol using Python?

Measurement protocol guide

I need an example of how a POST would look using Python.

Something like this, but working.

import httplib, urllib
conn = httplib.HTTPConnection("www.google-analytics.com")
conn.request("POST", "v=1&tid=UA-XXXXXX-Y&cid=666&t=event&ec=game&ea=start&ev=0")
response = conn.getresponse()
print response.status, response.reason
data = response.read()
conn.close()

You just needed a URL:

conn.request("POST", "/collect", "v=1&tid=UA-XXXXXX-Y&cid=666&t=event&ec=game&ea=start&ev=0")

Or with a dictionary:

import httplib, urllib

params = urllib.urlencode({
    'v': 1,
    'tid': 'UA-XXXXXX-Y',
    'cid': '666',
    't': 'event',
    'ec': 'game',
    'ea': 'start',
    'ev': 0
})

connection = httplib.HTTPConnection('www.google-analytics.com')
connection.request('POST', '/collect', params)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM