1 | /**
|
---|
2 | from http://www.blastam.com/blog/index.php/2013/03/how-to-track-downloads-in-google-analytics-v2
|
---|
3 | dhh 6aug2015 add GA download tracking
|
---|
4 | **/
|
---|
5 | if (typeof jQuery != 'undefined') {
|
---|
6 | jQuery(document).ready(function($) {
|
---|
7 | var filetypes = /\.(zip|gz|csv|pdf|doc.*|xls.*|ppt.*|txt)$/i;
|
---|
8 | var baseHref = '';
|
---|
9 | if (jQuery('base').attr('href') != undefined) baseHref = jQuery('base').attr('href');
|
---|
10 |
|
---|
11 | jQuery('a').on('click', function(event) {
|
---|
12 | var el = jQuery(this);
|
---|
13 | var track = true;
|
---|
14 | var href = (typeof(el.attr('href')) != 'undefined' ) ? el.attr('href') :"";
|
---|
15 | var isThisDomain = href.match(document.domain.split('.').reverse()[1] + '.' + document.domain.split('.').reverse()[0]);
|
---|
16 | if (!href.match(/^javascript:/i)) {
|
---|
17 | var elEv = []; elEv.value=0, elEv.non_i=false;
|
---|
18 | if (href.match(/^mailto\:/i)) {
|
---|
19 | elEv.category = "email";
|
---|
20 | elEv.action = "click";
|
---|
21 | elEv.label = href.replace(/^mailto\:/i, '');
|
---|
22 | elEv.loc = href;
|
---|
23 | }
|
---|
24 | else if (href.match(filetypes)) {
|
---|
25 | var extension = (/[.]/.exec(href)) ? /[^.]+$/.exec(href) : undefined;
|
---|
26 | elEv.category = "download";
|
---|
27 | elEv.action = "click-" + extension[0];
|
---|
28 | elEv.label = href.replace(/ /g,"-");
|
---|
29 | elEv.loc = baseHref + href;
|
---|
30 | }
|
---|
31 | else if (href.match(/^https?\:/i) && !isThisDomain) {
|
---|
32 | elEv.category = "external";
|
---|
33 | elEv.action = "click";
|
---|
34 | elEv.label = href.replace(/^https?\:\/\//i, '');
|
---|
35 | elEv.non_i = true;
|
---|
36 | elEv.loc = href;
|
---|
37 | }
|
---|
38 | else track = false;
|
---|
39 |
|
---|
40 | if (track) {
|
---|
41 | ga('send', 'event', elEv.category.toLowerCase(), elEv.action.toLowerCase(), elEv.label.toLowerCase(), 1);
|
---|
42 | // _gaq.push(['_trackEvent', elEv.category.toLowerCase(), elEv.action.toLowerCase(), elEv.label.toLowerCase(), elEv.value, elEv.non_i]);
|
---|
43 | /*
|
---|
44 | // dhh 6Aug2015 turn off popup window tracking
|
---|
45 | if ( el.attr('target') == undefined || el.attr('target').toLowerCase() != '_blank') {
|
---|
46 | setTimeout(function() { location.href = elEv.loc; }, 400);
|
---|
47 | return false;
|
---|
48 | }
|
---|
49 | */
|
---|
50 | }
|
---|
51 | }
|
---|
52 | });
|
---|
53 | });
|
---|
54 | }
|
---|