-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathga-download-tracker.js
More file actions
122 lines (103 loc) · 4.12 KB
/
ga-download-tracker.js
File metadata and controls
122 lines (103 loc) · 4.12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
/*
* Google Analytics Download Tracker
* https://github.com/bjarte/ga-download-tracker
*
* Version 1.1.0
*
* GADT sends a virtual pageview and an event to Google Analytics
* when a user clicks a link to download a file.
*
* Copyright (c) 2017 Bjarte Aune Olsen (https://basementmedia.no)
*
* Licensed under the MIT (http://en.wikipedia.org/wiki/MIT_License) license.
*
* Inspired by Entourage.js by Tian Davis (http://techoctave.com/c7)
*/
(function () {
var gadt = (function () {
// Filetypes to track
var filetypes = /\.pdf$|\.zip$|\.od*|\.doc*|\.xls*|\.ppt*|\.exe$|\.dmg$|\.mp\d$|\.mov$|\.avi$|\.wav$|\.ogg$/i;
var track = function (event) {
// This script only works if it is called from a link <a> with a href attribute
var link = this;
if (link.href === null || link.href.length === 0) {
return;
}
// Is Google Analytics loaded?
if (typeof (ga) === "undefined" || ga === null || ga.loaded !== true) {
console.log("Google Analytics is not loaded, cannot track download of file: " + link.pathname);
return;
}
var filename = link.pathname;
// Is this a link to a file?
if (filename.search(filetypes) === -1) {
return;
}
// Remove everything before the last slash in the path
filename = filename.substring(filename.lastIndexOf("/") + 1, filename.length);
if (filename.length === 0) {
return;
}
// Prevent the browser from downloading the file before
// Google Analytics has had time to track it
event.preventDefault();
var trackedPageview = false;
var trackedEvent = false;
// Send event to Google Analytics
ga("send",
{
hitType: "event",
eventCategory: "Downloaded file",
eventAction: filename,
eventLabel: link.pathname,
hitCallback: function () {
// When Google Analytics is done tracking, download file
console.log("Google Analytics tracked download of file as event: " + link.pathname);
trackedEvent = true;
// Only download file if both event and pageview tracking is finished
if (trackedPageview) {
document.location.href = link.href;
}
}
});
// Send virtual pageview to Google Analytics
ga("send",
{
hitType: "pageview",
page: link.pathname,
title: filename,
hitCallback: function () {
// When Google Analytics is done tracking, download file
console.log("Google Analytics tracked download of file as pageview: " + link.pathname);
trackedPageview = true;
// Only download file if both event and pageview tracking is finished
if (trackedEvent) {
document.location.href = link.href;
}
}
});
// Wait 3 seconds, and if Google Analytics hasn't responded,
// download file anyway
setTimeout(function () {
console.log("Google Analytics didn't respond, cannot track download of file: " + link.pathname);
document.location.href = link.href;
}, 3000);
};
var init = function () {
var links = document.links;
var linksLength = links.length;
// Add tracking code to all links on the page
for (var i = 0; i < linksLength; i++) {
links[i].onclick = track;
}
};
return {
init: init,
track: track
};
})();
// Add tracker to global scope
window.gadt = gadt;
// Initialize tracker when page is loaded
window.onload = gadt.init;
})();