Created
June 30, 2014 21:21
-
-
Save coreyward/60b7eb23373f40f9ebcb to your computer and use it in GitHub Desktop.
Track outbound link clicks in Google Analytics (Universal / analytics.js) with fallback
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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