Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Google Analytics, Server-Side Tracking & Bot Filter

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()
)
like image 507
Travis Swientek Avatar asked Nov 20 '18 15:11

Travis Swientek


1 Answers

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.

like image 79
s3cur3 Avatar answered Oct 19 '22 01:10

s3cur3