1/** 2 * Google Preview 3 * 4 * @author Ryan Chase 5 * @version 1.0 6 * @url http://www.blastam.com/blog/index.php/2013/03/how-to-track-downloads-in-google-analytics-v2 7 */ 8document.addEventListener('DOMContentLoaded', function(event) { 9 var filetypes = /\.(zip|exe|dmg|pdf|doc.*|xls.*|ppt.*|mp3|txt|rar|wma|mov|avi|wmv|flv|wav)$/i; 10 11 document.querySelector('a').addEventListener('click', function(event) { 12 var track = true, 13 href = this.href, 14 isThisDomain = href.match(document.domain.split('.').reverse()[1] + '.' + document.domain.split('.').reverse()[0]); 15 16 if(!href.match(/^javascript:/i)) { 17 var elEv = []; 18 elEv.value = 0, elEv.non_i = false; 19 if(href.match(/^mailto\:/i)) { 20 elEv.category = "email"; 21 elEv.action = "click"; 22 elEv.label = href.replace(/^mailto\:/i, ''); 23 elEv.loc = href; 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 = href; 30 } else if(href.match(/^https?\:/i) && !isThisDomain) { 31 elEv.category = "external"; 32 elEv.action = "click"; 33 elEv.label = href.replace(/^https?\:\/\//i, ''); 34 elEv.non_i = true; 35 elEv.loc = href; 36 } else if(href.match(/^tel\:/i)) { 37 elEv.category = "telephone"; 38 elEv.action = "click"; 39 elEv.label = href.replace(/^tel\:/i, ''); 40 elEv.loc = href; 41 } else track = false; 42 43 if(track) { 44 ga('send', 'event', elEv.category.toLowerCase(), elEv.action.toLowerCase(), elEv.label.toLowerCase(), elEv.value, {'nonInteraction': elEv.non_i}); 45 } 46 } 47 }); 48});
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.