> ## Documentation Index
> Fetch the complete documentation index at: https://docs.tracelit.io/llms.txt
> Use this file to discover all available pages before exploring further.

# Next.js — App Router

> Install Tracelit in a Next.js App Router project using a Client Component. SSR-safe, no window guard needed.

<img src="https://mintcdn.com/tracelit/yOAHPOHDFCUOtcaa/images/nextjs.svg?fit=max&auto=format&n=yOAHPOHDFCUOtcaa&q=85&s=dbfc38b06e972f7ce85cc7cb3c8fbc74" alt="Next.js" style={{ width: '40px', marginBottom: '4px' }} width="512" height="512" data-path="images/nextjs.svg" />

```bash theme={null}
npm install @tracelit/tracker
```

Because `init()` needs to run in the browser (not on the server), wrap it in a Client Component.

***

**1. Create the component**

```tsx theme={null}
// components/Tracelit.tsx
'use client'
import { useEffect } from 'react'
import { init } from '@tracelit/tracker'

export function Tracelit() {
  useEffect(() => {
    init({ token: 'YOUR_TOKEN_HERE', environment: 'production' })
  }, [])
  return null
}
```

<Warning>
  Always set `environment`. Only `"production"` and `"development"` are accepted. If omitted, events default to `development`.
</Warning>

**2. Add it to your root layout**

```tsx theme={null}
// app/layout.tsx
import { Tracelit } from '@/components/Tracelit'

export default function RootLayout({ children }: { children: React.ReactNode }) {
  return (
    <html>
      <body>
        {children}
        <Tracelit />
      </body>
    </html>
  )
}
```

<Note>
  `init()` is a no-op on the server — it checks for `window` internally. You don't need a `typeof window !== 'undefined'` guard.
</Note>

***

## Adding user identity (recommended)

Create a second Client Component to call `identify()` once your session is available:

```tsx theme={null}
// components/TracelitIdentity.tsx
'use client'
import { useEffect } from 'react'
import { identify, reset } from '@tracelit/tracker'

export function TracelitIdentity({
  userId,
  traits,
}: {
  userId?: string
  traits?: Record<string, string>
}) {
  useEffect(() => {
    if (userId) identify(userId, traits)
    else reset()
  }, [userId])
  return null
}
```

```tsx theme={null}
// app/layout.tsx
import { Tracelit } from '@/components/Tracelit'
import { TracelitIdentity } from '@/components/TracelitIdentity'
import { getServerSession } from 'next-auth'

export default async function RootLayout({ children }: { children: React.ReactNode }) {
  const session = await getServerSession()
  return (
    <html>
      <body>
        {children}
        <Tracelit />
        <TracelitIdentity
          userId={session?.user?.id}
          traits={{ email: session?.user?.email, plan: session?.user?.plan }}
        />
      </body>
    </html>
  )
}
```

***

## LLM Prompt

Paste this into your coding assistant to install Tracelit for you:

```text theme={null}
Install Tracelit in my Next.js App Router project.
Create a client component that runs:
init({ token: 'YOUR_TOKEN_HERE', environment: 'production' })
Mount it in app/layout.tsx.
Keep edits minimal, list changed files, then give a 30-second verify checklist.
```

***

## What's next?

| Topic                                   | Description                                   | Guide                             |
| --------------------------------------- | --------------------------------------------- | --------------------------------- |
| <Icon icon="user" /> **Identify users** | Full guide to linking sessions to real users. | [Read](/tracking/identify-users)  |
| <Icon icon="tag" /> **Feature tagging** | Tag flows in session replays.                 | [Read](/features/feature-tagging) |
