From 051911e49525bda3ecc0f30bb63d7e8d90407caf Mon Sep 17 00:00:00 2001 From: Harel Ben-Attia Date: Sat, 8 Feb 2020 14:23:02 +0200 Subject: fixed GA, including toc links --- mkdocs/docs/js/google-analytics.js | 49 ++++++++++++++++++++++++++++---------- 1 file changed, 37 insertions(+), 12 deletions(-) diff --git a/mkdocs/docs/js/google-analytics.js b/mkdocs/docs/js/google-analytics.js index c8322a7..69817c6 100644 --- a/mkdocs/docs/js/google-analytics.js +++ b/mkdocs/docs/js/google-analytics.js @@ -1,27 +1,26 @@ // Monitor all download links in GA -window.onload = function() { - var a = document.getElementsByTagName('a'); - var cnt = 0; - for (i = 0; i < a.length; i++) { - var url = a[i].href; + +var dlCnt = 0; + +function GAizeDownloadLink(a) { + var url = a.href; var x = url.indexOf("?"); if (x != -1) { url = url.substr(0, x); } - var url_test = url.match(/^https?:\/\/.+(\/rpms\/.*\.rpm|\/deb\/.*\.deb|single-binary\/Darwin\/.*\/q|\/archive\/.*\.tar\.gz|\/archive\/.*\.zip|\/windows\/.*\.exe)$/i); + var url_test = url.match(/^https?:\/\/.+(\/rpms\/.*\.rpm|\/deb\/.*\.deb|\/single-binary\/Darwin\/.*\/q|\/archive\/.*\.tar\.gz|\/archive\/.*\.zip|\/windows\/.*\.exe)$/i); if (url_test) { - console.log("Converting url to be GA aware: " + url); + console.log("Converting download link to be GA aware: " + url); if (url_test.length > 1) { var event_action = url_test[1]; } else { var event_action = 'unknown_action'; } - a[i].event_action = event_action; - cnt = cnt + 1; - a[i].onclick = function() { + a.event_action = event_action; + dlCnt = dlCnt + 1; + a.onclick = function() { console.log("Sending GA event for link" + url); var that = this; - //ga('send', 'event', 'Downloads', 'Click on ' + this.event_action, this.getAttribute('href')); gtag('event','perform download', { 'event_category': 'Downloads', 'event_label': 'Download ' + this.event_action , 'value': 1 }); setTimeout(function() { location.href = that.href; @@ -29,6 +28,32 @@ window.onload = function() { return false; }; } +} + +function GAizeTOCLink(l) { + l.onclick = function() { + url_test = l.href.match(/^https?:\/\/.+(#.*)$/i); + toc_name = url_test[1]; + var that = this; + console.log("Sending GA event for toc link " + this.href); + + gtag('event','navigate', { 'event_category': 'Navigation', 'event_label': 'go to ' + toc_name, 'value': 1 }); + setTimeout(function() { + location.href = that.href; + }, 500); + return false; + }; + +} + +window.onload = function() { + var anchors = document.getElementsByTagName('a'); + for (i = 0; i < anchors.length; i++) { + GAizeDownloadLink(anchors[i]); + } + var toc_links = document.querySelectorAll('div.md-sidebar[data-md-component=toc] a.md-nav__link'); + for (i = 0; i < toc_links.length; i++) { + GAizeTOCLink(toc_links[i]); } - console.log("Converted " + cnt + " links to be GA aware"); + console.log("Converted " + dlCnt + " links to be GA aware"); } -- cgit v1.2.3