I’m trying to send custom event to Google Analytics from backend Python code, I don’t know if this is the correct way but it did work. Here is the Python code I used:
import requests
import json
measurement_id = ...
api_secret = ...
client_id = ...
url = f"https://www.google-analytics.com/mp/collect?measurement_id={measurement_id}&api_secret={api_secret}"
payload = {
"client_id": client_id,
"non_personalized_ads": False,
"events": [
{
"name": "test_debug",
"params": {
"debug_mode": True
}
}
]
}
r = requests.post(url, data=json.dumps(payload), verify=True)
I checked the GA Realtime Report, I see the event, but the problem is when I check the parameter, the parameter ‘debug_mode‘ just disappeared and show as a blank, and of course I can not see it in the DebugView(as I know, the event with parameter ‘debug_mode‘ populated will show up in the DebugView).
But if I do this with JavaScript, this works well, Here is the code, the Realtime Report and DebugView:
gtag('event', 'test_event_by_gtag', {
'debug_mode': true
});
So what is worng with the Python code? Why did the parameter ‘debug_mode‘ disappeared?
-
I also tried the check the Data filters, all of them are Inactive, so this should not be the problem(also if it’s filter problem, the Java Script code will not be available eiher, right?)
-
I tried to send request with Postman, here is my request, and the result is also the same.
Weibin Ren is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.