How to add privacy-friendly analytics to NextTS

Search for a command to run...

No comments yet. Be the first to comment.
Code is often read than written, making code readability a crucial aspect of software development. Readable code is easy to understand, allowing developers to follow the logic effortlessly through the lines. In this article, drawing from my experienc...

This article is a supplement to the recent project I built Colordash (formerly Guess the color game) , you can play the game here and read about its making here. edit: change game name to Color Dash RGB color space is a mathematical model used to rep...

A nextJS game with its objective of identifying as many colors as you can in a given amount of time using RGB color codes. In this article, I briefly describe how I built this game. Source code available on github. UI Design When I started working on...

If you have known beforehand that you want your react-app to be a PWA(Progressive Web App) you could have used the create-react template pwa like so : npx create-react-app my-app --template pwa But you didn't😁So here we are. In this article, I will...

Privacy-friendly web analytics track a website's usage, gathering insightful data from visitors without collecting personal information. Goat Counter is my favorite tool that gets the job done. In this article, we will learn how to set it up and use it on a website that uses NextTS.
GoatCounter Dashboard
✅ Lightweight
✅ easy to use
✅ GDPR compliant
✅ can self-host
✅ hosted free for non-commercial websites
What we are going to do
by heading over to goatcounter.com/signup and fill-up the form to create your account

and once you have your code save it as an environment variable.
// /.env.local
NEXT_PUBLIC_GOAT_COUNTER_CODE = yoursupersecretcode
using the nextJS script component. You can place it anywhere you want. I prefer to place it on the app component.
// pages/_app.tsx
import Script from 'next/script'
...
<Script
data-goatcounter={`https://${process.env.NEXT_PUBLIC_GOAT_COUNTER_CODE}.goatcounter.com/count`}
data-goatcounter-settings='{"allow_local": true}'
src="//gc.zgo.at/count.js" />
...
💡 the attribute allow-local allows requests from local addresses (localhost, 192.168.0.0, etc.) for testing the integration locally. You will have to remove it when done testing.
to inform GoatCounter whenever a user navigates to a different page. next's router events to the rescue.
// src/hooks/useAnalytics.ts
import { useRouter } from 'next/router'
import { useEffect } from 'react'
// make typescript happy
declare global {
interface Window {
goatcounter: any
}
}
export function useAnalyticsInstance() {
const router = useRouter()
useEffect(() => {
const onRouteChangeComplete = () => {
if (window.goatcounter === undefined) return
window.goatcounter.count({
path: location.pathname + location.search + location.hash,
})
}
router.events.on('routeChangeComplete', onRouteChangeComplete)
return () => {
router.events.off('routeChangeComplete', onRouteChangeComplete)
}
}, [router.events])
}
We then import this hook into our app component.
// src/pages/_app.tsx
...
import { useAnalyticsInstance } from "../hooks/useAnalytics";
function MyApp({ Component, pageProps }: AppProps) {
useAnalyticsInstance();
return (
...
);
}
export default MyApp;
to track any action your user takes. Take a look at the following example code that shows the useAnalyticsEvent hook in action, tracking the number of times someone clicked the 'count' button.
// src/components/Example.tsx
import React, { useState } from 'react';
import { useAnalyticsEvent } from "../hooks/useAnalytics";
function Example() {
const [count, setCount] = useState(0);
const { trackCustomEvent } = useAnalyticsEvent();
const increment = () => {
setCount(count + 1 );
trackCustomEvent({eventName:'clicked_counter_incrementer',
eventTitle:'increment_counter'
});
}
return (
<div>
<p>You clicked {count} times</p>
<button onClick={() => increment()}>
Click me
</button>
</div>
);
}
Here is the implementation!
// src/hooks/useAnalytics.ts
export function useAnalyticsEvent() {
function trackCustomEvent({
eventName,
eventTitle,
}: {
eventName: string
eventTitle?: string
}) {
if (window.goatcounter === undefined) return
// still counting just like we do for route changes
window.goatcounter.count({
path: eventName,
title: eventTitle || eventName,
// only this time the event property is set to true
event: true,
})
}
return { trackCustomEvent }
}
fire up your local server,
go to yoursuperscretcode.goatcounter.com
and look for any hits!
The screenshot shows the number of times the count button from the Example code has been clicked

💡 Now that you have tested that everything is working correctly, on the settings page (GoatCounter dashboard ), under the track section, you can ignore your IP from being tracked for dev purposes.
TheItalianDev - How to add google analytics to nextjs
We learned how to set up Goat counter to track the number of page visits, views, and custom events without collecting any personal data from our visitors. Awesome.
Questions? suggestions? let me know in the comments!