-
Notifications
You must be signed in to change notification settings - Fork 14
/
auto-events.js
226 lines (190 loc) · 6.25 KB
/
auto-events.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
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
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
183
184
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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
(function saAutomatedEvents(window) {
// Skip server side rendered pages
if (typeof window === "undefined") return;
var log = function (message, type) {
var logger = type === "warn" ? console.warn : console.info;
return logger && logger("Simple Analytics auto events:", message);
};
var doc = window.document;
var scriptElement =
doc.currentScript || doc.querySelector('script[src*="auto-events.js"]');
var setting = function (attribute, type, defaultValue) {
var value = scriptElement && scriptElement.dataset[attribute];
// Booleans
if (type === "bool" && (value === "true" || value === "false"))
return value === "true";
else if (type === "bool") return defaultValue;
// Arrays
if (type === "array" && value)
return value
.split(",")
.map(function (item) {
return item.trim();
})
.filter(Boolean);
else if (type === "array") return defaultValue;
return value || defaultValue;
};
var collectTypes = setting("collect", "array", [
"outbound",
"emails",
"downloads",
]);
var fullUrls = setting("fullUrls", "bool", false);
var options = {
// What to collect
outbound: collectTypes.indexOf("outbound") > -1,
emails: collectTypes.indexOf("emails") > -1,
downloads: collectTypes.indexOf("downloads") > -1,
// Downloads: enter file extensions you want to collect
downloadsExtensions: setting("extensions", "array", [
"pdf",
"csv",
"docx",
"xlsx",
"zip",
"doc",
"xls",
]),
// All: use title attribute if set for event name (for all events)
// THIS TAKES PRECEDENCE OVER OTHER SETTINGS BELOW
title: setting("useTitle", "bool", true),
// Outbound: use full URL of the links? false for just the hostname
outboundFullUrl: fullUrls,
// Downloads: if taking event name from URL, use full URL or just filename (default)
downloadsFullUrl: fullUrls,
};
var saGlobal = setting("saGlobal", "string", "sa_event");
// For compiling the script
var optionsLink = options;
if (typeof optionsLink === "undefined")
log("options object not found, please specify", "warn");
window.saAutomatedLink = function saAutomatedLink(element, type) {
try {
if (!element) return log("no element found");
var sent = false;
var callback = function () {
if (!sent && !element.hasAttribute("target"))
document.location = element.getAttribute("href");
sent = true;
};
if (window[saGlobal] && window[saGlobal + "_loaded"]) {
var hostname = element.hostname;
var pathname = element.pathname;
var event;
var metadata = {
title: element.getAttribute("title") || undefined,
};
var url = element.href || undefined;
var useTitle = false;
if (optionsLink.title && element.hasAttribute("title")) {
var theTitle = element.getAttribute("title").trim();
if (theTitle != "") useTitle = true;
}
if (useTitle) {
event = theTitle;
} else {
switch (type) {
case "outbound": {
event = hostname + (optionsLink.outboundFullUrl ? pathname : "");
metadata.url = url;
break;
}
case "download": {
event = optionsLink.downloadsFullUrl
? hostname + pathname
: pathname.split("/").pop();
metadata.url = url;
break;
}
case "email": {
var href = element.getAttribute("href");
event = (href.split(":")[1] || "").split("?")[0];
metadata.email = event;
break;
}
}
}
var clean =
type +
"_" +
event.replace(/[^a-z0-9]+/gi, "_").replace(/(^_+|_+$)/g, "");
window[saGlobal](clean, metadata, callback);
log("collected " + clean);
return type === "email"
? callback()
: window.setTimeout(callback, 5000);
} else {
log(saGlobal + " is not defined", "warn");
return callback();
}
} catch (error) {
log(error.message, "warn");
}
};
function collectLink(link, onclick) {
var collect = false;
// Collect download clicks
if (
optionsLink.downloads &&
/^https?:\/\//i.test(link.href) &&
new RegExp(
"\\.(" + (optionsLink.downloadsExtensions || []).join("|") + ")$",
"i"
).test(link.pathname)
) {
collect = "download";
// Collect outbound links clicks
} else if (
optionsLink.outbound &&
/^https?:\/\//i.test(link.href) &&
link.hostname !== window.location.hostname
) {
collect = "outbound";
// Collect email clicks
} else if (optionsLink.emails && /^mailto:/i.test(link.href)) {
collect = "email";
}
if (!collect) return;
if (onclick) {
var onClickAttribute = "saAutomatedLink(this, '" + collect + "');";
if (
!link.hasAttribute("target") ||
link.getAttribute("target") === "_self"
)
onClickAttribute += " return false;";
link.setAttribute("onclick", onClickAttribute);
} else {
link.addEventListener("click", function (element) {
saAutomatedLink(element.target, collect);
});
}
}
function onDOMContentLoaded() {
try {
var a = document.getElementsByTagName("a");
// Loop over all links on the page
for (var i = 0; i < a.length; i++) {
var link = a[i];
var href = link.getAttribute("href");
// Skip links that don't have an href
if (!href) continue;
// We don't want to overwrite website behaviour so we check for the onclick attribute
if (!link.getAttribute("onclick") && !/^mailto:/.test(href)) {
collectLink(link, true);
} else {
collectLink(link, false);
}
}
} catch (error) {
log(error.message, "warn");
}
}
if (doc.readyState === "ready" || doc.readyState === "complete") {
onDOMContentLoaded();
} else {
document.addEventListener("readystatechange", function (event) {
if (event.target.readyState === "complete") onDOMContentLoaded();
});
}
})(window);