-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathajax-event-tracking-vanillajs.html
129 lines (125 loc) · 4.63 KB
/
ajax-event-tracking-vanillajs.html
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
<script>
/*
* Ajax Event Tracking without jQuery, in Google Tag Manager, for all ajax events and especially
* for ajax forms, modal windows, popups and confirmation dialogs,
* that don't propagate form success event or new page loads.
* It will only be compatible with AJAX calls in pure javascript (XMLHttpRequest).
* AJAX requests made via jquery ($.ajax, .load() and similar) will not be detected.
* For jQuery events use the ajax-event-tracking-jquery example instead.
*
* Idea by http://aukera.co.uk/blog/tracking-ajax-events-gtm/#gtm-ajax-6-tracking-js
* Modified by https:://github.com/underlines
* Version 2017-07-20 UTC 10:17
*
* Notes from aureka.co.uk:
* The script directly patches the native XMLHttpRequest.send() function, so it’ll
* be compatible with any browser that uses it and any script or library whose
* AJAX functionality depends on it.
* For greater compatibility, rather than depend on the OnStateChange property
* of the request, it directly monitors its status, which makes it compatible
* with almost all AJAX requests – including those made by jQuery (which wouldn’t
* otherwise work as they bypass OnStateChange and use their own callbacks instead).
* If you have any problems, try increasing the wait time for the setTimeout call.
*/
/* STEP 1
* Create new variable:
* Name: eventCategory
* Type: Data Layer Variable
* Data Layer Variable Name: eventCategory
*/
/* STEP 2
* Create new variable:
* Name: eventAction
* Type: Data Layer Variable
* Data Layer Variable Name: eventAction
*/
/* STEP 3
* Create new variable:
* Name: eventLabel
* Type: Data Layer Variable
* Data Layer Variable Name: eventLabel
*/
/* STEP 4
* Create new trigger
* Name: gtm.dom
* Choose Event: Page View
* Configure Trigger: DOM Ready
* Fire On: All Page Views
*/
/* STEP 5
* Create new tag
* Type: Custom HTML
* HTML: Choose either the script with or without post/get parameters
* Trigger: gtm.dom
* Don't forget <script> tags
*/
//Without post/get parameters
(function() {
var xhrSend = window.XMLHttpRequest.prototype.send;
window.XMLHttpRequest.prototype.send = function() {
var xhr = this;
var intervalId = window.setInterval(function() {
if(xhr.readyState != 4) {
return;
}
dataLayer.push({
'event': 'ajaxSuccess',
'eventCategory': 'AJAX',
'eventAction': xhr.responseURL,
'eventLabel': xhr.responseText
});
clearInterval(intervalId);
}, 1);
return xhrSend.apply(this, [].slice.call(arguments));
};
})();
//With post/get parameters
(function() {
var xhrOpen = window.XMLHttpRequest.prototype.open;
var xhrSend = window.XMLHttpRequest.prototype.send;
window.XMLHttpRequest.prototype.open = function() {
this.method = arguments[0];
this.url = arguments[1];
return xhrOpen.apply(this, [].slice.call(arguments));
};
window.XMLHttpRequest.prototype.send = function() {
var xhr = this;
var xhrData = arguments[0];
var intervalId = window.setInterval(function() {
if(xhr.readyState != 4) {
return;
}
dataLayer.push({
'event': 'ajaxSuccess',
'eventCategory': 'AJAX ' + xhr.method,
'eventAction': xhr.url + (xhr.method == 'POST' && xhrData ? ';' + xhrData : ''),
'eventLabel': xhr.responseText
});
clearInterval(intervalId);
}, 1);
return xhrSend.apply(this, [].slice.call(arguments));
};
})();
/* STEP 6
* Create new event
* Name: ajaxEvents
* Type: Custom Event
* Fire on: Event name: ajaxSucces
*/
/* STEP 7
* Create new tag
* Type: Universal Analytics
* Track Type: Event
* Category: {{eventCategory}}
* Action: {{eventAction}}
* Label: {{eventLabel}}
* Fire On: ajaxEvents
*
* Note: This is an example. You should not fire on all ajaxEvents,
* so after recognizing the particular event you want to measure,
* adapt the rule to your trigger on step 6 and don't fire on all events.
* Also note that in most cases you don't want to send the whole ajax
* request as a eventLabel, so either process it with another javascript
* variable, don't send it at all, or know exactly what you're doing.
*/
</script>