Next.js Integration

Connect Next.js and send data from Next.js with Rudderstack.
SourceEvent Stream
Frequently Asked Questions

The framework uses a combination of Node.js and React to create a seamless development experience, allowing developers to write code for both the server and the client in a single codebase. This approach improves performance and SEO while also making it easier to build complex web applications.

Next.js offers several features that make it a powerful web development framework, such as server-side rendering, automatic code splitting, and static site generation. The framework also includes built-in support for TypeScript, CSS modules, and API routes, making it easier to build modern web applications. Next.js also includes a data fetching library that enables businesses to load data on the server, reducing load times and improving SEO.

To get started with Next.js, businesses need to install Node.js and create a new Next.js project using the command-line interface. Next.js comes with extensive documentation and tutorials to help businesses learn the framework quickly. Additionally, the Next.js GitHub repository is a great resource for developers looking to dive deeper into the framework.

For the complete tutorial, make sure to follow this document

Step 1: Add rudderInitialize.js file in the Next.js project with following code snippet

export async function rudderInitialize() {

(function() {

var e = (window.rudderanalytics = window.rudderanalytics || []);

(e.methods = [

'load',

'page',

'track',

'identify',

'alias',

'group',

'ready',

'reset',

'getAnonymousId',

'setAnonymousId',

'getUserId',

'getUserTraits',

'getGroupId',

'getGroupTraits',

'startSession',

'endSession',

]),

(e.factory = function(t) {

return function() {

e.push([t].concat(Array.prototype.slice.call(arguments)));

};

});

for (var t = 0; t < e.methods.length; t++) {

var r = e.methods[t];

e[r] = e.factory(r);

}

(e.loadJS = function(e, t) {

var r = document.createElement('script');

(r.type = 'text/javascript'),

(r.async = !0),

(r.src = 'https://cdn.rudderlabs.com/v1.1/rudder-analytics.min.js');

var a = document.getElementsByTagName('script')[0];

a.parentNode.insertBefore(r, a);

}),

e.loadJS(),

e.load('WRITE-KEY', 'DATAPLANE-URL'), // Replace 'WRITE-KEY' and 'DATAPLANE-URL'

e.page();

})();

}

Step 2: Add following code on index.js inside pages folder

import React from 'react';

import { rudderInitialize } from '../rudderInitialize';

export default function Home() {

React.useEffect(() => {

rudderInitialize();

}, []);

const search = () => {

window.rudderanalytics.page();

window.rudderanalytics.track("Track Event.");

};

Use the Next.js integration with popular destinations
227 Integrations
About Next.js

Next.js is a JavaScript-based web development framework that enables businesses to build server-side rendered React applications with ease.