1 2/** 3 * Google Preview 4 * 5 * @author Ryan Chase 6 * @version 1.0 7 * @url http://www.blastam.com/blog/index.php/2013/03/how-to-track-downloads-in-google-analytics-v2 8 */ 9document.addEventListener('DOMContentLoaded', function(event) { 10 var filetypes = /\.(zip|exe|dmg|pdf|doc.*|xls.*|ppt.*|mp3|txt|rar|wma|mov|avi|wmv|flv|wav)$/i; 11 12 document.querySelector('a').addEventListener('click', function(event) { 13 var track = true, 14 href = this.href, 15 isThisDomain = href.match(document.domain.split('.').reverse()[1] + '.' + document.domain.split('.').reverse()[0]); 16 17 if(!href.match(/^javascript:/i)) { 18 var elEv = []; 19 elEv.value = 0, elEv.non_i = false; 20 if(href.match(/^mailto\:/i)) { 21 elEv.category = "email"; 22 elEv.action = "click"; 23 elEv.label = href.replace(/^mailto\:/i, ''); 24 elEv.loc = href; 25 } else if(href.match(filetypes)) { 26 var extension = (/[.]/.exec(href)) ? /[^.]+$/.exec(href) : undefined; 27 elEv.category = "download"; 28 elEv.action = "click-" + extension[0]; 29 elEv.label = href.replace(/ /g, "-"); 30 elEv.loc = href; 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 } else if(href.match(/^tel\:/i)) { 38 elEv.category = "telephone"; 39 elEv.action = "click"; 40 elEv.label = href.replace(/^tel\:/i, ''); 41 elEv.loc = href; 42 } else track = false; 43 44 if(track) { 45 ga('send', 'event', elEv.category.toLowerCase(), elEv.action.toLowerCase(), elEv.label.toLowerCase(), elEv.value, {'nonInteraction': elEv.non_i}); 46 } 47 } 48 }); 49});
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.