Loading...
Loading...
Add PostHog analytics to a web application, including event tracking, page views, feature flags, and session replay.
npx skill4agent add spencerpauly/awesome-cursor-skills adding-analyticsnext.config.*vite.config.*package.jsonindex.htmlnpm install posthog-jsnpm install posthog-js posthog-nodepip install posthognpm install posthog-nodeapp/providers.tsx"use client";
import posthog from "posthog-js";
import { PostHogProvider as PHProvider } from "posthog-js/react";
import { useEffect } from "react";
export function PostHogProvider({ children }: { children: React.ReactNode }) {
useEffect(() => {
posthog.init(process.env.NEXT_PUBLIC_POSTHOG_KEY!, {
api_host: process.env.NEXT_PUBLIC_POSTHOG_HOST ?? "https://us.i.posthog.com",
capture_pageview: false, // we capture manually for SPAs
});
}, []);
return <PHProvider client={posthog}>{children}</PHProvider>;
}{children}<PostHogProvider>_app.tsxuseEffectposthog.capture('$pageview').envNEXT_PUBLIC_POSTHOG_KEY=phc_...
NEXT_PUBLIC_POSTHOG_HOST=https://us.i.posthog.com.env.exampleposthog.capture("event_name", { ...properties })posthog.isFeatureEnabled("flag-name")useFeatureFlagEnabledsession_recording: { maskAllInputs: false }posthog-js