diff --git a/docs/download-attribution.js b/docs/download-attribution.js index 0b34eceb01..67f8e20893 100644 --- a/docs/download-attribution.js +++ b/docs/download-attribution.js @@ -3,33 +3,46 @@ // so the download site cannot read this one. Mintlify runs every .js file in docs/ on every page // (mintlify.com/docs/customize/custom-scripts). The id is added at click time only, so a copied link never carries it, // and only when PostHog is running here and the visitor has not opted out of it. -// They also name where this visit began, as the homepage's download links do: the first docs page's utm tags, the -// kind of ad click id it came with (never the id) and `ref`, the site that sent it. The download site reads them as -// the visit's landing source: the click's own Referer is these docs, or nothing at all, since Mintlify's navbar -// button is rel="noreferrer". +// Every link to hyperframes.studio (download, pricing, changelog, community) also names where this visit began, as +// the homepage's links do: its utm tags, the kind of ad click id it came with (never the id) and `ref`, the site that +// sent it. hyperframes.studio keeps that as the visit's landing source for a later download or sign-in: the click's +// own Referer is these docs, or nothing at all, since Mintlify's navbar button is rel="noreferrer". (function () { var TOKEN = "phc_zjjbX0PnWxERXrMHhkEJWj9A9BhGVLRReICgsfTMmpx"; var STATE = "ph_" + TOKEN + "_posthog"; // posthog-js keeps a visitor's opt-out under this key: "0" opted out, "1" opted in. It lives in localStorage by // default, in a cookie when a site configures that, so both are read. var OPT_OUT = "__ph_opt_in_out_" + TOKEN; - // The one download address the docs link to (docs.json navbar and footer, quickstart.mdx). - var DOWNLOAD = { protocol: "https:", hostname: "hyperframes.studio", pathname: "/studio/download" }; + var STUDIO = "hyperframes.studio"; + // The one address that reads `ph_id` (docs.json navbar and footer, quickstart.mdx link to it). + var DOWNLOAD = "/studio/download"; var UUID = /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i; // The campaign tags and ad click ids the download site reads, in its order. var CAMPAIGN = ["utm_source", "utm_medium", "utm_campaign", "utm_term", "utm_content"]; var AD_CLICKS = ["gclid", "gbraid", "wbraid", "dclid", "fbclid", "msclkid", "ttclid", "twclid", "li_fat_id", "rdt_cid", "sccid", "epik"]; // Our own sites and HeyGen's sign-in: a visit that moved between them says nothing about where it began. var NOT_A_SOURCE = /(^|\.)hyperframes\.(heygen\.com|studio|dev)$|^(app|accounts|api2)\.heygen\.com$/; - // The first docs page of this visit's source, kept for the tab: a later page's Referer is these docs. - var LANDING = "hf_docs_landing"; + // Where this visit began, shared with the homepage (web-marketing downloadUtms.ts, the same name, rules and + // .heygen.com scope), so a visit that moves between the docs and the homepage keeps its first source either way, for + // 30 days. A page that names no source keeps nothing; the first source is never replaced. + var LANDING = "hf_hub_landing"; + var LANDING_DAYS = 30; + // Longer tags than hyperframes.studio keeps (its TAG_MAX): it drops them, and the shared cookie must stay small. + var TAG_MAX = 64; + // The address a link had before a right-click menu put the source on it. + var UNTAGGED = "data-hf-untagged-href"; - function cookie(name) { + function rawCookie(name) { var prefix = name + "="; var raw = document.cookie.split("; ").find(function (c) { return c.indexOf(prefix) === 0; }); - return raw ? decodeURIComponent(raw.slice(prefix.length)) : null; + return raw ? raw.slice(prefix.length) : null; + } + + function cookie(name) { + var raw = rawCookie(name); + return raw === null ? null : decodeURIComponent(raw); } function stored(name) { @@ -73,7 +86,7 @@ var out = new URLSearchParams(); CAMPAIGN.forEach(function (name) { var value = page.get(name); - if (value) out.set(name, value); + if (value && value.length <= TAG_MAX) out.set(name, value); }); var adClick = AD_CLICKS.find(function (name) { return (page.get(name) || "").trim(); @@ -89,44 +102,57 @@ return out.toString(); } - // Kept from the first page this script ran on in the tab; sessionStorage blocked (some privacy modes) falls back - // to this page's own source. + // The kept source, else this page's own (cookies blocked: this page's). URLSearchParams' encoding is already + // cookie-safe (no space, comma or semicolon). var landing = visitSource(); try { - var kept = window.sessionStorage.getItem(LANDING); - if (kept === null) window.sessionStorage.setItem(LANDING, landing); - else landing = kept; + // Read as written: its value is already URL-encoded, and decoding would split a tag holding "&" or "=". + var kept = rawCookie(LANDING); + if (kept) landing = kept; + else if (landing) { + var heygen = /(^|\.)heygen\.com$/.test(window.location.hostname) ? "; domain=.heygen.com" : ""; + document.cookie = LANDING + "=" + landing + "; path=/" + heygen + "; max-age=" + LANDING_DAYS * 86400 + "; SameSite=Lax; Secure"; + } } catch { // keep this page's } - function tag(event) { + // The link to hyperframes.studio an event is on, as a URL, or null for any other link. + function studioLink(event) { var link = event.target instanceof Element && event.target.closest("a[href]"); - if (!link) return; - var url; + if (!link) return null; try { - url = new URL(link.href); + var url = new URL(link.href); + return url.protocol === "https:" && url.hostname === STUDIO && url.port === "" ? { link: link, url: url } : null; } catch { - return; + return null; } - if ( - url.protocol !== DOWNLOAD.protocol || - url.hostname !== DOWNLOAD.hostname || - url.port !== "" || - url.pathname !== DOWNLOAD.pathname - ) { - return; - } - var original = link.getAttribute("href"); - // A tag the link already names keeps its own value. Only the source's own names come back from storage, so a value - // kept there can never add any other parameter (ph_id belongs to the consent check below). + } + + // A tag the link already names keeps its own value. Only the source's own names come back from the cookie, under the + // same length cap as a fresh one, so a value kept there can never add any other parameter or grow a link. + function addSource(url) { new URLSearchParams(landing).forEach(function (value, name) { - var known = CAMPAIGN.indexOf(name) !== -1 || name === "ref" || (name === "ad_click" && AD_CLICKS.indexOf(value) !== -1); + var known = + (CAMPAIGN.indexOf(name) !== -1 && value.length <= TAG_MAX) || + name === "ref" || + (name === "ad_click" && AD_CLICKS.indexOf(value) !== -1); if (known && !url.searchParams.has(name)) url.searchParams.set(name, value); }); - var id = posthogLoaded() && !optedOut() && posthogId(); + } + + function tag(event) { + var found = studioLink(event); + if (!found) return; + var link = found.link; + var url = found.url; + var original = link.getAttribute("href"); + addSource(url); + var id = url.pathname === DOWNLOAD && posthogLoaded() && !optedOut() && posthogId(); if (id) url.searchParams.set("ph_id", id); - if (url.toString() === new URL(link.href).toString()) return; + // Nothing to add: the link as written (before a right-click menu tagged it) is what it already says. A Card compares + // against that, not its current href: it follows its own href prop, which a menu-tagged href doesn't change. + if (url.toString() === new URL(link.getAttribute(UNTAGGED) || link.href, location.href).toString()) return; // Mintlify's (its anchor carries data-client-link) opens its own href prop from React, never the link's current // href, so its click is followed here: a new tab for a middle click, Cmd/Ctrl-click or a target="_blank" link, else // this tab. Every other link keeps the browser's own follow of the href set below, so Mintlify's click handlers @@ -147,7 +173,19 @@ }, 0); } + // A right-click menu's Open in new tab or Copy link takes the href as it stands when the menu opens: the source goes on + // and stays (it names no person); the id never does, as it is only added for a click and taken off after. + function tagForMenu(event) { + var found = studioLink(event); + if (!found) return; + addSource(found.url); + if (found.url.toString() === found.link.href) return; + if (!found.link.hasAttribute(UNTAGGED)) found.link.setAttribute(UNTAGGED, found.link.href); + found.link.href = found.url.toString(); + } + // Capture phase, before the browser follows the link; auxclick covers a middle-click into a new tab. document.addEventListener("click", tag, true); document.addEventListener("auxclick", tag, true); + document.addEventListener("contextmenu", tagForMenu, true); })();