Skip to content

Commit dc116ae

Browse files
Merge pull request #76 from simpleanalytics/codex/add-tel--clicks-to-auto-events
Add tel link tracking
2 parents aa513da + 78bc66d commit dc116ae

File tree

1 file changed

+18
-2
lines changed

1 file changed

+18
-2
lines changed

src/auto-events.js

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
"outbound",
3838
"emails",
3939
"downloads",
40+
"phones",
4041
]);
4142
var fullUrls = setting("fullUrls", "bool", false);
4243

@@ -45,6 +46,7 @@
4546
outbound: collectTypes.indexOf("outbound") > -1,
4647
emails: collectTypes.indexOf("emails") > -1,
4748
downloads: collectTypes.indexOf("downloads") > -1,
49+
phones: collectTypes.indexOf("phones") > -1,
4850
// Downloads: enter file extensions you want to collect
4951
downloadsExtensions: setting("extensions", "array", [
5052
"pdf",
@@ -122,6 +124,12 @@
122124
metadata.email = event;
123125
break;
124126
}
127+
case "phone": {
128+
var hrefPhone = element.getAttribute("href");
129+
event = (hrefPhone.split(":")[1] || "").split("?")[0];
130+
metadata.phone = event;
131+
break;
132+
}
125133
}
126134
}
127135

@@ -134,7 +142,7 @@
134142

135143
log("collected " + clean);
136144

137-
return type === "email"
145+
return type === "email" || type === "phone"
138146
? callback()
139147
: window.setTimeout(callback, 5000);
140148
} else {
@@ -174,6 +182,10 @@
174182
// Collect email clicks
175183
} else if (optionsLink.emails && /^mailto:/i.test(link.href)) {
176184
collect = "email";
185+
186+
// Collect telephone clicks
187+
} else if (optionsLink.phones && /^tel:/i.test(link.href)) {
188+
collect = "phone";
177189
}
178190

179191
if (!collect) return;
@@ -208,7 +220,11 @@
208220
if (!href) continue;
209221

210222
// We don't want to overwrite website behaviour so we check for the onclick attribute
211-
if (!link.getAttribute("onclick") && !/^mailto:/.test(href)) {
223+
if (
224+
!link.getAttribute("onclick") &&
225+
!/^mailto:/.test(href) &&
226+
!/^tel:/.test(href)
227+
) {
212228
collectLink(link, true);
213229
} else {
214230
collectLink(link, false);

0 commit comments

Comments
 (0)