1

I'm trying to send post requests to https://www.google-analytics.com/collect? like so:

name = user.first_name
    if user.username:
      name += f'[{user.username}]'
    query_params = {
      'v': '1',
      't': 'event',
      'tid': self._config.ga_tracking_id,
      'cid': '555',
      'ec': category,
      'ea': action,
      'el': f'{name}: {label}'
    }
    query = urlencode(query_params)
    requests.post('https://www.google-analytics.com/collect?'+query)

The print output is giving me this:

https://www.google-analytics.com/collect?v=1&t=event&tid=UA-17106xxxx-1&cid=555&ec=category&ea=ACTION&el=Name%5BFullView%5D%3A+nana+with+6+results

everything look ok to me, but on my GA Events panel I don't see any report either under Real-Time or Behaviour

Any suggestion?

*** UPDATE ***

    query_params = {
      'v': '1',
      't': 'event',
      'tid': self._config.ga_tracking_id,
      'cid': '555',
      'ec': category,
      'ea': action,
      'el': f'[{now}]{name}: {label}',
      'dp': '/'
    }
    r = requests.post('https://www.google-analytics.com/collect', data=query_params)
Donovant
  • 3,091
  • 8
  • 40
  • 68

3 Answers3

2

Try to include user agent, for example 'ua'='Opera/9.80'. See documentation here: https://developers.google.com/analytics/devguides/collection/protocol/v1/devguide#ecom

Emerson Harkin
  • 889
  • 5
  • 13
Fuzayl
  • 43
  • 6
1

Assuming this is not a brand new Google analytics account and that it is at least 72 hours old then it should be recording data.

Post should contain post body.

requests.post('https://www.google-analytics.com/collect?'+query)

You are sending a post request but you are not sending the data in the post body.

POST /collect HTTP/1.1
Host: www.google-analytics.com

v=1&t=event&tid=UA-17106xxxx-1&cid=555&ec=category&ea=ACTION&el=Name%5BFullView%5D%3A+nana+with+6+results

post body

# importing the requests library 
import requests 
  
# api-endpoint 
URL = "https://www.google-analytics.com/collect"

# define user
name = user.first_name
if user.username:
   name += f'[{user.username}]'
  
# defining a params dict for the parameters to be sent to the API 
PARAMS = {
  'v': '1',
  't': 'event',
  'tid': self._config.ga_tracking_id,
  'cid': '555',
  'ec': category,
  'ea': action,
  'el': f'{name}: {label}'
}

# sending POST request and saving the response as response object 
r = requests.post(url = URL, params = PARAMS) 
Danila Vershinin
  • 8,725
  • 2
  • 29
  • 35
Linda Lawton - DaImTo
  • 106,405
  • 32
  • 180
  • 449
  • You're right, but I thought it depended of not having encoded properly the query-param values I create that account yesterday though, but just for testing I put all strings/values without any space or special character and I could see 2 actions yesterday :/ – Donovant Jun 30 '20 at 09:30
  • Anyway I've just tested and it worked well :), thanks – Donovant Jun 30 '20 at 09:33
  • Is that params= or data= ? – Donovant Jun 30 '20 at 09:48
0

Add headers to the request like so:

headers = {
  "Content-Type": "application/json",
  "User-Agent": "My User Agent 1.0",
  "Accept": "*/*",
  "Accept-Encoding": "gzip, deflate, br",
}
url = "https://www.google-analytics.com/collect?v=2&tid=G-XXXXXXXXXX&cid=555&t=event&en=eventName"
resp = requests.post(url, headers=headers)

This worked for me

Tyler2P
  • 2,324
  • 26
  • 22
  • 31
AMoDev
  • 1
  • 1