﻿/**********************************************************
Author:
Adam Barry
Klestrup | partners
www.klestrup-partners.dk

Date: February 20 2008

© 2008 Adam Barry, all rights reserved
-----------------------------------------------------------

Name:
Google Analytics track links script

-----------------------------------------------------------
Inspiration
http://www.terenzani.it/54/urchintrack-utility-tracciare-link-esterni-e-download-con-google-analytics
GA urchin.trackLinks 0.2

-----------------------------------------------------------
Description:
Function that enables Google Analytics tracking of
downloaded files - when placed in an <a> structure.

NB. This version is not compatible with the old Google
Analytics/Urchin tracking code.

-----------------------------------------------------------
Usage:
Simply place a link to the this script in the head-section
of the XHTML page. The script will then automatically
execute on page load.

<script type="text/javascript" src="GA.trackLinks.js"></script>

-----------------------------------------------------------
Example:
<script type="text/javascript" src="GA.trackLinks.js"></script>

<a href="filename.pdf">Download tracked PDF file</a>

-----------------------------------------------------------
Dependencies:
This script depends on the windowOnLoad-script to execute

**********************************************************/

function urchin() {
    // It contains the download extension separated by a "|"
    this.trackDownload = '';

    this.trackLinks = function() {
        var a = document.getElementsByTagName('a');

        // Extract the domain from the location (the domain is in domain[2])
        var domain = /^(http|https):\/\/([a-z-.0-9]+)[\/]{0,1}/i.exec(window.location);
        if (!domain) return;

        // Expression that checks for internal links
        var internalLink = new RegExp("^(http|https):\/\/" + domain[2], "i");

        // Expression that checks for downloads
        var isDownload = new RegExp("(" + this.trackDownload + ")$", "i");

        // For each anchor
        for (var i = 0; i < a.length; i++) {
            // If href points to an internal resource
            if (internalLink.test(a[i].href)) {
                // ... and if the file extension is in this.trackDownload ...
                if (this.trackDownload && isDownload.test(a[i].href))
                    a[i].onclick = function() {
                        // Clean and track the URL
                        pageTracker._trackPageview('/download/' + this.href.replace(/^(http|https):\/\/([a-z-.0-9]+)\//i, '').split('/').join('--'));
                    }
            }

            else
            // is external
                a[i].onclick = function() {
                    // Clean and track the URL
                    pageTracker._trackPageview('/outgoing/' + this.href.replace(/^http:\/\/|https:\/\//i, '').split('/').join('--'));
                }
        }
    }
}

function initDownloadTracker() {
    urchin = new urchin();
    urchin.trackDownload = "zip|rar|pdf|exe|gz|dmg|avi|jpg|gif|png|doc|dot|ashx";
    urchin.trackLinks();
};  
addLoadEvent(function() { initDownloadTracker(); });
