1

I have to send a POST request to the /batch endpoint of : 'https://www.google-analytics.com'.

As mentioned in the Documentation I have to send the request to /batch endpoint and specify each payload on its own line.

I was able to achieve this using POSTMAN as follows:

Postman

My query is to make a POST request using Python's requests library

I tried something like this :

import requests

text = '''v=1&cid=43223523&tid=UA-XXXXXX-1&t=event&ec=aggregated_stats&ea=daily_kpi&el=bookmarks&ev=13
v=1&cid=43223523&tid=UA-XXXXXX-1&t=event&ec=aggregated_stats&ea=daily_kpi&el=upvotes&ev=65
v=1&cid=43223523&tid=UA-XXXXXX-1&t=event&ec=aggregated_stats&ea=daily_kpi&el=questions&ev=15
v=1&cid=43223523&tid=UA-XXXXXX-1&t=event&ec=aggregated_stats&ea=daily_kpi&el=postviews&ev=95'''

response = requests.post('https://www.google-analytics.com/batch', data=text)

but it doesn't works.

UPDATE

I Tried this and it works !

import http.client

conn = http.client.HTTPSConnection("www.google-analytics.com")
payload = "v=1&cid=43223523&tid=UA-200248207-1&t=event&ec=aggregated_stats&ea=daily_kpi&el=bookmarks&ev=13\r\nv=1&cid=43223523&tid=UA-200248207-1&t=event&ec=aggregated_stats&ea=daily_kpi&el=upvotes&ev=63\r\nv=1&cid=43223523&tid=UA-200248207-1&t=event&ec=aggregated_stats&ea=daily_kpi&el=questions&ev=11\r\nv=1&cid=43223523&tid=UA-200248207-1&t=event&ec=aggregated_stats&ea=daily_kpi&el=postviews&ev=23"
headers = {
  'Content-Type': 'text/plain'
}
conn.request("POST", "/batch", payload, headers)
res = conn.getresponse()

But the question remains open, what's the issue with requests here.

Prakhar
  • 929
  • 1
  • 9
  • 19
  • 1
    Please Post your code and not screenshots. – Ram Jun 25 '21 at 07:18
  • ```response.status_code``` gives ```200```. What do you expect your code to do ? – Ram Jun 25 '21 at 07:28
  • As mentioned here : https://developers.google.com/analytics/devguides/collection/protocol/v1/validating-hits#overview It gives 200 even if it is malformed. But I can confirm from my analytics dashboard that request isn't made successfully when using python requests. – Prakhar Jun 25 '21 at 07:30
  • try simply remove `\\n` – georgexsh Jun 25 '21 at 07:41
  • I tried validating your requests to this end point as mentioned in the docs - ```https://www.google-analytics.com/debug/collect```. The response says that ```tid``` is invalid. May be you have to check that. @Prakhar – Ram Jun 25 '21 at 07:58
  • @Ram That's a secret data which I can't share that's why i have replaced that with XXXXXX. Wait I will create a testing account then update the question with `tid` – Prakhar Jun 25 '21 at 08:05
  • May be you could make a POST request to ```https://www.google-analytics.com/debug/collect``` with actual values and check the status. You can find the status in ```response.json()```. @Prakhar – Ram Jun 25 '21 at 08:08
  • ```/collect``` is for making a single hit, I have to send 10-12 hits at a time which according to documentation is best with ```/batch``` but unfortunately there is nothing like ```/debug/batch```. (I checked) – Prakhar Jun 25 '21 at 08:11
  • @Prakhar But you could make just one hit and check if everything is working fine. Right ? – Ram Jun 25 '21 at 08:45
  • @Ram i checked, /debug/collect returns ```valid=true``` but actually it doesn't works. – Prakhar Jun 25 '21 at 09:06

2 Answers2

1

You don't need to double-escape the newline symbol.

Moreover, you don't need the newline symbol at all for the multi-line string.

And also the indentations you put in your multi-line string are counted:

test = '''abc
def
ghi'''

print(test)

Here's an SO answer that explains this with some additional ways to make long stings: https://stackoverflow.com/a/10660443/4570170

Now the request body.

The documentation says

payload_data – The BODY of the post request. The body must include exactly 1 URI encoded payload and must be no longer than 8192 bytes.

So try uri-encoding your payload:

text = '''v=1&cid=43223523&tid=UA-XXXXXX-1&t=event&ec=aggregated_stats&ea=daily_kpi&el=bookmarks&ev=13
v=1&cid=43223523&tid=UA-XXXXXX-1&t=event&ec=aggregated_stats&ea=daily_kpi&el=upvotes&ev=65
v=1&cid=43223523&tid=UA-XXXXXX-1&t=event&ec=aggregated_stats&ea=daily_kpi&el=questions&ev=15
v=1&cid=43223523&tid=UA-XXXXXX-1&t=event&ec=aggregated_stats&ea=daily_kpi&el=postviews&ev=95'''

text_final = requests.utils.quote(text)

response = requests.post('https://www.google-analytics.com/batch', data=text_final)

Chillie
  • 1,356
  • 13
  • 16
  • @Chillie Thanks that's was an issue, I corrected that. But it still doesn't works. – Prakhar Jun 25 '21 at 08:06
  • @Prakhar i've updated the answer, try uri-encoding the data. – Chillie Jun 25 '21 at 08:26
  • ok i tried still it doesn't work. I tried making it work using ```http.client``` it works. But not with ```requests``` – Prakhar Jun 25 '21 at 09:04
  • 1
    @Prakhar then inspect your request body in `http.client` and see how it's sending your payloads. Debugging and manually inspecting the requests/responses is key here. – Chillie Jun 25 '21 at 09:24
0

Finally , I figured out the solution myself.

Updating for others help.

The problem was I was working on AWS Cloud9 and as mentioned in the documentation

Some environments are not able to send hits to Google Analytics directly. Examples of this are older mobile phones that can't run JavaScript or corporate intranets behind a firewall.

So we just need to include the User Agent parameter

ua=Opera/9.80

in each of our payloads

It works !

Prakhar
  • 929
  • 1
  • 9
  • 19