Newer
Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
/**
* @file
* Attaches several event listener to a web page (debugging version).
*/
(function ($, Drupal, drupalSettings) {
/* eslint no-console: 0, max-nested-callbacks: ["error", 4] */
'use strict';
Drupal.google_analytics = {};
$(document).ready(function () {
// Attach mousedown, keyup, touchstart events to document only and catch
// clicks on all elements.
$(document.body).on('mousedown keyup touchstart', function (event) {
console.group('Running Google Analytics for Drupal.');
console.info("Event '%s' has been detected.", event.type);
// 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.google_analytics.isInternal(this.href)) {
// Skip 'click' tracking, if custom tracking events are bound.
if ($(this).is('.colorbox') && (drupalSettings.google_analytics.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 (drupalSettings.google_analytics.trackDownload && Drupal.google_analytics.isDownload(this.href)) {
// Download link clicked.
console.info("Download url '%s' has been found. Tracked download as extension '%s'.", Drupal.google_analytics.getPageUrl(this.href), Drupal.google_analytics.getDownloadExtension(this.href).toUpperCase());
gtag('event', Drupal.google_analytics.getDownloadExtension(this.href).toUpperCase(), {
event_category: 'Downloads',
event_label: Drupal.google_analytics.getPageUrl(this.href),
transport_type: 'beacon'
});
}
else if (Drupal.google_analytics.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.google_analytics.getPageUrl(this.href));
// @todo: May require tracking ID
gtag('config', drupalSettings.google_analytics.account, {
page_path: Drupal.google_analytics.getPageUrl(this.href),
transport_type: 'beacon'
});
}
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 (drupalSettings.google_analytics.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));
gtag('event', 'Click', {
event_category: 'Mails',
event_label: this.href.substring(7),
transport_type: 'beacon'
});
}
else if (drupalSettings.google_analytics.trackTel && $(this).is("a[href^='tel:'],area[href^='tel:']")) {
// Tel link clicked.
console.info("Click on telephone number '%s' has been tracked.", this.href.substring(4));
gtag('event', 'Click', {
event_category: 'Telephone calls',
event_label: this.href.substring(4),
transport_type: 'beacon'
});
}
else if (drupalSettings.google_analytics.trackOutbound && this.href.match(/^\w+:\/\//i)) {
if (drupalSettings.google_analytics.trackDomainMode !== 2 || (drupalSettings.google_analytics.trackDomainMode === 2 && !Drupal.google_analytics.isCrossDomain(this.hostname, drupalSettings.google_analytics.trackCrossDomains))) {
// External link clicked / No top-level cross domain clicked.
console.info("Outbound link '%s' has been tracked.", this.href);
gtag('event', 'Click', {
event_category: 'Outbound links',
event_label: this.href,
transport_type: 'beacon'
});
}
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 (drupalSettings.google_analytics.trackUrlFragments) {
window.onhashchange = function () {
console.info("Track URL '%s' as pageview. Hash '%s' has changed.", location.pathname + location.search + location.hash, location.hash);
gtag('config', drupalSettings.google_analytics.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.
if (drupalSettings.google_analytics.trackColorbox) {
$(document).on('cbox_complete', function () {
var href = $.colorbox.element().attr('href');
if (href) {
console.info("Colorbox transition to url '%s' has been tracked.", Drupal.google_analytics.getPageUrl(href));
gtag('config', drupalSettings.google_analytics.account, {
page_path: Drupal.google_analytics.getPageUrl(href)
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
});
}
});
}
});
/**
* 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} isCrossDomain
*/
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} isDownload
*/
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} isInternal
*/
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} isInternalSpecial
*/
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.
*
* Examples:
* - https://mydomain.com/node/1 -> /node/1
* - https://example.com/foo/bar -> https://example.com/foo/bar
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
*
* @param {string} url
* The web url to check.
*
* @return {string} getPageUrl
* Internal website URL.
*/
Drupal.google_analytics.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} getDownloadExtension
* 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 === null) ? '' : extension[1];
};
})(jQuery, Drupal, drupalSettings);