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>
This commit is contained in:
26
packages/analytics/web/src/providers/plausible/env.ts
Normal file
26
packages/analytics/web/src/providers/plausible/env.ts
Normal file
@@ -0,0 +1,26 @@
|
||||
/* eslint-disable turbo/no-undeclared-env-vars */
|
||||
import { defineEnv } from "envin";
|
||||
import * as z from "zod";
|
||||
|
||||
import { envConfig } from "@turbostarter/shared/constants";
|
||||
|
||||
import type { Preset } from "envin/types";
|
||||
|
||||
export const preset = {
|
||||
id: "plausible",
|
||||
clientPrefix: "NEXT_PUBLIC_",
|
||||
client: {
|
||||
NEXT_PUBLIC_PLAUSIBLE_DOMAIN: z.string(),
|
||||
NEXT_PUBLIC_PLAUSIBLE_HOST: z.string(),
|
||||
},
|
||||
} as const satisfies Preset;
|
||||
|
||||
export const env = defineEnv({
|
||||
...envConfig,
|
||||
...preset,
|
||||
env: {
|
||||
...process.env,
|
||||
NEXT_PUBLIC_PLAUSIBLE_DOMAIN: process.env.NEXT_PUBLIC_PLAUSIBLE_DOMAIN,
|
||||
NEXT_PUBLIC_PLAUSIBLE_HOST: process.env.NEXT_PUBLIC_PLAUSIBLE_HOST,
|
||||
},
|
||||
});
|
||||
109
packages/analytics/web/src/providers/plausible/index.tsx
Normal file
109
packages/analytics/web/src/providers/plausible/index.tsx
Normal file
@@ -0,0 +1,109 @@
|
||||
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;
|
||||
42
packages/analytics/web/src/providers/plausible/server.ts
Normal file
42
packages/analytics/web/src/providers/plausible/server.ts
Normal file
@@ -0,0 +1,42 @@
|
||||
import { logger } from "@turbostarter/shared/logger";
|
||||
|
||||
import { env } from "./env";
|
||||
|
||||
import type { AnalyticsProviderServerStrategy } from "@turbostarter/analytics";
|
||||
|
||||
export const { track } = {
|
||||
track: (event, data) => {
|
||||
const url = typeof data?.url === "string" ? data.url : "app://server-side";
|
||||
const referrer =
|
||||
typeof data?.referrer === "string" ? data.referrer : undefined;
|
||||
const ip = typeof data?.ip === "string" ? data.ip : undefined;
|
||||
|
||||
const props = data
|
||||
? Object.fromEntries(
|
||||
Object.entries(data).filter(
|
||||
([key]) => !["url", "referrer", "ip"].includes(key),
|
||||
),
|
||||
)
|
||||
: undefined;
|
||||
|
||||
void fetch(`${env.NEXT_PUBLIC_PLAUSIBLE_HOST}/api/event`, {
|
||||
method: "POST",
|
||||
headers: {
|
||||
"Content-Type": "application/json",
|
||||
"User-Agent": "TurboStarter-Server/1.0 (Server-side tracking)",
|
||||
...(ip && { "X-Forwarded-For": ip }),
|
||||
},
|
||||
body: JSON.stringify({
|
||||
domain: env.NEXT_PUBLIC_PLAUSIBLE_DOMAIN,
|
||||
name: event,
|
||||
url: url,
|
||||
...(referrer && { referrer }),
|
||||
...(props && Object.keys(props).length > 0 && { props }),
|
||||
}),
|
||||
}).then((res) => {
|
||||
if (!res.ok) {
|
||||
logger.error("Failed to post event to Plausible: ", res);
|
||||
}
|
||||
});
|
||||
},
|
||||
} satisfies AnalyticsProviderServerStrategy;
|
||||
Reference in New Issue
Block a user