Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
152 views
in Technique[技术] by (71.8m points)

Google Analytics POST request works fine in REST Client, but not using Python Requests

I'm trying to send an event to Google Analytics using a Python script.

When I try to send the event using the Google Analyics "Hit Builder", it works fine. When I try to do the same thing with a simple POST request using the VSCode REST Client, it also works fine. But, for reasons I can't understand, making the exact same POST request with the Python Requests library doesn't work.

Here's what I want to do. This request works perfectly using the VSCode REST Client:

###
# @name analytics_post

POST www.google-analytics.com/collect
?v=1
&t=event
&tid=UA-XXXXXX-Y
&cid=Client%20ID%20Goes%20Here
&ec=Event%20Category%20Goes%20Here
&ea=Event%20Action%20Goes%20Here
&el=Event%20Label%20Goes%20Here
&ev=123

When I make that request, the event shows up right away in Google Analytics - great! But when I try to do the exact same thing in Python with Requests, it fails:

import requests
from urllib.parse import urlencode, quote

params = {}

params["v"] = 1                             # Version
params["t"] = "event"                       # Event hit type
params["tid"] = "UA-XXXXXX-YY"              # Tracking ID
params["cid"] = "Client ID Goes Here"       # Anonymous Client ID
params["ec"] = "Event Category Goes Here"   # Event Category
params["ea"] = "Event Action Goes Here"     # Event Action
params["el"] = "Event Label Goes Here"      # Event label
params["ev"] = 123

url = 'https://www.google-analytics.com/collect'

query = urlencode(params, quote_via=quote)

response = requests.post(url=url, params=query)

After running this, I get a 200 Response object and everything looks fine. The response even has a url object, which is exactly the same as the URL I formatted for the REST client request... so there aren't any issues with the way the URL is formatted. But the data doesn't appear in Google Analytics.

Just to be sure, I tried the same code but leaving out the quote_via=quote parameter, so that it would default to quote_plus, meaning that spaces would be rendered as + characters rather than %20. I also tried doing data=query instead of params=query in the POST request (last line). Neither of these worked. What am I doing wrong?

question from:https://stackoverflow.com/questions/65599780/google-analytics-post-request-works-fine-in-rest-client-but-not-using-python-re

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Turns out, this was an issue with the default headers used by requests. The solution mentioned here worked for me. I just had to also add {"User-Agent": ""} as the set of headers for the POST request.

This worked for me:

import requests

params = {}

params["v"] = 1                             # Version
params["t"] = "event"                       # Event hit type
params["tid"] = "UA-XXXXXX-YY"              # Tracking ID
params["cid"] = "Client ID Goes Here"       # Anonymous Client ID
params["ec"] = "Event Category Goes Here"   # Event Category
params["ea"] = "Event Action Goes Here"     # Event Action
params["el"] = "Event Label Goes Here"      # Event label
params["ev"] = 123

url = 'https://www.google-analytics.com/collect'

headers= {"User-Agent": ""}

response = requests.post(url=url, params=params, headers=headers)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...