Files
turbostarter/packages/analytics/web/src/providers/plausible/index.tsx
Alejandro Gutiérrez 3527e732d4 feat: turbostarter boilerplate
Production-ready Next.js boilerplate with:
- Runtime env validation (fail-fast on missing vars)
- Feature-gated config (S3, Stripe, email, OAuth)
- Docker + Coolify deployment pipeline
- PostgreSQL + pgvector, MinIO S3, Better Auth
- TypeScript strict mode (no ignoreBuildErrors)
- i18n (en/es), AI modules, billing, monitoring

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2026-02-04 01:01:55 +01:00

110 lines
2.4 KiB
TypeScript

import { z } from "zod";
import { env } from "./env";
import type {
AllowedPropertyValues,
AnalyticsProviderClientStrategy,
} from "@turbostarter/analytics";
declare global {
interface Window {
plausible?: (
event: string,
options?: { props?: Record<string, unknown> },
) => void;
}
}
const STORAGE_KEYS = {
USER_ID: "plausible_user_id",
USER_TRAITS: "plausible_user_traits",
} as const;
const ValueSchema = z.union([z.string(), z.number(), z.boolean()]);
const TraitsSchema = z.record(z.string(), ValueSchema);
const getStoredIdentity = () => {
if (typeof window === "undefined") {
return { userId: undefined, traits: undefined };
}
try {
const userId = localStorage.getItem(STORAGE_KEYS.USER_ID) ?? undefined;
const traitsStr = localStorage.getItem(STORAGE_KEYS.USER_TRAITS);
let traits: Record<string, AllowedPropertyValues> | undefined;
if (traitsStr) {
const parsed = TraitsSchema.safeParse(JSON.parse(traitsStr));
if (parsed.success) {
traits = parsed.data;
}
}
return { userId, traits };
} catch {
return { userId: undefined, traits: undefined };
}
};
export const { Provider, track, identify, reset } = {
Provider: ({ children }) => {
return (
<>
{children}
<script
defer
data-domain={env.NEXT_PUBLIC_PLAUSIBLE_DOMAIN}
src={`${env.NEXT_PUBLIC_PLAUSIBLE_HOST}/js/script.js`}
/>
</>
);
},
track: (event, data) => {
if (typeof window === "undefined" || !window.plausible) {
return;
}
const { userId, traits } = getStoredIdentity();
const props: Record<string, unknown> = {
...traits,
...data,
};
if (userId) {
props.userId = userId;
}
window.plausible(event, {
props,
});
},
identify: (userId, traits) => {
if (typeof window === "undefined") {
return;
}
try {
localStorage.setItem(STORAGE_KEYS.USER_ID, userId);
if (traits) {
localStorage.setItem(STORAGE_KEYS.USER_TRAITS, JSON.stringify(traits));
}
} catch {
// Ignore storage errors
}
},
reset: () => {
if (typeof window === "undefined") {
return;
}
try {
localStorage.removeItem(STORAGE_KEYS.USER_ID);
localStorage.removeItem(STORAGE_KEYS.USER_TRAITS);
} catch {
// Ignore storage errors
}
},
} satisfies AnalyticsProviderClientStrategy;