How To Event Stream From Your Next.Js App Using Open Source RudderStack
Tags: nextjs, datawarehouse, dataanlytics, rudderstack
Published: 2021-12-15 14:50:30

[RudderStack](https://rudderstack.com/) is an open-source smart customer data pipeline tool that lets you track, collect, and send events from your web, mobile, and server-side sources in real-time. You can check out our open-sourced primary repository - [rudder-server](https://github.com/rudderlabs/rudder-server) - on GitHub.

This blog will show how you can easily integrate your [Next.js](https://nextjs.org/) app with RudderStack using our [JavaScript SDK](https://github.com/rudderlabs/rudder-sdk-js). With this integration, you can track real-time user events and send them to your preferred destinations.

You can instrument event streams on your Next.js app using RudderStack by following these three steps:

1.  Integrate your Next.js app with RudderStack's JavaScript SDK and set up the tracking code
2.  Set up a tool or warehouse destination in RudderStack to route all the event data in real-time
3.  Deploy your app and verify the event stream

Pre-Requisites
--------------

This post assumes you have already installed and set up your Next.js app. If you haven't done so, we recommend following the [official Next.js documentation](https://nextjs.org/docs) to get started.

Step 1: Integrate Your Next.Js App With RudderStack's JavaScript SDK
--------------------------------------------------------------------

### Creating A Source In RudderStack

Before you get started, you will need to set up a JavaScript source in your RudderStack dashboard, which you can then use to track events from your Next.js app. Follow these steps to set up the source:

1\. Log into your [RudderStack dashboard](https://app.rudderstack.com/). If you're new to RudderStack, [sign up here](https://app.rudderlabs.com/signup?type=freetrial).

2\. Upon login, you should see the following dashboard:

![](https://cdn.sanity.io/images/mwovquhr/production/d103d2c6452dcee6f44c27ee8087e864cdbb84a2-630x319.png)

3\. Note the Data Plane URL. You will need this URL to integrate your Next.js app with RudderStack.

![](https://cdn.sanity.io/images/mwovquhr/production/654cffa6fa2c334832f4281927f69d3d6ce49fd7-518x194.png)

4\. Next, create a Source. To do so, simply click on the Add Source button. You can also click on the Directory option in the left nav bar and select Event Streams under Sources, as shown. Then, select JavaScript.

![](https://cdn.sanity.io/images/mwovquhr/production/e906f26d6b0132d870057e3700cff46b4f9a107c-630x327.png)

5\. After assigning a name to your source, click on Next.

![](https://cdn.sanity.io/images/mwovquhr/production/d0e2ef39955da5294c0984f3bba7f429c52dd18c-630x195.png)

6\. Your JavaScript source is now configured and ready to track events. Note the Write Key associated with this source. This is also required to set up the RudderStack integration with your Next.js app

![](https://cdn.sanity.io/images/mwovquhr/production/00105d7adc808d9f3f0323050edbcb516d7703ae-630x149.png)

### Integrate Your Next.Js App With RudderStack

To integrate RudderStack with your Next.js app and set up the tracking code, follow these steps:

1\. Create a new `_document.js` file in your app's pages folder, as shown:

![](https://cdn.sanity.io/images/mwovquhr/production/60dbb40166aa471d50c432b42f90da243489da5e-1852x726.png)

2\. Add the following lines in your index.js file (present in your app's pages folder), as shown:

![](https://cdn.sanity.io/images/mwovquhr/production/85d98b905abf53ab2d1960868c6d7b09c78b72c1-507x289.png)

Important: You can refer to our sample Next.js app present in our [RudderStack Next.js repository](https://github.com/rudderlabs/rudder-analytics-next) for more information on modifying these files.

Step 2: Create A Tool Destination In RudderStack For Routing Your Next.Js App Events
------------------------------------------------------------------------------------

RudderStack currently supports more than [80 third-party tools](https://rudderstack.com/integration/) and platforms to which you can reliably send your tracked events. For this tutorial, we will route the Next.js app events to [Google Analytics](https://docs.rudderstack.com/destinations/google-analytics-ga). To configure Google Analytics as a destination in RudderStack, follow these steps:

1\. In the left navigation bar of your dashboard, click on Destinations, followed by Add Destination. Since we have already configured a source, you can simply click on the source and choose the Add Destination option, as shown:

Note: If you have already configured a destination in RudderStackand want to send your event data to that platform, use the Connect Destinations option.

![](https://cdn.sanity.io/images/mwovquhr/production/bb027d1e3ad7665d7f7bdf68bd3df7da833daae8-630x316.png)

2\. From the list of destinations, select Google Analytics.

![](https://cdn.sanity.io/images/mwovquhr/production/dee0813b1679e76a5fb623df69bfd21dafd7e804-630x316.png)

3\. Give a name to your destination and click on Next, as shown:

![](https://cdn.sanity.io/images/mwovquhr/production/638e3e2ef556357ec424b5ec30d3943504fb9827-630x317.png)

4\. Then, select the JavaScript source that we have already configured for this tutorial.

![](https://cdn.sanity.io/images/mwovquhr/production/22da8027cb80f55cc4c81cc56e8e49aa30996b89-630x317.png)

5\. In the Connection Settings, configure your Google Analytics destination with your Google Analytics Tracking ID and other optional settings, as shown below. Then, click on Next.

![](https://cdn.sanity.io/images/mwovquhr/production/53654b0aee376a392973f2b0f7c55b66a2749272-630x334.png)

6\. You can also transform your events before sending them to Google Analytics. For more information on this feature, check out our [docs](https://docs.rudderstack.com/adding-a-new-user-transformation-in-rudderstack).

7\. That's it! Google Analytics is now configured as a destination. You should now see the following source-destination connection in your dashboard:

![](https://cdn.sanity.io/images/mwovquhr/production/6cd115d36f1604e2a795e207d55fd4c9f2f96f3b-630x244.png)

(Alternate) Step 2: Create A Warehouse Destination In RudderStack For Your Next.Js App Events
---------------------------------------------------------------------------------------------

Important: Before you configure a data warehouse as a destination in RudderStack, you will need to set up a new project in your data warehouse. Also, you need to create a new RudderStack user role with the relevant permissions.

Follow our [docs](https://docs.rudderstack.com/data-warehouse-integrations) to get detailed, step-by-step instructions on how to do so for the data warehouse of your choice.

For this tutorial, we will set up a Google BigQuery warehouse destination to route all the events from our Next.js app. As mentioned above, you can set up a BigQuery project with the required permissions for the service account by following [our doc](https://docs.rudderstack.com/data-warehouse-integrations/google-bigquery).

Once you have set up the project and assigned the required user permissions, follow these steps:

1\. From the list of destinations, select Google BigQuery:

![](https://cdn.sanity.io/images/mwovquhr/production/d901af95b4b3a046ea62b4e7354473f7a4932deb-630x328.png)

2\. Give a name to this destination. Then, click on Next.

![](https://cdn.sanity.io/images/mwovquhr/production/fdf892d0f37e4c6c9ec4c31bd48253b26a6da774-630x290.png)

3\. Choose the JavaScript source from which we will track our Next.js app events. Then, click on Next.

![](https://cdn.sanity.io/images/mwovquhr/production/ae4a58bd7965bf9088d15e2b85233639fd7fbc67-630x290.png)

4\. Specify the Connection Credentials. Enter the BigQuery Project ID and the Staging Bucket Name. Follow these [instructions](https://docs.rudderstack.com/data-warehouse-integrations/google-bigquery#setting-up-google-bigquery) to get this information.

![](https://cdn.sanity.io/images/mwovquhr/production/e92473af23c60ee47bdaef090e8131796594864c-357x834.png)

5\. Finally, copy the contents of the private JSON file. Find more information on how to do this in our [doc](https://docs.rudderstack.com/data-warehouse-integrations/google-bigquery#setting-up-the-service-account-for-rudderstack).

That's it! You have successfully set up BigQuery as a warehouse destination in RudderStack.

Step 3: Deploy Your Next.Js App And Verify The Event Stream
-----------------------------------------------------------

Now that we've successfully configured our event source and destination, it's time to verify if our event stream works correctly. To do so, let's deploy our Next.js application and test if the events are tracked by the JavaScript source and delivered to Google Analytics - our destination.

To do this, follow these steps:

1\. Navigate to the folder containing your Next.js app.

2\. To deploy the app, run the command `npm run dev`, as shown:

![](https://cdn.sanity.io/images/mwovquhr/production/f0d4c82df32677cd551b34597a88a4b40e12f0b1-1116x701.png)

3\. Open your app by going to the URL [http://localhost:3000](http://localhost:3000/) on your browser. Refresh the page and click on various links to track different events:

![](https://cdn.sanity.io/images/mwovquhr/production/26631f8da19461b783b0f23cfc40899393f14ef2-1366x883.png)

4\. To verify if `rudder-analytics.js` (RudderStack's JavaScript SDK) has loaded correctly, you can go to your browser's developer tools and navigate to the Network tab. The following screenshot highlights this option for Google Chrome:

![](https://cdn.sanity.io/images/mwovquhr/production/65e99dd121fbcc6c2c85bf1db27effe7f017c22d-557x754.png)

5\. See if RudderStack can track the different pageviews and clicks by going to the Live Events tab of your JavaScript source on the RudderStack dashboard page:

Note: After deploying your app, there can sometimes be a lag before events start sending and are visible in your dashboard and destination. Don't worry. All events are captured and sent; just be aware that they can take a few minutes to show up.

![](https://cdn.sanity.io/images/mwovquhr/production/1859e5a94847c323bd450e19b15fcb46b8973bee-630x272.jpg)

6\. RudderStack has successfully tracked and captured the events, as seen below:

![](https://cdn.sanity.io/images/mwovquhr/production/92b6ce9a8ea8cd8e47c1b32a1fd8631cb213749c-1600x958.png)

7\. Now, let's check if the events are sent to our Google Analytics destination. Go to your Google Analytics dashboard and navigate to the Realtime - Events option.

![](https://cdn.sanity.io/images/mwovquhr/production/7d9a742d6ab60b82f35732cc0b2bc846335a0790-1786x576.png)

We see one active user on our Next.js app, and the track event is visible too. This means the event was tracked and delivered successfully. Similarly, you should also receive the event in your Google BigQuery warehouse.

### Sign Up For Free And Start Sending Data

Test out our event stream, ELT, and reverse-ETL pipelines. Use our HTTP source to send data in less than 5 minutes, or install one of our 12 SDKs in your website or app.[Get Started](https://app.rudderlabs.com/signup?type=freetrial)