﻿// The following code is used when calculating the age of content
// Use this JavaScript to send the value to Google Analytics: _gaq.push(['_setCustomVar', 1, 'Content', getDays('<!--@DwPageUpdateDate-->')]);
function getDays(sDDMMYYYY) {
    var aDate = sDDMMYYYY.split('-')
    var ms = new Date() - new Date(aDate[2], aDate[1] - 1, aDate[0]);
    var iDay = 1000 * 60 * 60 * 24;
    return parseInt(ms / iDay)
}

// The following code is used for cross-domain tracking
_gaq.push(['t2._setAccount', 'UA-20452145-1']);
_gaq.push(['t2._setDomainName', 'none']);
_gaq.push(['t2._setAllowLinker', true]);

// The following code is used if the site is using Dynamicweb 404-pages
var url = location.pathname;
if (url.indexOf("404") != -1) {
    _gaq.push(['_trackPageview', '/404.html?page=' + document.location.pathname + document.location.search + '&from=' + document.referrer]);
    _gaq.push(['t2._trackPageview', '/404.html?page=' + document.location.pathname + document.location.search + '&from=' + document.referrer]);
} else {
    _gaq.push(['_trackPageview']);
    _gaq.push(['t2._trackPageview']);
}

$(document).ready(function () {
    // Handle form abandonment rate
    $(':input').blur(function () {
        if($(this).val().length > 0){
            _gaq.push(['_trackEvent', 'Form', location.pathname + '/' + $(this).parents('form').attr('name'), $(this).attr('name')]);
            _gaq.push(['t2._trackEvent', 'Form', location.pathname + '/' + $(this).parents('form').attr('name'), $(this).attr('name')]);
        } 
    });

    // Handle file downloads
    $('a[href*="Files"]').click(function (data) {
        //_gaq.push(['_trackPageview', this.href]);
        _gaq.push(['_trackEvent', 'Download', this.href, location.pathname]);
        _gaq.push(['t2._trackEvent', 'Download', this.href, location.pathname]);
    });

    // Handle login form submit
    $('#ExtUserFormPage').submit(function (data) {
        _gaq.push(['_trackEvent', 'Form', 'ExtUserFormPage', 'User_logged_in']);
        _gaq.push(['t2._trackEvent', 'Form', 'ExtUserFormPage', 'User_logged_in']);
    });

    // Handle mailto: clicks
    $('a[href*="mailto:"]').click(function (data) {
        _gaq.push(['_trackEvent', 'Email', location.pathname, this.href]);
        _gaq.push(['t2._trackEvent', 'Email', location.pathname, this.href]);
    });

    // Handle external links
    $("a[href^='http']").not($('a[href*="' + document.domain + '"]')).click(function () {
        _gaq.push(['_trackEvent', 'udgående', "'" + this.href + "'", "'" + location.pathname + "'"]);
        _gaq.push(['t2._trackEvent', 'udgående', "'" + this.href + "'", "'" + location.pathname + "'"]);
    });
});

// Insert GA-code
  (function() {
    var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
    ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
    var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
  })();
