- Start by installing the SDK.
-
Set up environment variables for your Mixpanel token
a. Create a
.env.localfile in the root of your Next.js project (note: for production use.env.production). b. Then, add your Mixpanel project token. -
Create a utility file to initialize the Mixpanel SDK in your Next.js application
a. Create a file (e.g.,
mixpanelClient.js) under thelib/folder of your Next.js project b. Add the following code to the file: -
Initialize Mixpanel in your Next.js application
a. If using page router, open or create the file
_app.jsorapp.tsxin your pages/ directory. Then import and initialize Mixpanel insideuseEffect. You’ll also need to track pageviews manually usinguseRouter.b. If using app router, first create a client component in a file (e.g.components/MixpanelProvider.js) to handle the client-side initialization.Next, open or create the filelayout.jsorlayout.tsxin your app/ directory and wrap the body component with the MixpanelProvider. - Run the development server, test your application (click some things, submit some things, etc.) and check the Mixpanel live event view to confirm that events are sending via Autocapture.
Integrations