Next.js integration

Once you have added your website to Panelbear, all that is left to do is integrate our analytics snippet.

It's a small piece of code that runs on your website, and it collect the metrics necessary to show you the insights in the dashboard. For more information on what it collects check out What do we collect?.

Integrating Panelbear with a Next.js website

Installing the analytics script on your Next.js based website is super simple.

We're going to be using the Panelbear client library to build a simple React hook that will track navigation changes for a NextJS website.

Simply install the library in your project:

npm install @panelbear/panelbear-js

# Or with yarn:
yarn add @panelbear/panelbear-js

Now add the following React hook to your project:

// ./hooks/panelbear.js

import * as Panelbear from "@panelbear/panelbear-js";
import { useRouter } from "next/router";
import { useEffect } from "react";

export const usePanelbear = (site, config = {}) => {
  const router = useRouter();

  useEffect(() => {
    Panelbear.load(site, config);
    
    // Trigger initial page view
    Panelbear.trackPageview();

    // Add on route change handler for client-side navigation
    const handleRouteChange = () => Panelbear.trackPageview();
    router.events.on("routeChangeComplete", handleRouteChange);

    return () => {
      router.events.off("routeChangeComplete", handleRouteChange);
    };
  }, []);
};

And now load the hook on your main App:

Don't forget to replace YOUR_SITE_ID with the Site ID you got from Panelbear.

// ./pages/_app.js

import { usePanelbear } from './../hooks/panelbear';

function CustomApp({ Component, pageProps }) {
    // Load Panelbear only once during the app lifecycle
    usePanelbear("YOUR_SITE_ID", {
      // Uncomment to allow sending events on localhost, and log to console too.
      // debug: true
    });

    return <Component {...pageProps} />
}

export default CustomApp;

That's all you need to add analytics to your NextJS project.

Where do I find the Site ID?

  1. Go to the settings page for the website on Panelbear.
  2. You will see the Site ID listed under your site name.

You should see something like this:

Site ID

Check your installation

After you have added our analytics script to the HTML of your website, you should be able to see pageviews show up in the dashboard within a minute of the visitors coming in to your website.

If you need help, you can reach out to us at [email protected].

Tip: Check out our local development guide for some tips and tricks when testing Panelbear on your machine.

Mission complete!

Congratulations on completing the integration with Panelbear. That was easy, wasn't it? You now benefit of blazingly fast analytics, without compromising the privacy of your visitors.

Now all that's left to do is to let Panelbear do it's magic over time and you can view the insights in the dashboard.