From cf726687b1b0aeb4a04ed3a922cf05283dac4d54 Mon Sep 17 00:00:00 2001 From: k-ave <145393896+k-ave@users.noreply.github.com> Date: Mon, 18 Sep 2023 22:37:45 +0200 Subject: [PATCH] fix link behaviour --- builders/html/assets/js/src/stats.js | 34 ++++++++++++++++------------ 1 file changed, 19 insertions(+), 15 deletions(-) diff --git a/builders/html/assets/js/src/stats.js b/builders/html/assets/js/src/stats.js index fa5232d11..492a385f7 100644 --- a/builders/html/assets/js/src/stats.js +++ b/builders/html/assets/js/src/stats.js @@ -12,23 +12,27 @@ $(function(){ }, 750); // track outbound links $("a").click(function(){ - var url = $(this).attr("href"); - if (!url || url.indexOf("#") === 0) + let href = $(this).attr("href"); + + // ignore if no href or no length + if(!href || !href.length) return; + + try { + // ignore if same hostname + if(window.location.hostname == new URL(href).hostname) return; + } catch(e){ + // ignore invalid url return; - if (url.indexOf("http") === -1){ - document.location = url; // ignore local urls - } else { - try { - gtag('event', 'click', { - 'event_category': 'outbound', - 'event_label': url, - 'transport_type': 'beacon', - 'event_callback': function(){document.location = url;} - }); - } catch(e){ - document.location = url; - } } + + // only track outgoing links + try { + gtag('event', 'click', { + 'event_category': 'outbound', + 'event_label': href, + 'transport_type': 'beacon' + }); + } catch(e){} }); });