Repository navigation
Expand file tree
/
Copy pathanalytics.js
More file actions
220 lines (202 loc) · 9.31 KB
/
Copy pathanalytics.js
File metadata and controls
220 lines (202 loc) · 9.31 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
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
(function () {
// Skip analytics for developers and bots.
// Set once in your browser console: localStorage.setItem('hvt_notrack', '1')
try {
if (localStorage.getItem("hvt_notrack") === "1") return;
} catch (_) {}
var ua = navigator.userAgent || "";
// Automation (Puppeteer, Playwright, Selenium) sets navigator.webdriver even
// when it spoofs a normal Chrome user agent.
if (navigator.webdriver) return;
if (/HeadlessChrome|Puppeteer|Playwright|Claude\/[\d.]+.*Electron\/|bot|crawl|spider|curl|wget|python-requests/i.test(ua)) return;
var ATTRIBUTION_KEY = "hvtracker_attribution_v1";
var LAST_COMPARE_KEY = "hvtracker_last_compare_v1";
function safeStorage(kind) {
try {
return kind === "local" ? window.localStorage : window.sessionStorage;
} catch (_) {
return null;
}
}
function readJson(storage, key) {
if (!storage) return null;
try {
var raw = storage.getItem(key);
return raw ? JSON.parse(raw) : null;
} catch (_) {
return null;
}
}
function writeJson(storage, key, value) {
if (!storage) return;
try {
storage.setItem(key, JSON.stringify(value));
} catch (_) {}
}
function inferPageType(pathname) {
if (pathname === "/" || pathname === "") return "leaderboard";
if (pathname.indexOf("/agents/") === 0) return "agent_profile";
if (pathname.indexOf("/categories/") === 0) return "category_page";
if (pathname.indexOf("/blog/") === 0) {
return pathname === "/blog/" || pathname === "/blog" ? "blog_index" : "blog_article";
}
if (pathname.indexOf("/compare/") === 0 || pathname === "/compare") return "compare_page";
if (pathname.indexOf("/methodology") === 0) return "methodology";
if (pathname.indexOf("/spec/") === 0 || pathname === "/spec") return "spec_page";
if (pathname.indexOf("/badges/") === 0 || pathname === "/badges") return "badges";
if (pathname.indexOf("/roadmap/") === 0 || pathname === "/roadmap") return "roadmap";
return "unknown";
}
function trimValue(value, maxLen) {
return value ? String(value).slice(0, maxLen) : "";
}
function captureAttribution() {
var sessionStore = safeStorage("session");
var localStore = safeStorage("local");
var params = new URLSearchParams(window.location.search);
var hasUtm = ["utm_source", "utm_medium", "utm_campaign", "utm_term", "utm_content"].some(function (key) {
return params.get(key);
});
var referrer = document.referrer || "";
var referrerUrl = null;
var externalReferrer = "";
try {
referrerUrl = referrer ? new URL(referrer) : null;
if (referrerUrl && referrerUrl.hostname !== window.location.hostname) {
externalReferrer = referrerUrl.hostname;
}
} catch (_) {}
var existing = readJson(sessionStore, ATTRIBUTION_KEY) || readJson(localStore, ATTRIBUTION_KEY) || {};
var next = existing;
if (hasUtm || externalReferrer || !existing.source) {
next = {
source: trimValue(params.get("utm_source"), 100) || externalReferrer || existing.source || "(direct)",
medium: trimValue(params.get("utm_medium"), 100) || (externalReferrer ? "referral" : existing.medium || "(none)"),
campaign: trimValue(params.get("utm_campaign"), 120) || existing.campaign || "",
term: trimValue(params.get("utm_term"), 120) || existing.term || "",
content: trimValue(params.get("utm_content"), 120) || existing.content || "",
referrer_host: trimValue(externalReferrer, 120) || existing.referrer_host || "",
landing_path: existing.landing_path || window.location.pathname,
landing_page: existing.landing_page || window.location.pathname + window.location.search
};
writeJson(sessionStore, ATTRIBUTION_KEY, next);
writeJson(localStore, ATTRIBUTION_KEY, next);
}
return next;
}
function baseParams() {
var body = document.body || {};
var data = body.dataset || {};
var attribution = captureAttribution();
var params = {
page_type: data.pageType || inferPageType(window.location.pathname),
page_path: window.location.pathname
};
if (data.agentSlug) params.agent_slug = data.agentSlug;
if (data.agentName) params.agent_name = data.agentName;
if (data.category) params.category = data.category;
if (data.globalRank) params.global_rank = Number(data.globalRank);
if (data.trustScore) params.trust_score = Number(data.trustScore);
if (attribution.source) params.session_source = attribution.source;
if (attribution.medium) params.session_medium = attribution.medium;
if (attribution.campaign) params.session_campaign = attribution.campaign;
if (attribution.referrer_host) params.referrer_host = attribution.referrer_host;
return params;
}
window.hvtTrack = function (eventName, params) {
if (typeof window.gtag !== "function") return;
window.gtag("event", eventName, Object.assign(baseParams(), params || {}));
};
captureAttribution();
if (typeof window.gtag === "function") {
window.hvtTrack("hvt_pageview", {
page_location: window.location.href,
page_title: document.title
});
}
document.addEventListener("click", function (event) {
var link = event.target.closest("a");
if (!link) return;
var href = link.getAttribute("href") || "";
var url = new URL(link.href, window.location.href);
var params = {
link_url: url.href,
link_text: link.textContent.trim().slice(0, 80)
};
var isBadgesPath = url.pathname === "/badges/" || url.pathname === "/badges";
var isComparePath = url.pathname.startsWith("/compare/");
var isRoadmapPath = url.pathname === "/roadmap/" || url.pathname === "/roadmap";
// Correction URLs are GitHub issue templates with our [Correction] title prefix.
var isCorrectionLink = url.hostname === "github.com" && href.indexOf("%5BCorrection%5D") !== -1;
// Leaving the /badges/ page via an in-site, same-tab link (badge adoption funnel).
// Fired alongside the destination event below so we capture both the exit and where it went.
if (document.body.dataset.pageType === "badges" && !link.target &&
!isBadgesPath && url.hostname === window.location.hostname) {
window.hvtTrack("badges_exit", Object.assign({}, params, { exit_to: url.pathname }));
}
if (link.closest(".pr-offer")) {
window.hvtTrack("badge_pr_click", params);
} else if (isCorrectionLink) {
// Distinct from generic github_issue_click — correction is a conversion event.
window.hvtTrack("correction_click", Object.assign({}, params, {
from_area: link.closest(".maintainer-cta") ? "maintainer_cta" :
link.closest(".review-actions") ? "review_card" :
link.closest(".hero-actions") ? "hero" : "other"
}));
} else if (isComparePath) {
window.hvtTrack("compare_click", Object.assign({}, params, {
from_area: link.closest(".compare-strip") ? "compare_strip" :
link.closest(".review-actions") ? "review_card" :
link.closest(".hero-actions") ? "hero" :
link.closest(".siblings") ? "head_to_head" : "other"
}));
} else if (isRoadmapPath) {
window.hvtTrack("roadmap_click", params);
} else if (isBadgesPath) {
// Landing on /badges/: distinguish the per-agent "Badge guide for maintainers" link.
window.hvtTrack(link.closest("#embed-badge") ? "badge_guide_click" : "badges_click", params);
} else if (url.pathname.startsWith("/agents/")) {
var parts = url.pathname.split("/").filter(Boolean);
window.hvtTrack("agent_click", Object.assign(params, {
clicked_agent_slug: parts[1] || "",
link_area: link.closest(".movers") ? "movers" :
link.closest(".siblings") ? "siblings" :
document.body.dataset.pageType || "unknown"
}));
} else if (url.pathname.startsWith("/data/")) {
window.hvtTrack("data_api_click", Object.assign(params, { endpoint: url.pathname }));
} else if (url.pathname.startsWith("/methodology")) {
window.hvtTrack("methodology_click", params);
} else if (url.pathname.startsWith("/spec")) {
window.hvtTrack("specs_click", params);
} else if (url.pathname === "/") {
window.hvtTrack("leaderboard_click", params);
} else if (url.hostname === "github.com") {
window.hvtTrack(href.includes("/issues/new") || href.includes("/issues")
? "github_issue_click"
: "github_click", params);
} else if (url.hostname !== window.location.hostname) {
window.hvtTrack("outbound_click", Object.assign(params, { outbound_domain: url.hostname }));
}
});
if (window.location.pathname.indexOf("/compare") === 0) {
var compareState = window.location.search;
var sessionStore = safeStorage("session");
var previousState = sessionStore ? sessionStore.getItem(LAST_COMPARE_KEY) : null;
if (compareState && compareState !== previousState) {
var selections = new URLSearchParams(window.location.search).get("a") || "";
var slugs = selections ? selections.split(",").filter(Boolean) : [];
if (slugs.length >= 2) {
window.hvtTrack("compare_view", {
compared_agents: slugs.join(","),
compared_count: slugs.length
});
}
}
if (sessionStore) {
try {
sessionStore.setItem(LAST_COMPARE_KEY, compareState);
} catch (_) {}
}
}
})();