Next.js Configuration
Configure Journium Next.js SDK with Provider component, automatic route tracking, and underlying JavaScript SDK options.
Next.js Configuration
The Journium Next.js SDK extends the React SDK with Next.js-specific optimizations including automatic route change tracking for both App Router and Pages Router.
Next.js-Specific Setup
Provider Configuration
import { NextJourniumProvider } from '@journium/nextjs';
const journiumConfig = {
publishableKey: process.env.NEXT_PUBLIC_JOURNIUM_PUBLISHABLE_KEY!,
options: {
debug: process.env.NODE_ENV === 'development',
autoTrackPageviews: true, // Auto-tracks route changes
autocapture: true
}
};
export default function RootLayout({
children,
}: {
children: React.ReactNode;
}) {
return (
<html lang="en">
<body>
<NextJourniumProvider config={journiumConfig}>
{children}
</NextJourniumProvider>
</body>
</html>
);
}import type { AppProps } from 'next/app';
import { NextJourniumProvider } from '@journium/nextjs';
const journiumConfig = {
publishableKey: process.env.NEXT_PUBLIC_JOURNIUM_PUBLISHABLE_KEY!,
options: {
debug: process.env.NODE_ENV === 'development',
autoTrackPageviews: true, // Auto-tracks route changes
autocapture: true
}
};
export default function App({ Component, pageProps }: AppProps) {
return (
<NextJourniumProvider config={journiumConfig}>
<Component {...pageProps} />
</NextJourniumProvider>
);
}Configuration Options
The config prop accepts a JourniumConfig object with the same options as the JavaScript SDK.
For detailed information about all available configuration options, see the JavaScript SDK Configuration Guide.
Next.js-specific considerations:
autoTrackPageviews- Set totrueto automatically track Next.js route changes (recommended)- Environment variables should use
NEXT_PUBLIC_prefix for client-side access - Configuration is SSR-compatible
Next.js Components
<NextJourniumProvider>
Provider component with Next.js optimizations and automatic route tracking.
Props:
| Prop | Type | Required | Description |
|---|---|---|---|
config | JourniumConfig | Yes | Configuration object |
config.publishableKey | string | Yes | Your publishable key |
config.apiHost | string | No | Custom API endpoint |
config.options | JourniumLocalOptions | No | Configuration options |
children | ReactNode | Yes | React children to wrap |
Next.js Features:
- ✅ Automatic route change detection (App Router and Pages Router)
- ✅ SSR-compatible initialization
- ✅ Works with
next/navigationandnext/router - ✅ Optimized for serverless deployments
Automatic Route Tracking
The Next.js provider automatically tracks route changes in both App Router and Pages Router:
// Automatically tracked:
router.push('/products/123');
router.replace('/checkout');
<Link href="/about">About</Link>Disable automatic tracking:
<NextJourniumProvider
config={{
publishableKey: 'pk_...',
options: {
autoTrackPageviews: false // Disable automatic tracking
}
}}
>Then manually track pageviews:
'use client';
import { useTrackPageview } from '@journium/nextjs';
import { usePathname } from 'next/navigation';
import { useEffect } from 'react';
export function PageTracker() {
const pathname = usePathname();
const trackPageview = useTrackPageview();
useEffect(() => {
trackPageview({ pathname });
}, [pathname, trackPageview]);
return null;
}React Hooks
The Next.js SDK includes all React hooks. See the React Hooks documentation for details:
useTrackEvent()- Track custom eventsuseIdentify()- Identify usersuseReset()- Reset user identityuseTrackPageview()- Manual pageview trackinguseAutoTrackPageview()- Automatic pageview trackinguseAutocapture()- Control autocaptureuseJournium()- Access analytics instance
Server-Side Rendering (SSR)
The SDK is SSR-compatible and will not cause hydration issues:
// ✅ Safe in Next.js
export default function Layout({ children }: { children: React.ReactNode }) {
return (
<html>
<body>
<NextJourniumProvider config={{ publishableKey: 'pk_...' }}>
{children}
</NextJourniumProvider>
</body>
</html>
);
}The analytics instance is only initialized on the client side.
Environment Variables
App Router / Pages Router
NEXT_PUBLIC_JOURNIUM_PUBLISHABLE_KEY=pk_your_key_hereconst config = {
publishableKey: process.env.NEXT_PUBLIC_JOURNIUM_PUBLISHABLE_KEY!
};Different Keys per Environment
# Development
NEXT_PUBLIC_JOURNIUM_PUBLISHABLE_KEY=pk_test_...
# .env.production
NEXT_PUBLIC_JOURNIUM_PUBLISHABLE_KEY=pk_live_...TypeScript Support
The Next.js SDK is fully typed with the same types as the JavaScript SDK.
For complete type definitions, see the JavaScript SDK TypeScript Types documentation.
import {
type JourniumConfig,
type JourniumLocalOptions,
type AutocaptureOptions
} from '@journium/nextjs';API Reference
For detailed API method reference, see the JavaScript SDK API Methods.
The Next.js SDK provides the same methods through React hooks:
| Hook | Underlying Method | Documentation |
|---|---|---|
useTrackEvent() | analytics.track() | API Ref |
useIdentify() | analytics.identify() | API Ref |
useReset() | analytics.reset() | API Ref |
Deployment Considerations
Vercel
Works out of the box. Environment variables are automatically available with NEXT_PUBLIC_ prefix.
Self-Hosted
Ensure environment variables are configured:
NEXT_PUBLIC_JOURNIUM_PUBLISHABLE_KEY=pk_live_...Then build normally:
npm run buildNext Steps
Last updated on