Commit b64bce0d authored by hass's avatar hass
Browse files

Integrate with Drupal.* javascript object and change all regexes into

functions.
parent 61a45ec5
/**
* This file is for developers only.
*
* This tests are made for the javascript functions used in GA module.
* These tests verify if the return values are properly working.
*
* Hopefully this can be added somewhere else once Drupal core has JavaScript
* unit testing integrated.
*/
// JavaScript debugging
var base_url = window.location.protocol + '//' + window.location.host;
console.dir(Drupal);
console.group("Test 'isDownload':");
console.log("Check if '/node/8' url is a file download. Expected: false, Result: %s.", Drupal.googleanalytics.isDownload(base_url + '/node/8'));
console.log("Check if '/files/foo1.zip' url is a file download. Expected: true, Result: %s.", Drupal.googleanalytics.isDownload(base_url + '/files/foo1.zip'));
console.log("Check if '/files/foo2.ddd' url is a file download. Expected: false, Result: %s.", Drupal.googleanalytics.isDownload(base_url + '/files/foo2.ddd'));
console.groupEnd();
console.group("Test 'isInternal':");
console.log("Check if base_url + '/node/1#foo=bar' url is internal. Expected: true, Result: %s.", Drupal.googleanalytics.isInternal(base_url + '/node/1#foo=bar'));
console.log("Check if base_url + '/node/2' url is internal. Expected: true, Result: %s.", Drupal.googleanalytics.isInternal(base_url + '/node/2'));
console.log("Check if base_url + '/go/foo' url is internal. Expected: true, Result: %s.", Drupal.googleanalytics.isInternal(base_url + '/go/foo'));
console.log("Check if 'http://example.com/node/3' url is internal. Expected: false, Result: %s.", Drupal.googleanalytics.isInternal('http://example.com/node/3'));
console.groupEnd();
console.group("Test 'isInternalSpecial':");
console.log("Check if base_url + '/go/foo' url is internal special. Expected: true, Result: %s.", Drupal.googleanalytics.isInternalSpecial(base_url + '/go/foo'));
console.log("Check if base_url + '/node/1' url is internal special. Expected: false, Result: %s.", Drupal.googleanalytics.isInternalSpecial(base_url + '/node/1'));
console.groupEnd();
console.group("Test 'getInternalUrl':");
console.log("Get absolute internal url from full qualified url. Expected: '/node/1', Result: '%s'.", Drupal.googleanalytics.getInternalUrl(base_url + '/node/1'));
console.log("Get absolute internal url from absolute url. Expected: '/node/1', Result: '%s'.", Drupal.googleanalytics.getInternalUrl('/node/1'));
console.log("Get full qualified external url. Expected: 'http://example.com/node/2', Result: '%s'.", Drupal.googleanalytics.getInternalUrl('http://example.com/node/2'));
console.groupEnd();
console.group("Test 'getDownloadExtension':");
console.log("Get extension of download filename. Expected: 'zip', Result: '%s'.", Drupal.googleanalytics.getDownloadExtension(base_url + '/files/foo1.zip'));
console.log("Get empty extension if not a download extension. Expected: '', Result: '%s'.", Drupal.googleanalytics.getDownloadExtension(base_url + '/files/foo2.dddd'));
console.groupEnd();
// List of top-level domains: example.com, example.net
console.group("Test 'isCrossDomain' (requires cross domain configuration):");
console.dir(Drupal.settings.google_analytics.trackCrossDomains);
console.log("Check if url is in cross domain list. Expected: true, Result: %s.", Drupal.googleanalytics.isCrossDomain('example.com', Drupal.settings.google_analytics.trackCrossDomains));
console.log("Check if url is in cross domain list. Expected: true, Result: %s.", Drupal.googleanalytics.isCrossDomain('example.net', Drupal.settings.google_analytics.trackCrossDomains));
console.log("Check if url is in cross domain list. Expected: false, Result: %s.", Drupal.googleanalytics.isCrossDomain('www.example.com', Drupal.settings.google_analytics.trackCrossDomains));
console.log("Check if url is in cross domain list. Expected: false, Result: %s.", Drupal.googleanalytics.isCrossDomain('www.example.net', Drupal.settings.google_analytics.trackCrossDomains));
console.groupEnd();
(function ($) {
$(document).ready(function() {
Drupal.googleanalytics = {};
// Expression to check for absolute internal links.
var isInternal = new RegExp("^(https?):\/\/" + window.location.host, "i");
$(document).ready(function() {
// Attach onclick event to document only and catch clicks on all elements.
$(document.body).click(function(event) {
// Catch the closest surrounding link of a clicked element.
$(event.target).closest("a,area").each(function() {
// Expression to check for special links like gotwo.module /go/* links.
var isInternalSpecial = new RegExp("(\/go\/.*)$", "i");
// Expression to check for download links.
var isDownload = new RegExp("\\.(" + Drupal.settings.googleanalytics.trackDownloadExtensions + ")$", "i");
// Is the clicked URL internal?
if (isInternal.test(this.href)) {
if (Drupal.googleanalytics.isInternal(this.href)) {
// Skip 'click' tracking, if custom tracking events are bound.
if ($(this).is('.colorbox')) {
// Do nothing here. The custom event will handle all tracking.
//console.debug("Detected click on colorbox.");
}
// Is download tracking activated and the file extension configured for download tracking?
else if (Drupal.settings.googleanalytics.trackDownload && isDownload.test(this.href)) {
else if (Drupal.settings.googleanalytics.trackDownload && Drupal.googleanalytics.isDownload(this.href)) {
// Download link clicked.
var extension = isDownload.exec(this.href);
ga("send", "event", "Downloads", extension[1].toUpperCase(), this.href.replace(isInternal, ''));
ga("send", "event", "Downloads", Drupal.googleanalytics.getDownloadExtension(this.href).toUpperCase(), Drupal.googleanalytics.getInternalUrl(this.href));
}
else if (isInternalSpecial.test(this.href)) {
else if (Drupal.googleanalytics.isInternalSpecial(this.href)) {
// Keep the internal URL for Google Analytics website overlay intact.
ga("send", "pageview", { page: this.href.replace(isInternal, '')});
ga("send", "pageview", { page: Drupal.googleanalytics.getInternalUrl(this.href) });
}
}
else {
......@@ -38,9 +33,10 @@ $(document).ready(function() {
ga("send", "event", "Mails", "Click", this.href.substring(7));
}
else if (Drupal.settings.googleanalytics.trackOutbound && this.href.match(/^\w+:\/\//i)) {
if (Drupal.settings.googleanalytics.trackDomainMode == 2 && isCrossDomain(this.hostname, Drupal.settings.googleanalytics.trackCrossDomains)) {
if (Drupal.settings.googleanalytics.trackDomainMode == 2 && Drupal.googleanalytics.isCrossDomain(this.hostname, Drupal.settings.googleanalytics.trackCrossDomains)) {
// Top-level cross domain clicked. document.location is handled by _link internally.
event.preventDefault();
//console.debug("Detected click to cross domain url '%s'.", this.href);
// @todo: unknown upgrade path
//_gaq.push(["_link", this.href]);
//ga("link", this.href); ???
......@@ -56,10 +52,10 @@ $(document).ready(function() {
// Colorbox: This event triggers when the transition has completed and the
// newly loaded content has been revealed.
$(document).bind("cbox_complete", function() {
$(document).bind("cbox_complete", function () {
var href = $.colorbox.element().attr("href");
if (href) {
ga("send", "pageview", { page: href.replace(isInternal, '') });
ga("send", "pageview", { page: Drupal.googleanalytics.getInternalUrl(href) });
}
});
......@@ -75,7 +71,7 @@ $(document).ready(function() {
*
* @return boolean
*/
function isCrossDomain(hostname, crossDomains) {
Drupal.googleanalytics.isCrossDomain = function (hostname, crossDomains) {
/**
* jQuery < 1.6.3 bug: $.inArray crushes IE6 and Chrome if second argument is
* `null` or `undefined`, http://bugs.jquery.com/ticket/10076,
......@@ -91,4 +87,75 @@ function isCrossDomain(hostname, crossDomains) {
}
}
/**
* Check whether this is a download URL or not.
*
* @param string url
* The web url to check.
*
* @return boolean
*/
Drupal.googleanalytics.isDownload = function (url) {
var isDownload = new RegExp("\\.(" + Drupal.settings.googleanalytics.trackDownloadExtensions + ")$", "i");
return isDownload.test(url);
}
/**
* Check whether this is an absolute internal URL or not.
*
* @param string url
* The web url to check.
*
* @return boolean
*/
Drupal.googleanalytics.isInternal = function (url) {
var isInternal = new RegExp("^(https?):\/\/" + window.location.host, "i");
return isInternal.test(url);
}
/**
* Check whether this is a special URL or not.
*
* URL types:
* - gotwo.module /go/* links.
*
* @param string url
* The web url to check.
*
* @return boolean
*/
Drupal.googleanalytics.isInternalSpecial = function (url) {
var isInternalSpecial = new RegExp("(\/go\/.*)$", "i");
return isInternalSpecial.test(url);
}
/**
* Extract the relative internal URL from an absolute internal URL.
*
* @param string url
* The web url to check.
*
* @return string
* Internal website URL
*/
Drupal.googleanalytics.getInternalUrl = function (url) {
var extractInternalUrl = new RegExp("^(https?):\/\/" + window.location.host, "i");
return url.replace(extractInternalUrl, '');
}
/**
* Extract the download file extension from the URL.
*
* @param string url
* The web url to check.
*
* @return string
* The file extension of the passed url. e.g. "zip", "txt"
*/
Drupal.googleanalytics.getDownloadExtension = function (url) {
var extractDownloadextension = new RegExp("\\.(" + Drupal.settings.googleanalytics.trackDownloadExtensions + ")$", "i");
var extension = extractDownloadextension.exec(url);
return (extension === null) ? '' : extension[1];
}
})(jQuery);
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment