如何使用Python发布到Google Measurement协议?

时间:2013-08-20 06:04:38

标签: python http post google-analytics

Measurement protocol guide

我需要一个关于POST如何使用Python的示例。

这样的事情,但是有效。

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()

1 个答案:

答案 0 :(得分:7)

您只需要一个网址:

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

或者使用字典:

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)