webpack-bootstrap-ui-kit/src/js/drivers/google.track.external.links.js

62 lines
1.6 KiB
JavaScript
Raw Normal View History

2021-08-09 18:04:09 +02:00
function _gaLt(event) {
2021-08-18 20:51:15 +02:00
if (typeof ga !== "function") {
2021-08-09 18:04:09 +02:00
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 &&
2021-08-18 20:51:15 +02:00
(typeof el.tagName == "undefined" ||
el.tagName.toLowerCase() != "a" ||
2021-08-09 18:04:09 +02:00
!el.href)
) {
el = el.parentNode;
}
if (el && el.href) {
/* link */
var link = el.href;
if (link.indexOf(location.host) == -1 && !link.match(/^javascript:/i)) {
/* external link */
/* HitCallback function to either open link in either same or new window */
2021-08-18 20:51:15 +02:00
var hitBack = function (link, target) {
2021-08-09 18:04:09 +02:00
target ? window.open(link, target) : (window.location.href = link);
};
/* Is target set and not _(self|parent|top)? */
var target =
el.target && !el.target.match(/^_(self|parent|top)$/i)
? el.target
: false;
/* send event with callback */
ga(
2021-08-18 20:51:15 +02:00
"send",
"event",
"Outgoing Links",
2021-08-09 18:04:09 +02:00
link,
document.location.pathname + document.location.search,
2021-08-18 20:51:15 +02:00
{ hitCallback: hitBack(link, target) }
2021-08-09 18:04:09 +02:00
);
/* Prevent standard click */
event.preventDefault ? event.preventDefault() : (event.returnValue = !1);
}
}
}
/* Attach the event to all clicks in the document after page has loaded */
var w = window;
w.addEventListener
? w.addEventListener(
2021-08-18 20:51:15 +02:00
"load",
2021-08-09 18:04:09 +02:00
() => {
2021-08-18 20:51:15 +02:00
document.body.addEventListener("click", _gaLt, !1);
2021-08-09 18:04:09 +02:00
},
2021-08-18 20:51:15 +02:00
!1
2021-08-09 18:04:09 +02:00
)
: w.attachEvent &&
2021-08-18 20:51:15 +02:00
w.attachEvent("onload", () => {
document.body.attachEvent("onclick", _gaLt);
2021-08-09 18:04:09 +02:00
});