Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Tracking to Google Analytics from python

I'm trying to track GA events from python backend for ecommerce analysis. The way it's written in the official documentation, that is sending a post request, nothing shows up, I still have zero visitors in the dashboard, but google replies with 200 and a GIF.

In this example I'm sending a pageview, which obviously makes no sense doing from the backend, but it's simple enough for debugging.

This is my code:

import requests

payload = {
    'v': 1,
    'tid': 'UA-XXXXXX-Y',
    'cid': 555,
    't': 'pageview',
    'dp': '/home',
}
print 'sending to GA...'
r = requests.post("http://www.google-analytics.com/collect", data=payload)
if r.ok:
    print 'ok'
else:
    print ':('

I have disabled all filters in GA.

like image 845
rioted Avatar asked Feb 14 '26 00:02

rioted


2 Answers

Had the same problem, requests from python did not appear, postman worked. Solved it by removing the checkmark in google analytics under Settings > View > View Settings > Bot Filtering: Exclude all hits from known bots and spiders.

like image 111
beatsteak0815 Avatar answered Feb 15 '26 12:02

beatsteak0815


I think it is an authentication problem try using postman and try your post url works or not

like image 20
Keval Gohil Avatar answered Feb 15 '26 12:02

Keval Gohil



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!