How-to Integrate Plausible Analytics With Next.js and Vercel?
Last Updated :
30 May, 2024
Adding basic analytics to your website or your application is a critical way to gauge the performance and even detect some usability issues through the lifespan of your app.
In this tutorial, we will learn how to create a basic Nextjs application integrated with Plausible and deploy it on Vercel. Here is a quick preview of what we will be building.
Prerequisites:
Why Plausible?
Plausible is an open-source, privacy-focused alternative to Google Analytics. If you want to use it for a business application where you need to keep track of what buttons users click and what they do, Plausible may not be for you.
But, if you just want a simple implementation where you want to know how many visits your blog is getting, plausible is the quickest and best solution by far. It is lightweight, open source, and fully compliant with GDPR, and CCPA. So it might just be the next powerful tool in your arsenal.
You can even check out the live demo on the Plausible website to see how it will look once you deploy it on your website. Checkout the demo: https://plausible.io/plausible.io
Approach to integrating Plausible with Next.js
- Creating a basic Nextjs application using Create Next App npm package by Vercel. Then we will be adding different pages to our application which we will be tracking using plausible.
- Deploying our application to Vercel by importing it from GitHub.
- Creating a free plausible account for learning purpose and configuring it to track our deployed app, using the production domain Vercel provides.
- Using a package to integrate plausible into our Nextjs application and redeploying it to Vercel.
Steps to Setup a NextJS App
Step 1: Create a new next app
$ npx create-next-app@latest plausible-integration
then agree to the default options
output of selected configurationsStep 2: Moving into the folder we created
$ cd plausible-integration
Step 3: Removing the boilerplate code and creating a basic application.
Project structure:
Folder structure of the 'scr' directory
Example: Below is an example of creating a simple NextJS App.
CSS
/* src/app/globals.css */
@tailwind base;
@tailwind components;
@tailwind utilities;
.padding-x {
 padding-left: max(calc(50vw - 30rem), 4vw);
 padding-right: max(calc(50vw - 30rem), 4vw);
}
JavaScript
// src/components/header.tsx
import Link from "next/link";
export default function Header() {
return (
<header className="w-full flex justify-between">
<Link href="/">
<h1 className="text-lg font-medium">LOGO</h1>
</Link>
<div className="flex gap-6">
<Link href="/products" className="text-slate-500 hover:underline">
Products
</Link>
<Link href="/pricing" className="text-slate-500 hover:underline">
Pricing
</Link>
<Link href="/about" className="text-slate-500 hover:underline">
About
</Link>
</div>
</header>
);
}
JavaScript
// src/app/layout.tsx
import type { Metadata } from "next";
import { Inter } from "next/font/google";
import Header from "@/components/header";
import "./globals.css";
const inter = Inter({ subsets: ["latin"] });
export const metadata: Metadata = {
title: "Plausible Integrations",
description: "Intergrating plausible with next app",
};
export default function RootLayout({
children,
}: Readonly<{
children: React.ReactNode;
}>) {
return (
<html lang="en">
<body
className={
inter.className +
" w-full h-full min-h-screen flex flex-col padding-x py-6"
}
>
<Header />
{children}
</body>
</html>
);
}
JavaScript
// src/app/page.tsx
export default function Home() {
return (
<main className="w-full h-full grow flex text-4xl
justify-center items-center">
Home Page
</main>
);
}
JavaScript
// src/app/about/page.tsx
export default function About() {
return (
<main className="w-full h-full grow flex text-4xl
justify-center items-center">
About Page
</main>
);
}
JavaScript
// src/app/products/page.tsx
export default function Products() {
return (
<main className="w-full h-full grow flex text-4xl
justify-center items-center">
Products Page
</main>
);
}
JavaScript
// src/app/pricing/page.tsx
export default function Pricing() {
return (
<main className="w-full h-full grow flex text-4xl
justify-center items-center">
Pricing Page
</main>
);
}
Start your application using the below command:
npm run dev
Output:
Website preview
Deploying to Vercel
Step 1: Login into your Vercel account
Vercel Login page
Vercel website loginStep 2: Add new project
Click on Add New and select project.
creating a new vercel projectStep 3: Import repository
If you have configure Vercel on GitHub than Import your git repository or else, import via third party repository.
importing repository via GitHub or third-partyStep 4: Deploy
Click Deploy and your good to go!! Wait for Vercel to deploy your app, the build process might take some time. But you have to only do this once. When you push new commits from git, Vercel will trigger a CI/CD pipeline and deploy your commits for you.
Save the production domain as we will need it for plausible later.
Copy the production domainSteps to Setup a Plausible
Register and Setup domain
Go to the plausible website and start your free trial. Once registered it will ask you for your domain. Input your Vercel production domain and continue.
configure plausibility for Nextjs appClick on Add snippet. They'll give you a JavaScript snippet to embed into your app, but since we're doing this through Nextjs we'll do it a little differently, so let's move on for now.
Integration with Nextjs
Plausible has a community created Nextjs integration. Using the next-plausible npm package.
GitHub repository of next plausible package: https://github.com/4lejandrito/next-plausible
If you looking into the packages README.md file it specifies two integration methods. One for the Pages router and one for the App router.
JavaScript
// App router
// app/layout.js
import PlausibleProvider from 'next-plausible'
export default function RootLayout({ children }) {
return (
<html>
<head>
<PlausibleProvider domain="example.com" />
</head>
<body>{children}</body>
</html>
)
}
JavaScript
// Pages router
// pages/_app.js
import PlausibleProvider from 'next-plausible'
export default function MyApp({ Component, pageProps }) {
return (
<PlausibleProvider domain="example.com">
<Component {...pageProps} />
</PlausibleProvider>
)
}
Since in this project we are using the App router we will proceed with the app router integration.
Step 1: Install plausible package
$ npm i next-plausible
Step 2: Update Layout
JavaScript
// src/app/layout.tsx
import type { Metadata } from "next";
import { Inter } from "next/font/google";
import Header from "@/components/header";
import PlausibleProvider from "next-plausible";
import "./globals.css";
const inter = Inter({ subsets: ["latin"] });
export const metadata: Metadata = {
title: "Plausible Integrations",
description: "Intergrating plausible with next app",
};
export default function RootLayout({
children,
}: Readonly<{
children: React.ReactNode;
}>) {
return (
<html lang="en">
<head>
<PlausibleProvider domain="plausible-integration.vercel.app" />
</head>
<body
className={
inter.className +
" w-full h-full min-h-screen flex flex-col padding-x py-6"
}
>
<Header />
{children}
</body>
</html>
);
}
Step 3: Commit and Deploy
Once you have updated the application commit your changes and push it to GitHub. Vercel will trigger an event and redeploy your application. Once done visit your website and check plausible dashboard.
Similar Reads
How to Add Analytics in Next.js with Plausible
In this article, we will learn how to add analytics to our Next.js app using Plausible. Adding analytics to any app is a crucial aspect of web development as it helps in understanding how the website is performing for users and how many visitors the website is getting. We will accomplish this by int
5 min read
How to Integrate Vite with Vue.js?
Vite provides the flexibility and speed needed for modern web development by offering a fast-build tool and development server. Integrating Vite with Vue.js allows developers to use Viteâs efficient hot module replacement and optimized build processes while working with Vue's reactive framework. Thi
3 min read
How to Change URL Without Page Refresh in Next.js?
In web development, the ability to change URLs dynamically without triggering a full page refresh is essential for creating smooth, seamless user experiences. Next.js, a popular React framework, offers built-in features to achieve this efficiently through its client-side routing capabilities. In thi
3 min read
AI-Powered Chatbot Platform with Node and Express.js
An AI Powered Chatbot using NodeJS and ExpressJS can be created using the free OpenAI's API Key that is provided for every user login. This article covers a basic syntax of how we can use ES6 (EcmaScript Version 6) to implement the functionalities of Node.js and Express.js including the use of REST
4 min read
How to add Video Player in Next.js ?
Adding a video player in a Next.js application enhances the user experience by enabling video playback. The react-player library simplifies integrating various video sources such as YouTube, Vimeo, and local files. In this article, we are going to learn how we can add Video Player in NextJS. Approa
2 min read
How to Handle a Post Request in Next.js?
NextJS is a React framework that is used to build full-stack web applications. It is used both for front-end as well as back-end. It comes with a powerful set of features to simplify the development of React applications. In this article, we will learn about How to handle a post request in NextJS. A
2 min read
Create a Quiz App with Next JS and Tailwind
Weâll explore the process of building a Quiz App utilizing NextJS and Tailwind. The quiz app provides users with a series of multiple-choice questions with options. Users can select the option and move to the next question. At the end, the user will be able to see the analysis of the quiz. Prerequis
6 min read
How to Fix CORS Errors in Next.js and Vercel?
Cross-Origin Resource Sharing (CORS) is a security feature that restricts web apps from making requests to a different domain than the one that served the web page to it. This is needed for security, although sometimes it can block legitimate requests, especially when working with APIs. In this arti
4 min read
How to Type a Page Component With Props in Next.js?
Next.js is a popular React framework used for building server-side rendered and static web applications. In many situations, we likely import the components wherever necessary in the project and pass the properties or props accordingly to the component. So, in this article, we will create a componen
3 min read
Create a To-Do List App with Next JS and Tailwind
To be productive and to remember all the tasks for the day, the To-Do app is very important. In this article, you will see the whole process of creating a to-do app from scratch. This to-do list app includes functionalities like adding new task, editing the task and deleting the task by clicking on
8 min read