-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
37 additions
and
12 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,34 +1,59 @@ | ||
// 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; | ||
}, 500); | ||
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"); | ||
} |