Home > Web Front-end > JS Tutorial > Setup Mixpanel Analytics in a NextJS Application

Setup Mixpanel Analytics in a NextJS Application

Barbara Streisand
Release: 2024-12-05 08:07:12
Original
957 people have browsed it

Analytics is crucial for any profitable application, whether it’s a small application with 100 users or a large application with 10,000 users.

Understanding your users is one of the most critical things. And mixpanel is one of the best tools to do that.

Today, we will learn how to integrate and start mixpanel tracking.

Setup the project

I assume you already have a NextJS project setup. Also, create a new Mixpanel account from here (If you haven’t already).

Keep in mind that I am showing for NextJS but it’s applicable for any ReactJS app too.

Then, install the dependency

npm install mixpanel-browser
Copy after login
Copy after login

Get a token

First, add the following environment variable

NEXT_PUBLIC_MIXPANEL_TOKEN="YOUR_TOKEN_HERE"
Copy after login
Copy after login

Now, you can get the mixpanel token from your project’s dashboard.

Then go to Settings -> Project Settings

Setup Mixpanel Analytics in a NextJS Application

Then grab the Project Token and add it in the environment file.

Create Configuration File

Create a file named mixpanel.ts and add the following code

import mixpanel from 'mixpanel-browser';

// Define event names as constants to prevent typos
export const ANALYTICS_EVENTS = {
  PAGE_VIEW: 'Page View',
  BUTTON_CLICK: 'Button Click',
  FORM_SUBMIT: 'Form Submit',
  USER_SIGNED_IN: 'User Signed In',
  USER_SIGNED_UP: 'User Signed Up',
  USER_SIGNED_OUT: 'User Signed Out',
  // ... other events
};

// Initialize mixpanel
const MIXPANEL_TOKEN = process.env.NEXT_PUBLIC_MIXPANEL_TOKEN;

if (MIXPANEL_TOKEN) {
  mixpanel.init(MIXPANEL_TOKEN, {
    debug: process.env.NODE_ENV === 'development',
    track_pageview: true,
    persistence: 'localStorage',
    ignore_dnt: true,
  });
}
Copy after login
Copy after login

So, initialize the mixpanel as high as possible in your component tree.

Add Analytics Functions

Now, after you add the configuration, it’s time to add some reusable functions to track mixpanel events.

So add the following code in the same file:

export const MixpanelAnalytics = {
  track: <T extends Record<string, any>>(
    eventName: string,
    properties?: T & CommonEventProperties
  ) => {
    try {
      if (MIXPANEL_TOKEN) {
        mixpanel.track(eventName, {
          ...properties,
          timestamp: Date.now(),
          path: typeof window !== 'undefined' ? window.location.pathname : undefined,
        });
      }
    } catch (error) {
      console.error('Error tracking event:', error);
    }
  },

  pageView: (pageName: string, properties?: Record<string, any>) => {
    try {
      if (MIXPANEL_TOKEN) {
        MixpanelAnalytics.track(ANALYTICS_EVENTS.PAGE_VIEW, {
          page: pageName,
          ...properties,
        });
      }
    } catch (error) {
      console.error('Error tracking page view:', error);
    }
  }
};
Copy after login
Copy after login

If you analyze these 2 functions above

track

This function is used to track any kind of event.

For example, if you want to track a user, click a button to visit an external website. Maybe for affiliate calculation

You can do the following:

MixpanelAnalytics.track("VISIT_WEBSITE", {
  website_name: name,
  website_url: website,
  visit_count: (mixpanel.get_property('total_website_visits') ?? 0) + 1,
});
Copy after login
Copy after login

pageView

This is a pretty straightforward method to track every page view inside your application.

Now remember — when we initialized mixpanel, we already told it to track page views:

mixpanel.init(MIXPANEL_TOKEN, {
  track_pageview: true,  << HERE
  ...others
});
Copy after login
Copy after login

So this custom tracking is only for more detailed analysis.

Know your users

Now, tracking clicks is cool and all, but many times, it’s not enough.

Maybe you want to track specific users. Maybe you want to know who is doing what. Maybe you are creating a funnel to analyze user behavior.

For these scenarios,mixpanel provides 2 functions.

  1. identify

  2. reset

