Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
339 changes: 339 additions & 0 deletions LICENSE.txt

Large diffs are not rendered by default.

346 changes: 194 additions & 152 deletions googleanalytics.admin.inc

Large diffs are not rendered by default.

78 changes: 37 additions & 41 deletions googleanalytics.debug.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
(function ($) {

Drupal.googleanalytics = {};

Expand All @@ -9,36 +10,33 @@ $(document).ready(function() {
console.group("Running Google Analytics for Drupal.");
console.info("Event '%s' has been detected.", event.type);

// Catch only the first parent link of a clicked element.
$(event.target).parents("a:first,area:first").andSelf().filter("a,area").each(function() {
// Catch the closest surrounding link of a clicked element.
$(event.target).closest("a,area").each(function() {
console.info("Closest element '%o' has been found. URL '%s' extracted.", 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')) {
if ($(this).is('.colorbox') && (Drupal.settings.googleanalytics.trackColorbox)) {
// 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", {
"hitType": "event",
"eventCategory": "Downloads",
"eventAction": Drupal.googleanalytics.getDownloadExtension(this.href).toUpperCase(),
"eventLabel": Drupal.googleanalytics.getPageUrl(this.href),
"transport": "beacon"
gtag('event', Drupal.googleanalytics.getDownloadExtension(this.href).toUpperCase(), {
event_category: 'Downloads',
event_label: Drupal.googleanalytics.getPageUrl(this.href),
transport_type: 'beacon'
});
}
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", {
"hitType": "pageview",
"page": Drupal.googleanalytics.getPageUrl(this.href),
"transport": "beacon"
gtag('config', Drupal.settings.googleanalytics.account, {
page_path: Drupal.googleanalytics.getPageUrl(this.href),
transport_type: 'beacon'
});
}
else {
Expand All @@ -50,24 +48,20 @@ $(document).ready(function() {
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", {
"hitType": "event",
"eventCategory": "Mails",
"eventAction": "Click",
"eventLabel": this.href.substring(7),
"transport": "beacon"
gtag('event', 'Click', {
event_category: 'Mails',
event_label: this.href.substring(7),
transport_type: 'beacon'
});
}
else if (Drupal.settings.googleanalytics.trackOutbound && this.href.match(/^\w+:\/\//i)) {
if (Drupal.settings.googleanalytics.trackDomainMode !== 2 || (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", {
"hitType": "event",
"eventCategory": "Outbound links",
"eventAction": "Click",
"eventLabel": this.href,
"transport": "beacon"
gtag('event', 'Click', {
event_category: 'Outbound links',
event_label: this.href,
transport_type: 'beacon'
});
}
else {
Expand All @@ -84,25 +78,25 @@ $(document).ready(function() {
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", {
"hitType": "pageview",
"page": location.pathname + location.search + location.hash
gtag('config', Drupal.settings.googleanalytics.account, {
page_path: 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", {
"hitType": "pageview",
"page": Drupal.googleanalytics.getPageUrl(href)
});
}
});
if (Drupal.settings.googleanalytics.trackColorbox) {
$(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));
gtag('config', Drupal.settings.googleanalytics.account, {
page_path: Drupal.googleanalytics.getPageUrl(href)
});
}
});
}

});

Expand All @@ -119,7 +113,7 @@ $(document).ready(function() {
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,
* `null` or `undefined`, https://bugs.jquery.com/ticket/10076,
* https://github.com/jquery/jquery/commit/a839af034db2bd934e4d4fa6758a3fed8de74174
*
* @todo: Remove/Refactor in D8
Expand Down Expand Up @@ -178,8 +172,8 @@ Drupal.googleanalytics.isInternalSpecial = function (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
* - https://mydomain.com/node/1 -> /node/1
* - https://example.com/foo/bar -> https://example.com/foo/bar
*
* @param string url
* The web url to check.
Expand All @@ -206,3 +200,5 @@ Drupal.googleanalytics.getDownloadExtension = function (url) {
var extension = extractDownloadextension.exec(url);
return (extension === null) ? '' : extension[1];
};

})(jQuery);
33 changes: 33 additions & 0 deletions googleanalytics.gtag-shim.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
window.ga = window.ga || function() {
var type = arguments[0];
if (type === 'create') {
gtag('config', arguments[1], arguments[2]);
} else if (type === 'set') {
gtag('set', arguments[1], arguments[2]);
} else if (type === 'require') {
// Anything needed in `require` is bundled into Google Tag.
}
else if (type === 'send') {
if (typeof arguments[1] === 'object') {
gtag('event', arguments[1].eventAction, {
event_category: arguments[1].eventCategory,
event_label: arguments[1].eventLabel,
transport_type: arguments[1].transport || 'beacon',
})
}
else if (arguments[1] === 'pageview') {
gtag('event', 'page_view');
}
else if (arguments.length === 2) {
gtag('event', arguments[1]);
}
else {
gtag('event', arguments[3] || '', {
event_category: arguments[2] || '',
event_label: arguments[4] || '',
})
}
} else {
gtag(arguments);
}
}
1 change: 1 addition & 0 deletions googleanalytics.info
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ name = Google Analytics
description = Allows your site to be tracked by Google Analytics by adding a Javascript tracking code to every page.
core = 6.x
package = Statistics

62 changes: 25 additions & 37 deletions googleanalytics.install
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,12 @@
* Installation file for Google Analytics module.
*/

/**
* Implements hook_uninstall().
*/
function googleanalytics_uninstall() {
variable_del('googleanalytics_account');
variable_del('googleanalytics_premium');
variable_del('googleanalytics_cache');
variable_del('googleanalytics_codesnippet_create');
variable_del('googleanalytics_codesnippet_before');
Expand All @@ -22,6 +26,7 @@ function googleanalytics_uninstall() {
variable_del('googleanalytics_roles');
variable_del('googleanalytics_site_search');
variable_del('googleanalytics_trackadsense');
variable_del('googleanalytics_trackcolorbox');
variable_del('googleanalytics_trackdoubleclick');
variable_del('googleanalytics_tracker_anonymizeip');
variable_del('googleanalytics_trackfiles');
Expand All @@ -32,8 +37,10 @@ function googleanalytics_uninstall() {
variable_del('googleanalytics_trackmailto');
variable_del('googleanalytics_trackmessages');
variable_del('googleanalytics_trackoutgoing');
variable_del('googleanalytics_trackoutbound');
variable_del('googleanalytics_translation_set');
variable_del('googleanalytics_visibility');
variable_del('googleanalytics_visibility_pages');
variable_del('googleanalytics_visibility_roles');
variable_del('googleanalytics_privacy_donottrack');

Expand All @@ -54,13 +61,14 @@ function googleanalytics_disable() {
*/
function googleanalytics_requirements($phase) {
$requirements = array();
$t = get_t();

if ($phase == 'runtime') {
// Raise warning if Google user account has not been set yet.
if (!preg_match('/^UA-\d{4,}-\d+$/', variable_get('googleanalytics_account', 'UA-'))) {
$requirements['googleanalytics'] = array(
'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/settings/googleanalytics'))),
if (!_google_analytics_valid_property_id(variable_get('googleanalytics_account', 'UA-'))) {
$requirements['googleanalytics_account'] = array(
'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/settings/googleanalytics'))),
'severity' => REQUIREMENT_WARNING,
'value' => t('Not configured'),
);
Expand All @@ -80,71 +88,51 @@ function googleanalytics_requirements($phase) {
return $requirements;
}


function googleanalytics_update_1() {
$ret = array();

$result = db_query("SELECT * FROM {role}");
while ($role = db_fetch_object($result)) {
// can't use empty spaces in varname
$role_varname = str_replace(' ', '_', $role->name);
variable_set('googleanalytics_track_'. $role->rid, !variable_get("googleanalytics_track_{$role_varname}", FALSE));
variable_del("googleanalytics_track_{$role_varname}");
}
variable_set('googleanalytics_track__user1', FALSE);

return $ret;
}

/**
* Upgrade old extension variable to new and use old name as enabled/disabled flag.
* Upgrade old extension variable to new and use old name as enabled/disabled
* flag.
*/
function googleanalytics_update_6000() {
$ret = array();

variable_set('googleanalytics_trackfiles_extensions', variable_get('googleanalytics_trackfiles', '7z|aac|avi|csv|doc|exe|flv|gif|gz|jpe?g|js|mp(3|4|e?g)|mov|pdf|phps|png|ppt|rar|sit|tar|torrent|txt|wma|wmv|xls|xml|zip'));
$trackfiles = variable_get('googleanalytics_trackfiles', '7z|aac|avi|csv|doc|exe|flv|gif|gz|jpe?g|js|mp(3|4|e?g)|mov|pdf|phps|png|ppt|rar|sit|tar|torrent|txt|wma|wmv|xls|xml|zip') ? TRUE : FALSE;
variable_set('googleanalytics_trackfiles', $trackfiles);
$ret[] = array('success' => TRUE, 'query' => 'Updated download tracking settings.');

return $ret;
return t('Updated download tracking file extensions.');
}

function googleanalytics_update_6001() {
$ret = array();

variable_set('googleanalytics_visibility', 0);

// Remove tracking from all administrative pages, see http://drupal.org/node/34970.
// Remove tracking from all administrative pages, see:
// https://drupal.org/node/34970.
$pages = array(
'admin*',
'user*',
'node/add*',
'node/*/*',
);
variable_set('googleanalytics_pages', implode("\n", $pages));
$ret[] = array('success' => TRUE, 'query' => 'Added page tracking to every page except the listed pages: '. implode(', ', $pages));

return $ret;
return t('Added page tracking to every page except the listed pages: @pages.', array('@pages' => implode(', ', $pages)));
}

/**
* Upgrade role settings and per user tracking settings
* of "User 1" and remove outdated tracking variables.
* Upgrade role settings and per user tracking settings of "User 1" and remove
* outdated tracking variables.
*/
function googleanalytics_update_6002() {
$ret = array();

// Upgrade enabled/disabled roles to new logic (correct for upgrades from 5.x-1.4 and 6.x-1.0).
// Upgrade enabled/disabled roles to new logic (correct for upgrades from
// 5.x-1.4 and 6.x-1.0).
$roles = array();
$messages = array();
foreach (user_roles() as $rid => $name) {
if (variable_get('googleanalytics_track_'. $rid, FALSE)) {
// Role ID is activated for user tracking.
$roles[$rid] = $rid;
$ret[] = array('success' => TRUE, 'query' => 'Enabled page tracking for role: '. $name);
$messages[] = t('Enabled page tracking for role: @name.', array('@name' => $name));
}
else {
$ret[] = array('success' => TRUE, 'query' => 'Disabled page tracking for role: '. $name);
$messages[] = t('Disabled page tracking for role: @name.', array('@name' => $name));
}
}
variable_set('googleanalytics_roles', $roles);
Expand Down
Loading