Commit 14748892 authored by Riccardo Padovani's avatar Riccardo Padovani

Updated google_analytics to 7.x-2.0

parent b107c7ed
...@@ -15,7 +15,7 @@ Requirements ...@@ -15,7 +15,7 @@ Requirements
Installation Installation
============ ============
* Copy the 'googleanalytics' module directory in to your Drupal Copy the 'googleanalytics' module directory in to your Drupal
sites/all/modules directory as usual. sites/all/modules directory as usual.
...@@ -51,19 +51,22 @@ choice for "Add if the following PHP code returns TRUE." Sample PHP snippets ...@@ -51,19 +51,22 @@ choice for "Add if the following PHP code returns TRUE." Sample PHP snippets
that can be used in this textarea can be found on the handbook page that can be used in this textarea can be found on the handbook page
"Overview-approach to block visibility" at http://drupal.org/node/64135. "Overview-approach to block visibility" at http://drupal.org/node/64135.
Custom variables Custom dimensions and metrics
================= =============================
One example for custom variables tracking is the "User roles" tracking. Enter One example for custom dimensions tracking is the "User roles" tracking.
the below configuration data into the custom variables settings form under
admin/config/system/googleanalytics. 1. In the Google Analytics Management Interface you need to setup Dimension #1
with name e.g. "User roles". This step is required. Do not miss it, please.
2. Enter the below configuration data into the custom dimensions settings form
under admin/config/system/googleanalytics. You can also choose another index,
but keep it always in sync with the index used in step #1.
Slot: 1 Index: 1
Name: User roles Value: [current-user:role-names]
Value: [current-user:role-names]
Scope: Visitor
More details about Custom variables can be found in the Google API documentation at More details about custom dimensions and metrics can be found in the Google API
http://code.google.com/intl/en/apis/analytics/docs/tracking/gaTrackingCustomVariables.html documentation at https://developers.google.com/analytics/devguides/collection/analyticsjs/custom-dims-mets
Advanced Settings Advanced Settings
================= =================
...@@ -72,5 +75,5 @@ code textarea. These can be found on the official Google Analytics pages ...@@ -72,5 +75,5 @@ code textarea. These can be found on the official Google Analytics pages
and a few examples at http://drupal.org/node/248699. Support is not and a few examples at http://drupal.org/node/248699. Support is not
provided for any customisations you include. provided for any customisations you include.
To speed up page loading you may also cache the Analytics ga.js To speed up page loading you may also cache the Google Analytics "analytics.js"
file locally. file locally.
...@@ -91,7 +91,7 @@ Drupal.behaviors.trackingSettingsSummary = { ...@@ -91,7 +91,7 @@ Drupal.behaviors.trackingSettingsSummary = {
vals.push(Drupal.t('AdSense ads')); vals.push(Drupal.t('AdSense ads'));
} }
if ($('input#edit-googleanalytics-trackdoubleclick', context).is(':checked')) { if ($('input#edit-googleanalytics-trackdoubleclick', context).is(':checked')) {
vals.push(Drupal.t('DoubleClick data')); vals.push(Drupal.t('Display features'));
} }
if (!vals.length) { if (!vals.length) {
return Drupal.t('Not tracked'); return Drupal.t('Not tracked');
......
(function ($) {
Drupal.googleanalytics = {};
$(document).ready(function() {
// Attach mousedown, keyup, touchstart events to document only and catch
// clicks on all elements.
$(document.body).bind("mousedown keyup touchstart", function(event) {
console.group("Running Google Analytics for Drupal.");
console.info(event);
// Catch the closest surrounding link of a clicked element.
$(event.target).closest("a,area").each(function() {
console.info("Element '%o' has been detected. Link '%s' found.", this, this.href);
// Is the clicked URL internal?
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.info("Click on .colorbox item has been detected.");
}
// Is download tracking activated and the file extension configured for download tracking?
else if (Drupal.settings.googleanalytics.trackDownload && Drupal.googleanalytics.isDownload(this.href)) {
// Download link clicked.
console.info("Download url '%s' has been found. Tracked download as extension '%s'.", Drupal.googleanalytics.getPageUrl(this.href), Drupal.googleanalytics.getDownloadExtension(this.href).toUpperCase());
ga("send", "event", "Downloads", Drupal.googleanalytics.getDownloadExtension(this.href).toUpperCase(), Drupal.googleanalytics.getPageUrl(this.href));
}
else if (Drupal.googleanalytics.isInternalSpecial(this.href)) {
// Keep the internal URL for Google Analytics website overlay intact.
console.info("Click on internal special link '%s' has been tracked.", Drupal.googleanalytics.getPageUrl(this.href));
ga("send", "pageview", { page: Drupal.googleanalytics.getPageUrl(this.href) });
}
else {
// e.g. anchor in same page or other internal page link
console.info("Click on internal link '%s' detected, but not tracked by click.", this.href);
}
}
else {
if (Drupal.settings.googleanalytics.trackMailto && $(this).is("a[href^='mailto:'],area[href^='mailto:']")) {
// Mailto link clicked.
console.info("Click on e-mail '%s' has been tracked.", this.href.substring(7));
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 && !Drupal.googleanalytics.isCrossDomain(this.hostname, Drupal.settings.googleanalytics.trackCrossDomains)) {
// External link clicked / No top-level cross domain clicked.
console.info("Outbound link '%s' has been tracked.", this.href);
ga("send", "event", "Outbound links", "Click", this.href);
}
else {
console.info("Internal link '%s' clicked, not tracked.", this.href);
}
}
}
});
console.groupEnd();
});
// Track hash changes as unique pageviews, if this option has been enabled.
if (Drupal.settings.googleanalytics.trackUrlFragments) {
window.onhashchange = function() {
console.info("Track URL '%s' as pageview. Hash '%s' has changed.", location.pathname + location.search + location.hash, location.hash);
ga('send', 'pageview', location.pathname + location.search + location.hash);
}
}
// Colorbox: This event triggers when the transition has completed and the
// newly loaded content has been revealed.
$(document).bind("cbox_complete", function () {
var href = $.colorbox.element().attr("href");
if (href) {
console.info("Colorbox transition to url '%s' has been tracked.", Drupal.googleanalytics.getPageUrl(href));
ga("send", "pageview", { page: Drupal.googleanalytics.getPageUrl(href) });
}
});
});
/**
* Check whether the hostname is part of the cross domains or not.
*
* @param string hostname
* The hostname of the clicked URL.
* @param array crossDomains
* All cross domain hostnames as JS array.
*
* @return boolean
*/
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,
* https://github.com/jquery/jquery/commit/a839af034db2bd934e4d4fa6758a3fed8de74174
*
* @todo: Remove/Refactor in D8
*/
if (!crossDomains) {
return false;
}
else {
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.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.
*
* Examples:
* - http://mydomain.com/node/1 -> /node/1
* - http://example.com/foo/bar -> http://example.com/foo/bar
*
* @param string url
* The web url to check.
*
* @return string
* Internal website URL
*/
Drupal.googleanalytics.getPageUrl = 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);
...@@ -5,9 +5,9 @@ package = Statistics ...@@ -5,9 +5,9 @@ package = Statistics
configure = admin/config/system/googleanalytics configure = admin/config/system/googleanalytics
files[] = googleanalytics.test files[] = googleanalytics.test
test_dependencies[] = token test_dependencies[] = token
; Information added by drupal.org packaging script on 2013-10-17 ; Information added by Drupal.org packaging script on 2014-07-01
version = "7.x-1.4" version = "7.x-2.0"
core = "7.x" core = "7.x"
project = "google_analytics" project = "google_analytics"
datestamp = "1382021586" datestamp = "1404257628"
...@@ -5,33 +5,21 @@ ...@@ -5,33 +5,21 @@
* Installation file for Google Analytics module. * Installation file for Google Analytics module.
*/ */
/**
* Implements hook_install().
*/
function googleanalytics_install() {
// By German laws it's always best to enable the anonymizing of IP addresses.
// NOTE: If this is also an important default setting in other countries, please let us know!
$countries = array(
'DE',
);
if (in_array(variable_get('site_default_country', ''), $countries)) {
variable_set('googleanalytics_tracker_anonymizeip', 1);
}
}
/** /**
* Implements hook_uninstall(). * Implements hook_uninstall().
*/ */
function googleanalytics_uninstall() { function googleanalytics_uninstall() {
variable_del('googleanalytics_account'); variable_del('googleanalytics_account');
variable_del('googleanalytics_cache'); variable_del('googleanalytics_cache');
variable_del('googleanalytics_codesnippet_create');
variable_del('googleanalytics_codesnippet_before'); variable_del('googleanalytics_codesnippet_before');
variable_del('googleanalytics_codesnippet_after'); variable_del('googleanalytics_codesnippet_after');
variable_del('googleanalytics_cross_domains'); variable_del('googleanalytics_cross_domains');
variable_del('googleanalytics_custom'); variable_del('googleanalytics_custom');
variable_del('googleanalytics_custom_var'); variable_del('googleanalytics_custom_dimension');
variable_del('googleanalytics_custom_metric');
variable_del('googleanalytics_debug');
variable_del('googleanalytics_domain_mode'); variable_del('googleanalytics_domain_mode');
variable_del('googleanalytics_js_scope');
variable_del('googleanalytics_last_cache'); variable_del('googleanalytics_last_cache');
variable_del('googleanalytics_pages'); variable_del('googleanalytics_pages');
variable_del('googleanalytics_roles'); variable_del('googleanalytics_roles');
...@@ -41,6 +29,9 @@ function googleanalytics_uninstall() { ...@@ -41,6 +29,9 @@ function googleanalytics_uninstall() {
variable_del('googleanalytics_tracker_anonymizeip'); variable_del('googleanalytics_tracker_anonymizeip');
variable_del('googleanalytics_trackfiles'); variable_del('googleanalytics_trackfiles');
variable_del('googleanalytics_trackfiles_extensions'); variable_del('googleanalytics_trackfiles_extensions');
variable_del('googleanalytics_tracklinkid');
variable_del('googleanalytics_trackurlfragments');
variable_del('googleanalytics_trackuserid');
variable_del('googleanalytics_trackmailto'); variable_del('googleanalytics_trackmailto');
variable_del('googleanalytics_trackmessages'); variable_del('googleanalytics_trackmessages');
variable_del('googleanalytics_trackoutbound'); variable_del('googleanalytics_trackoutbound');
...@@ -52,6 +43,8 @@ function googleanalytics_uninstall() { ...@@ -52,6 +43,8 @@ function googleanalytics_uninstall() {
// Remove backup variables if exist. Remove this code in D8. // Remove backup variables if exist. Remove this code in D8.
variable_del('googleanalytics_codesnippet_after_backup_6300'); variable_del('googleanalytics_codesnippet_after_backup_6300');
variable_del('googleanalytics_codesnippet_before_backup_6300'); variable_del('googleanalytics_codesnippet_before_backup_6300');
variable_del('googleanalytics_codesnippet_after_backup_7200');
variable_del('googleanalytics_codesnippet_before_backup_7200');
variable_del('googleanalytics_segmentation'); variable_del('googleanalytics_segmentation');
} }
...@@ -73,8 +66,8 @@ function googleanalytics_requirements($phase) { ...@@ -73,8 +66,8 @@ function googleanalytics_requirements($phase) {
if ($phase == 'runtime') { if ($phase == 'runtime') {
// Raise warning if Google user account has not been set yet. // Raise warning if Google user account has not been set yet.
if (!preg_match('/^UA-\d{4,}-\d+$/', variable_get('googleanalytics_account', 'UA-'))) { if (!preg_match('/^UA-\d+-\d+$/', variable_get('googleanalytics_account', 'UA-'))) {
$requirements['googleanalytics'] = array( $requirements['googleanalytics_account'] = array(
'title' => $t('Google Analytics module'), 'title' => $t('Google Analytics module'),
'description' => $t('Google Analytics module has not been configured yet. Please configure its settings from the <a href="@url">Google Analytics settings page</a>.', array('@url' => url('admin/config/system/googleanalytics'))), 'description' => $t('Google Analytics module has not been configured yet. Please configure its settings from the <a href="@url">Google Analytics settings page</a>.', array('@url' => url('admin/config/system/googleanalytics'))),
'severity' => REQUIREMENT_WARNING, 'severity' => REQUIREMENT_WARNING,
...@@ -82,6 +75,15 @@ function googleanalytics_requirements($phase) { ...@@ -82,6 +75,15 @@ function googleanalytics_requirements($phase) {
); );
} }
} }
// Raise warning if debugging is enabled.
if (variable_get('googleanalytics_debug', 0)) {
$requirements['google_analytics_debugging'] = array(
'title' => $t('Google Analytics module'),
'description' => $t('Google Analytics module has debugging enabled. Please disable debugging setting in production sites from the <a href="@url">Google Analytics settings page</a>.', array('@url' => url('admin/config/system/googleanalytics'))),
'severity' => REQUIREMENT_WARNING,
'value' => $t('Debugging enabled'),
);
}
return $requirements; return $requirements;
} }
...@@ -427,10 +429,55 @@ function googleanalytics_update_7006() { ...@@ -427,10 +429,55 @@ function googleanalytics_update_7006() {
} }
/** /**
* Delete obsolete googleanalytics_trackpageloadtime variable. * Delete obsolete googleanalytics_trackpageloadtime variable.
*/ */
function googleanalytics_update_7007() { function googleanalytics_update_7007() {
variable_del('googleanalytics_trackpageloadtime'); variable_del('googleanalytics_trackpageloadtime');
return t('Deleted obsolete googleanalytics_trackpageloadtime variable.'); return t('Deleted obsolete googleanalytics_trackpageloadtime variable.');
} }
/**
* Delete custom ga.js code snipptes to prevent malfunctions in new Universal Analytics tracker. A backup of your snippets will be created.
*/
function googleanalytics_update_7200() {
$messages = array();
// ga.js code will cause the tracker to break. Remove custom code snippets.
$googleanalytics_codesnippet_before = variable_get('googleanalytics_codesnippet_before', '');
if (!empty($googleanalytics_codesnippet_before)) {
variable_set('googleanalytics_codesnippet_before_backup_7200', $googleanalytics_codesnippet_before);
variable_del('googleanalytics_codesnippet_before');
drupal_set_message(Database::getConnection()->prefixTables("A backup of your previous Google Analytics code snippet has been saved in database table '{variable}' as 'googleanalytics_codesnippet_before_backup_7200'. You need to manually upgrade the custom 'before' code snippet."), 'warning');
$messages[] = t('Manual upgrade of custom "before" code snippet is required.');
}
$googleanalytics_codesnippet_after = variable_get('googleanalytics_codesnippet_after', '');
if (!empty($googleanalytics_codesnippet_after)) {
variable_set('googleanalytics_codesnippet_after_backup_7200', $googleanalytics_codesnippet_after);
variable_del('googleanalytics_codesnippet_after');
drupal_set_message(Database::getConnection()->prefixTables("A backup of your previous Google Analytics code snippet has been saved in database table '{variable}' as 'googleanalytics_codesnippet_before_backup_7200'. You need to manually upgrade the custom 'before' code snippet."), 'warning');
$messages[] = t('Manual upgrade of custom "after" code snippet is required.');
}
return empty($messages) ? t('No custom code snipped found. Nothing to do.') : implode(' ', $messages);
}
/**
* Delete obsolete custom variables. Custom variables are now custom dimensions and metrics.
*/
function googleanalytics_update_7201() {
variable_del('googleanalytics_custom_var');
return t('Deleted obsolete custom variables. Custom variables are now custom dimensions and metrics and you need to manually configure them!');
}
/**
* Delete obsolete JavaScript scope variable.
*/
function googleanalytics_update_7202() {
// Remove obsolete scope variable
variable_del('googleanalytics_js_scope');
return t('Removed obsolete JavaScript scope variable.');
}
(function ($) { (function ($) {
Drupal.googleanalytics = {};
$(document).ready(function() { $(document).ready(function() {
// Expression to check for absolute internal links. // Attach mousedown, keyup, touchstart events to document only and catch
var isInternal = new RegExp("^(https?):\/\/" + window.location.host, "i"); // clicks on all elements.
$(document.body).bind("mousedown keyup touchstart", function(event) {
// 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. // Catch the closest surrounding link of a clicked element.
$(event.target).closest("a,area").each(function() { $(event.target).closest("a,area").each(function() {
var ga = Drupal.settings.googleanalytics;
// 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("\\.(" + ga.trackDownloadExtensions + ")$", "i");
// Is the clicked URL internal? // 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. // Skip 'click' tracking, if custom tracking events are bound.
if ($(this).is('.colorbox')) { if ($(this).is('.colorbox')) {
// Do nothing here. The custom event will handle all tracking. // Do nothing here. The custom event will handle all tracking.
//console.info("Click on .colorbox item has been detected.");
} }
// Is download tracking activated and the file extension configured for download tracking? // Is download tracking activated and the file extension configured for download tracking?
else if (ga.trackDownload && isDownload.test(this.href)) { else if (Drupal.settings.googleanalytics.trackDownload && Drupal.googleanalytics.isDownload(this.href)) {
// Download link clicked. // Download link clicked.
var extension = isDownload.exec(this.href); ga("send", "event", "Downloads", Drupal.googleanalytics.getDownloadExtension(this.href).toUpperCase(), Drupal.googleanalytics.getPageUrl(this.href));
_gaq.push(["_trackEvent", "Downloads", extension[1].toUpperCase(), this.href.replace(isInternal, '')]);
} }
else if (isInternalSpecial.test(this.href)) { else if (Drupal.googleanalytics.isInternalSpecial(this.href)) {
// Keep the internal URL for Google Analytics website overlay intact. // Keep the internal URL for Google Analytics website overlay intact.
_gaq.push(["_trackPageview", this.href.replace(isInternal, '')]); ga("send", "pageview", { page: Drupal.googleanalytics.getPageUrl(this.href) });
} }
} }
else { else {
if (ga.trackMailto && $(this).is("a[href^='mailto:'],area[href^='mailto:']")) { if (Drupal.settings.googleanalytics.trackMailto && $(this).is("a[href^='mailto:'],area[href^='mailto:']")) {
// Mailto link clicked. // Mailto link clicked.
_gaq.push(["_trackEvent", "Mails", "Click", this.href.substring(7)]); ga("send", "event", "Mails", "Click", this.href.substring(7));
}
else if (ga.trackOutbound && this.href.match(/^\w+:\/\//i)) {
if (ga.trackDomainMode == 2 && isCrossDomain(this.hostname, ga.trackCrossDomains)) {
// Top-level cross domain clicked. document.location is handled by _link internally.
event.preventDefault();
_gaq.push(["_link", this.href]);
} }
else { else if (Drupal.settings.googleanalytics.trackOutbound && this.href.match(/^\w+:\/\//i)) {
// External link clicked. if (Drupal.settings.googleanalytics.trackDomainMode != 2 && !Drupal.googleanalytics.isCrossDomain(this.hostname, Drupal.settings.googleanalytics.trackCrossDomains)) {
_gaq.push(["_trackEvent", "Outbound links", "Click", this.href]); // External link clicked / No top-level cross domain clicked.
ga("send", "event", "Outbound links", "Click", this.href);
} }
} }
} }
}); });
}); });
// Track hash changes as unique pageviews, if this option has been enabled.
if (Drupal.settings.googleanalytics.trackUrlFragments) {
window.onhashchange = function() {
ga('send', 'pageview', location.pathname + location.search + location.hash);
}
}
// Colorbox: This event triggers when the transition has completed and the // Colorbox: This event triggers when the transition has completed and the
// newly loaded content has been revealed. // newly loaded content has been revealed.
$(document).bind("cbox_complete", function() { $(document).bind("cbox_complete", function () {
var href = $.colorbox.element().attr("href"); var href = $.colorbox.element().attr("href");
if (href) { if (href) {
_gaq.push(["_trackPageview", href.replace(isInternal, '')]); ga("send", "pageview", { page: Drupal.googleanalytics.getPageUrl(href) });
} }
}); });
...@@ -74,7 +71,7 @@ $(document).ready(function() { ...@@ -74,7 +71,7 @@ $(document).ready(function() {
* *
* @return boolean * @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 * jQuery < 1.6.3 bug: $.inArray crushes IE6 and Chrome if second argument is
* `null` or `undefined`, http://bugs.jquery.com/ticket/10076, * `null` or `undefined`, http://bugs.jquery.com/ticket/10076,
...@@ -90,4 +87,79 @@ function isCrossDomain(hostname, crossDomains) { ...@@ -90,4 +87,79 @@ 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.
*
* Examples:
* - http://mydomain.com/node/1 -> /node/1
* - http://example.com/foo/bar -> http://example.com/foo/bar
*
* @param string url
* The web url to check.
*
* @return string
* Internal website URL
*/
Drupal.googleanalytics.getPageUrl = 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); })(jQuery);
(function ($) {
/**
* 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.
*/
"use strict";
Drupal.googleanalytics.test = {};
Drupal.googleanalytics.test.assertSame = function (value1, value2, message) {
if (value1 === value2) {
console.info(message);
}
else {
console.error(message);
}
}
Drupal.googleanalytics.test.assertNotSame = function (value1, value2, message) {
if (value1 !== value2) {
console.info(message);
}
else {
console.error(message);
}
}
Drupal.googleanalytics.test.assertTrue = function (value1, message) {
if (value1 === true) {
console.info(message);
}
else {
console.error(message);
}
}
Drupal.googleanalytics.test.assertFalse = function (value1, message) {
if (value1 === false) {
console.info(message);
}
else {
console.error(message);
}
}
// Run after the documented is ready or Drupal.settings is undefined.
$(document).ready(function() {
/**
* Run javascript tests against the GA module.
*/
// JavaScript debugging
var base_url = window.location.protocol + '//' + window.location.host;
var base_path = window.location.pathname;
console.dir(Drupal);
console.group("Test 'isDownload':");
Drupal.googleanalytics.test.assertFalse(Drupal.googleanalytics.isDownload(base_url + Drupal.settings.basePath + 'node/8'), "Verify that '/node/8' url is not detected as file download.");
Drupal.googleanalytics.test.assertTrue(Drupal.googleanalytics.isDownload(base_url + Drupal.settings.basePath + 'files/foo1.zip'), "Verify that '/files/foo1.zip' url is detected as a file download.");
Drupal.googleanalytics.test.assertTrue(Drupal.googleanalytics.isDownload(base_url + Drupal.settings.basePath + 'files/foo1.zip#foo'), "Verify that '/files/foo1.zip#foo' url is detected as a file download.");
Drupal.googleanalytics.test.assertTrue(Drupal.googleanalytics.isDownload(base_url + Drupal.settings.basePath + 'files/foo1.zip?foo=bar'), "Verify that '/files/foo1.zip?foo=bar' url is detected as a file download.");
Drupal.googleanalytics.test.assertTrue(Drupal.googleanalytics.isDownload(base_url + Drupal.settings.basePath + 'files/foo1.zip?foo=bar#foo'), "Verify that '/files/foo1.zip?foo=bar#foo' url is detected as a file download.");
Drupal.googleanalytics.test.assertFalse(Drupal.googleanalytics.isDownload(base_url + Drupal.settings.basePath + 'files/foo2.ddd'), "Verify that '/files/foo2.ddd' url is not detected as file download.");
console.groupEnd();
console.group("Test 'isInternal':");
Drupal.googleanalytics.test.assertTrue(Drupal.googleanalytics.isInternal(base_url + Drupal.settings.basePath + 'node/1'), "Link '" + base_url + Drupal.settings.basePath + "node/2' has been detected as internal link.");
Drupal.googleanalytics.test.assertTrue(Drupal.googleanalytics.isInternal(base_url + Drupal.settings.basePath + 'node/1#foo'), "Link '" + base_url + Drupal.settings.basePath + "node/1#foo' has been detected as internal link.");
Drupal.googleanalytics.test.assertTrue(Drupal.googleanalytics.isInternal(base_url + Drupal.settings.basePath + 'node/1?foo=bar'), "Link '" + base_url + Drupal.settings.basePath + "node/1?foo=bar' has been detected as internal link.");
Drupal.googleanalytics.test.assertTrue(Drupal.googleanalytics.isInternal(base_url + Drupal.settings.basePath + 'node/1?foo=bar#foo'), "Link '" + base_url + Drupal.settings.basePath + "node/1?foo=bar#foo' has been detected as internal link.");
Drupal.googleanalytics.test.assertTrue(Drupal.googleanalytics.isInternal(base_url + Drupal.settings.basePath + 'go/foo'), "Link '" + base_url + Drupal.settings.basePath + "go/foo' has been detected as internal link.");
Drupal.googleanalytics.test.assertFalse(Drupal.googleanalytics.isInternal('http://example.com/node/3'), "Link 'http://example.com/node/3' has been detected as external link.");
console.groupEnd();
console.group("Test 'isInternalSpecial':");
Drupal.googleanalytics.test.assertTrue(Drupal.googleanalytics.isInternalSpecial(base_url + Drupal.settings.basePath + 'go/foo'), "Link '" + base_url + Drupal.settings.basePath + "go/foo' has been detected as special internal link.");
Drupal.googleanalytics.test.assertFalse(Drupal.googleanalytics.isInternalSpecial(base_url + Drupal.settings.basePath + 'node/1'), "Link '" + base_url + Drupal.settings.basePath + "node/1' has been detected as special internal link.");
console.groupEnd();
console.group("Test 'getPageUrl':");
Drupal.googleanalytics.test.assertSame(base_path + 'node/1', Drupal.googleanalytics.getPageUrl(base_url + Drupal.settings.basePath + 'node/1'), "Absolute internal URL '" + Drupal.settings.basePath + "node/1' has been extracted from full qualified url '" + base_url + base_path + "node/1'.");
Drupal.googleanalytics.test.assertSame(base_path + 'node/1', Drupal.googleanalytics.getPageUrl(Drupal.settings.basePath + 'node/1'), "Absolute internal URL '" + Drupal.settings.basePath + "node/1' has been extracted from absolute url '" + base_path + "node/1'.");
Drupal.googleanalytics.test.assertSame('http://example.com/node/2', Drupal.googleanalytics.getPageUrl('http://example.com/node/2'), "Full qualified external url 'http://example.com/node/2' has been extracted.");
Drupal.googleanalytics.test.assertSame('//example.com/node/2', Drupal.googleanalytics.getPageUrl('//example.com/node/2'), "Full qualified external url '//example.com/node/2' has been extracted.");
console.groupEnd();
console.group("Test 'getDownloadExtension':");
Drupal.googleanalytics.test.assertSame('zip', Drupal.googleanalytics.getDownloadExtension(base_url + Drupal.settings.basePath + '/files/foo1.zip'), "Download extension 'zip' has been found in '" + base_url + Drupal.settings.basePath + "files/foo1.zip'.");
Drupal.googleanalytics.test.assertSame('zip', Drupal.googleanalytics.getDownloadExtension(base_url + Drupal.settings.basePath + '/files/foo1.zip#foo'), "Download extension 'zip' has been found in '" + base_url + Drupal.settings.basePath + "files/foo1.zip#foo'.");
Drupal.googleanalytics.test.assertSame('zip', Drupal.googleanalytics.getDownloadExtension(base_url + Drupal.settings.basePath + '/files/foo1.zip?foo=bar'), "Download extension 'zip' has been found in '" + base_url + Drupal.settings.basePath + "files/foo1.zip?foo=bar'.");
Drupal.googleanalytics.test.assertSame('zip', Drupal.googleanalytics.getDownloadExtension(base_url + Drupal.settings.basePath + '/files/foo1.zip?foo=bar#foo'), "Download extension 'zip' has been found in '" + base_url + Drupal.settings.basePath + "files/foo1.zip?foo=bar'.");
Drupal.googleanalytics.test.assertSame('', Drupal.googleanalytics.getDownloadExtension(base_url + Drupal.settings.basePath + '/files/foo2.dddd'), "No download extension found in '" + base_url + Drupal.settings.basePath + "files/foo2.dddd'.");
console.groupEnd();
// List of top-level domains: example.com, example.net
console.group("Test 'isCrossDomain' (requires cross domain configuration with 'example.com' and 'example.net'):");
if (Drupal.settings.googleanalytics.trackCrossDomains) {
console.dir(Drupal.settings.googleanalytics.trackCrossDomains);
Drupal.googleanalytics.test.assertTrue(Drupal.googleanalytics.isCrossDomain('example.com', Drupal.settings.googleanalytics.trackCrossDomains), "URL 'example.com' has been found in cross domain list.");
Drupal.googleanalytics.test.assertTrue(Drupal.googleanalytics.isCrossDomain('example.net', Drupal.settings.googleanalytics.trackCrossDomains), "URL 'example.com' has been found in cross domain list.");
Drupal.googleanalytics.test.assertFalse(Drupal.googleanalytics.isCrossDomain('www.example.com', Drupal.settings.googleanalytics.trackCrossDomains), "URL 'www.example.com' not found in cross domain list.");
Drupal.googleanalytics.test.assertFalse(Drupal.googleanalytics.isCrossDomain('www.example.net', Drupal.settings.googleanalytics.trackCrossDomains), "URL 'www.example.com' not found in cross domain list.");
}
else {
console.warn('Cross domain tracking is not enabled. Tests skipped.');
}
console.groupEnd();
});
})(jQuery);
...@@ -45,7 +45,7 @@ function googleanalytics_validate_googleanalytics_account($variable) { ...@@ -45,7 +45,7 @@ function googleanalytics_validate_googleanalytics_account($variable) {
// Replace all type of dashes (n-dash, m-dash, minus) with the normal dashes. // Replace all type of dashes (n-dash, m-dash, minus) with the normal dashes.
$variable['value'] = str_replace(array('–', '—', '−'), '-', $variable['value']); $variable['value'] = str_replace(array('–', '—', '−'), '-', $variable['value']);
if (!preg_match('/^UA-\d{4,}-\d+$/', $variable['value'])) { if (!preg_match('/^UA-\d+-\d+$/', $variable['value'])) {
return t('A valid Google Analytics Web Property ID is case sensitive and formatted like UA-xxxxxxx-yy.'); return t('A valid Google Analytics Web Property ID is case sensitive and formatted like UA-xxxxxxx-yy.');
} }
} }
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