Make a views counter with google analytics
Tue, Jan 10, 2023
Is not necessary make a complex system to count the views of your blog. You can use google analytics to do it, you dont need persiste the data in a database.
Here we dont explain how to create a Google analytics account, you can follow this tutorial to do it.
1. Create a endpoint to get the data using the class BetaAnalyticsDataClient
export default async function handler(req: NextApiRequest, res: NextApiResponse<Data>) {
const { slug = '/' } = query;
const analyticsDataClient = new BetaAnalyticsDataClient({
client_email: process.env.ANALYTICS_CLIENT_EMAIL,
private_key: process.env.ANALYTICS_PRIVATE_KEY?.replace(/\\n/g, '\n'),
projectId: process.env.ANALYTICS_PROJECT_ID,
const [response] = await analyticsDataClient.runReport({
property: `properties/348472560`,
if (!response || !response.rows) {
res.status(500).json({ error: 'Error while parsing analytics data' });
let total = response.rows.reduce((prev: Object, curr: Object) => {
return prev + parseInt(curr.metricValues[0].value, 0);
res.status(200).json({ total });
res.status(500).json({ error: err });
2. Create a component to show the data
You can create a component to consume the api from the client using useEffect or from a page retrieving the data from the server. It is your choice
In my case I am going to make 3 different renderings, one for when it is loading, one for the error and the last one for the information.
children?: React.ReactNode;
const Container = ({ children }: Props) => {
<div className={styles.views} title="Number of views in this post from Google analytics">
const StarCounter = () => {
const [views, setViews] = React.useState(-1);
const { asPath } = useRouter();
const { total } = await fetch(`/api/analytics?slug=${asPath}`).then((res) => res.json());
<RxCross2 className={styles.error} title="Error on endpoint" />
<FaSpinner className={styles.spinner} title="Loading views" />
export default StarCounter;
3. Include your component wherever you want to display it
To see the result of the above code just look at the top of the page and you will see a book icon with a counter on the right hand side.
You can see the complete code in my repository if you need it, and if you find it useful or like it you can give me a star