# Next.js | App router

{% hint style="info" %}
**Client Side Rendering**

Intelligems currently recommends using client-side rendering rather than SSR for Next.js App Router integrations to keep the implementation simple and avoid hydration mismatches.
{% endhint %}

{% hint style="warning" %}
ESM Errors

Depending on the setup of your site, you may encounter an `ERR_REQUIRE_ESM` error when using our package. Try adding the snippet below to `next.config.js` and/or reach out to support.

`transpilePackages: ['@intelligems/headless']`
{% endhint %}

### Environment Variables

Add the following to your `.env` file:

```dotenv
NEXT_PUBLIC_INTELLIGEMS_ORG_ID=<Intelligems-ID>
NEXT_PUBLIC_SHOPIFY_STOREFRONT_ACCESS_TOKEN=<Storefront-API-Token>
```

### Provider Integration

The [`IntelligemsNextClientsideAppDirectoryProvider`](https://headless.intelligems.io/reference/providers/provider-props) component stores Intelligems data in React Context so hooks and components can access it throughout your app.

#### Integration Example

**IntelligemsProvider**

Create `IntelligemsProvider`:

```tsx
"use client";

import { IntelligemsNextClientsideAppDirectoryProvider } from "@intelligems/headless/next-clientside-app-directory";

export function IntelligemsProvider({
  children,
}: {
  children: React.ReactNode;
}) {
  return (
    <IntelligemsNextClientsideAppDirectoryProvider
      organizationId={process.env.NEXT_PUBLIC_INTELLIGEMS_ORG_ID}
      storefrontApiToken={
        process.env.NEXT_PUBLIC_SHOPIFY_STOREFRONT_ACCESS_TOKEN
      }
      activeCurrencyCode="USD" // Must be provided
      antiFlicker={true}
    >
      {children}
    </IntelligemsNextClientsideAppDirectoryProvider>
  );
}
```

**RootLayout**

Insert `IntelligemsProvider` inside your `RootLayout`:

```tsx
import { IntelligemsProvider } from "./intelligems-provider";

export default function RootLayout({
  children,
}: {
  children: React.ReactNode;
}) {
  return (
    <html lang="en">
      <body>
        <IntelligemsProvider>{children}</IntelligemsProvider>
      </body>
    </html>
  );
}
```

### Page Views Tracking

`useIgTrack` is a client-side hook tracking page views.

To provide accurate analytics ensure it is configured with valid parameters:

* `cartOrCheckoutToken`
* `currency`
* `country`

**Create a client component**

```tsx
"use client";

import React from "react";
import { useIgTrack } from "@intelligems/headless/next-clientside-app-directory";

export function IntelligemsTracker() {
  const { checkout } = React.useContext(StoreContext);

  useIgTrack({
    cartOrCheckoutToken: checkout?.id,
    currency: checkout?.totalTaxV2?.currencyCode,
    country: "US",
  });

  return null;
}
```

**Render it inside your provider wrapper**

```tsx
import { IntelligemsProvider } from "./intelligems-provider";
import { IntelligemsTracker } from "./intelligems-tracker";

export default function RootLayout({
  children,
}: {
  children: React.ReactNode;
}) {
  return (
    <html lang="en">
      <body>
        <IntelligemsProvider>
          <IntelligemsTracker />
          {children}
        </IntelligemsProvider>
      </body>
    </html>
  );
}
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://headless.intelligems.io/version-1.2.16/integration-guides/next.js-or-app-router.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
