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

62 lines
1.6 KiB
JavaScript
Raw Normal View History

2022-05-03 20:50:57 +02:00
function _gaLt (event) {
if (typeof ga !== 'function') {
return
2021-08-09 18:04:09 +02:00
}
2022-05-03 20:50:57 +02:00
let el = event.srcElement || event.target
2021-08-09 18:04:09 +02:00
/* Loop up the DOM tree through parent elements if clicked element is not a link (eg: an image inside a link) */
while (
el &&
2022-05-03 20:50:57 +02:00
(typeof el.tagName === 'undefined' ||
el.tagName.toLowerCase() != 'a' ||
2021-08-09 18:04:09 +02:00
!el.href)
) {
2022-05-03 20:50:57 +02:00
el = el.parentNode
2021-08-09 18:04:09 +02:00
}
if (el && el.href) {
/* link */
2022-05-03 20:50:57 +02:00
const link = el.href
2021-08-09 18:04:09 +02:00
if (link.indexOf(location.host) == -1 && !link.match(/^javascript:/i)) {
/* external link */
/* HitCallback function to either open link in either same or new window */
2022-05-03 20:50:57 +02:00
const hitBack = function (link, target) {
target ? window.open(link, target) : (window.location.href = link)
}
2021-08-09 18:04:09 +02:00
/* Is target set and not _(self|parent|top)? */
2022-05-03 20:50:57 +02:00
const target =
2021-08-09 18:04:09 +02:00
el.target && !el.target.match(/^_(self|parent|top)$/i)
? el.target
2022-05-03 20:50:57 +02:00
: false
2021-08-09 18:04:09 +02:00
/* send event with callback */
ga(
2022-05-03 20:50:57 +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) }
2022-05-03 20:50:57 +02:00
)
2021-08-09 18:04:09 +02:00
/* Prevent standard click */
2022-05-03 20:50:57 +02:00
event.preventDefault ? event.preventDefault() : (event.returnValue = !1)
2021-08-09 18:04:09 +02:00
}
}
}
/* Attach the event to all clicks in the document after page has loaded */
2022-05-03 20:50:57 +02:00
const w = window
2021-08-09 18:04:09 +02:00
w.addEventListener
? w.addEventListener(
2022-05-03 20:50:57 +02:00
'load',
2021-08-09 18:04:09 +02:00
() => {
2022-05-03 20:50:57 +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 &&
2022-05-03 20:50:57 +02:00
w.attachEvent('onload', () => {
document.body.attachEvent('onclick', _gaLt)
})