Created
November 25, 2019 14:43
-
-
Save esabook/41d4c4b36f3f084089ce82fa942d09e5 to your computer and use it in GitHub Desktop.
Listen outbonds link from current view, and send to google analytics
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
<!-- Here you can add your Google Analytics Tracking code. If you do so, do not | |
forget to set the include_analytics attribute to true on the _config.yml file --> | |
<script async src="https://www.googletagmanager.com/gtag/js?id=G-XB3ENLMJ9V"></script> | |
<script> | |
window.dataLayer = window.dataLayer || []; | |
function gtag() { dataLayer.push(arguments); } | |
gtag('js', new Date()); | |
gtag('config', 'G-XB3ENLMJ9V'); | |
gtag('send', 'pageview'); | |
function _gaLt(event) { | |
/* If GA is blocked or not loaded, or not main|middle|touch click then don't track */ | |
// if (!ga.hasOwnProperty("loaded") || ga.loaded != true || (event.which != 1 && event.which != 2)) { | |
// return; | |
// } | |
var el = event.srcElement || event.target; | |
/* Loop up the DOM tree through parent elements if clicked element is not a link (eg: an image inside a link) */ | |
while (el && (typeof el.tagName == 'undefined' || el.tagName.toLowerCase() != 'a' || !el.href)) { | |
el = el.parentNode; | |
} | |
/* if a link with valid href has been clicked */ | |
if (el && el.href) { | |
var link = el.href; | |
/* Only if it is an external link */ | |
if (link.indexOf(location.host) == -1 && !link.match(/^javascript\:/i)) { | |
/* Is actual target set and not _(self|parent|top)? */ | |
var target = (el.target && !el.target.match(/^_(self|parent|top)$/i)) ? el.target : false; | |
/* Assume a target if Ctrl|shift|meta-click */ | |
if (event.ctrlKey || event.shiftKey || event.metaKey || event.which == 2) { | |
target = "_blank"; | |
} | |
var hbrun = false; // tracker has not yet run | |
/* HitCallback to open link in same window after tracker */ | |
var hitBack = function () { | |
/* run once only */ | |
if (hbrun) return; | |
hbrun = true; | |
window.location.href = link; | |
}; | |
if (target) { /* If target opens a new window then just track */ | |
gtag("event", "Outgoing Links", { | |
"to": link, | |
"from": document.location.pathname + document.location.search | |
}); | |
} else { /* Prevent standard click, track then open */ | |
event.preventDefault ? event.preventDefault() : event.returnValue = !1; | |
/* send event with callback */ | |
gtag("event", "Outgoing Links", { | |
"to": link, | |
"from": document.location.pathname + document.location.search, | |
"hitCallback": hitBack | |
}); | |
gtag("event", "page_view") | |
/* Run hitCallback again if GA takes longer than 1 second */ | |
setTimeout(hitBack, 1000); | |
} | |
} | |
} | |
} | |
var _w = window; | |
/* Use "click" if touchscreen device, else "mousedown" */ | |
var _gaLtEvt = ("ontouchstart" in _w) ? "click" : "mousedown"; | |
/* Attach the event to all clicks in the document after page has loaded */ | |
_w.addEventListener ? _w.addEventListener("load", | |
function () { document.body.addEventListener(_gaLtEvt, _gaLt, !1) }, !1) | |
: _w.attachEvent && _w.attachEvent("onload", | |
function () { document.body.attachEvent("on" + _gaLtEvt, _gaLt) }); | |
</script> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment