﻿var urlTracker = {

  trackLinks: function() {
    var hrefs = document.getElementsByTagName('a'),
        urlHost = document.domain.replace(/^www\./i, ""),
        urlPattern = "^(?:https?:)?\/\/(?:(?:www)\.)?" + urlHost + "\/?";

    var a, href, total = 0;
    for (var i = 0; i < hrefs.length; i++) {
      a = hrefs[i], href = a.getAttribute('href');

      if (href && href.match(/^https?\:/i) && !href.match(urlPattern)) {
        Spif.DOMEvents.attach(a, "click", function() {
          pageTracker._trackEvent('Externe links', this.href);
        });
        if (window.console) console.log("Tracking clicks on " + a);
        total++;
      }
    }
    if (window.console) console.log("Tracking " + total + " links.");
  }

};
Spif.DOMEvents.attach(window, "load", urlTracker.trackLinks);
