1/* 2 * Google Analytics tracking 3 * 4 * Tracks: 5 * - mailto-links 6 * - external links 7 * - PDF and JPG links 8 * - links with class trackbtn and optionally data-title="Title to show in results" 9 * 10 * @author Jonas De Smet 11 * 12 * requires jQuery and Google Analytics async tracking 13 */ 14$(function() { 15 16 var external = []; 17 var mails = []; 18 var files = []; 19 var trackbtns = []; 20 var filetypes = ['pdf','jpg']; 21 var links = $('a'); 22 var l_length = links.length; 23 for (i=0;i<l_length;i++) 24 { 25 var $elem = $(links[i]); 26 var href = $elem.attr('href'); 27 28 if (href !== undefined) 29 { 30 if ((href.match(/^https?\:/i)) && (!href.match(document.domain))) 31 { 32 external.push(links[i]); 33 } 34 else if (href.match(/^mailto\:/i)) 35 { 36 mails.push(links[i]); 37 } 38 else if ($.inArray(href.split('.')[href.split('.').length - 1], filetypes) >= 0) 39 { 40 files.push(links[i]); 41 } 42 else if($elem.hasClass('tracking')) 43 { 44 trackbtns.push(links[i]); 45 } 46 } 47 } 48 49 $(external).on('click', function(){ 50 var link = $(this); 51 var linktxt = (link.attr('title') !== '') ? link.attr('title') + ' [' + link.attr('href').replace(/^https?\:\/\//i, '') + ']' : link.attr('href').replace(/^https?\:\/\//i, ''); 52 ga('send', 'event', 'external', 'visit', linktxt); 53 }); 54 55 $(mails).on('click', function(){ 56 var email = $(this); 57 var emailtxt = email.attr('href').substring(7); 58 ga('send', 'event', 'mailto', 'click', emailtxt); 59 }); 60 61 $(files).on('click', function(){ 62 var file = $(this); 63 var filehref = file.attr('href'); 64 var fileext = filehref.split('.')[filehref.split('.').length - 1]; 65 var filename = filehref.substring(filehref.lastIndexOf('/')+1); 66 ga('send', 'event', 'download', fileext, filename); 67 }); 68 69 $(trackbtns).on('click', function(){ 70 var link = $(this); 71 var txt = (link.data('title') !== 'undefined') ? link.data('title') : link.attr('href').replace(/^https?\:\/\//i, ''); 72 ga('send', 'event', 'button', 'click', txt); 73 }); 74 75});
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.