From 4b9d178f9073fca9e27d3395370a74ff4331d244 Thu, 3 Oct 2013 12:50:59 +0200
From: hass <hass@85918.no-reply.drupal.org>
Date: Thu, 3 Oct 2013 12:50:39 +0200
Subject: [PATCH] Integrate with Drupal.* javascript object and change all regexes into functions.

diff --git a/google_analytics.js b/google_analytics.js
index 2449daf..10ee36a 100644
--- a/google_analytics.js
+++ b/google_analytics.js
@@ -2,34 +2,25 @@
 
 $(document).ready(function() {
 
-  // Expression to check for absolute internal links.
-  var Google_Analytics.isInternal = new RegExp("^(https?):\/\/" + window.location.host, "i");
-
   // 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 Google_Analytics.isInternalSpecial = new RegExp("(\/go\/.*)$", "i");
-      // Expression to check for download links.
-      var Google_Analytics.isDownload = new RegExp("\\.(" + drupalSettings.google_analytics.trackDownloadExtensions + ")$", "i");
-
       // Is the clicked URL internal?
-      if (Google_Analytics.isInternal.test(this.href)) {
+      if (Drupal.google_analytics.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.
         }
         // Is download tracking activated and the file extension configured for download tracking?
-        else if (drupalSettings.google_analytics.trackDownload && Google_Analytics.isDownload.test(this.href)) {
+        else if (drupalSettings.google_analytics.trackDownload && Drupal.google_analytics.isDownload(this.href)) {
           // Download link clicked.
-          var extension = Google_Analytics.isDownload.exec(this.href);
-          ga("send", "event", "Downloads", extension[1].toUpperCase(), this.href.replace(Google_Analytics.isInternal, ''));
+          ga("send", "event", "Downloads", Drupal.google_analytics.getDownloadExtension(this.href).toUpperCase(), Drupal.google_analytics.getInternalUrl(this.href));
         }
-        else if (Google_Analytics.isInternalSpecial.test(this.href)) {
+        else if (Drupal.google_analytics.isInternalSpecial(this.href)) {
           // Keep the internal URL for Google Analytics website overlay intact.
-          ga("send", "pageview", { page: this.href.replace(Google_Analytics.isInternal, '')});
+          ga("send", "pageview", { page: Drupal.google_analytics.getInternalUrl(this.href) });
         }
       }
       else {
@@ -38,7 +29,7 @@
           ga("send", "event", "Mails", "Click", this.href.substring(7));
         }
         else if (drupalSettings.google_analytics.trackOutbound && this.href.match(/^\w+:\/\//i)) {
-          if (drupalSettings.google_analytics.trackDomainMode == 2 && Google_Analytics.isCrossDomain(this.hostname, drupalSettings.google_analytics.trackCrossDomains)) {
+          if (drupalSettings.google_analytics.trackDomainMode == 2 && Drupal.google_analytics.isCrossDomain(this.hostname, drupalSettings.google_analytics.trackCrossDomains)) {
             // Top-level cross domain clicked. document.location is handled by _link internally.
             event.preventDefault();
             // @todo: unknown upgrade path
@@ -59,7 +50,7 @@
   $(document).bind("cbox_complete", function() {
     var href = $.colorbox.element().attr("href");
     if (href) {
-      ga("send", "pageview", { page: href.replace(Google_Analytics.isInternal, '') });
+      ga("send", "pageview", { page: Drupal.google_analytics.getInternalUrl(href) });
     }
   });
 
@@ -75,8 +66,79 @@
  *
  * @return boolean
  */
-function Google_Analytics.isCrossDomain(hostname, crossDomains) {
+Drupal.google_analytics.isCrossDomain = function(hostname, crossDomains) {
   return $.inArray(hostname, crossDomains) > -1 ? true : false;
 }
 
+/**
+ * Check whether this is a download URL or not.
+ *
+ * @param string url
+ *   The web url to check.
+ *
+ * @return boolean
+ */
+Drupal.google_analytics.isDownload = function(url) {
+  var isDownload = new RegExp("\\.(" + drupalSettings.google_analytics.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.google_analytics.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.google_analytics.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.google_analytics.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.google_analytics.getDownloadExtension = function(url) {
+  var extractDownloadextension = new RegExp("\\.(" + drupalSettings.google_analytics.trackDownloadExtensions + ")$", "i");
+  var extension = extractDownloadextension.exec(url);
+  return extension[1];
+}
+
 })(jQuery, Drupal, drupalSettings);
