-
Notifications
You must be signed in to change notification settings - Fork 160
/
Copy pathgtm.js
52 lines (47 loc) · 1.8 KB
/
gtm.js
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
// Copyright (c) 2020, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
window.dataLayer = window.dataLayer || [];
window.dataLayer.push({
'gtm.start': new Date().getTime(),
event: 'gtm.js'
});
// Setup listening to send Google Analytics events when any element
// with a 'data-ga-click-event' attribute is clicked.
window.addEventListener('DOMContentLoaded', function () {
function sendEvent(e) {
var hasModifierKey = (e.altKey == true) || (e.ctrlKey == true) || (e.metaKey == true) || (e.shiftKey == true);
var element = e.currentTarget;
var customEvent = {
event: 'custom-event',
customEventCategory: 'click',
customEventAction: element.dataset.gaClickEvent,
customEventLabel: 'path:' + window.location.pathname,
customEventValue: 1
};
if (element.hasAttribute('href') && (!hasModifierKey)) {
var done = false;
// Change location after handling the event:
// https://developers.google.com/tag-manager/enhanced-ecommerce
customEvent.eventCallback = function () {
if (!done) {
done = true;
document.location = element.href;
}
};
e.preventDefault();
// Fallback location change in case the Google Tag Manager is blocked.
setTimeout(customEvent.eventCallback, 100);
}
// Push custom-event to GTM, which is configured to forward it to
// Google Analytics.
window.dataLayer.push(customEvent);
}
function addListeners() {
var elements = document.querySelectorAll('[data-ga-click-event]');
for (var i = 0; i < elements.length; i++) {
elements[i].addEventListener('click', sendEvent);
}
}
addListeners();
});