# Next.js | Pages router

{% 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 [`IntelligemsNextClientsideProvider`](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

```tsx
import { IntelligemsNextClientsideProvider } from "@intelligems/headless/next-clientside";

export default function MyApp({ Component, pageProps }: AppProps) {
  return (
    <>
      <IntelligemsNextClientsideProvider
        organizationId={process.env.NEXT_PUBLIC_INTELLIGEMS_ORG_ID}
        storefrontApiToken={
          process.env.NEXT_PUBLIC_SHOPIFY_STOREFRONT_ACCESS_TOKEN
        }
        activeCurrencyCode="USD" // Must be provided
        antiFlicker={true}
      >
        ...
      </IntelligemsNextClientsideProvider>
    </>
  );
}
```

#### 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`

```tsx
import React from "react";

import {
  IntelligemsNextClientsideProvider,
  useIgTrack,
} from "@intelligems/headless/next-clientside";

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

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

  return null;
};

export default function MyApp({ Component, pageProps }: AppProps) {
  return (
    <IntelligemsNextClientsideProvider
      organizationId={process.env.NEXT_PUBLIC_INTELLIGEMS_ORG_ID}
      storefrontApiToken={
        process.env.NEXT_PUBLIC_SHOPIFY_STOREFRONT_ACCESS_TOKEN
      }
      antiFlicker={true}
    >
      <StoreProvider>
        <IntelligemsTracker />
        <Component {...pageProps} />
      </StoreProvider>
    </IntelligemsNextClientsideProvider>
  );
}
```


---

# 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-pages-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.
