Skip to content

The Open Standard for Generative UI

Codex/add thesys link attribution (#825)

Zahle Khan55d47e23f7b0
diff --git a/docs/app/providers.tsx b/docs/app/providers.tsx
index b9577fc28..253ebec37 100644
--- a/docs/app/providers.tsx
+++ b/docs/app/providers.tsx
@@ -1,5 +1,6 @@
"use client";
+import { addThesysLinkAttribution } from "@/lib/thesys-link-attribution";
import posthog from "posthog-js";
import { PostHogProvider } from "posthog-js/react";
import { useEffect } from "react";
@@ -14,6 +15,62 @@ export function PHProvider({ children }: { children: React.ReactNode }) {
sampleRate: 0.1,
},
});
+
+ const decorateLink = (anchor: HTMLAnchorElement) => {
+ const href = anchor.getAttribute("href");
+ if (!href) return;
+
+ const attributedHref = addThesysLinkAttribution(
+ href,
+ window.location.href,
+ posthog.get_distinct_id(),
+ posthog.get_session_id(),
+ );
+
+ if (attributedHref !== href) anchor.setAttribute("href", attributedHref);
+ };
+
+ const decorateLinksWithin = (root: ParentNode) => {
+ if (root instanceof HTMLAnchorElement) decorateLink(root);
+ root.querySelectorAll<HTMLAnchorElement>("a[href]").forEach(decorateLink);
+ };
+
+ decorateLinksWithin(document);
+
+ const observer = new MutationObserver((mutations) => {
+ for (const mutation of mutations) {
+ if (mutation.type === "attributes") {
+ if (mutation.target instanceof HTMLAnchorElement) decorateLink(mutation.target);
+ continue;
+ }
+
+ for (const node of mutation.addedNodes) {
+ if (node instanceof Element) decorateLinksWithin(node);
+ }
+ }
+ });
+
+ observer.observe(document.body, {
+ attributes: true,
+ attributeFilter: ["href"],
+ childList: true,
+ subtree: true,
+ });
+
+ const refreshLinkAtInteraction = (event: Event) => {
+ if (!(event.target instanceof Element)) return;
+ const anchor = event.target.closest<HTMLAnchorElement>("a[href]");
+ if (anchor) decorateLink(anchor);
+ };
+
+ document.addEventListener("pointerdown", refreshLinkAtInteraction, true);
+ document.addEventListener("click", refreshLinkAtInteraction, true);
+
+ return () => {
+ observer.disconnect();
+ document.removeEventListener("pointerdown", refreshLinkAtInteraction, true);
+ document.removeEventListener("click", refreshLinkAtInteraction, true);
+ };
}, []);
return <PostHogProvider client={posthog}>{children}</PostHogProvider>;
}
diff --git a/docs/lib/thesys-link-attribution.ts b/docs/lib/thesys-link-attribution.ts
new file mode 100644
index 000000000..132ae8cb9
--- /dev/null
+++ b/docs/lib/thesys-link-attribution.ts
@@ -0,0 +1,45 @@
+const THESYS_HOST = "thesys.dev";
+
+const UTM_PARAMS = {
+ utm_source: "openui",
+ utm_medium: "referral",
+ utm_campaign: "openui_to_thesys",
+} as const;
+
+export const POSTHOG_DISTINCT_ID_PARAM = "posthog_distinct_id";
+export const POSTHOG_SESSION_ID_PARAM = "posthog_session_id";
+
+function isThesysHost(hostname: string) {
+ return hostname === THESYS_HOST || hostname.endsWith(`.${THESYS_HOST}`);
+}
+
+export function addThesysLinkAttribution(
+ href: string,
+ baseUrl: string,
+ posthogDistinctId: string | undefined,
+ posthogSessionId: string | undefined,
+) {
+ let url: URL;
+
+ try {
+ url = new URL(href, baseUrl);
+ } catch {
+ return href;
+ }
+
+ if (!isThesysHost(url.hostname)) return href;
+
+ for (const [key, value] of Object.entries(UTM_PARAMS)) {
+ if (!url.searchParams.has(key)) url.searchParams.set(key, value);
+ }
+
+ if (posthogDistinctId) {
+ url.searchParams.set(POSTHOG_DISTINCT_ID_PARAM, posthogDistinctId);
+ }
+
+ if (posthogSessionId) {
+ url.searchParams.set(POSTHOG_SESSION_ID_PARAM, posthogSessionId);
+ }
+
+ return url.toString();
+}