Add Analytics

Hook up analytics using PostHog.

Use PostHog to track page views and user interactions, as well as to show a visitor hit counter in your production build.

PostHog Setup

Retrieve PostHog Variables

From the PostHog dashboard's left sidebar, click Settings:

Under Settings - General:

  1. Copy your Project token. Enter this into your .env.local file under your NEXT_PUBLIC_POSTHOG_KEY:

    NEXT_PUBLIC_POSTHOG_KEY=<your-project-token>
    
  2. Under SDK setup, grab the api_host value from one of the snippets (For example, https://us.i.posthog.com). Enter this into your .env.local file under your NEXT_PUBLIC_POSTHOG_HOST:

    NEXT_PUBLIC_POSTHOG_HOST=<your-project-api-host>
    
  3. Copy your Project ID. Enter this into your .env.local file under your POSTHOG_PROJECT_ID:

    POSTHOG_PROJECT_ID=<your-project-id>
    

Configure PostHog

For cloud deployment:

If you are deploying to Vercel:

  1. In the left sidebar of your guestbook project, select Environment Variables.

  2. Click Add Environment Variable.

  3. Enter the following as Config Type variables:

    NEXT_PUBLIC_POSTHOG_KEY=<your-project-token>
    NEXT_PUBLIC_POSTHOG_HOST=<your-project-api-host>
    FLAG_COUNTER=true
    # End `FLAG_COUNTER_URL` with a trailing slash for one exact page
    FLAG_COUNTER_URL=https://<your-project-url>.vercel.app/
    # Use PostHog UTC time: `YYYY-MM-DD HH:mm:ss` (or `YYYY-MM-DD` for midnight)
    # Leave `FLAG_COUNTER_DATE` empty to count all time
    FLAG_COUNTER_DATE=2026-09-16 00:00:00
    
  4. Enter the following as Secret Type variables:

    # IMPORTANT: MAKE SURE THEY ARE SECRET SO THEY ARE HIDDEN
    POSTHOG_PERSONAL_API_KEY=<your-personal-api-key>
    POSTHOG_PROJECT_ID=<your-project-id>
    POSTHOG_API_HOST=<your-api-host>
    

Enabling Hit Counter

Enable Hit Counter

In your next.config.ts (or next.config.js), insert FLAG_COUNTER under env:

const nextConfig: NextConfig = {
  env: {
    FLAG_COUNTER: process.env.FLAG_COUNTER,
  },
  // your existing options
};

Enable Live Counter

Create app/api/hits/route.ts which polls /api/hits in production:

export { GET } from "y2k-guestbook/hits";
export const dynamic = "force-dynamic";

Enable Web Tracking

  1. Install posthog-js to enable capture of client pageviews, clicks, and session replay:

    npm install posthog-js
    
  2. Copy instrumentation-client.ts.example as src/instrumentation-client.ts (or the project root if there is no src/):

    npx y2k-guestbook init-instrumentation
    
  3. From the PostHog dashboard's left sidebar, click Settings.

  4. Under Products, select Session replay. Toggle ON:

    • Record user sessions
    • Log capture
    • Canvas capture

Test Hit Counter & Tracking

Development builds do not send $pageview events. To grow the count, build and serve (or use a Vercel deployment) so instrumentation-client.ts can capture:

# Linter check for errors
npm run lint
# Compile
npm run build
# Serve build 
npm run start

Confirm the hit counter matches an exact-page query (not a $current_url LIKE, which includes subpages):

  1. From the PostHog dashboard's left sidebar, click Product analytics.

  2. Click + New, then select SQL.

  3. Enter a SQL query that matches FLAG_COUNTER_URL and FLAG_COUNTER_DATE:

    -- Home: FLAG_COUNTER_URL=https://<your-project-url>.vercel.app/
    -- FLAG_COUNTER_DATE=2026-09-18 01:57:00
    SELECT uniq(distinct_id)
    FROM events
    WHERE event = '$pageview'
    AND properties.$host = '<your-project-url>.vercel.app'
    AND properties.$pathname = '/'
    AND timestamp >= toDateTime('2026-09-18 01:57:00', 'UTC')
    
    -- Nested board: FLAG_COUNTER_URL=https://example.com/guestbook/
    SELECT uniq(distinct_id)
    FROM events
    WHERE event = '$pageview'
    AND properties.$host = 'example.com'
    AND properties.$pathname = '/guestbook'
    AND timestamp >= toDateTime('2026-09-18 01:57:00', 'UTC')
    
    -- Several exact pages
    SELECT uniq(distinct_id)
    FROM events
    WHERE event = '$pageview'
    AND (
        (properties.$host = 'example.com' AND properties.$pathname = '/')
        OR (properties.$host = 'example.com' AND properties.$pathname = '/guestbook')
    )
    AND timestamp >= toDateTime('2026-09-18 01:57:00', 'UTC')
    
  4. Click Run.

  5. Confirm that the returned Results match your displayed hit counter.

What's Next

Customize guestbook settings and manage comments: