Repository navigation
Expand file tree
/
Copy pathhelp-link-tracking.js
More file actions
194 lines (184 loc) · 6.44 KB
/
Copy pathhelp-link-tracking.js
File metadata and controls
194 lines (184 loc) · 6.44 KB
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
(function () {
if (window.__teableHelpLinkTrackingInitialized) {
return;
}
window.__teableHelpLinkTrackingInitialized = true;
var officialHosts = ["teable.ai", "www.teable.ai", "teable.cn", "www.teable.cn"];
var locales = ["en", "zh", "ru", "de", "fr", "es", "it", "ja", "tr", "uk", "ar"];
// Keep this public product catalog in sync with the website arrival validator.
var productRoutes = [
"customer-feedback-analysis",
"custom-inventory-management",
"employee-database",
"products",
"csv-compare",
"email-permutator",
"pdf-to-table",
"ai-flowchart-generator",
"lead-enrichment",
"ai-market-research",
"seo-reporting-tool",
"ai-email-reply-generator",
"online-database-builder",
"ai-newsletter-generator",
"client-portal",
"purchase-order-generator",
"ai-social-media-manager",
"ai-ads-agency",
"ai-proposal-generator",
"vendor-portal",
"directory-website-builder",
"ai-spreadsheet-generator",
"employee-portal",
"ai-report-generator",
"dashboard-creator",
"ai-app-builder",
"no-code-app-builder",
"excel-to-app",
"spreadsheet-to-app",
"online-form-builder",
"ai-web-scraper",
"ai-email-parser",
"find-influencers",
"workflow-generator",
"ai-project-management",
"ctms-system",
];
function publicSegments(pathname) {
if (pathname.length > 100 || !/^\/[a-z0-9-]+(?:\/[a-z0-9-]+)*\/?$/.test(pathname)) {
return null;
}
return pathname.replace(/\/$/, "").slice(1).split("/");
}
function linkCategory(pathname) {
var parts = publicSegments(pathname);
if (!parts) { return null; }
if (locales.indexOf(parts[0]) !== -1) { parts.shift(); }
if (parts.length === 1 && productRoutes.indexOf(parts[0]) !== -1) { return "product"; }
if (parts.length <= 2 && parts[0] === "compare") { return "compare"; }
if (parts.length <= 2 && parts[0] === "blog") { return "blog"; }
if (parts.length <= 2 && parts[0] === "customers") { return "customer"; }
return null;
}
function trustedTarget(link) {
try {
var target = new URL(link.href, window.location.href);
if (target.protocol !== "https:" || target.port || target.username || target.password ||
officialHosts.indexOf(target.hostname) === -1 || !linkCategory(target.pathname)) {
return null;
}
return target;
} catch (_) {
return null;
}
}
function sourceContext() {
var location = window.location;
if (location.protocol !== "https:" ||
["help.teable.ai", "help.teable.cn"].indexOf(location.hostname) === -1) {
return null;
}
var parts = publicSegments(location.pathname);
if (!parts || locales.indexOf(parts[0]) === -1) { return null; }
if (parts.length > 1 && ["basic", "deploy", "api-doc", "compare"].indexOf(parts[1]) === -1 &&
!(parts.length === 2 && /^changelog(?:-\d{4}(?:-\d{2})?)?$/.test(parts[1]))) {
return null;
}
return { path: location.pathname, host: location.hostname };
}
function addSourceMarkers(target, source) {
// Keep existing campaign parameters and their encoding exactly as supplied.
var original = target.search ? target.search.slice(1).split("&") : [];
var kept = original.filter(function (entry) {
try {
var name = decodeURIComponent(entry.split("=")[0].replace(/\+/g, " "));
return name !== "help_source" && name !== "help_host";
} catch (_) { return true; }
});
kept.push("help_source=" + encodeURIComponent(source.path));
kept.push("help_host=" + encodeURIComponent(source.host));
target.search = "?" + kept.join("&");
}
function prepareLink(link) {
var target = trustedTarget(link);
if (!target) {
return;
}
var tokens = (link.getAttribute("rel") || "").split(/\s+/).filter(function (token) {
return token && token.toLowerCase() !== "noreferrer";
});
if (!tokens.some(function (token) { return token.toLowerCase() === "noopener"; })) {
tokens.push("noopener");
}
var rel = tokens.join(" ");
if (link.getAttribute("rel") !== rel) {
link.setAttribute("rel", rel);
}
var source = sourceContext();
if (source) {
addSourceMarkers(target, source);
if (link.href !== target.href) { link.setAttribute("href", target.href); }
}
}
function trackClick(event) {
if ((event.type === "click" && event.button !== 0) ||
(event.type === "auxclick" && event.button !== 1)) { return; }
var link = event.target.closest && event.target.closest("a[href]");
if (!link) { return; }
var target = trustedTarget(link);
var source = sourceContext();
if (!target || !source) { return; }
prepareLink(link);
var locale = source.path.split("/")[1];
var parameters = {
send_to: "G-LQ8H9TEDRD",
source_path: source.path,
target_path: target.pathname,
source_domain: source.host,
link_category: linkCategory(target.pathname),
locale: locale,
page_location: "https://" + source.host + source.path
};
// Queue early clicks through the same gtag data layer, without configuring
// another tag or sending a page_view. Existing consent rules still apply.
function queueEvent() {
window.dataLayer = window.dataLayer || [];
window.dataLayer.push(arguments);
}
(typeof window.gtag === "function" ? window.gtag : queueEvent)(
"event", "teable_help_link_click", parameters
);
}
function prepareTree(root) {
if (root.matches && root.matches("a[href]")) {
prepareLink(root);
}
if (root.querySelectorAll) {
root.querySelectorAll("a[href]").forEach(prepareLink);
}
}
// Mintlify inserts and updates links as users navigate between documents.
prepareTree(document);
var observedPath = window.location.pathname;
function refreshForRoute() {
if (observedPath !== window.location.pathname) {
observedPath = window.location.pathname;
prepareTree(document);
}
}
document.addEventListener("click", trackClick, true);
document.addEventListener("auxclick", trackClick, true);
window.addEventListener("popstate", refreshForRoute);
new MutationObserver(function (mutations) {
refreshForRoute();
mutations.forEach(function (mutation) {
if (mutation.type === "attributes") {
prepareLink(mutation.target);
} else {
mutation.addedNodes.forEach(prepareTree);
}
});
}).observe(document.documentElement, {
childList: true, subtree: true, attributes: true, attributeFilter: ["href", "rel"]
});
})();