6
votes

When submitting an event, using the Google Analytics Measurement Protocol... GA is classifying the events as bot traffic. I can determine this by configuring two views in GA, one with bot filtering on, and one with bot filtering disabled. The events show up consistently in the view with bot filtering disabled.

We do not want to disable the bot filter in our primary view, as this would include a ton of unnecessary bot traffic.

What about this code is tripping up the bot filter?

payload = {
    'v': 1,
    't': 'event',
    'tid': tracking_id,
    'ec': category,
    'ea': action,
    'el': label
}

if value and type(value) is int:
    payload['ev'] = value

if user_id:
    payload['uid'] = user_id
else:
    payload['cid'] = str(uuid4())

requests.post(
    'https://www.google-analytics.com/collect',
    data=payload,
    headers=requests.utils.default_headers()
)
1
Just a heads up: the indentation on the requests.post() call is off by two spaces. I can't edit it myself, because the diff is too small for SO to allow! :) - s3cur3
@s3cur3 Fixed! Sorry about that. - Travis Swientek

1 Answers

8
votes

requests.utils.default_headers() gives you a the default user agent of "python-requests" (per the code for default_headers() and default_user_agent()).

Announcing that you're a Python program—presumably calling their servers up repeatedly from the same IP—sounds like the definition of a bot! :)

You might have better luck lying about your user agent—by grabbing the user agent string from your (real) web browser, for instance.