Skip to main content

Command Palette

Search for a command to run...

Add analytics to NextJS with Plausible.io

Updated
โ€ข2 min read
Add analytics to NextJS with Plausible.io
D

Over the past 2 years, I've worked with small companies, big ones and start-ups to successfully help them reach their full potential and attract new customers.

My focus is improving my clients businesses, through enjoyable and meaningful software. I specialize in responsive and automotive websites and applications.

๐—”๐—•๐—ข๐—จ๐—ง I am a driven front-end developer with get-it-done, on-time and high-quality product spirit!

๐—ง๐—˜๐—–๐—›๐—ก๐—ข๐—Ÿ๐—ข๐—š๐—œ๐—˜๐—ฆ React and Angular are the tools, I am most comfortable with. I also work with Next.js Vue.js, plain JavaScript, HTML, CSS and express.js. My plan for the future is to continue with the front end development and expand into mobile development with Flutter and React Native.

๐—˜๐—ซ๐—ฃ๐—˜๐—ฅ๐—œ๐—˜๐—ก๐—–๐—˜ I have worked and managed a lot of people with a diverse background. Thanks to that, my team working skills are in a good fit and I became a great problem solver.

My first code I wrote three years ago for my C# Unity game. I lunched it, made some other games, got a lot of downloads and learned enough to feel confident in front of the IDE. A year later I decited to step in the front end world and I am still developing till today.

๐—–๐—ข๐—ก๐—ง๐—”๐—–๐—ง Want to hear me? I'd love to chat!

What is Plausible ?

Plausible market itself as lightweight and open source web analytics. The authors can say it best

โ€œWe are dedicated to making web analytics more privacy-friendly. Our mission is to reduce corporate surveillance by providing an alternative web analytics tool which doesnโ€™t come from the AdTech world. The full-time team consists of Uku Taht and Marko Saric. We are completely independent, self-funded and bootstrapped.โ€œ

Plausible is an incredible Google Analytics alternative that doesnโ€™t collect personal information.

Installation with Plausible

The way to install Plausible to your Next.js is pretty straightforward and easy. The developers have made it simply perfect.

There are two options to get your Plausible Analytics running.

Next/Head of Document file.

Going on with the registration you will receive this script

<script async defer data-domain="<your-domain.com>" src="https://plausible.io/js/plausible.js"></script>

Custom Document file is needed to continue. The moment you have it running, add it to the Head

import Document, {Html, Head, Main, NextScript} from 'next/document'

class MyDocument extends Document {
    render() {
        return (
            <Html>
                <Head>
                    <base href="/"></base>
                    <script async defer data-domain="<your-          domain.com>" src="https://plausible.io/js/plausible.js"></script>

                </Head>
                <body>
                <Main/>
                <NextScript/>
                </body>
            </Html>
        )
    }
}

export default MyDocument

Thatโ€™s all you have to do to set it up.

NextJS library

4lejandrito has made a great tool to connect your Plausible analytics to NextJS.

yarn add next-plausible

npm install next-plausible

Important: If you're using a version of NextJS bellow 11.1.0 use

next-plausible@2

Custom App has to be created and set up to use the method for all your pages.

import PlausibleProvider from "next-plausible";

function MyApp({ Component, pageProps }) {
  return 
      <PlausibleProvider domain="<Your domain>">
          <Component {...pageProps} />
      </PlausibleProvider>
}

export default MyApp

Custom Events

Plausible allows us to track any kind of information. It could be likes of a post, counts, and any data you think itโ€™s important

We can use the hook, they provide like that

import {usePlausible} from 'next-plausible'

export default PlausibleButton() {
    const plausible = usePlausible()
    return (
        <button onClick={() => plausible('customEventName')}>
            Send
        </button>
    )
}

Troubleshooting

Keep in mind, that as every tracking script, if a user has a tracking blocking extension - you wonโ€™t get any results.

I am pointing it out, because I spend whole 10 minutes, before I figured out why on Chrome it wasnโ€™t working, but only in Firefox ( where, I didnโ€™t have any privacy extensions installed )

Additional information could be found in the official docs here

Links

https://github.com/plausible/analytics

More from this blog

Dimitar Stoev

10 posts