Created
November 5, 2015 16:44
-
-
Save joshcanhelp/c81dfde5390196d94109 to your computer and use it in GitHub Desktop.
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
<script> | |
( function() { | |
"use strict"; | |
// Get headline link URLs | |
var wpdHeadlines = document.getElementsByClassName('headline-link'); | |
// Combine and win | |
var wpdTrackThese = Array.prototype.slice.call(wpdHeadlines); | |
// Get image links | |
var wpdHeadlineImgWrap = document.getElementsByClassName('link-list-image'); | |
for (var i = 0; i < wpdHeadlineImgWrap.length; ++i) { | |
wpdTrackThese | |
.push(wpdHeadlineImgWrap[i] | |
.getElementsByTagName('a')[0]); | |
} | |
// Get single page external links | |
var wpdExternalLinkWrap = document.getElementsByClassName('external-link'); | |
for ( i = 0; i < wpdExternalLinkWrap.length; ++i) { | |
wpdTrackThese | |
.push(wpdExternalLinkWrap[i].getElementsByTagName('a')[0]) | |
.push(wpdExternalLinkWrap[i].getElementsByTagName('a')[1]); | |
} | |
// Attach click events | |
for ( i = 0; i < wpdTrackThese.length; ++i) { | |
var el = wpdTrackThese[i]; | |
// Make sure we only set this for external links | |
if ( el.host === window.location.host ){ | |
// continue; | |
} | |
if (el.addEventListener) { | |
el.addEventListener('click', wpdGAClickEvent, false); | |
} else if (el.attachEvent) { | |
el.attachEvent('onclick', wpdGAClickEvent); | |
} | |
} | |
function wpdGAClickEvent( event ) { | |
ga('send', 'event', 'WPD Outbound Link', 'click', 'URL', event.srcElement.href); | |
} | |
}()); | |
</script> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment