Tuesday, 6 September 2011

A guide: How to Track Downloads and all other Non-open Pages





Using the javascript we can track the following file extensions:
doc, eps, jpg, png, svg, xls, ppt, pdf, xls, zip, txt, vsd, vxd, js, css, rar, exe, wma, mov, avi, wmv, mp3




 How do you install the tracking JavaScript?




 Step 1: Upload the attached file to our website
(Add it to this folder: http://www.yoursite.com/scripts/ )




 Step 2: Add the following JavaScript code just before your Google Analytics tracking code:
<script src="http://www.yoursite.com/scripts/gatag.js" type="text/javascript"></script>



So everywhere we need to place this.The file will lok something like this mentioned hereunder:




 The Code




if (document.getElementsByTagName) {
        // Initialize external link handlers
        var hrefs = document.getElementsByTagName("a");
        for (var l = 0; l < hrefs.length; l++) {
    // try {} catch{} block added by erikvold VKI
   try{
                 //protocol, host, hostname, port, pathname, search, hash
                 if (hrefs[l].protocol == "mailto:") {
                         startListening(hrefs[l],"click",trackMailto);
                 } else if (hrefs[l].hostname == location.host) {
                         var path = hrefs[l].pathname + hrefs[l].search;
       var isDoc = path.match(/\.(?:doc|eps|jpg|png|svg|xls|ppt|pdf|xls|zip|txt|vsd|vxd|js|css|rar|exe|wma|mov|avi|wmv|mp3)($|\&|\?)/);
                         if (isDoc) {
                                 startListening(hrefs[l],"click",trackExternalLinks);
                         }
                 } else if (!hrefs[l].href.match(/^javascript:/)) {
                         startListening(hrefs[l],"click",trackExternalLinks);
                 }
   }
   catch(e){
     continue;
   }
        }
}

function startListening (obj,evnt,func) {
        if (obj.addEventListener) {
                obj.addEventListener(evnt,func,false);
        } else if (obj.attachEvent) {
                obj.attachEvent("on" + evnt,func);
        }
}

function trackMailto (evnt) {
        var href = (evnt.srcElement) ? evnt.srcElement.href : this.href;
        var mailto = "/mailto/" + href.substring(7);
        if (typeof(pageTracker) == "object") pageTracker._trackPageview(mailto);
}

function trackExternalLinks (evnt) {
        var e = (evnt.srcElement) ? evnt.srcElement : this;
        while (e.tagName != "A") {
                e = e.parentNode;
        }
        var lnk = (e.pathname.charAt(0) == "/") ? e.pathname : "/" + e.pathname;
        if (e.search && e.pathname.indexOf(e.search) == -1) lnk += e.search;
        if (e.hostname != location.host) lnk = "/external/" + e.hostname + lnk;
        if (typeof(pageTracker) == "object") pageTracker._trackPageview(lnk); 
}



 
Once you have uploaded the file , this is what you need to do next:



Implementation is really simple because you can just search through your entire site replacing your standard analytics code:



This is your current Analytics Code (just replace the current one with this)





< script type="text/javascript">
var gaJsHost = (("https:" == document.location.protocol) ? "https://
ssl." : "
http://www.");
[removed](unescape("< script src='" gaJsHost "google-
< /script >"));
< /script >
< script type="text/javascript">
var pageTracker = _gat._getTracker("UA-0000-1");
pageTracker._initData();
pageTracker._trackPageview();
< /script >






This is your New Analytics Code





< script type="text/javascript" src="http://www.yoursite.com/script/
< /script >
< script type="text/javascript">
var gaJsHost = (("https:" == document.location.protocol) ? "https://
ssl." : "
http://www.");
[removed](unescape("< script src='" gaJsHost "google-
< /script >"));
< /script >
< script type="text/javascript">
var pageTracker = _gat._getTracker("UA-0000-1");
pageTracker._initData();
pageTracker._trackPageview();
< /script > rch && e.pathname.indexOf(e.search) == -1) lnk += e.search;
        if (e.hostname != location.host) lnk = "/external/" + e.hostname + lnk;
        if (typeof(pageTracker) == "object") pageTracker._trackPageview(lnk);
}

No comments:

Post a Comment