//	This javascript tags file downloads and external links in Google Analytics.

//	The script detects to see if you are opening the file in a new window or not and automatically uses setTimeout() for 200ms if you are not.  If you have multiple tracking objects on a page or have a named tracker, then you’ll want to customize this script.  This script uses the asynchronous ga.js tagging syntax.

//If you are using the automated tagging method, you'll want to have your GA tag above that script tag. If you are using the manual method, it is highly recommended to move your GA tag into the head of the html. Additionally, please ensure you are using the asynchronous GA tracking script. Placing it in the top of the source code is recommended for the async version.
							
//In the GA v5 (new version), you'll want to go to Reports > Content > Events > Top Events. You'll see your event category listed there and you drill down into the action and label to view the number of events recorded for that file.

if (typeof jQuery != 'undefined') {
    jQuery(document).ready(function($) {
        var filetypes = /\.(?:doc*|pdf|eps|jpg|png|svg|xls*|ppt*|pdf|xls|zip|txt|vsd|vxd|js|css|rar|exe|wma|mov|avi|wmv|mp3)$/i;
        var baseHref = '';
        if (jQuery('base').attr('href') != undefined)
            baseHref = jQuery('base').attr('href');
        jQuery('a').each(function() {
            var href = jQuery(this).attr('href');
            if (href && (href.match(/^https?\:/i)) && (!href.match(document.domain))) {
                jQuery(this).click(function() {
                    var extLink = href.replace(/^https?\:\/\//i, '');
                    _gaq.push(['_trackEvent', 'External', 'Click', extLink]);
                    if (jQuery(this).attr('target') != undefined && jQuery(this).attr('target').toLowerCase() != '_blank') {
                        setTimeout(function() { location.href = href; }, 200);
                        return false;
                    }
                });
            }
            else if (href && href.match(/^mailto\:/i)) {
                jQuery(this).click(function() {
                    var mailLink = href.replace(/^mailto\:/i, '');
                    _gaq.push(['_trackEvent', 'Email', 'Click', mailLink]);
                });
            }
            else if (href && href.match(filetypes)) {
                jQuery(this).click(function() {
                    var extension = (/[.]/.exec(href)) ? /[^.]+$/.exec(href) : undefined;
                    var filePath = href;
                    _gaq.push(['_trackEvent', 'Download', 'Click-' + extension, filePath]);
                    if (jQuery(this).attr('target') != undefined && jQuery(this).attr('target').toLowerCase() != '_blank') {
                        setTimeout(function() { location.href = baseHref + href; }, 200);
                        return false;
                    }
                });
            }
        });
    });
}
