AutoGPT is the vision of accessible AI for everyone, to use and to build on. Our mission is to provide the tools, so that you can focus on what matters.
fix(frontend): keep the website visitor's PostHog id when they sign up (#15940)
Krzysztof Czerwinski9c77fa1b671f
diff --git a/autogpt_platform/frontend/src/app/(no-navbar)/login/__tests__/cookie-settings.test.tsx b/autogpt_platform/frontend/src/app/(no-navbar)/login/__tests__/cookie-settings.test.tsxindex 5dc67263a6..66cf287a1e 100644--- a/autogpt_platform/frontend/src/app/(no-navbar)/login/__tests__/cookie-settings.test.tsx+++ b/autogpt_platform/frontend/src/app/(no-navbar)/login/__tests__/cookie-settings.test.tsx@@ -5,7 +5,12 @@ import {installCookiebot,removeCookiebot,} from "@/tests/integrations/cookiebot";-import { fireEvent, render, screen } from "@/tests/integrations/test-utils";+import {+ act,+ fireEvent,+ render,+ screen,+} from "@/tests/integrations/test-utils";import LoginPage from "../page";const mockUseAuth = vi.hoisted(() => vi.fn());@@ -50,6 +55,32 @@ describe("LoginPage cookie settings", () => {expect(renew).toHaveBeenCalledOnce();});+ test("shows the link once Cookiebot asks a visitor with no answer yet", async () => {+ // uc.js loads after hydration, and can arrive after the page finished+ // loading; a visitor with no answer gets no consent event, only the+ // dialog's.+ configureCookiebot();+ vi.spyOn(document, "readyState", "get").mockReturnValue("complete");++ render(<LoginPage />);+ expect(+ await screen.findByText("Log in to your account to continue"),+ ).toBeDefined();+ expect(+ screen.queryByRole("button", { name: "Cookie settings" }),+ ).toBeNull();++ const { renew } = installCookiebot();+ act(() => {+ window.dispatchEvent(new Event("CookiebotOnDialogInit"));+ });+ fireEvent.click(+ await screen.findByRole("button", { name: "Cookie settings" }),+ );++ expect(renew).toHaveBeenCalledOnce();+ });+test("hides the link when no consent banner is configured", async () => {vi.stubEnv("NEXT_PUBLIC_COOKIEBOT_CBID", "");diff --git a/autogpt_platform/frontend/src/lib/auth/hooks/useAuthStore.ts b/autogpt_platform/frontend/src/lib/auth/hooks/useAuthStore.tsindex fc4acb5877..ea3a2e547a 100644--- a/autogpt_platform/frontend/src/lib/auth/hooks/useAuthStore.ts+++ b/autogpt_platform/frontend/src/lib/auth/hooks/useAuthStore.ts@@ -1,6 +1,7 @@"use client";import type BackendAPI from "@/lib/autogpt-server-api/client";+import { setAnalyticsIdentitySignedIn } from "@/services/analytics/anonymous-id";import { resetAnalyticsIdentity } from "@/services/analytics/reset-identity";import { teardownPushSubscriptionBeforeLogout } from "@/services/push-notifications/helpers";import type { AppRouterInstance } from "next/dist/shared/lib/app-router-context.shared-runtime";@@ -101,6 +102,13 @@ export const useAuthStore = create<AuthStoreState>((set, get) => {hasLoadedUser: true,isUserLoading: false,});+ } else if (!result.hasLoadedUser) {+ // fetchUser answered without an error that there is no session,+ // and the fallback found none either. Only then is the visitor+ // known to be signed out; after an error the session stays+ // unknown, so an older account can't take the marketing site's+ // visitor id (anonymous-id.ts).+ setAnalyticsIdentitySignedIn(false);}}} else {@@ -298,6 +306,10 @@ export const useAuthStore = create<AuthStoreState>((set, get) => {});useAuthStore.subscribe((state, previousState) => {+ // Signing out is reported here; the signed-out state on load once+ // initialize() is done.+ if (state.user) setAnalyticsIdentitySignedIn(true);+ else if (previousState.user) setAnalyticsIdentitySignedIn(false);if (previousState.user && previousState.user.id !== state.user?.id) {resetAnalyticsIdentity();clearCopilotCompletedSessions();diff --git a/autogpt_platform/frontend/src/providers/posthog/__tests__/posthog-provider.test.tsx b/autogpt_platform/frontend/src/providers/posthog/__tests__/posthog-provider.test.tsxindex 299d1b5626..14c2fc5ec7 100644--- a/autogpt_platform/frontend/src/providers/posthog/__tests__/posthog-provider.test.tsx+++ b/autogpt_platform/frontend/src/providers/posthog/__tests__/posthog-provider.test.tsx@@ -41,6 +41,7 @@ vi.mock("@/services/analytics/anonymous-id", () => ({}));vi.mock("../posthog-consent", () => ({+ dropWithoutConsent: vi.fn(),followAnalyticsConsent: () => () => {},forgetPostHogStorageWithoutConsent: vi.fn(),getConsentGatedConfig: () => ({}),@@ -69,17 +70,19 @@ describe("PostHogProvider", () => {);});- it("marks the events an admin sends while acting as a user", async () => {+ it("drops events without consent, then marks those an admin sends while acting as a user", async () => {const { PostHogProvider } = await import("../posthog-provider");const { markImpersonatedEvent } = await import("@/services/analytics/posthog-request-context");+ const { dropWithoutConsent } = await import("../posthog-consent");render(<PostHogProvider>app</PostHogProvider>);- expect(posthog.init.mock.calls[0][1].before_send).toBe(+ expect(posthog.init.mock.calls[0][1].before_send).toEqual([+ dropWithoutConsent,markImpersonatedEvent,- );+ ]);});it("talks to a self-hosted PostHog directly", async () => {diff --git a/autogpt_platform/frontend/src/providers/posthog/posthog-consent.ts b/autogpt_platform/frontend/src/providers/posthog/posthog-consent.tsindex ec3bfcabde..1af11f4ee2 100644--- a/autogpt_platform/frontend/src/providers/posthog/posthog-consent.ts+++ b/autogpt_platform/frontend/src/providers/posthog/posthog-consent.ts@@ -1,5 +1,15 @@-import { hasConsentFor, subscribeToConsent } from "@/services/consent/consent";-import posthog, { type PostHogConfig } from "posthog-js";+import {+ getAnonymousID,+ isAnonymousIDSettled,+ subscribeToAnonymousIDSettled,+} from "@/services/analytics/anonymous-id";+import { environment } from "@/services/environment";+import {+ hasConsentFor,+ hasDeclinedConsentFor,+ subscribeToConsent,+} from "@/services/consent/consent";+import posthog, { type CaptureResult, type PostHogConfig } from "posthog-js";const POSTHOG_CONSENT_KEY = "agpt_posthog_consent";const PERSISTENT_STORAGE = "localStorage+cookie";@@ -7,13 +17,14 @@ const PERSISTENT_STORAGE = "localStorage+cookie";/*** PostHog starts opted out: capture() drops events instead of queueing them* and session replay stays off. Without analytics consent it also keeps its- * state in memory, and forgetPostHogStorageWithoutConsent has deleted- * anything an earlier visit stored. Feature flags still evaluate, so- * experiments keep working for visitors who decline.+ * state in memory, so nothing an earlier visit stored is read, and+ * forgetPostHogStorageWithoutConsent has deleted it if the visitor declined.+ * Feature flags still evaluate, so experiments keep working for visitors who+ * decline.*/export function getConsentGatedConfig() {return {- persistence: hasConsentFor("analytics") ? PERSISTENT_STORAGE : "memory",+ persistence: canPersist() ? PERSISTENT_STORAGE : "memory",opt_out_capturing_by_default: true,opt_out_persistence_by_default: true,opt_out_capturing_persistence_type: "localStorage",@@ -23,20 +34,61 @@ export function getConsentGatedConfig() {}/**- * Cookiebot is the source of truth. Run before init: without analytics- * consent, delete what PostHog stored on an earlier visit (its identity,- * which may belong to an identified user, and its own opt-in), since memory- * persistence only stops it being read.+ * Cookiebot is the source of truth. Run before init. Once the visitor has+ * answered without analytics, delete what PostHog stored (its identity, which+ * may belong to an identified user, and its own opt-in), since memory+ * persistence only stops it being read. While the answer is pending only+ * PostHog's own opt-in goes, so it starts opted out; its identity stays,+ * including the `.agpt.co` cookie the marketing site writes, which a grant+ * adopts (services/analytics/anonymous-id.ts).*/export function forgetPostHogStorageWithoutConsent(token: string) {- if (hasConsentFor("analytics")) return;- clearPostHogStorage(token);+ if (canPersist()) return;+ if (hasDeclinedConsentFor("analytics")) clearPostHogStorage(token);+ else clearPostHogOptIn();+}++// With consent, once the anonymous id is decided: until the session is known+// it may still move to the website's, and PostHog must not write (or send+// under) the page's id meanwhile.+function canPersist() {+ return hasConsentFor("analytics") && isAnonymousIDSettled();+}++/**+ * PostHog `before_send` hook: drops every event (autocapture, page leave and+ * replay included) while analytics is not allowed, for anything sent between+ * the answer changing and syncPostHogConsent catching up.+ */+export function dropWithoutConsent(+ event: CaptureResult | null,+): CaptureResult | null {+ return hasConsentFor("analytics") ? event : null;+}++/**+ * Delete what PostHog stored, the shared `.agpt.co` cookie included. For an+ * account change without analytics consent: PostHog then runs in memory, so+ * its reset() leaves the stored identity behind for the next account to+ * adopt.+ */+export function forgetPostHogStorage() {+ clearPostHogStorage(+ posthog.config?.token || environment.getPostHogCredentials().key || "",+ );}/** Call once posthog.init has run; returns the unsubscribe. */export function followAnalyticsConsent() {syncPostHogConsent();- return subscribeToConsent(syncPostHogConsent);+ const unfollowConsent = subscribeToConsent(syncPostHogConsent);+ // The anonymous id can settle later than the grant, once the session is+ // known (anonymous-id.ts).+ const unfollowSettled = subscribeToAnonymousIDSettled(syncPostHogConsent);+ return () => {+ unfollowConsent();+ unfollowSettled();+ };}/**@@ -45,17 +97,27 @@ export function followAnalyticsConsent() {*/export function syncPostHogConsent() {if (!hasConsentFor("analytics")) {+ posthog.stopSessionRecording();+ if (!hasDeclinedConsentFor("analytics")) {+ pauseCapture();+ return;+ }// Withdrawal also reloads the page; stop and drop PostHog's storage now,// whatever PostHog itself believes its opt-in to be.- posthog.stopSessionRecording();+ keepPersistenceWithCapture();posthog.clear_opt_in_out_capturing();clearPostHogStorage(posthog.config.token);return;}+ // The settled listener comes back once the id is decided.+ if (!isAnonymousIDSettled()) return;+ // Before the storage turns persistent, which writes the shared cookie.+ adoptAnonymousID();if (!posthog.has_opted_in_capturing()) {posthog.opt_in_capturing({ captureEventName: false });}+ keepPersistenceWithCapture();if (posthog.config.persistence !== PERSISTENT_STORAGE) {posthog.set_config({ persistence: PERSISTENT_STORAGE });}@@ -64,6 +126,56 @@ export function syncPostHogConsent() {}}+/**+ * Pending after PostHog started with consent (Cookiebot no longer accepts the+ * stored answer): stop capturing, which is what stops PostHog writing, since+ * capture() updates the session and person info before before_send drops the+ * event. Its storage stays, the shared `.agpt.co` cookie included: with+ * opt_out_persistence_by_default on, posthog-js would delete it as capture+ * stops (persistence.set_disabled -> remove), as it would on a switch to+ * memory persistence (update_config -> clear), so that is turned off first.+ */+function pauseCapture() {+ if (posthog.config.persistence === "memory") return;+ if (!posthog.has_opted_in_capturing()) return;+ posthog.set_config({ opt_out_persistence_by_default: false });+ posthog.clear_opt_in_out_capturing();+}++// Undoes pauseCapture's setting. Only once PostHog captures again, or when+// its storage is to go anyway: with it on, an opted-out PostHog deletes its+// storage.+function keepPersistenceWithCapture() {+ if (posthog.config.opt_out_persistence_by_default) return;+ posthog.set_config({ opt_out_persistence_by_default: true });+}++/**+ * PostHog started on this page's in-memory id while the answer was pending. On+ * a grant the visitor's anonymous id can turn out to be another one: the+ * website's PostHog device id from the shared `.agpt.co` cookie+ * (anonymous-id.ts). Move PostHog onto it, so its storage keeps that id+ * rather than overwriting the cookie with this page's. Nothing was captured+ * under the in-memory id, since PostHog was opted out, so there is nothing to+ * merge; flags are read again for the adopted id. This is the same call+ * posthog-js makes for `bootstrap`, and it spares the visitor a reload.+ */+function adoptAnonymousID() {+ if (posthog.get_property("$user_state") === "identified") return;+ const id = getAnonymousID();+ if (!id || posthog.get_distinct_id() === id) return;+ posthog.register({ distinct_id: id, $device_id: id });+ posthog.reloadFeatureFlags();+}++function clearPostHogOptIn() {+ try {+ window.localStorage.removeItem(POSTHOG_CONSENT_KEY);+ } catch {+ // Storage blocked: PostHog cannot have stored an opt-in either.+ }+}+// PostHog keeps ph_<token>_posthog in a cookie and localStorage, and// ph_<token>_window_id / _primary_window_exists in sessionStorage.function clearPostHogStorage(token: string) {@@ -71,11 +183,7 @@ function clearPostHogStorage(token: string) {const prefix = `ph_${token}_`;clearStorage(() => window.localStorage, prefix);clearStorage(() => window.sessionStorage, prefix);- try {- window.localStorage.removeItem(POSTHOG_CONSENT_KEY);- } catch {- // Storage blocked: PostHog cannot have stored an opt-in either.- }+ clearPostHogOptIn();document.cookie.split(";").map((part) => part.trim().split("=")[0])diff --git a/autogpt_platform/frontend/src/providers/posthog/posthog-provider.tsx b/autogpt_platform/frontend/src/providers/posthog/posthog-provider.tsxindex a593847fe0..dbd73d3a27 100644--- a/autogpt_platform/frontend/src/providers/posthog/posthog-provider.tsx+++ b/autogpt_platform/frontend/src/providers/posthog/posthog-provider.tsx@@ -7,6 +7,7 @@ import {getAnonymousID,} from "@/services/analytics/anonymous-id";import { getPostHogBaseProperties } from "@/services/analytics/posthog-base-properties";+import { capturePostHogEvent } from "@/services/analytics/posthog-capture";import { PageEvent } from "@/services/analytics/posthog-events";import { markImpersonatedEvent } from "@/services/analytics/posthog-request-context";import { useConsent } from "@/services/consent/useConsent";@@ -18,6 +19,7 @@ import { usePathname, useSearchParams } from "next/navigation";import posthog from "posthog-js";import { ReactNode, useEffect, useRef } from "react";import {+ dropWithoutConsent,followAnalyticsConsent,forgetPostHogStorageWithoutConsent,getConsentGatedConfig,@@ -36,7 +38,8 @@ export function PostHogProvider({ children }: { children: ReactNode }) {// Seed PostHog's anonymous identity with the first-party anonymous id// LaunchDarkly and the backend also use, so pre-signup activity from// every tool lands on the same person once identify() runs. Without- // analytics consent that id only lives for this page load.+ // analytics consent that id only lives for this page load, and a grant+ // moves PostHog onto the visitor's lasting id (posthog-consent.ts).const anonymousID = getAnonymousID();forgetPostHogStorageWithoutConsent(postHogCredentials.key);posthog.init(postHogCredentials.key, {@@ -50,7 +53,7 @@ export function PostHogProvider({ children }: { children: ReactNode }) {bootstrap: { distinctID: anonymousID, isIdentifiedID: false },}),...getConsentGatedConfig(),- before_send: markImpersonatedEvent,+ before_send: [dropWithoutConsent, markImpersonatedEvent],});posthog.register(getPostHogBaseProperties());unfollowConsent = followAnalyticsConsent();@@ -114,7 +117,9 @@ export function PostHogPageViewTracker() {if (searchParams && searchParams.toString()) {url = url + `?${searchParams.toString()}`;}- posthog.capture(PageEvent.PAGEVIEW, { $current_url: url });+ // Held until PostHog captures: it can start after this page's first+ // render, and after the grant while the anonymous id settles.+ capturePostHogEvent(PageEvent.PAGEVIEW, { $current_url: url });}}, [pathname, searchParams, isPostHogEnabled, analytics]);diff --git a/autogpt_platform/frontend/src/services/analytics/__tests__/anonymous-id.test.ts b/autogpt_platform/frontend/src/services/analytics/__tests__/anonymous-id.test.tsindex c5e20a7458..eec8f7c4a9 100644--- a/autogpt_platform/frontend/src/services/analytics/__tests__/anonymous-id.test.ts+++ b/autogpt_platform/frontend/src/services/analytics/__tests__/anonymous-id.test.ts@@ -13,6 +13,7 @@ import {readFirstLanding,resetAnonymousID,resetAnonymousIDForTests,+ setAnalyticsIdentitySignedIn,} from "../anonymous-id";function setAnalyticsConsent(analytics: boolean): void {@@ -289,6 +290,45 @@ describe("following analytics consent", () => {unfollow();});+ it("leaves the stored id and landing alone while the answer is pending", () => {+ configureCookiebot();+ installCookiebot();+ window.localStorage.setItem(ANONYMOUS_ID_KEY, "stored-id");+ window.localStorage.setItem(FIRST_LANDING_KEY, "{}");++ const unfollow = followAnalyticsConsentForIdentity();++ expect(getAnonymousID()).not.toBe("stored-id");+ expect(window.localStorage.getItem(ANONYMOUS_ID_KEY)).toBe("stored-id");+ expect(window.localStorage.getItem(FIRST_LANDING_KEY)).toBe("{}");++ answerCookiebot({ statistics: true });++ expect(getAnonymousID()).toBe("stored-id");+ unfollow();+ });++ it("adopts the website's PostHog device id from the shared cookie on a grant", () => {+ configureCookiebot();+ installCookiebot();+ document.cookie = `ph_phc_test_posthog=${encodeURIComponent(+ JSON.stringify({ $device_id: "website-device" }),+ )}; Path=/`;+ setAnalyticsIdentitySignedIn(false);+ const pageID = getAnonymousID();+ const unfollow = followAnalyticsConsentForIdentity();++ answerCookiebot({ statistics: true });++ expect(pageID).not.toBe("website-device");+ expect(getAnonymousID()).toBe("website-device");+ expect(window.localStorage.getItem(ANONYMOUS_ID_KEY)).toBe(+ "website-device",+ );+ unfollow();+ document.cookie = "ph_phc_test_posthog=; Path=/; Max-Age=0";+ });+it("keeps a first landing stored on an earlier visit", () => {window.localStorage.setItem(FIRST_LANDING_KEY,diff --git a/autogpt_platform/frontend/src/services/analytics/__tests__/auth-identity.test.ts b/autogpt_platform/frontend/src/services/analytics/__tests__/auth-identity.test.tsindex 10a9eaee4f..c55c4a8b20 100644--- a/autogpt_platform/frontend/src/services/analytics/__tests__/auth-identity.test.ts+++ b/autogpt_platform/frontend/src/services/analytics/__tests__/auth-identity.test.ts@@ -1,6 +1,8 @@+import { fetchUser, validateSession } from "@/lib/auth/hooks/helpers";import { useAuthStore } from "@/lib/auth/hooks/useAuthStore";import type { User } from "@/lib/auth/types";import {+ answerCookiebot,configureCookiebot,installCookiebot,removeCookiebot,@@ -37,7 +39,7 @@ const userA = { id: "user-A", email: "[email protected]" } as User;const userB = { id: "user-B", email: "[email protected]" } as User;beforeEach(() => {- useAuthStore.setState({ user: null });+ useAuthStore.setState({ user: null, isUserLoading: true });window.localStorage.clear();resetAnonymousIDForTests();vi.resetAllMocks();@@ -126,6 +128,120 @@ describe("analytics identity on account transitions", () => {expect(useAuthStore.getState().user).toBeNull();});+ describe("with the marketing site's PostHog cookie", () => {+ const websiteCookie = `ph_phc_test_posthog=${encodeURIComponent(+ JSON.stringify({ $device_id: "website-device" }),+ )}; Path=/`;++ beforeEach(() => {+ document.cookie = websiteCookie;+ configureCookiebot();+ installCookiebot();+ });++ afterEach(() => {+ document.cookie = "ph_phc_test_posthog=; Path=/; Max-Age=0";+ document.cookie = "agpt_account_created=; Path=/; Max-Age=0; Secure";+ });++ function initializeAuth() {+ return useAuthStore.getState().initialize({+ api: {} as never,+ router: {} as never,+ path: "/",+ });+ }++ it("adopts the website's id once the session shows nobody signed in", async () => {+ vi.mocked(fetchUser).mockResolvedValue({+ user: null,+ hasLoadedUser: false,+ isUserLoading: false,+ });+ vi.mocked(validateSession).mockResolvedValue({+ isValid: true,+ shouldUpdateUser: false,+ });+ answerCookiebot({ statistics: true });+ expect(getAnonymousID()).not.toBe("website-device");+ expect(window.localStorage.getItem("agpt_anonymous_id")).toBeNull();++ await initializeAuth();++ expect(getAnonymousID()).toBe("website-device");+ expect(window.localStorage.getItem("agpt_anonymous_id")).toBe(+ "website-device",+ );+ });++ it("leaves the session unknown when the auth requests fail", async () => {+ vi.mocked(fetchUser).mockResolvedValue({+ user: null,+ hasLoadedUser: true,+ isUserLoading: false,+ });+ vi.mocked(validateSession).mockResolvedValue({+ isValid: false,+ shouldUpdateUser: true,+ });+ answerCookiebot({ statistics: true });++ await initializeAuth();++ expect(getAnonymousID()).not.toBe("website-device");+ expect(window.localStorage.getItem("agpt_anonymous_id")).toBeNull();+ });++ it("keeps it from an account the fallback validation finds", async () => {+ // fetchUser can miss a session right after login; validation finds it.+ vi.mocked(fetchUser).mockResolvedValue({+ user: null,+ hasLoadedUser: false,+ isUserLoading: false,+ });+ vi.mocked(validateSession).mockResolvedValue({+ isValid: true,+ user: userA,+ shouldUpdateUser: true,+ });+ answerCookiebot({ statistics: true });++ await initializeAuth();++ expect(useAuthStore.getState().user).toEqual(userA);+ expect(getAnonymousID()).not.toBe("website-device");+ });++ it("adopts it for an account created here after the signup flag is gone", () => {+ // The ads signup conversion clears the flag, which can be before the+ // consent answer.+ document.cookie = "agpt_account_created=email; Path=/; Secure";+ useAuthStore.setState({ user: userA });+ document.cookie = "agpt_account_created=; Path=/; Max-Age=0; Secure";++ answerCookiebot({ statistics: true });++ expect(getAnonymousID()).toBe("website-device");+ });++ it("keeps it from an older account signed in when the answer arrives", () => {+ useAuthStore.setState({ user: userA });++ answerCookiebot({ statistics: true });++ expect(getAnonymousID()).not.toBe("website-device");+ });++ it("adopts it for an account created in this browser", () => {+ document.cookie = "agpt_account_created=email; Path=/; Secure";+ useAuthStore.setState({ user: userA });++ answerCookiebot({ statistics: true });++ expect(getAnonymousID()).toBe("website-device");+ });+ });+it("preserves the anonymous identity through login and same-user refreshes", () => {const previous = getAnonymousID();useAuthStore.setState({ user: userA });diff --git a/autogpt_platform/frontend/src/services/analytics/__tests__/posthog-consent.test.tsx b/autogpt_platform/frontend/src/services/analytics/__tests__/posthog-consent.test.tsxindex 5961f938a5..91cd2e2875 100644--- a/autogpt_platform/frontend/src/services/analytics/__tests__/posthog-consent.test.tsx+++ b/autogpt_platform/frontend/src/services/analytics/__tests__/posthog-consent.test.tsx@@ -8,7 +8,7 @@ import {} from "@/tests/integrations/cookiebot";import { act, render } from "@testing-library/react";import { http, HttpResponse } from "msw";-import { PostHog } from "posthog-js";+import { PostHog, type CaptureResult } from "posthog-js";import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";// Runs the real posthog-js client against a stubbed PostHog host, so these@@ -51,16 +51,24 @@ interface FlagsRequest {let requests: string[] = [];let flagsRequests: FlagsRequest[] = [];let captured: string[] = [];+let capturedEvents: CaptureResult[] = [];+let clients: PostHog[] = [];interface RenderOptions {user?: User | null;+ // Whether the auth store has reported the session yet.+ sessionKnown?: boolean;}// Each call is a fresh page load: a new client (init() is a no-op on one that// already ran) and fresh modules, so in-memory ids start over too.-async function renderPostHog({ user = null }: RenderOptions = {}) {+async function renderPostHog({+ user = null,+ sessionKnown = true,+}: RenderOptions = {}) {vi.resetModules();const client = new PostHog();+ clients.push(client);vi.doMock("posthog-js", () => ({default: client,posthog: client,@@ -69,12 +77,21 @@ async function renderPostHog({ user = null }: RenderOptions = {}) {vi.doMock("@/lib/auth/hooks/useAuth", () => ({useAuth: () => ({ user, isUserLoading: false }),}));- const [provider, { trackTabIntro }, { resetAnalyticsIdentity }] =- await Promise.all([- import("@/providers/posthog/posthog-provider"),- import("@/services/onboarding/tab-intro-analytics"),- import("@/services/analytics/reset-identity"),- ]);+ const [+ provider,+ { trackTabIntro },+ { resetAnalyticsIdentity },+ { setAnalyticsIdentitySignedIn },+ { buildAttributionPayload },+ ] = await Promise.all([+ import("@/providers/posthog/posthog-provider"),+ import("@/services/onboarding/tab-intro-analytics"),+ import("@/services/analytics/reset-identity"),+ import("@/services/analytics/anonymous-id"),+ import("@/services/analytics/attribution-payload"),+ ]);+ // What the auth store reports once the session is known.+ if (sessionKnown) setAnalyticsIdentitySignedIn(Boolean(user));const startSessionRecording = vi.spyOn(client, "startSessionRecording");const identify = vi.spyOn(client, "identify");const { PostHogProvider, PostHogPageViewTracker, PostHogUserTracker } =@@ -87,8 +104,11 @@ async function renderPostHog({ user = null }: RenderOptions = {}) {);client.on("eventCaptured", (event) => {captured.push(event.event);+ capturedEvents.push(event);});return {+ buildAttributionPayload,+ setAnalyticsIdentitySignedIn,posthog: client,trackTabIntro,startSessionRecording,@@ -186,6 +206,7 @@ beforeEach(() => {requests = [];flagsRequests = [];captured = [];+ capturedEvents = [];server.use(http.options(`${POSTHOG_HOST}/*`,@@ -203,6 +224,10 @@ beforeEach(() => {});afterEach(() => {+ // A client from an earlier test would otherwise keep writing (and+ // deleting) the next test's cookies when a late response arrives.+ clients.forEach((client) => client.set_config({ persistence: "memory" }));+ clients = [];removeCookiebot();window.localStorage.clear();window.sessionStorage.clear();@@ -257,11 +282,21 @@ describe("PostHog before analytics consent", () => {expect(storedPostHogKeys()).toEqual([]);});- it("deletes what an earlier visit stored before PostHog starts", async () => {+ it("leaves what an earlier visit stored while the answer is pending", async () => {seedEarlierVisit();+ const stored = storedPostHogKeys().sort();await renderPostHog();+ expect(storedPostHogKeys().sort()).toEqual(stored);+ });++ it("deletes what an earlier visit stored once the visitor declines", async () => {+ seedEarlierVisit();+ await renderPostHog();++ act(() => answerCookiebot({}));+expect(storedPostHogKeys()).toEqual([]);});@@ -435,7 +470,7 @@ describe("PostHog storage on a parent domain", () => {it("deletes the host cookie and every parent-domain variant", async () => {setPageURL("https://platform.agpt.co/");configureCookiebot();- installCookiebot();+ installCookiebot({});const { forgetPostHogStorageWithoutConsent } = await import("@/providers/posthog/posthog-consent");@@ -458,6 +493,281 @@ describe("PostHog storage on a parent domain", () => {});});+// The marketing site (agpt.co) runs PostHog on the same project and keeps its+// state in the ph_<key>_posthog cookie on .agpt.co. A visitor who comes over+// keeps that identity, so their website visits join the account they create.+describe("PostHog hand-over from the marketing site", () => {+ const PERSISTENCE = `ph_${POSTHOG_KEY}_posthog`;+ const WEBSITE_ID = "website-device";++ function seedWebsiteCookie() {+ const state = JSON.stringify({+ distinct_id: WEBSITE_ID,+ $device_id: WEBSITE_ID,+ });+ document.cookie = `${PERSISTENCE}=${encodeURIComponent(state)}; Path=/; Domain=.agpt.co`;+ }++ function sharedCookie(): Record<string, unknown> | null {+ const entry = document.cookie+ .split("; ")+ .find((part) => part.startsWith(`${PERSISTENCE}=`));+ if (!entry) return null;+ return JSON.parse(decodeURIComponent(entry.slice(PERSISTENCE.length + 1)));+ }++ beforeEach(() => {+ setPageURL("https://platform.agpt.co/");+ configureCookiebot();+ seedWebsiteCookie();+ });++ afterEach(() => {+ document.cookie = `${PERSISTENCE}=; Path=/; Domain=.agpt.co; Expires=Thu, 01 Jan 1970 00:00:00 GMT`;+ setPageURL("http://localhost:3000/");+ });++ it("keeps the website's cookie while the visitor has not answered", async () => {+ installCookiebot();++ const { posthog } = await renderPostHog();+ await vi.waitFor(() => expect(flagsRequests).toHaveLength(1));++ expect(sharedCookie()).toEqual({+ distinct_id: WEBSITE_ID,+ $device_id: WEBSITE_ID,+ });+ expect(posthog.get_distinct_id()).not.toBe(WEBSITE_ID);+ expect(flagsRequests[0].distinct_id).not.toBe(WEBSITE_ID);+ });++ it("deletes the website's cookie when the visitor declines", async () => {+ installCookiebot();+ await renderPostHog();++ act(() => answerCookiebot({}));++ expect(sharedCookie()).toBeNull();+ });++ it("moves onto the website's id when the visitor accepts", async () => {+ installCookiebot();+ const { posthog } = await renderPostHog();+ await vi.waitFor(() => expect(flagsRequests).toHaveLength(1));++ act(() => answerCookiebot({ statistics: true }));+ posthog.capture("after_consent");++ expect(posthog.get_distinct_id()).toBe(WEBSITE_ID);+ expect(posthog.get_property("$device_id")).toBe(WEBSITE_ID);+ expect(window.localStorage.getItem("agpt_anonymous_id")).toBe(WEBSITE_ID);+ expect(sharedCookie()).toMatchObject({+ distinct_id: WEBSITE_ID,+ $device_id: WEBSITE_ID,+ });+ expect(captured).toEqual(["$pageview", "after_consent"]);+ await vi.waitFor(() =>+ expect(flagsRequests.map((body) => body.distinct_id)).toContain(+ WEBSITE_ID,+ ),+ );+ });++ it("moves onto the website's id when Cookiebot grants after PostHog started", async () => {+ // Regions with implied consent: no stored answer yet, and uc.js arrives+ // after PostHog has started on an in-memory id.+ const { posthog } = await renderPostHog();++ act(() => {+ installCookiebot({ statistics: true });+ window.dispatchEvent(new Event("CookiebotOnConsentReady"));+ });++ expect(posthog.get_distinct_id()).toBe(WEBSITE_ID);+ expect(sharedCookie()).toMatchObject({ $device_id: WEBSITE_ID });+ });++ it("starts on the website's id for a visitor who allowed analytics before", async () => {+ installCookiebot({ statistics: true });++ const { posthog } = await renderPostHog();++ expect(posthog.get_distinct_id()).toBe(WEBSITE_ID);+ expect(window.localStorage.getItem("agpt_anonymous_id")).toBe(WEBSITE_ID);+ });++ it("merges the website visits into an account created here", async () => {+ document.cookie = "agpt_account_created=email; Path=/; Secure";+ installCookiebot();+ const { posthog, buildAttributionPayload } = await renderPostHog({+ user: USER,+ });++ act(() => answerCookiebot({ statistics: true }));++ const identify = capturedEvents.find(+ (event) => event.event === "$identify",+ );+ expect(identify?.properties.$anon_distinct_id).toBe(WEBSITE_ID);+ expect(posthog.get_distinct_id()).toBe(USER.id);+ expect(buildAttributionPayload()).toMatchObject({+ anonymous_id: WEBSITE_ID,+ signup_method: "email",+ });+ document.cookie = "agpt_account_created=; Path=/; Max-Age=0; Secure";+ });++ it("gives the backend the website's id once the visitor accepts", async () => {+ installCookiebot();+ const { buildAttributionPayload } = await renderPostHog();++ act(() => answerCookiebot({ statistics: true }));++ expect(buildAttributionPayload()).toMatchObject({+ anonymous_id: WEBSITE_ID,+ posthog_distinct_id: WEBSITE_ID,+ });+ });++ it("doesn't hand the website visits to an older account signed in here", async () => {+ installCookiebot();+ const { posthog } = await renderPostHog({ user: USER });+ const pageID = posthog.get_distinct_id();++ act(() => answerCookiebot({ statistics: true }));++ const identify = capturedEvents.find(+ (event) => event.event === "$identify",+ );+ expect(identify?.properties.$anon_distinct_id).toBe(pageID);+ expect(pageID).not.toBe(WEBSITE_ID);+ expect(window.localStorage.getItem("agpt_anonymous_id")).toBe(pageID);+ expect(sharedCookie()).not.toMatchObject({ $device_id: WEBSITE_ID });+ });++ it("forgets the website's id on logout while the answer is pending", async () => {+ installCookiebot();+ const { resetAnalyticsIdentity } = await renderPostHog({ user: USER });++ resetAnalyticsIdentity();++ expect(sharedCookie()).toBeNull();+ });++ it("keeps a returning visitor's own id", async () => {+ window.localStorage.setItem("agpt_anonymous_id", "app-id");+ installCookiebot();+ const { posthog } = await renderPostHog();++ act(() => answerCookiebot({ statistics: true }));++ expect(posthog.get_distinct_id()).toBe("app-id");+ expect(window.localStorage.getItem("agpt_anonymous_id")).toBe("app-id");+ });++ it("keeps the cookie and stops writing when Cookiebot asks again", async () => {+ installCookiebot({ statistics: true });+ const { posthog } = await renderPostHog();+ posthog.capture("before_asking");+ const writes = recordCookieWrites();++ act(() => {+ if (window.Cookiebot) window.Cookiebot.hasResponse = false;+ window.dispatchEvent(new Event("CookiebotOnDialogInit"));+ window.dispatchEvent(new Event("CookiebotOnLoad"));+ });+ const writesBeforeCapture = writes.length;+ posthog.capture("while_asking");++ expect(captured).toContain("before_asking");+ expect(captured).not.toContain("while_asking");+ expect(sharedCookie()).toMatchObject({+ distinct_id: WEBSITE_ID,+ $device_id: WEBSITE_ID,+ });+ expect(+ writes.filter((value) => value.includes("Expires=Thu, 01 Jan 1970")),+ ).toEqual([]);+ expect(writes.length).toBe(writesBeforeCapture);+ Reflect.deleteProperty(document, "cookie");++ act(() => answerCookiebot({ statistics: true }));+ posthog.capture("after_answer");++ expect(posthog.get_distinct_id()).toBe(WEBSITE_ID);+ expect(posthog.config.opt_out_persistence_by_default).toBe(true);+ expect(captured).toContain("after_answer");+ expect(sharedCookie()).toMatchObject({ $device_id: WEBSITE_ID });+ });++ it("deletes the cookie when the visitor declines after Cookiebot asks again", async () => {+ installCookiebot({ statistics: true });+ await renderPostHog();+ act(() => {+ if (window.Cookiebot) window.Cookiebot.hasResponse = false;+ window.dispatchEvent(new Event("CookiebotOnLoad"));+ });++ act(() => answerCookiebot({}));++ expect(sharedCookie()).toBeNull();+ expect(storedPostHogKeys()).toEqual([]);+ });++ it("waits for the session before taking the website's id", async () => {+ installCookiebot();+ const { posthog, setAnalyticsIdentitySignedIn } = await renderPostHog({+ sessionKnown: false,+ });+ const pageID = posthog.get_distinct_id();++ act(() => answerCookiebot({ statistics: true }));++ // Nothing is sent or stored under the page's id meanwhile.+ expect(posthog.get_distinct_id()).toBe(pageID);+ expect(posthog.has_opted_out_capturing()).toBe(true);+ expect(window.localStorage.getItem("agpt_anonymous_id")).toBeNull();+ expect(sharedCookie()).toEqual({+ distinct_id: WEBSITE_ID,+ $device_id: WEBSITE_ID,+ });+ expect(captured).toEqual([]);++ act(() => setAnalyticsIdentitySignedIn(false));++ expect(posthog.get_distinct_id()).toBe(WEBSITE_ID);+ expect(window.localStorage.getItem("agpt_anonymous_id")).toBe(WEBSITE_ID);+ expect(sharedCookie()).toMatchObject({ $device_id: WEBSITE_ID });+ // The landing page view was held, and goes out under the website's id.+ await vi.waitFor(() => expect(captured).toContain("$pageview"));+ expect(+ capturedEvents.find((event) => event.event === "$pageview")?.properties+ .distinct_id,+ ).toBe(WEBSITE_ID);+ });++ it("starts in memory on a stored grant until the session is known", async () => {+ installCookiebot({ statistics: true });+ window.localStorage.setItem("agpt_posthog_consent", "1");++ const { posthog, setAnalyticsIdentitySignedIn } = await renderPostHog({+ sessionKnown: false,+ });++ expect(posthog.config.persistence).toBe("memory");+ expect(posthog.has_opted_out_capturing()).toBe(true);+ expect(sharedCookie()).toEqual({+ distinct_id: WEBSITE_ID,+ $device_id: WEBSITE_ID,+ });++ act(() => setAnalyticsIdentitySignedIn(true));++ expect(posthog.config.persistence).toBe("localStorage+cookie");+ expect(posthog.get_distinct_id()).not.toBe(WEBSITE_ID);+ });+});+describe("PostHog without NEXT_PUBLIC_COOKIEBOT_CBID", () => {it("stays opted out even for a browser holding an old answer", async () => {installCookiebot({ statistics: true });diff --git a/autogpt_platform/frontend/src/services/analytics/__tests__/useReportAttribution.test.tsx b/autogpt_platform/frontend/src/services/analytics/__tests__/useReportAttribution.test.tsxindex dd59e37bd8..d83114d7e9 100644--- a/autogpt_platform/frontend/src/services/analytics/__tests__/useReportAttribution.test.tsx+++ b/autogpt_platform/frontend/src/services/analytics/__tests__/useReportAttribution.test.tsx@@ -1,11 +1,12 @@import { server } from "@/mocks/mock-server";import {+ answerCookiebot,configureCookiebot,installCookiebot,removeCookiebot,} from "@/tests/integrations/cookiebot";import { QueryClient, QueryClientProvider } from "@tanstack/react-query";-import { renderHook, waitFor } from "@testing-library/react";+import { act, renderHook, waitFor } from "@testing-library/react";import { http, HttpResponse } from "msw";import { ReactNode } from "react";import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";@@ -93,6 +94,79 @@ describe("useReportAttribution", () => {expect(bodies[0]).toEqual({ anonymous_id: "anon-1" });});+ it("waits for the consent answer, then reports", async () => {+ // A grant can still change the anonymous id the report carries.+ const bodies = captureReports();+ configureCookiebot();+ installCookiebot();++ renderHook(() => useReportAttribution(), { wrapper });+ await new Promise((resolve) => setTimeout(resolve, 20));+ expect(bodies).toHaveLength(0);++ act(() => answerCookiebot({ statistics: true }));++ await waitFor(() => expect(bodies).toHaveLength(1));+ });++ it("waits while Cookiebot is still loading", async () => {+ const bodies = captureReports();+ removeCookiebot();+ configureCookiebot();+ vi.spyOn(document, "readyState", "get").mockReturnValue("interactive");++ renderHook(() => useReportAttribution(), { wrapper });+ await new Promise((resolve) => setTimeout(resolve, 20));++ expect(bodies).toHaveLength(0);+ });++ it("reports at once when Cookiebot could not load", async () => {+ const bodies = captureReports();+ removeCookiebot();+ configureCookiebot();+ vi.spyOn(document, "readyState", "get").mockReturnValue("complete");++ renderHook(() => useReportAttribution(), { wrapper });++ await waitFor(() => expect(bodies).toHaveLength(1));+ });++ it("gives a late uc.js time to arrive after the page has loaded", async () => {+ const bodies = captureReports();+ removeCookiebot();+ configureCookiebot();+ vi.spyOn(document, "readyState", "get").mockReturnValue("complete");+ vi.spyOn(performance, "getEntriesByType").mockReturnValue([+ {+ loadEventStart: 990,+ loadEventEnd: 1000,+ } as unknown as PerformanceEntry,+ ]);+ const now = vi.spyOn(performance, "now").mockReturnValue(2000);++ renderHook(() => useReportAttribution(), { wrapper });+ await new Promise((resolve) => setTimeout(resolve, 20));+ expect(bodies).toHaveLength(0);++ now.mockReturnValue(1000 + 10_001);+ act(() => {+ window.dispatchEvent(new Event("load"));+ });++ await waitFor(() => expect(bodies).toHaveLength(1));+ });++ it("reports at once without a consent banner", async () => {+ const bodies = captureReports();+ removeCookiebot();+ vi.stubEnv("NEXT_PUBLIC_COOKIEBOT_CBID", "");++ renderHook(() => useReportAttribution(), { wrapper });++ await waitFor(() => expect(bodies).toHaveLength(1));+ });+it("does nothing while signed out", async () => {const bodies = captureReports();auth.user = null;diff --git a/autogpt_platform/frontend/src/services/analytics/anonymous-id.ts b/autogpt_platform/frontend/src/services/analytics/anonymous-id.tsindex edb50205e1..44801c07f4 100644--- a/autogpt_platform/frontend/src/services/analytics/anonymous-id.ts+++ b/autogpt_platform/frontend/src/services/analytics/anonymous-id.ts@@ -17,12 +17,26 @@* It only outlives the page with analytics consent. Without it the id is* minted fresh for each page load and kept in memory, nothing an earlier* visit stored is read, and the first landing stays in memory too. Granting- * persists both without a reload; withdrawing deletes them. With consent an- * existing PostHog device id is adopted rather than replaced, so returning- * visitors keep their history.+ * persists both without a reload; declining deletes them. While the answer is+ * still pending, what is stored is left alone.+ *+ * With consent the id is, in order: the one this browser stored on an+ * earlier visit, so returning visitors keep theirs; else PostHog's device id,+ * which the marketing site (agpt.co) leaves in the shared `.agpt.co` cookie,+ * so the website visits join this visitor at signup; else this page's id.+ * The website's id is only taken while nobody is signed in, or for an account+ * created in this browser: an older account must not take over the website+ * history of whoever used this browser on agpt.co. Until the session is known+ * that choice waits: the id stays this page's, unsaved, and settles (telling+ * subscribeToAnonymousIDSettled) once the auth store reports who is signed in.*/-import { hasConsentFor, subscribeToConsent } from "@/services/consent/consent";+import { readAccountCreatedFlag } from "@/services/analytics/account-created-cookie";+import {+ hasConsentFor,+ hasDeclinedConsentFor,+ subscribeToConsent,+} from "@/services/consent/consent";const ANONYMOUS_ID_KEY = "agpt_anonymous_id";const FIRST_LANDING_KEY = "agpt_first_landing";@@ -48,22 +62,86 @@ export interface FirstLanding {at: string;}+type Session = "unknown" | "signed-in" | "signed-out";+let memoryID: string | null = null;+// Whether memoryID is settled for this page: resolved with consent, or set by+// a reset. Before that it only stands in.+let settled = false;+let session: Session = "unknown";+// Seen while signed in on this page. The flag itself goes as soon as the ads+// signup conversion is out, which can be before the consent answer.+let accountCreatedHere = false;+// PostHog's device ids as this browser held them before this page wrote any+// (its own localStorage, and the shared cookie the website may have written),+// read once with consent: PostHog rewrites both as soon as its storage turns+// persistent.+let ownDeviceID: string | null | undefined;+let websiteID: string | null | undefined;let pageLanding: FirstLanding | null = null;+const settledListeners = new Set<() => void>();++/** Who is signed in, once the session is known; kept by the auth store. */+export function setAnalyticsIdentitySignedIn(signedIn: boolean): void {+ session = signedIn ? "signed-in" : "signed-out";+ if (signedIn && readAccountCreatedFlag()) accountCreatedHere = true;+ if (settled || typeof window === "undefined") return;+ if (!hasConsentFor("analytics")) return;+ syncStoredIdentity();+ if (settled) settledListeners.forEach((listener) => listener());+}++/**+ * Whether the consented id is decided. False while it waits for the session+ * (see above); PostHog stays opted out and in memory until then.+ */+export function isAnonymousIDSettled(): boolean {+ getAnonymousID();+ return settled;+}++/** Calls `listener` when a consented id settles after the session is known. */+export function subscribeToAnonymousIDSettled(+ listener: () => void,+): () => void {+ settledListeners.add(listener);+ return () => settledListeners.delete(listener);+}export function getAnonymousID(): string | null {if (typeof window === "undefined") return null;- if (memoryID) return memoryID;-- if (!hasConsentFor("analytics")) {- memoryID = newID();- return memoryID;+ if (hasConsentFor("analytics") && !settled) {+ const id = resolveConsentedID();+ if (id) {+ memoryID = id;+ settled = true;+ }}+ memoryID ??= newID();+ return memoryID;+}++// Null while the choice waits for the session.+function resolveConsentedID(): string | null {const stored = readStorage(ANONYMOUS_ID_KEY);- const id = stored ?? readPostHogDeviceID() ?? newID();- if (!stored) writeStorage(ANONYMOUS_ID_KEY, id);- memoryID = id;- return id;+ if (stored) return stored;+ if (ownDeviceID === undefined) ownDeviceID = readOwnPostHogDeviceID();+ const id = ownDeviceID ?? chooseWebsiteID();+ if (id === undefined) return null;+ const chosen = id ?? memoryID ?? newID();+ writeStorage(ANONYMOUS_ID_KEY, chosen);+ return chosen;+}++// The website's id, null when there is none or it may not be taken, and+// undefined while that depends on a session not known yet.+function chooseWebsiteID(): string | null | undefined {+ if (websiteID === undefined) websiteID = readSharedPostHogDeviceID();+ if (!websiteID) return null;+ if (session === "unknown") return undefined;+ if (session === "signed-out") return websiteID;+ if (accountCreatedHere || readAccountCreatedFlag()) return websiteID;+ return null;}/**@@ -90,9 +168,9 @@ export function readFirstLanding(): FirstLanding | null {}/**- * Keep the stored identity in step with analytics consent: persist this page's- * id and landing on a grant, delete them otherwise (also clearing whatever a- * visit with consent left behind). Returns the unsubscribe.+ * Keep the stored identity in step with analytics consent: persist the id and+ * landing on a grant, delete them on an answer without it (also clearing+ * whatever a visit with consent left behind). Returns the unsubscribe.*/export function followAnalyticsConsentForIdentity(): () => void {if (typeof window === "undefined") return () => {};@@ -102,11 +180,11 @@ export function followAnalyticsConsentForIdentity(): () => void {function syncStoredIdentity(): void {if (!hasConsentFor("analytics")) {- forgetStoredIdentity();+ if (hasDeclinedConsentFor("analytics")) forgetStoredIdentity();return;}const id = getAnonymousID();- if (id && readStorage(ANONYMOUS_ID_KEY) !== id) {+ if (settled && id && readStorage(ANONYMOUS_ID_KEY) !== id) {writeStorage(ANONYMOUS_ID_KEY, id);}persistFirstLanding();@@ -137,6 +215,9 @@ export function resetAnonymousID(nextID?: string): void {return;}memoryID = nextID || newID();+ settled = true;+ ownDeviceID = null;+ websiteID = null;pageLanding = null;forgetStoredIdentity();if (hasConsentFor("analytics")) writeStorage(ANONYMOUS_ID_KEY, memoryID);@@ -144,6 +225,12 @@ export function resetAnonymousID(nextID?: string): void {export function resetAnonymousIDForTests(): void {memoryID = null;+ settled = false;+ session = "unknown";+ accountCreatedHere = false;+ ownDeviceID = undefined;+ websiteID = undefined;+ settledListeners.clear();pageLanding = null;}@@ -155,10 +242,27 @@ export function getPostHogDeviceID(): string | null {}function readPostHogDeviceID(): string | null {+ return readOwnPostHogDeviceID() ?? readSharedPostHogDeviceID();+}++// localStorage is this app's own (per origin).+function readOwnPostHogDeviceID(): string | null {+ const name = getPostHogPersistenceName();+ return name ? parseDeviceID(readStorage(name)) : null;+}++// The cookie sits on `.agpt.co`, so the marketing site may have written it.+function readSharedPostHogDeviceID(): string | null {+ const name = getPostHogPersistenceName();+ return name ? parseDeviceID(readCookie(name)) : null;+}++function getPostHogPersistenceName(): string | null {const key = process.env.NEXT_PUBLIC_POSTHOG_KEY;- if (!key) return null;- const persistenceKey = `ph_${key}_posthog`;- const raw = readStorage(persistenceKey) ?? readCookie(persistenceKey);+ return key ? `ph_${key}_posthog` : null;+}++function parseDeviceID(raw: string | null): string | null {if (!raw) return null;try {const parsed = JSON.parse(raw) as { $device_id?: unknown };diff --git a/autogpt_platform/frontend/src/services/analytics/reset-identity.ts b/autogpt_platform/frontend/src/services/analytics/reset-identity.tsindex 9a4258310b..0788b3ab22 100644--- a/autogpt_platform/frontend/src/services/analytics/reset-identity.ts+++ b/autogpt_platform/frontend/src/services/analytics/reset-identity.ts@@ -1,4 +1,8 @@-import { syncPostHogConsent } from "@/providers/posthog/posthog-consent";+import {+ forgetPostHogStorage,+ syncPostHogConsent,+} from "@/providers/posthog/posthog-consent";+import { hasConsentFor } from "@/services/consent/consent";import posthog from "posthog-js";import { resetAnonymousID } from "./anonymous-id";import { getPostHogBaseProperties } from "./posthog-base-properties";@@ -17,6 +21,15 @@ export function resetAnalyticsIdentity(): void {}if (didReset) restoreBaseProperties();resetAnonymousID(anonymousID);+ // With consent reset() rewrote PostHog's storage with the new identity;+ // without it (declined or still pending) the old one would stay stored.+ if (!hasConsentFor("analytics")) {+ try {+ forgetPostHogStorage();+ } catch {+ // Analytics must never block an account change.+ }+ }// reset() also drops PostHog's own opt-in, which opts a consenting visitor// out until the next page load unless it is restored here.if (didReset) {diff --git a/autogpt_platform/frontend/src/services/analytics/useReportAttribution.ts b/autogpt_platform/frontend/src/services/analytics/useReportAttribution.tsindex 9043c99e4e..7cded5b297 100644--- a/autogpt_platform/frontend/src/services/analytics/useReportAttribution.ts+++ b/autogpt_platform/frontend/src/services/analytics/useReportAttribution.ts@@ -3,6 +3,7 @@import { usePostAnalyticsReportUserAttribution } from "@/app/api/__generated__/endpoints/analytics/analytics";import { useAuth } from "@/lib/auth/hooks/useAuth";import { buildAttributionPayload } from "@/services/analytics/attribution-payload";+import { useConsentAnswerPending } from "@/services/consent/useConsent";import { useEffect } from "react";const REPORTED_KEY = "agpt_attribution_reported";@@ -11,15 +12,19 @@ let reportedInMemory: string | null = null;/*** Once per user per browser, tell the backend where this user came from.* The backend only ever fills empty fields, so reporting from a second- * browser later is harmless.+ * browser later is harmless. While the consent answer is pending it waits:+ * a grant can still change the anonymous id (to the one the marketing site+ * left, see anonymous-id.ts), and the backend would otherwise keep this+ * page's throwaway id while PostHog moves on.*/export function useReportAttribution() {const { user } = useAuth();const { mutate: reportAttribution } = usePostAnalyticsReportUserAttribution();+ const isAnswerPending = useConsentAnswerPending();const userID = user?.id ?? null;useEffect(() => {- if (!userID || !claimReport(userID)) return;+ if (!userID || isAnswerPending || !claimReport(userID)) return;reportAttribution({ data: buildAttributionPayload() },{@@ -29,7 +34,7 @@ export function useReportAttribution() {onError: () => releaseReport(userID),},);- }, [userID, reportAttribution]);+ }, [userID, isAnswerPending, reportAttribution]);}function claimReport(userID: string): boolean {diff --git a/autogpt_platform/frontend/src/services/consent/consent.ts b/autogpt_platform/frontend/src/services/consent/consent.tsindex 2ef3ba8c2b..c4b26cf731 100644--- a/autogpt_platform/frontend/src/services/consent/consent.ts+++ b/autogpt_platform/frontend/src/services/consent/consent.ts@@ -72,6 +72,21 @@ export function hasConsentFor(category: ConsentCategory): boolean {return getConsent()[category];}+/**+ * Whether the visitor has answered without allowing `category`. False while+ * the answer is still pending (uc.js not loaded yet and no stored answer, or+ * the banner asking): nothing for the category is read or written then, but+ * what an earlier visit stored stays until the visitor actually declines.+ * Cookiebot answers per domain, so the visitor may have allowed it on agpt.co,+ * whose PostHog cookie on `.agpt.co` this app shares. Without a consent+ * manager nothing can ever be allowed, so that counts as declined.+ */+export function hasDeclinedConsentFor(category: ConsentCategory): boolean {+ if (!isConsentManagerConfigured()) return true;+ const answer = getConsentAnswer();+ return answer !== null && !answer[category];+}+export function isSameConsent(a: ConsentState, b: ConsentState): boolean {return (a.analytics === b.analytics &&@@ -118,6 +133,61 @@ export function openConsentSettings(): void {getCookiebotAPI()?.renew();}+// uc.js is injected after hydration, so it can arrive after the page has+// finished loading. Only once it is this late is it taken as blocked.+export const COOKIEBOT_LOAD_GRACE_MS = 10_000;++/**+ * Whether the visitor's answer is still to come: a consent manager is+ * configured and has no answer yet, and it is asking, or uc.js may still+ * arrive. A uc.js that hasn't arrived COOKIEBOT_LOAD_GRACE_MS after the page+ * loaded counts as blocked: it never answers.+ */+export function isConsentAnswerPending(): boolean {+ if (typeof window === "undefined") return true;+ if (!isConsentManagerConfigured() || getConsentAnswer() !== null) {+ return false;+ }+ if (getCookiebotAPI()) return true;+ return msSincePageLoaded() < COOKIEBOT_LOAD_GRACE_MS;+}++/** Like subscribeToConsentManagerStatus, plus the end of the grace. */+export function subscribeToConsentAnswerPending(+ listener: () => void,+): () => void {+ if (typeof window === "undefined") return () => {};+ let timer: ReturnType<typeof setTimeout> | undefined;+ function scheduleGraceEnd() {+ clearTimeout(timer);+ if (document.readyState !== "complete") return;+ const remaining = COOKIEBOT_LOAD_GRACE_MS - msSincePageLoaded();+ if (remaining >= 0) timer = setTimeout(listener, remaining + 1);+ }+ function onChange() {+ scheduleGraceEnd();+ listener();+ }+ scheduleGraceEnd();+ const unsubscribe = subscribeToConsentManagerStatus(onChange);+ return () => {+ clearTimeout(timer);+ unsubscribe();+ };+}++// 0 until the page has loaded; unbounded where the browser has no+// navigation timing to tell when that was.+function msSincePageLoaded(): number {+ if (document.readyState !== "complete") return 0;+ const [navigation] = performance.getEntriesByType("navigation") as+ | PerformanceNavigationTiming[]+ | [];+ if (!navigation) return Number.POSITIVE_INFINITY;+ const loadedAt = navigation.loadEventEnd || navigation.loadEventStart;+ return loadedAt ? performance.now() - loadedAt : 0;+}+export type ConsentManagerStatus = "loading" | "ready" | "unavailable";/**@@ -131,17 +201,24 @@ export function getConsentManagerStatus(): ConsentManagerStatus {return document.readyState === "complete" ? "unavailable" : "loading";}+// The script is injected after hydration, so the page can finish loading+// before it arrives (and before its tag exists to listen to), and a visitor+// with no answer yet gets no consent event until they reply: the dialog+// events are what say Cookiebot arrived for them.export function subscribeToConsentManagerStatus(listener: () => void,): () => void {if (typeof window === "undefined") return () => {};const script = document.getElementById(COOKIEBOT_SCRIPT_ID);- window.addEventListener("load", listener);+ const windowEvents = ["load", ...COOKIEBOT_DIALOG_EVENTS];+ windowEvents.forEach((event) => window.addEventListener(event, listener));script?.addEventListener("load", listener);script?.addEventListener("error", listener);const unsubscribe = subscribeToConsent(listener);return () => {- window.removeEventListener("load", listener);+ windowEvents.forEach((event) =>+ window.removeEventListener(event, listener),+ );script?.removeEventListener("load", listener);script?.removeEventListener("error", listener);unsubscribe();diff --git a/autogpt_platform/frontend/src/services/consent/useConsent.ts b/autogpt_platform/frontend/src/services/consent/useConsent.tsindex ca37d5b17f..6a5d80174e 100644--- a/autogpt_platform/frontend/src/services/consent/useConsent.ts+++ b/autogpt_platform/frontend/src/services/consent/useConsent.ts@@ -4,9 +4,11 @@ import { useSyncExternalStore } from "react";import {getConsent,getConsentManagerStatus,+ isConsentAnswerPending,isSameConsent,NO_CONSENT,subscribeToConsent,+ subscribeToConsentAnswerPending,subscribeToConsentManagerStatus,type ConsentManagerStatus,type ConsentState,@@ -42,3 +44,12 @@ export function useConsentManagerStatus() {(): ConsentManagerStatus => "loading",);}++/** Whether the visitor's answer is still to come; see isConsentAnswerPending. */+export function useConsentAnswerPending() {+ return useSyncExternalStore(+ subscribeToConsentAnswerPending,+ isConsentAnswerPending,+ () => true,+ );+}diff --git a/docs/engineering/tracking-plan.md b/docs/engineering/tracking-plan.mdindex 6a58361d06..90ad423018 100644--- a/docs/engineering/tracking-plan.md+++ b/docs/engineering/tracking-plan.md@@ -67,7 +67,7 @@ Every event must carry:| Property | Value || --- | --- || `environment` | `settings.config.app_env` on the backend (`local`, `dev`, `prod`), the matching app environment in the browser. |-| `source` | Which emitter sent it: `chat_copilot` (the events `copilot/tracking.py` captures itself; `chat_message_sent` goes through `product_analytics` and is `platform`), `platform` (every other backend emitter), `web` (the browser). |+| `source` | Which emitter sent it: `chat_copilot` (the events `copilot/tracking.py` captures itself; `chat_message_sent` goes through `product_analytics` and is `platform`), `platform` (every other backend emitter), `web` (the browser), `website` (the marketing site, see [Marketing site](#marketing-site)). || `revision` | The deployed build's git sha, which Sentry reports as its release: `SENTRY_RELEASE` on the backend, the build's commit in the browser (`SENTRY_RELEASE`, else Vercel's `VERCEL_GIT_COMMIT_SHA`). Left out of a build that has none, such as a local one, and from `$ai_generation` (below). |Every backend event goes through `capture()` in@@ -222,6 +222,110 @@ mount-time steps (`tour_start`, the first `tour_scenario_start`) on a firstvisit where the answer arrives only after the page loads; `tour_start` isretried on the next tour visit in that session.+## Marketing site++The marketing site, `agpt.co` (repo `Significant-Gravitas/autogpt-marketing-site`),+sends to the same PostHog project as the app, so one funnel runs from a+website visit to a paid account (SECRT-2769). Its code is+`lib/posthog/posthog.ts` and `lib/posthog/goals.ts` there.++- **Consent.** The site asks through Cookiebot too, but Cookiebot keeps one+ answer per domain, so a visitor answers once on `agpt.co` and once on+ `platform.agpt.co`. The site downloads and starts `posthog-js` only once its+ answer allows analytics; before that nothing reaches PostHog, not even+ `/flags`.+- **Settings.** The app's: opted out by default, `localStorage+cookie`+ persistence with consent, autocapture and page leave, no session replay, and+ the same-origin relay at `/relay/pnosthog`.+- **Base properties.** `environment` (`prod`, `preview`, `local`, from+ Vercel), `source: website`, and `revision` (the commit) where the build has+ one.++| Event | Fires when |+| --- | --- |+| `$pageview` | A route or query string changes, and once more when consent arrives, so the landing page counts. |+| `$pageleave`, `$autocapture` | As in the app. |+| `section_viewed` | A section marked for DataFast's scroll goals comes on screen: as soon as any of it shows, and again after it left the screen and comes back. `section` is the part after `scroll_to_` (e.g. `faq`). |+| DataFast goals | Every DataFast goal is also sent to PostHog under an `object_action`, past-tense name, with the same parameters (`data-fast-goal-*` attributes, hyphens turned into underscores, or the properties passed from code, such as `pricing_billing_toggled.billing`). |++The goal names (`POSTHOG_GOAL_EVENTS` on the site, where a test fails for a+goal without one):++| DataFast goal | PostHog event |+| --- | --- |+| `demo_tour_click` | `demo_tour_clicked` |+| `demo_video_play` | `demo_video_played` |+| `demo_video_sound` | `demo_video_sound_toggled` |+| `get_started_click` | `get_started_clicked` |+| `github_selfhost_click` | `github_selfhost_clicked` |+| `github_stars_click` | `github_stars_clicked` |+| `hero_prompt_chip_click` | `hero_prompt_chip_clicked` |+| `hero_prompt_focus` | `hero_prompt_focused` |+| `hero_prompt_submit` | `hero_prompt_submitted` |+| `marketplace_agent_click` | `marketplace_agent_clicked` |+| `marketplace_browse_click` | `marketplace_browse_clicked` |+| `nav_product_click` | `nav_product_clicked` |+| `press_source_click` | `press_source_clicked` |+| `pricing_billing_toggle` | `pricing_billing_toggled` |+| `pricing_click` | `pricing_clicked` |+| `pricing_plan_cta_click` | `pricing_plan_cta_clicked` |+| `product_demo_interact` | `product_demo_interacted` |+| `product_video_play` | `product_video_played` |+| `see_it_in_action_click` | `see_it_in_action_clicked` |+| `signin_click` | `signin_clicked` |+| `testimonial_source_click` | `testimonial_source_clicked` |+| `usecase_cta_click` | `usecase_cta_clicked` |+| `watch_demo_click` | `watch_demo_clicked` |++These names follow the [naming rules](#naming) and are never renamed. They+live in the site's repo, not in `posthog-events.ts`.++### Hand-over to the app++The site keeps PostHog's state in the `ph_<project key>_posthog` cookie on+`.agpt.co`, which `platform.agpt.co` can read. It carries `$device_id`. The+app adopts that id as the visitor's anonymous id, so `identify()` at signup+merges the website pageviews into the new user:++- **While the app's answer is pending,** the app leaves the cookie (and its+ own stored identity) alone and runs PostHog on an in-memory id. Only an+ answer without analytics deletes them, the shared cookie included; the site+ does the same on its side. If Cookiebot asks again after PostHog started+ with an earlier grant, PostHog stops capturing (so it writes nothing until+ the visitor answers) but keeps its storage, the shared cookie included; a+ grant resumes it on the same id.+- **On a grant,** during the page or from an earlier answer, the visitor's+ anonymous id is, in order: the app's own `agpt_anonymous_id`; else the+ device id in the app's own PostHog storage; else the cookie's `$device_id`;+ else the page's in-memory id (`src/services/analytics/anonymous-id.ts`).+ Both device ids are read before PostHog rewrites them. If PostHog started+ on the in-memory id, it moves onto the adopted one without a reload+ (`src/providers/posthog/posthog-consent.ts`).+- **Returning app visitors keep their own id.** A browser that already has+ `agpt_anonymous_id` from an earlier visit with consent keeps it, so+ website visits made under another id do not merge into the account.+- **Only a new visitor or a new account takes the website's id.** The+ cookie's `$device_id` is adopted only when nobody is signed in, or the+ account was created in this browser (the `agpt_account_created` signup flag,+ as seen while signed in on that page: the ads signup conversion clears it).+ An older account signed in on a browser someone else used on agpt.co keeps+ the in-memory id, so `identify()` doesn't merge that person's website visits+ into it. Until the auth store knows the session (after its fallback+ validation; an auth error leaves it unknown for the page), the choice+ waits: PostHog stays opted out and in memory, events+ are held, and nothing is stored. The id then settles, PostHog moves onto it+ and starts, before `identify()`.+- **Logout and account switches forget the shared cookie** when analytics is+ not allowed (declined or still pending): PostHog then runs in memory, so its+ `reset()` would leave the previous person's id there for the next account.+- **The backend gets the id PostHog ends up with.** The attribution report+ (`useReportAttribution`, the `anonymous_id` and `posthog_distinct_id` on+ `UserAttribution`) waits while the answer is pending, then sends the adopted+ id on a grant, or the in-memory id (and no PostHog id) on a decline. It+ goes at once without a Cookiebot domain group, and when uc.js still hasn't+ arrived 10 seconds after the page loaded (blocked). A visitor who never+ answers is reported on the first later page where they do.+## Onboarding and activation| Event | Sender | Status | Required properties | Fires when |