Skip to content

Instantly share code, notes, and snippets.

@pwenzel
Created February 20, 2015 05:19
Show Gist options
  • Save pwenzel/eb4f04552ef410f60406 to your computer and use it in GitHub Desktop.
Save pwenzel/eb4f04552ef410f60406 to your computer and use it in GitHub Desktop.
Track 404 errors as events with Google Universal Analytics, capturing the referrer as well.
<script type="text/javascript">
// Track 404 errors with Universal Analytics
if($('body').hasClass('error404')) {
ga('send', 'event', 'error', '404', 'page: ' + document.location.pathname + document.location.search + ' ref: ' + document.referrer, {'nonInteraction': 1});
}
</script>
@jnachtigall
Copy link

Not 100% sure, but I think it should be

ga('send', 'event', 'error', '404', 'page: ' + document.location.pathname + document.location.search + ' ref: ' + document.referrer, undefined, {nonInteraction: true});

That is, if the 4th argument is optional then pass in "undefined" and for nonInteraction it should be "true" (though 1 might work too depending on how Google has implemented it.

@bjorn-ali-goransson
Copy link

@jnachtigall undefined is unnecessary, Google themselves do not use it in their examples. But you're correct on the "1" thing.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment