|
| 1 | +--- |
| 2 | +import { env } from "cloudflare:workers"; |
| 3 | +
|
| 4 | +const runtimeEnv = env as unknown as Record<string, string | undefined>; |
| 5 | +const posthogKey = |
| 6 | + runtimeEnv.PUBLIC_POSTHOG_KEY || import.meta.env.PUBLIC_POSTHOG_KEY || ""; |
| 7 | +const posthogUiHost = |
| 8 | + runtimeEnv.PUBLIC_POSTHOG_UI_HOST || |
| 9 | + import.meta.env.PUBLIC_POSTHOG_UI_HOST || |
| 10 | + "https://us.posthog.com"; |
| 11 | +--- |
| 12 | + |
| 13 | +<script is:inline define:vars={{ posthogKey, posthogUiHost }}> |
| 14 | + (function () { |
| 15 | + function sendBeaconEvent(type, detail) { |
| 16 | + var payload = JSON.stringify({ |
| 17 | + type: type, |
| 18 | + path: window.location.pathname, |
| 19 | + target: (detail && detail.target) || "", |
| 20 | + label: (detail && detail.label) || "", |
| 21 | + value: (detail && detail.value) || 1, |
| 22 | + }); |
| 23 | + |
| 24 | + try { |
| 25 | + if (navigator.sendBeacon) { |
| 26 | + navigator.sendBeacon("/api/event", payload); |
| 27 | + } else { |
| 28 | + fetch("/api/event", { |
| 29 | + method: "POST", |
| 30 | + body: payload, |
| 31 | + keepalive: true, |
| 32 | + headers: { "Content-Type": "application/json" }, |
| 33 | + }).catch(function () {}); |
| 34 | + } |
| 35 | + } catch (_e) {} |
| 36 | + |
| 37 | + try { |
| 38 | + if (window.posthog && typeof window.posthog.capture === "function") { |
| 39 | + window.posthog.capture(type, { |
| 40 | + path: window.location.pathname, |
| 41 | + target: detail && detail.target, |
| 42 | + label: detail && detail.label, |
| 43 | + value: detail && detail.value, |
| 44 | + }); |
| 45 | + } |
| 46 | + } catch (_e) {} |
| 47 | + } |
| 48 | + |
| 49 | + // Global helper for future LMS course/lesson components & auth gates |
| 50 | + window.ccTrack = sendBeaconEvent; |
| 51 | + |
| 52 | + // Passive click delegation for [data-track], outbound links, search, and chat |
| 53 | + document.addEventListener( |
| 54 | + "click", |
| 55 | + function (e) { |
| 56 | + var target = e.target; |
| 57 | + if (!(target instanceof Element)) return; |
| 58 | + |
| 59 | + var trackedEl = target.closest("[data-track]"); |
| 60 | + if (trackedEl) { |
| 61 | + var eventType = trackedEl.getAttribute("data-track") || "ui_interaction"; |
| 62 | + var label = |
| 63 | + trackedEl.getAttribute("data-track-label") || |
| 64 | + trackedEl.getAttribute("aria-label") || |
| 65 | + (trackedEl.textContent || "").trim().slice(0, 80); |
| 66 | + var href = |
| 67 | + trackedEl.getAttribute("data-track-target") || |
| 68 | + trackedEl.getAttribute("href") || |
| 69 | + ""; |
| 70 | + sendBeaconEvent(eventType, { target: href, label: label }); |
| 71 | + return; |
| 72 | + } |
| 73 | + |
| 74 | + var anchor = target.closest("a[href]"); |
| 75 | + if (anchor) { |
| 76 | + var rawHref = anchor.getAttribute("href") || ""; |
| 77 | + if (rawHref.indexOf("http") === 0) { |
| 78 | + try { |
| 79 | + var parsed = new URL(rawHref, window.location.origin); |
| 80 | + if (parsed.hostname !== window.location.hostname) { |
| 81 | + var linkLabel = |
| 82 | + anchor.getAttribute("aria-label") || |
| 83 | + (anchor.textContent || "").trim().slice(0, 80) || |
| 84 | + parsed.hostname; |
| 85 | + sendBeaconEvent("outbound_click", { |
| 86 | + target: rawHref, |
| 87 | + label: linkLabel, |
| 88 | + }); |
| 89 | + } |
| 90 | + } catch (_e) {} |
| 91 | + } |
| 92 | + } |
| 93 | + }, |
| 94 | + { passive: true, capture: true }, |
| 95 | + ); |
| 96 | + |
| 97 | + // Track podcast audio plays passively |
| 98 | + document.addEventListener( |
| 99 | + "play", |
| 100 | + function (e) { |
| 101 | + var el = e.target; |
| 102 | + if (el instanceof HTMLAudioElement) { |
| 103 | + sendBeaconEvent("podcast_listen", { |
| 104 | + target: el.currentSrc || el.src || window.location.pathname, |
| 105 | + label: "html5_audio", |
| 106 | + }); |
| 107 | + } |
| 108 | + }, |
| 109 | + { passive: true, capture: true }, |
| 110 | + ); |
| 111 | + |
| 112 | + // Optional PostHog initialization via first-party /ingest reverse proxy |
| 113 | + if (posthogKey) { |
| 114 | + !(function (t, e) { |
| 115 | + var o, n, p, r; |
| 116 | + e.__SV || |
| 117 | + ((window.posthog = e), |
| 118 | + (e._i = []), |
| 119 | + (e.init = function (i, s, a) { |
| 120 | + function g(t, e) { |
| 121 | + var o = e.split("."); |
| 122 | + 2 == o.length && ((t = t[o[0]]), (e = o[1])), |
| 123 | + (t[e] = function () { |
| 124 | + t.push([e].concat(Array.prototype.slice.call(arguments, 0))); |
| 125 | + }); |
| 126 | + } |
| 127 | + ((p = t.createElement("script")).type = "text/javascript"), |
| 128 | + (p.crossOrigin = "anonymous"), |
| 129 | + (p.async = !0), |
| 130 | + (p.src = |
| 131 | + s.api_host.replace(".i.posthog.com", "-assets.i.posthog.com") + |
| 132 | + "/static/array.js"), |
| 133 | + (r = t.getElementsByTagName("script")[0]).parentNode.insertBefore( |
| 134 | + p, |
| 135 | + r, |
| 136 | + ); |
| 137 | + var u = e; |
| 138 | + for ( |
| 139 | + void 0 !== a ? (u = e[a] = []) : (a = "posthog"), |
| 140 | + u.people = u.people || [], |
| 141 | + u.toString = function (t) { |
| 142 | + var e = "posthog"; |
| 143 | + return ( |
| 144 | + "posthog" !== a && (e += "." + a), t || (e += " (stub)"), e |
| 145 | + ); |
| 146 | + }, |
| 147 | + u.people.toString = function () { |
| 148 | + return u.toString(1) + ".people (stub)"; |
| 149 | + }, |
| 150 | + o = |
| 151 | + "init capture register register_once register_for_session unregister opt_out_capturing has_opted_out_capturing opt_in_capturing reset isFeatureEnabled onFeatureFlags getFeatureFlag getFeatureFlagPayload reloadFeatureFlags group updateEarlyAccessFeatureEnrollment getEarlyAccessFeatures getActiveMatchingSurveys getSurveys getNextSurveyStep onSessionId setPersonPropertiesForFlags".split( |
| 152 | + " ", |
| 153 | + ), |
| 154 | + n = 0; |
| 155 | + n < o.length; |
| 156 | + n++ |
| 157 | + ) |
| 158 | + g(u, o[n]); |
| 159 | + e._i.push([i, s, a]); |
| 160 | + }), |
| 161 | + (e.__SV = 1)); |
| 162 | + })(document, window.posthog || []); |
| 163 | + |
| 164 | + window.posthog.init(posthogKey, { |
| 165 | + api_host: "/ingest", |
| 166 | + ui_host: posthogUiHost, |
| 167 | + person_profiles: "identified_only", |
| 168 | + capture_pageview: true, |
| 169 | + capture_pageleave: true, |
| 170 | + disable_session_recording: |
| 171 | + window.location.pathname.indexOf("/courses") !== 0, |
| 172 | + }); |
| 173 | + } |
| 174 | + })(); |
| 175 | +</script> |
0 commit comments