forked from NSPG13/agent-bounties
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathanalytics.js
More file actions
317 lines (292 loc) · 10.8 KB
/
Copy pathanalytics.js
File metadata and controls
317 lines (292 loc) · 10.8 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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
(function () {
"use strict";
const API_URL = "https://api.agentbounties.app/v1/analytics/events";
const VISITOR_KEY = "bountyboard.analytics.visitor.v1";
const SESSION_KEY = "bountyboard.analytics.session.v1";
const ATTRIBUTION_KEY = "bountyboard.analytics.attribution.v1";
const OPT_OUT_KEY = "bountyboard.analytics.disabled.v1";
const GOOGLE_CONSENT_KEY = "agent-bounties.analytics.google-consent.v1";
const VISITOR_TTL_MS = 90 * 24 * 60 * 60 * 1000;
const HOSTS = new Set([
"agentbounties.app",
"www.agentbounties.app",
"bountyboard.global",
"www.bountyboard.global",
]);
const EVENTS = new Set([
"page_view",
"market_view",
"funded_bounty_click",
"unfunded_post_started",
"unfunded_post_completed",
"funding_started",
"claim_started",
"claim_confirmed",
"competition_entry_started",
"competition_entry_confirmed",
"competition_reveal_started",
"competition_reveal_confirmed",
"canonical_post_started",
"canonical_post_confirmed",
]);
function storage(kind) {
try {
return kind === "session" ? window.sessionStorage : window.localStorage;
} catch (_error) {
return null;
}
}
function randomId() {
if (window.crypto && typeof window.crypto.randomUUID === "function") {
return window.crypto.randomUUID();
}
const bytes = new Uint8Array(16);
window.crypto.getRandomValues(bytes);
bytes[6] = (bytes[6] & 15) | 64;
bytes[8] = (bytes[8] & 63) | 128;
const hex = Array.from(bytes, (value) => value.toString(16).padStart(2, "0"));
return `${hex.slice(0, 4).join("")}-${hex.slice(4, 6).join("")}-${hex
.slice(6, 8)
.join("")}-${hex.slice(8, 10).join("")}-${hex.slice(10).join("")}`;
}
function safeToken(value) {
const normalized = String(value || "")
.trim()
.toLowerCase()
.replace(/[^a-z0-9._-]+/g, "-")
.replace(/^-+|-+$/g, "")
.slice(0, 64);
return /^[a-z0-9][a-z0-9._-]*$/.test(normalized) ? normalized : null;
}
function privacySignalEnabled() {
const dnt = navigator.doNotTrack || window.doNotTrack || navigator.msDoNotTrack;
return navigator.globalPrivacyControl === true || dnt === "1" || dnt === "yes";
}
function explicitOptOut() {
const local = storage("local");
const params = new URLSearchParams(window.location.search);
if (params.get("analytics") === "off" && local) {
local.setItem(OPT_OUT_KEY, "true");
}
return local ? local.getItem(OPT_OUT_KEY) === "true" : true;
}
function enabled() {
return HOSTS.has(window.location.hostname) && !privacySignalEnabled() && !explicitOptOut();
}
function googleMeasurementId() {
const value = window.agentBountiesAnalyticsConfig?.googleMeasurementId;
return /^G-[A-Z0-9]+$/.test(value || "") ? value : null;
}
function googleConsent() {
const local = storage("local");
return local ? local.getItem(GOOGLE_CONSENT_KEY) : null;
}
function setGoogleConsent(value) {
const local = storage("local");
if (local) local.setItem(GOOGLE_CONSENT_KEY, value ? "granted" : "denied");
}
function loadGoogleAnalytics() {
const measurementId = googleMeasurementId();
if (!measurementId || privacySignalEnabled() || explicitOptOut()) return false;
if (document.querySelector(`script[data-google-tag="${measurementId}"]`)) return true;
window.dataLayer = window.dataLayer || [];
window.gtag = function () {
window.dataLayer.push(arguments);
};
window.gtag("js", new Date());
window.gtag("config", measurementId, {
allow_google_signals: false,
allow_ad_personalization_signals: false,
});
const script = document.createElement("script");
script.async = true;
script.dataset.googleTag = measurementId;
script.src = `https://www.googletagmanager.com/gtag/js?id=${encodeURIComponent(
measurementId
)}`;
document.head.appendChild(script);
return true;
}
function offerGoogleAnalytics() {
if (!googleMeasurementId() || privacySignalEnabled() || explicitOptOut()) return;
const consent = googleConsent();
if (consent === "granted") {
loadGoogleAnalytics();
return;
}
if (consent === "denied" || document.querySelector("[data-google-analytics-consent]")) return;
const notice = document.createElement("aside");
notice.className = "analytics-consent";
notice.dataset.googleAnalyticsConsent = "";
notice.setAttribute("aria-label", "Optional analytics choice");
notice.innerHTML =
'<p><strong>Help improve Agent Bounties?</strong> Allow Google Analytics for anonymous traffic and page-use measurement. Wallets, bounty evidence, and payment data are not sent.</p>' +
'<div class="actions"><button class="button primary" type="button" data-google-analytics-allow>Allow</button><button class="button secondary" type="button" data-google-analytics-deny>No thanks</button><a href="privacy.html">Privacy</a></div>';
document.body.appendChild(notice);
notice.querySelector("[data-google-analytics-allow]").addEventListener("click", function () {
setGoogleConsent(true);
notice.remove();
loadGoogleAnalytics();
});
notice.querySelector("[data-google-analytics-deny]").addEventListener("click", function () {
setGoogleConsent(false);
notice.remove();
});
}
function browserId() {
const local = storage("local");
if (!local) return null;
const now = Date.now();
try {
const existing = JSON.parse(local.getItem(VISITOR_KEY) || "null");
if (existing && existing.id && existing.expires_at > now) {
return existing.id;
}
} catch (_error) {
// Replace corrupt first-party state with a new opaque identifier.
}
const next = { id: randomId(), expires_at: now + VISITOR_TTL_MS };
local.setItem(VISITOR_KEY, JSON.stringify(next));
return next.id;
}
function sessionId() {
const session = storage("session");
if (!session) return null;
let id = session.getItem(SESSION_KEY);
if (!id) {
id = randomId();
session.setItem(SESSION_KEY, id);
}
return id;
}
function currentAttribution() {
const local = storage("local");
if (!local) return { source: "direct", campaign: null, referrer_host: null };
try {
const existing = JSON.parse(local.getItem(ATTRIBUTION_KEY) || "null");
if (existing && existing.expires_at > Date.now()) return existing;
} catch (_error) {
// Replace corrupt first-touch state below.
}
const params = new URLSearchParams(window.location.search);
const utmSource = safeToken(params.get("utm_source"));
const sharedFrom = safeToken(params.get("from"));
const campaign = safeToken(params.get("utm_campaign"));
let referrerHost = null;
try {
const candidate = document.referrer ? new URL(document.referrer).hostname.toLowerCase() : null;
if (candidate && !HOSTS.has(candidate) && /^[a-z0-9.-]+$/.test(candidate)) {
referrerHost = candidate.slice(0, 253);
}
} catch (_error) {
referrerHost = null;
}
const attribution = {
source: utmSource || sharedFrom || safeToken(referrerHost) || "direct",
campaign,
referrer_host: referrerHost,
expires_at: Date.now() + VISITOR_TTL_MS,
};
local.setItem(ATTRIBUTION_KEY, JSON.stringify(attribution));
return attribution;
}
function validDetail(value, pattern, maxLength) {
if (!value) return null;
const normalized = String(value).trim().slice(0, maxLength);
return pattern.test(normalized) ? normalized : null;
}
function track(eventName, details) {
if (!enabled() || !EVENTS.has(eventName)) return false;
const visitorId = browserId();
const currentSessionId = sessionId();
if (!visitorId || !currentSessionId) return false;
const attribution = currentAttribution();
const detail = details || {};
const params = new URLSearchParams(window.location.search);
const opportunityId = validDetail(
detail.opportunity_id || params.get("discovery_id"),
/^[A-Za-z0-9:/._-]+$/,
200,
);
const bountyContract = validDetail(
detail.bounty_contract || params.get("bountyContract"),
/^0x[0-9a-fA-F]{40}$/,
42,
);
const event = {
event_id: randomId(),
visitor_id: visitorId,
session_id: currentSessionId,
event_name: eventName,
page_path: window.location.pathname.slice(0, 160) || "/",
source: safeToken(attribution.source) || "direct",
campaign: safeToken(attribution.campaign),
referrer_host: attribution.referrer_host,
opportunity_id: opportunityId,
bounty_contract: bountyContract ? bountyContract.toLowerCase() : null,
occurred_at: new Date().toISOString(),
};
window.fetch(API_URL, {
method: "POST",
headers: { "content-type": "application/json" },
body: JSON.stringify(event),
credentials: "omit",
referrerPolicy: "no-referrer",
keepalive: true,
}).catch(function () {
// Analytics must never block or alter a bounty action.
});
if (window.gtag && googleConsent() === "granted" && eventName !== "page_view") {
window.gtag("event", eventName, { page_path: event.page_path });
}
return true;
}
function optOut() {
const local = storage("local");
const session = storage("session");
if (local) {
local.setItem(OPT_OUT_KEY, "true");
local.setItem(GOOGLE_CONSENT_KEY, "denied");
local.removeItem(VISITOR_KEY);
local.removeItem(ATTRIBUTION_KEY);
}
const measurementId = googleMeasurementId();
if (measurementId) window[`ga-disable-${measurementId}`] = true;
if (session) session.removeItem(SESSION_KEY);
return status();
}
function optIn() {
const local = storage("local");
if (local) local.removeItem(OPT_OUT_KEY);
return status();
}
function status() {
return {
enabled: enabled(),
explicit_opt_out: explicitOptOut(),
privacy_signal: privacySignalEnabled(),
google_analytics: googleConsent(),
};
}
const analytics = { track, optOut, optIn, status };
window.agentBountiesAnalytics = analytics;
window.bountyBoardAnalytics = analytics;
document.addEventListener("click", function (event) {
const target = event.target.closest("[data-analytics-event]");
if (!target) return;
track(target.dataset.analyticsEvent, {
opportunity_id: target.dataset.analyticsOpportunityId,
bounty_contract: target.dataset.analyticsBountyContract,
});
});
document.addEventListener("click", function (event) {
const optOutControl = event.target.closest("[data-analytics-opt-out]");
if (!optOutControl) return;
optOut();
document.querySelector("[data-google-analytics-consent]")?.remove();
optOutControl.textContent = "Analytics disabled on this browser";
optOutControl.setAttribute("aria-pressed", "true");
});
offerGoogleAnalytics();
track("page_view");
})();