Integrate Vemetric into your Next.js application
Easily integrate Vemetric into your Next.js application.
Next.js is a powerful React framework that supports both client-side and server-side rendering. With its evolving architecture, you have multiple options for integrating Vemetric depending on your Next.js version and use case.
This guide covers all integration methods, helping you choose the right approach for your Next.js application.
Client-Side Setup
Client-side tracking captures user interactions in the browser, including page views, custom events, and outbound link clicks.
Next.js 15.3 introduced the instrumentation-client.ts file, which runs on the client before any other code. This works with both App Router and Pages Router.
Install the JavaScript SDK
In your project directory, run:
We use
@vemetric/webhere becauseinstrumentation-client.tsruns before React initializes.
Create instrumentation-client.ts
Create a file called instrumentation-client.ts in your project root (same level as the app or pages directory):
See the JavaScript SDK docs for detailed explanations of each option.
Make sure to set the
NEXT_PUBLIC_VEMETRIC_TOKENenvironment variable with your project token (found in the project settings).
Verify installation
Once added, Vemetric will automatically start tracking page views. You can verify this by checking your Vemetric dashboard for incoming events a few seconds after loading a page.
Data from
localhostwon’t be tracked by default.
Optional: Track custom events and identify logged-in users
You can track custom events from any Client Component by importing the vemetric object:
See the JavaScript SDK docs for a detailed explanation of the available parameters.
Also learn more about User Identification to track authenticated users and see their full journey.
For Next.js apps on version 15.2 and below, initialize Vemetric in your root layout or app file.
Install the React SDK
In your project directory, run:
Initialize in layout.tsx
Import and include the VemetricScript component in your root layout.tsx file:
See the React SDK docs for detailed explanations of each option.
Make sure to set the
NEXT_PUBLIC_VEMETRIC_TOKENenvironment variable with your project token (found in the project settings).
Verify installation
Once added, Vemetric will automatically start tracking page views. You can verify this by checking your Vemetric dashboard for incoming events a few seconds after loading a page.
Data from
localhostwon’t be tracked by default.
Optional: Track custom events and identify logged-in users
You can track custom events from any Client Component by importing the vemetric object:
See the React SDK docs for a detailed explanation of the available parameters.
Also learn more about User Identification to track authenticated users and see their full journey.
Install the React SDK
In your project directory, run:
Initialize in _app.tsx
Import and include the VemetricScript component in your _app.tsx file:
See the React SDK docs for detailed explanations of each option.
Make sure to set the
NEXT_PUBLIC_VEMETRIC_TOKENenvironment variable with your project token (found in the project settings).
Verify installation
Once added, Vemetric will automatically start tracking page views. You can verify this by checking your Vemetric dashboard for incoming events a few seconds after loading a page.
Data from
localhostwon’t be tracked by default.
Optional: Track custom events and identify logged-in users
You can track custom events from any Component by importing the vemetric object:
See the React SDK docs for a detailed explanation of the available parameters.
Also learn more about User Identification to track authenticated users and see their full journey.
Server-Side Setup
Server-side tracking is useful for tracking backend events like purchases, sign-ups, or other important conversions that happen on the server.
Server-side tracking complements client-side tracking but doesn’t track page views. Use
@vemetric/nodein addition to client-side tracking, not as a replacement.
Install the Node.js SDK
Create a shared client
Create a file like lib/vemetric.ts to store your Vemetric client:
It’s fine to use the same
NEXT_PUBLIC_VEMETRIC_TOKENenvironment variable as your client-side code.
Use in Server Components, Actions, and Routes
Import and use the vemetric client anywhere on the server:
Server Component:
Server Action:
Route Handler app/api/data/route.ts:
Install the Node.js SDK
Create a shared client
Create a file like lib/vemetric.ts to store your Vemetric client:
It’s fine to use the same
NEXT_PUBLIC_VEMETRIC_TOKENenvironment variable as your client-side code.
Use in API routes and getServerSideProps
API Route pages/api/purchase.ts:
getServerSideProps:
See the Node.js SDK docs for all available methods and options.
Related Resources
We highly recommend you to use a custom proxy in front of Vemetric for better results. See our guide on setting up a custom proxy with Next.js for more information.
Also keep in mind that one project can be used to track data from multiple websites or applications. See our guide on tracking across subdomains for more information.
Easily integrate Vemetric into your WordPress website.
Easily integrate Vemetric into your React Router application.