-
Notifications
You must be signed in to change notification settings - Fork 1
/
ga-click-tracking-events-pages.js
161 lines (145 loc) · 4.35 KB
/
ga-click-tracking-events-pages.js
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
/**
* Google Analytics link and download click event and virtual pageview tracking
* 20130419
*
* Use Google Analytics (GA) to track clicks on PDF and TXT files as
* virtual pageviews.
* GA will track clicks as events on the following link types:
* External (Different host than current site or rel="external"),
* file (MP3s, EXEs, ZIPs, etc.), email (mailto:) and telephone (tel:).
*
* For more information about Google Analytics link tracking, see:
* https://support.google.com/analytics/answer/1136922
*
* Based on code from Blast Analytics & Marketing
* "How to Track Downloads & Outbound Links in Google Analytics"
* http://bit.ly/11K1Rcm
*
* Copyright 2013 Jeffrey Barke
* Released under the MIT license
* http://opensource.org/licenses/MIT
*/
/*jslint browser: true, nomen: true, white: true */
/*global _gaq, jQuery */
// Make sure there is a Google Analytics and a jQuery, otherwise not much point.
if (!(window._gaq === undefined || window.jQuery === undefined)) {
// Wrap in anonymous function to safely alias jQuery.
(function ($) {
'use strict';
// Document ready.
$(function () {
var eventTypes = '\\.(' +
'dmg|exe|' +
'rar|zip|' +
'mp3|' +
'avi|flv|mov|wav|wma|wmv|' +
'docx?|xlsx?|pptx?' +
')$',
viewTypes = '\\.(' +
'pdf|txt' +
')$',
regExes = {
download: new RegExp(eventTypes, 'i'),
email: /^mailto\:/i,
external: /^https?\:\/\//i,
hostName: new RegExp(location.host, 'i'),
pageview: new RegExp(viewTypes, 'i'),
phone: /^tel\:/i
},
baseHref = $('base').attr('href') || '',
// Helper functions
isDownload = function (url) {
return regExes.download.test(url);
},
isEmail = function (url) {
return regExes.email.test(url);
},
isExternal = function (url, rel) {
// External links have either been marked rel=external
// or must begin with https?
// and must not include current host name.
return ((rel || '').indexOf('external') !== -1) ||
(regExes.external.test(url) &&
!regExes.hostName.test(url));
},
isPageview = function (url) {
return regExes.pageview.test(url);
},
isPhone = function (url) {
return regExes.phone.test(url);
},
getFileName = function (url) {
var parts = url.split('/');
return parts[parts.length - 1];
},
getFileExt = function (fileName) {
var ext = (regExes.download.test(fileName)) ?
fileName.match(regExes.download)[1] :
fileName.match(regExes.pageview)[1];
return ext.toUpperCase();
},
setData = function (loc, category, action, label) {
loc = loc || '';
category = category || 'Link clicks';
action = action || '';
label = label || '';
return {
category: category,
action: action,
label: label,
loc: loc
};
};
$('body').on('click', 'a', function () {
var $el = $(this),
url = $el.attr('href') || '',
rel = $el.attr('rel') || '',
target = ($el.attr('target') || '').toLowerCase(),
fileName,
action = 'event',
data;
// External link.
if (isExternal(url, rel)) {
data = setData(url, null, 'External link', url);
// Download link.
} else if (isDownload(url)) {
fileName = getFileName(url);
data = setData(baseHref + url, 'Downloads',
getFileExt(fileName) + ' download', fileName);
// Virtual pageview link.
} else if (isPageview(url)) {
action = 'pageview';
fileName = getFileName(url);
data = setData(baseHref + url,
'/' + getFileExt(fileName) + '/' + fileName);
// Email link.
} else if (isEmail(url)) {
data = setData(url, null, 'Email link',
url.replace(regExes.email, ''));
// Telephone link.
} else if (isPhone(url)) {
data = setData(url, null, 'Telephone link',
url.replace(regExes.phone, ''));
// Internal link. Don't track.
} else {
return;
}
// Track it.
if (action === 'event') {
_gaq.push(['_trackEvent', data.category, data.action,
data.label]);
} else if (action === 'pageview') {
_gaq.push(['_trackPageview', data.category]);
}
// If link doesn't open in new window, give GA time to
// track it.
if ('_blank' !== target) {
setTimeout(function () {
location.href = data.loc;
}, 400);
return false;
}
});
});
}(jQuery));
}