Ognjen Regoje bio photo

Ognjen Regoje
But you can call me Oggy


I make things that run on the web (mostly).
More ABOUT me and my PROJECTS.

me@ognjen.io LinkedIn

Tracking code comes after functionality

#development #product

Events in Google Analytics (or Mixpanel, etc) are a very useful way of tracking very specific usage within your application.

When triggering them, however, it’s imperative that the tracking code comes after functionality.

A lot of users, especially more technically savvy ones, run some equivalent of Adblock. Features will break if the code that’s supposed to trigger an event comes before functionality.

Adblockers exclude scripts completely. As a result, not only are events not sent but they actually cause exceptions. This then causes the functional code to not be executed.

So, this is broken:

# coffeescript

do_something = ->
  gtag 'event', 'Create',
    'event_category': 'Leads'
    'event_label': 'Homepage'
    'value': 0
  console.log(a)

But this is fine:

# coffeescript

do_something = ->
  console.log(a)
  gtag 'event', 'Create',
    'event_category': 'Leads'
    'event_label': 'Homepage'
    'value': 0

Uber’s website fails to do this. I tried to log in using my Google Account but it kept failing. I checked the console to see if there were any clues:

Uber Google login failing because it tries to trigger tracking first

It seems that tracking is being triggered first. And since it throws an exception the logging in doesn’t function then.

Another service I use has found a creative way of fixing that.

Site showing a popup if tracking is disabled