Gero
Gero

Reputation: 2967

Post to Google Analytics, works from Node, fails from Python.... why?

I'm trying to post events to Google Analytics. It works fine when I do it using the NodeJS code below, but fails when I use the Python code below. Both do return a HTTP 200 and even when posting to the debug URL (https://www.google-analytics.com/debug/collect) Google Analytics returns success details in both cases (see valid: true in the response below). The problem is that when posting from NodeJS the result shows up in the GA website, when posting from Python it never shows up. I did compare the requests for both and have not been able to spot a difference.

{
  "hitParsingResult": [ {
    "valid": true,
    "parserMessage": [ ],
    "hit": "/debug/collect?v=1\u0026t=event\u0026tid=XXXXXXX\u0026cid=YYYYYYu0026ec=Slack\u0026ea=SlashCommand\u0026el=whowasat-curl\u0026an=staging.Whereis-Everybody?\u0026aid=staging.whereis-everybody.com"
  } ],
  "parserMessage": [ {
    "messageType": "INFO",
    "description": "Found 1 hit in the request."
  } ]
} 

The NodeJS code is (result does show up in Google Analytics):

'use strict';

var request = require('request');
require('request-debug')(request);

function postEventToGA(category, action, label) {

    var options = {
        v: '1',
        t: 'event',
        tid: process.env.GOOGLEANALYTICS_TID,
        cid: process.env.GOOGLEANALYTICS_CID,
        ec: category,
        ea: action,
        el: label,
        an: process.env.STAGE_INFIX + "appname",
        aid: process.env.STAGE_INFIX + "appname"
    };

    console.log("payload: " + JSON.stringify(options))
    request.post({ url: 'https://www.google-analytics.com/collect', form: options }, function (err, response, body) {
        console.log(request)
        if (err) {
            console.log("Failed to post event to Google Analytics, error: " + err);
        } else {
            if (200 != response.statusCode) {
                console.log("Failed to post event to Google Analytics, response code: " + response.statusCode + " error: " + err);
            }
        }
    });

}

postEventToGA("some-category", "some-action", "some-label")

And the Python code is (result does not show up in Google Analytics):

import json
import logging
import os
import requests

LOGGER = logging.getLogger()
LOGGER.setLevel(logging.INFO)

GOOGLEANALYTICS_TID = os.environ["GOOGLEANALYTICS_TID"]
GOOGLEANALYTICS_CID = os.environ["GOOGLEANALYTICS_CID"]
STAGE_INFIX = os.environ["STAGE_INFIX"]

def post_event(category, action, label):

    payload = {
        "v": "1",
        "t": "event",
        "tid": GOOGLEANALYTICS_TID,
        "cid": GOOGLEANALYTICS_CID,
        "ec": category,
        "ea": action,
        "el": label,
        "an": STAGE_INFIX + "appname,
        "aid": STAGE_INFIX + "appname",
    }

    response = requests.post("https://www.google-analytics.com/collect", payload)

    print(response.request.method)
    print(response.request.path_url)
    print(response.request.url)
    print(response.request.body)
    print(response.request.headers)

    print(response.status_code)
    print(response.text)

    if response.status_code != 200:
        LOGGER.warning(
            "Got non 200 response code (%s) while posting to GA.", response.status_code
        )


post_event("some-category", "some-action", "some-label")

Any idea why the NodeJS post will show up in Google Analytics and the Python post does not? (while both return a HTTP200)

Upvotes: 1

Views: 253

Answers (1)

Gero
Gero

Reputation: 2967

Did some more testing and discovered that the user agent HTTP header was causing the problem. When I set it to an empty string in the Python code it works. Like this:

headers = {"User-Agent": ""}
response = requests.post(
    "https://www.google-analytics.com/collect", payload, headers=headers
)

The documentation at https://developers.google.com/analytics/devguides/collection/protocol/v1/reference does state that the user agent is used, but does not clearly state what the requirements are. "python-requests/2.22.0" (default value by python-requests lib) is apparently not accepted.

Upvotes: 1

Related Questions