﻿/* Analytics | Get more out of Google | A tutorial for .NET magazine by Public
* @author Tim Whitlock 
*/


// We're going to have multiple functions, so the GA tracker can be declared a global
var pageTracker;

/**
* The initializing function. This gets called as soon as GA is available in the HTML document
* @param string your GA tracking code
* @return void 
*/
function runGA(trackingCode) {
    try {
        // intialize the tracker and perform the default page track
        pageTracker = _gat._getTracker(trackingCode);
        pageTracker._trackPageview();
        // Now we'll kick start the external link tracking magic
        initializeExternalLinks();
    }
    // Always trap JavaScript errors: 
    // You can display these while developing, and silence them once you're live.
    catch (Er) {
        //alert(Er.message);
    }
}


/**
* This function adds click listeners to all external links on the page.
* We'll call it only when GA and all links on the page are available
* @return void
*/
function initializeExternalLinks() {
    // Grab all <a> elements from the DOM and check whether they are external to this site.
    var lnks = document.getElementsByTagName('a');
    var regLocal = new RegExp("^https?:\\/\\/" + window.location.hostname);
    var i, elLnk;
    for (i = 0; i < lnks.length; i++) {
        elLnk = lnks[i];
        // If a link is not a web address, or if it is on our current site we skip it.
        if (!/^https?:\/\//.test(elLnk.href) || regLocal.test(elLnk.href)) {
            continue;
        }
        // otherwise we'll use the browser-specific even listening method to give it an additional click action. 
        // This does not interfere with any other actions on the link.
        if (elLnk.attachEvent) {
            elLnk.attachEvent('onclick', onExternalClick);
        }
        else if (elLnk.addEventListener) {
            elLnk.addEventListener('click', onExternalClick, true);
        }
    }
}


/**
* Utility function to perform the old-school method of tracking external links using artificial content paths.
* @param HTMLAnchorElement the browser's native DOM object for a link.
* @return bool always true
*/
function trackExternal(elLnk) {
    // We'll use a Regular Expression to create a string like "/external/www.publicreative.com/"
    var path = elLnk.href.replace(/^https?:\/\//, '/external/');
    // If we failed, we won't track it, (it could also be a mailto: link)
    if (path && path.indexOf('/external/') === 0) {
        // hand off the artificial path to the GA tracker
        pageTracker._trackPageview(path);
    }
    return true;
}


/**
* Click handler, invoked by the browser
* @param Event internally passed event object
* @return bool always true
*/
function onExternalClick(Evt) {
    try {
        // A little bit of wizardry handles cross-browser nuances and obtains a reference to the clicked link.
        // There are many ways to achieve this, and you could also use a library like jQuery.
        var elLnk = Evt ? (Evt.target ? Evt.target : Evt.srcElement) : event.srcElement;
        // We'll hand off to the trackExternal function which uses the old-school method of tracking external links
        trackExternal(elLnk);
        // If you have the new GA event tracking function enabled on your account you should use _trackEvent directly instead:
        // Passing the links href attribute ensures the full external URL is logged as the event value.
        // pageTracker._trackEvent( 'links', 'external', elLnk.href );
    }
    catch (Er) {
        // you should comment out error alerts when you go live
        alert(Er.message);
    }
    return true;
}

function trackLink(elLnk) {
    var regLocal = new RegExp("^https?:\\/\/" + window.location.hostname);
    var path = elLnk.href.replace(regLocal, '');
    if (path && path.indexOf('/') === 0) {
        pageTracker._trackPageview(path);
    }
    
}