Skip to main content

Next.js Integration

MentiQ Analytics provides seamless integration with Next.js applications, supporting both the App Router (app/) and Pages Router (pages/) patterns with automatic page tracking and server-side analytics.

App Router Integration

The App Router (Next.js 13+) uses React Server Components. Here’s how to integrate MentiQ Analytics:
1

Install MentiQ SDK

2

Create Analytics Provider

Wrap your application with the AnalyticsProvider in your root layout:
app/layout.tsx
3

Use Analytics in Client Components

Mark your component with 'use client' and use the analytics hooks:
app/components/TrackingButton.tsx

Alternative: Using withAnalytics Wrapper

For more control over page tracking, use the withAnalytics wrapper:
app/layout.tsx
The withAnalytics wrapper automatically tracks route changes by intercepting window.history.pushState and window.history.replaceState.

Pages Router Integration

For applications using the traditional Pages Router:
1

Wrap Application in _app.tsx

pages/_app.tsx
2

Track Events in Pages

pages/index.tsx

Alternative: Using trackPageView Helper

For simpler integration with Pages Router:
pages/_app.tsx

Server-Side Analytics

Track events from API routes and server components:
app/api/signup/route.ts

Server Event Context

The trackServerEvent function accepts a context object for server-side enrichment:
Server-side events are sent directly to the MentiQ API without queuing or batching. Make sure to handle errors appropriately.

API Route Analytics Helper

For consistent tracking across multiple API routes, use the createAnalyticsApiHandler:
lib/analytics.ts
Then use it in your API routes:
pages/api/purchase.ts

Middleware for Request Tracking

Track all API requests automatically with the analytics middleware:
This middleware tracks all API requests. Use carefully in production as it may generate high event volumes.
middleware.ts
The middleware automatically tracks:
  • api_request_start - When request begins
  • api_request_complete - When request completes (with duration and status code)

Environment Variables

Create a .env.local file in your project root:
.env.local
Use NEXT_PUBLIC_* prefixed variables for client-side code and unprefixed variables for server-side code to maintain security.

TypeScript Configuration

Ensure your tsconfig.json includes proper type resolution:
tsconfig.json

Best Practices

1

Separate Client and Server Keys

Use different API keys for client-side and server-side tracking to maintain security and enable proper rate limiting.
2

Handle Errors Gracefully

Always wrap analytics calls in try-catch blocks to prevent analytics failures from breaking your application:
3

Optimize Batch Settings

Configure appropriate batch sizes and flush intervals based on your traffic:
4

Use Debug Mode in Development

Enable debug logging during development:

Troubleshooting

Page Views Not Tracking

Make sure enableAutoPageTracking: true is set in your config, and that the AnalyticsProvider wraps your entire application in the root layout.
Ensure you’re tracking route changes with router.events.on("routeChangeComplete") or using the trackPageView helper.

Server-Side Events Failing

Verify your server-side API key is correct and has proper permissions
Ensure the endpoint URL is accessible from your server
Check that you’re using the private API key (not the public one)

TypeScript Errors

Next Steps

TypeScript Types

Learn about TypeScript interfaces and types

Event Batching

Understand batching and queuing system

Privacy Compliance

Implement privacy features and data masking