Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 16 additions & 5 deletions pages/docs/tracking-methods/integrations/nextjs.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -73,23 +73,34 @@ import { dataItems } from "/utils/constants";
export default MyApp;
```

b. **If using app router**, open or create the file `layout.js` or `layout.tsx` in your app/ directory
b. **If using app router**, first create a client component in a file (e.g. `components/MixpanelProvider.js`) to handle the client-side initialization.
```javascript
'use client';

import { useEffect } from 'react';
import { usePathname } from 'next/navigation';
import { initMixpanel } from '../lib/mixpanelClient';
export default function RootLayout({ children }) {

export default function MixpanelProvider({ children }) {

useEffect(() => {
initMixpanel(); // Initialize Mixpanel
}, []);

return <>{children}</>;
}
```

Next, open or create the file `layout.js` or `layout.tsx` in your app/ directory and wrap the body component with the MixpanelProvider.
```javascript
import { MixpanelProvider } from '../components/MixpanelProvider';

export default function RootLayout({ children }) {

return (
<html lang="en">
<body>{children}</body>
<body>
<MixpanelProvider>{children}</MixpanelProvider>
</body>
</html>
);
}
Expand Down