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.
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";
export function resetAnalyticsIdentity(): void {
let anonymousID: string | undefined;
let didReset = false;
try {
if (posthog.get_distinct_id()) {
posthog.reset(true);
didReset = true;
anonymousID = posthog.get_distinct_id();
}
} catch {
anonymousID = undefined;
}
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) {
try {
syncPostHogConsent();
} catch {
// Analytics must never block an account change.
}
}
}
// reset() wipes the super properties along with the identity.
function restoreBaseProperties() {
try {
posthog.register(getPostHogBaseProperties());
} catch {
// Analytics is never worth a broken logout.
}
}