Skip to content

Instantly share code, notes, and snippets.

@coreyward
Created June 30, 2014 21:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save coreyward/60b7eb23373f40f9ebcb to your computer and use it in GitHub Desktop.
Save coreyward/60b7eb23373f40f9ebcb to your computer and use it in GitHub Desktop.
Track outbound link clicks in Google Analytics (Universal / analytics.js) with fallback
# only bind if/when GA loads
ga ->
$('a[href*="//"]').click (e) ->
e.preventDefault()
url = $(this).attr('href')
# define the callback
action = -> document.location = url
# record the click as an event
ga 'send', 'event', 'outbound', 'click', url, { hitCallback: action }
# in case GA is down/slow, redirect after 3 seconds
setTimeout action, 3000
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment