﻿//jQuery based GA, with an add-on from http://permanentinkdesign.com/articles/google-analytics-jquery-and-file-downloads/ to auto-tag outbound links and downloads
// with a few changes to fix URLs - pmd 20090224

$(document).ready(function() {
    var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www.");
    $.getScript(gaJsHost + "google-analytics.com/ga.js", function() {
        // This code needs to be changed to the code Google Analytics provides for your account
        try {
            var pageTracker = _gat._getTracker("UA-6257137-1");
            pageTracker._trackPageview();
        } catch (err) { }

        // The slashes are to ensure the period is in the url, the $ is to make sure it is the end of the url, the i is to make it case insensitive.
        filetypes = /\.doc$|\.xls$|\.exe$|\.zip$|\.pdf$/i;
        $("a").live("click", function() {
            // Track mailto links
            if ($(this).attr("href").match(/^mailto\:/i)) {
                var url = $(this).attr("href").replace(/^mailto\:/i, "");
                pageTracker._trackPageview("/mailto/" + url);
                //alert("/mailto/" + url);
            }
            // Track external links
            else if (location.host != this.host.replace(/\:80$/i, "")) {
                var url = $(this).attr("href").replace(/^http\:\/\/(www\.)*/i, "");
                pageTracker._trackPageview("/outgoing/" + url)
                //alert("/outgoing/" + url);
            }
            // Track downloads (links with a given extension)
            else if ($(this).attr("href").match(filetypes)) {
                // The URL needs to be changed for each site this is applied to.
                var host = location.host.replace("/\./g", "\\.");
                var pattern = new RegExp('^(http\:\/\/)*(www\.)*(' + host + ')*\/', 'i');
                var url = $(this).attr("href").replace(pattern, "").replace("../", "");
                pageTracker._trackPageview("/downloads/" + url);
                //alert("/downloads/" + url);
            }
        });
    });
});
