Next.js Installation
Installing TrackFox on your Next.js website is straightforward and takes just a few minutes. The tracking script should be placed in your root layout to ensure it loads on every page.
Quick Install with CLI
The fastest way to get started is using the TrackFox CLI:
npx trackfox add
The CLI will automatically detect your Next.js setup (App Router or Pages Router) and install the tracking script in the right place. Learn more →
Manual Installation
Step 1: Get Your Tracking Script
- Log in to your TrackFox dashboard
- Click on the website dropdown in the top navigation
- Select "Site Settings" from the dropdown menu
- In the General tab, copy your unique tracking script
Your tracking script will look like this:
<script
defer
src="https://trackfox.app/script.js"
data-website-id="your-website-id-here"
data-domain="yourdomain.com"
></script>
Step 2: Add Script to Root Layout
Navigate to your root layout file, typically located at app/layout.tsx (App Router) or pages/_app.tsx (Pages Router).
For App Router (app/layout.tsx):
import type { Metadata } from 'next'
import { Inter } from 'next/font/google'
import './globals.css'
const inter = Inter({ subsets: ['latin'] })
export const metadata: Metadata = {
title: 'Your App',
description: 'Your app description',
}
export default function RootLayout({
children,
}: {
children: React.ReactNode
}) {
return (
<html lang="en">
<head>
<script
defer
src="https://trackfox.app/script.js"
data-website-id="your-website-id-here"
data-domain="yourdomain.com"
/>
</head>
<body className={inter.className}>
{children}
</body>
</html>
)
}
For Pages Router (pages/_app.tsx):
import type { AppProps } from 'next/app'
import Head from 'next/head'
import './globals.css'
export default function App({ Component, pageProps }: AppProps) {
return (
<>
<Head>
<script
defer
src="https://trackfox.app/script.js"
data-website-id="your-website-id-here"
data-domain="yourdomain.com"
/>
</Head>
<Component {...pageProps} />
</>
)
}
Alternative: Using next/script
You can also use Next.js's next/script component for more control over script loading:
import Script from 'next/script'
export default function RootLayout({
children,
}: {
children: React.ReactNode
}) {
return (
<html lang="en">
<body>
<Script
src="https://trackfox.app/script.js"
data-website-id="your-website-id-here"
data-domain="yourdomain.com"
strategy="afterInteractive"
/>
{children}
</body>
</html>
)
}
Step 3: Verify Installation
- Save your changes and restart your Next.js development server
- Visit your website in a browser
- Return to your TrackFox dashboard
- Use the "Verify Installation" button in Site Settings
- Confirm that page views are being tracked in your dashboard
Custom Event Tracking
Once the base script is installed, you can track custom events like purchases or conversions:
// Track a payment event
useEffect(() => {
// Example: Track when a purchase is completed
const handlePurchase = () => {
window?.trackfox("payment", {
email: "customer@example.com",
amnt: 2999, // $29.99 in cents
});
};
// Call handlePurchase when purchase is completed
}, []);
// Track custom events
const trackCustomEvent = () => {
window?.trackfox("signup", {
email: "user@example.com",
});
};
Environment Variables
Create a .env.local file with your TrackFox configuration:
NEXT_PUBLIC_TRACKFOX_WEBSITE_ID=your_website_id
NEXT_PUBLIC_TRACKFOX_DOMAIN=your_domain.com
TypeScript Support
Add type definitions for better TypeScript support:
// types/trackfox.d.ts
declare global {
interface Window {
trackfox: (action: string, ...args: any[]) => void;
}
}
export {};
Testing
Test your installation by:
- Opening your website in a browser
- Checking the Network tab for requests to
trackfox.app - Verifying data appears in your TrackFox dashboard
Troubleshooting
Common issues and solutions:
- Script not loading: Check your website ID and domain configuration
- No data appearing: Ensure the script is loaded before page navigation
- TypeScript errors: Add the type definitions above
Important Notes
Important: Make sure to replace
your-website-id-hereandyourdomain.comwith your actual website ID and domain from the TrackFox dashboard.
Next Steps
Now that you have TrackFox installed on your Next.js website:
Need help? Contact us for assistance.
Suggest features? We'd love your feedback