So, on a high level, after a user logs in, you can call

mixpanel.identify("whatever identified you want (usually email or userid)")
Copy after login
Copy after login

And on logout, you can reset it

mixpanel.reset()
Copy after login

Now you can also add additional context or details about your users using the people.set() method

For example,

npm install mixpanel-browser
Copy after login
Copy after login

There are some additional methods like append, union, increment etc., to handle more scenarios, but skip them as they are not the focus of this article. You can read more here

But what about anonymous users?

Now, in many applications (especially public sites) — it’s not mandatory to log in to see the contents.

But how do we track those people if they don’t log in?

To handle all these scenarios, let’s create two more utility functions.

NEXT_PUBLIC_MIXPANEL_TOKEN="YOUR_TOKEN_HERE"
Copy after login
Copy after login

So you can track your known and unknown users with this.

An example usage can look like the following: In one of the root files — (layout.tsx file in app router, _app.tsx in pages router)

Add the following

import mixpanel from 'mixpanel-browser';

// Define event names as constants to prevent typos
export const ANALYTICS_EVENTS = {
  PAGE_VIEW: 'Page View',
  BUTTON_CLICK: 'Button Click',
  FORM_SUBMIT: 'Form Submit',
  USER_SIGNED_IN: 'User Signed In',
  USER_SIGNED_UP: 'User Signed Up',
  USER_SIGNED_OUT: 'User Signed Out',
  // ... other events
};

// Initialize mixpanel
const MIXPANEL_TOKEN = process.env.NEXT_PUBLIC_MIXPANEL_TOKEN;

if (MIXPANEL_TOKEN) {
  mixpanel.init(MIXPANEL_TOKEN, {
    debug: process.env.NODE_ENV === 'development',
    track_pageview: true,
    persistence: 'localStorage',
    ignore_dnt: true,
  });
}
Copy after login
Copy after login

So this will initialize the user appropriately when they visit the site.

You can gather data and assign it to this particular user going forward.

Example usage

Now comes the fun part. Notice the following code and update it according to your needs.

export const MixpanelAnalytics = {
  track: <T extends Record<string, any>>(
    eventName: string,
    properties?: T & CommonEventProperties
  ) => {
    try {
      if (MIXPANEL_TOKEN) {
        mixpanel.track(eventName, {
          ...properties,
          timestamp: Date.now(),
          path: typeof window !== 'undefined' ? window.location.pathname : undefined,
        });
      }
    } catch (error) {
      console.error('Error tracking event:', error);
    }
  },

  pageView: (pageName: string, properties?: Record<string, any>) => {
    try {
      if (MIXPANEL_TOKEN) {
        MixpanelAnalytics.track(ANALYTICS_EVENTS.PAGE_VIEW, {
          page: pageName,
          ...properties,
        });
      }
    } catch (error) {
      console.error('Error tracking page view:', error);
    }
  }
};
Copy after login
Copy after login

In the above function we are tracking the particular user’s profile with the tracking data and also making sure that we are counting their visits to the particular website as well.

Cool right?

Best practices

When working with analytics — it’s very important to keep the data consistent.

So, make sure to add proper types for analytics events.

For example

Define constants for the events.

Never use plain strings for event names.

MixpanelAnalytics.track("VISIT_WEBSITE", {
  website_name: name,
  website_url: website,
  visit_count: (mixpanel.get_property('total_website_visits') ?? 0) + 1,
});
Copy after login
Copy after login

Type safety

For events payload, make sure to use consistent structure by using types

mixpanel.init(MIXPANEL_TOKEN, {
  track_pageview: true,  << HERE
  ...others
});
Copy after login
Copy after login

User Properties

Always maintain consistent user properties across sessions.

mixpanel.identify("whatever identified you want (usually email or userid)")
Copy after login
Copy after login

Otherwise, down the road, the data will be useless.

Conclusion

Remember to handle analytics initialization properly in your client-side components.

Also, ensure that sensitive user data is handled appropriately according to your privacy policy and data protection regulations.

Hope you learned something new today.

Have a great day!

The above is the detailed content of Setup Mixpanel Analytics in a NextJS Application. For more information, please follow other related articles on the PHP Chinese website!

source:dev.to
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template