﻿// Name: BleauGoogleAnalyticsFunctions.js
// You will need to add these lines in the HEAD of code for it to work
/* 
 <script type="text/javascript">
  // This object contains a Google Analytics UA for each Dynamicweb Area ID.
  var _gaq = _gaq || [];
  _gaq.push(['_setAccount', 'UA-8042213-1']); // Change to your own UA code
 </script>
 <script type="text/javascript" src="/Files/System/js/BleauGoogleAnalyticsFunctions.js"></script>
*/


// Use this JavaScript to send the value to Google Analytics: _gaq.push(['_setCustomVar', 1, 'Content', getDays('<!--@DwPageUpdateDate-->')]);
function getDays(sDDMMYYYY) {
  /// <summary>The following code is used when calculating the age of content</summary>
  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 if the site is using Dynamicweb 404-pages
var url = location.pathname;
// STH: Removed as it might trigger on wrong instances if (url.indexOf("404") != -1) {
if (document.title.indexOf('The page does not exist')>-1) {
    _gaq.push(['_trackPageview', '/404.html?page=' + document.location.pathname + document.location.search + '&from=' + document.referrer]);
} else {
    // STH: 2012-01-04 Added check if page is searchresult page, in such case Google Site Search has other syntax for the "_trackPageview"
    var sUrl = location.toString();
    var isSearchResultPage = sUrl.match(new RegExp('\\?q=|\&q=', 'ig'))!==null;
    if (!isSearchResultPage) {
      if (typeof console!='undefined') console.log('This is NOT a search');
      _gaq.push(['_trackPageview']);
    } else {
      // STH: This page is a search result page 
      // Check if it uses Google Site Search
      if (typeof aUsesGCS[sCurrentAreaId]!=='undefined') {
         if (aUsesGCS[sCurrentAreaId]) {
          if (typeof console!='undefined') console.log('This is a search AND we are using GCS');
        } else {
          if (typeof console!='undefined') console.log('This is a search BUT we are NOT using GCS');
           _gaq.push(['_trackPageview']);
        }
      } else {
          if (typeof console!='undefined') console.log('This is a search BUT we are NOT using GCS');
          _gaq.push(['_trackPageview']);
      }
    }
}
_gaq.push(['_trackPageLoadTime']);

jQuery(document).ready(function () {
    // Handle form abandonment rate
    jQuery(':input').blur(function () {
        if(jQuery(this).val().length > 0){
            _gaq.push(['_trackEvent', 'Form', location.pathname + '/' + jQuery(this).parents('form').attr('name'), jQuery(this).attr('name')]);
        } 
    });

    // Handle file downloads
    jQuery('a[href*="Files"]').click(function (data) {
        //_gaq.push(['_trackPageview', this.href]);
        _gaq.push(['_trackEvent', 'Download', this.href, location.pathname]);
    });

    // Handle login form submit
    jQuery('#ExtUserFormPage').submit(function (data) {
        _gaq.push(['_trackEvent', 'Form', 'ExtUserFormPage', 'User_logged_in']);
    });

    // Handle mailto: clicks
    jQuery('a[href*="mailto:"]').click(function (data) {
        _gaq.push(['_trackEvent', 'Email', location.pathname, this.href]);
    });

    // Handle external links
    jQuery("a[href^='http']").not(jQuery('a[href*="' + document.domain + '"]')).click(function () {
        _gaq.push(['_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);
})();


  function _trackQuery(control, searcher, query) {
    var loc = document.location;
    var url = [
      loc.pathname,
      loc.search,
      loc.search ? '&' : '?',
      encodeURIComponent('q'),
      '=',
      encodeURIComponent(query)
    ];
    _gaq.push(["_trackPageview", url.join('')]);
  }
