Lazy Panda



How to find total page views and most visited pages from your website using Google Analytics

Once I wrote my blog, I am always wondering how many people have been visiting my blog. Also, have another question, what are the top-performing page from my website. To find out the answer I open up my Google analytics web application and checked the number. But still, I am not happy yet because I want to publish that data on my website as well. So my website visitors are also aware of the (x) number of users who had read that article. 

Now the question stands - 

  • How to get total number of visitors to my website?
  • How to get total number of visitors to my single blog post?
  • How to get top 5 or 10 blog post from my website?

Let's find out the answers- 


I am assuming, you already have a Google Analytics account and configure the Google Analytics respective code snippets to your page <head> tag.

How to check the total number of visitors to your website from Google Analytics

Please open up your google analytics web application and navigate to the Audience > Overview tab and select the date range from the right side top corner. Tiy can see the number of page views during that period. 


Now, how you can publish that data to your website. For that, you need to do the following setup -

Step 1: Open the google cloud platform ( and navigate to the console. 


Step 2: Create a new Project, like below - 


Step 3: Once the project is configured, need to set up a Service Account. 

Please follow the google link to set up the service account -, Once you set up the, please download the service-account.json file as we are going to use the same in our Node Js application. 


Step 4: Enable Google Analytics API

From the left Hamburger menu, navigate to Marketplace and select Google Analytics API and Enable it for further access.



Here I am going to create a NodeJS with TypeScript wrapper to access Google Analytics APIs. It is not recommended to add secret or confidential data to client side. Here, service-account.json file is a very valuable and confidential file and should never be exposed to client-side.

Step 5: Create a node js TypeScript application In order to access Google Analytics APIs. 

I have already created a boilerplate nodejs with the TypeScript application. To set up a new application, please check this out.

Step 6: How to get total number of visitors to my website

In this step, we are going to call Google Analytics REST API (v3) to get the data. But to call the API you need to authenticate first. 

First authenticate yourself to google and get respective JWT token in order to access the Google Analytics public APIs. Here, I am going to use googleapis npm package so install it.

npm install googleapis

Once done we need to call the following function to get the jwt token- 

import { google } from "googleapis";

import key from "../config/service-account.json";


private async getToken() {

if (!this.token) {

const jwtClient = new google.auth.JWT(

key.client_email,  // here key.client_email is the property from service-account.json


key.private_key,  // here key.private_key is the property from service-account.json





this.token = await jwtClient.authorize();



return this.token;



As the above method will provide jwt token based on the service-account.json data and you need to pass the jwt token to access any of google APIs. 

Let's have a look how the API look like to get total number of visitors. 

To get total number of visitors to your website -'S-DATE&max-results=1

import request from "request-promise";


private async totalPageView() {

const url = `${this.endDate}&max-results=1`;


const options = {


method: "GET",

headers: {

"Content-Type": "application/json"


auth: {

"bearer": this.token.access_token




const resp = await request(options);

return resp;


Step 7: How to get total number of visitors to my single blog post?

In this step you can get specific number of users have been visiting to your single blog post. Let's see how the API looks like -'S_DATE&max-results=1

To call this API, you need to authenticate as well. 

Step 8: How to get top 5 or 10 blog post from my website

The same way, you can call the following API to get top 10 blog post. But there could be a chance to have a duplicate value. So, you can consolidate all and have one single object for specific page.,ga:pageTitle&metrics=ga:pageviews&sort=-ga:pageviews&start-date=2019-01-01&end-date=TODAY'S-DATE&max-results=20&filters=ga:medium==organic

Have a look the entire code 

Hope you enjoyed the articale. Please post your comments in the comments section. 

Happy Coding!