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

LazyPanda
LazyPanda

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. 

total-number-of-page-view​​​​​

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 (https://cloud.google.com/) and navigate to the console. 

 

Step 2: Create a new Project, like below - 

google-platform-console-analytics​​​​​

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 - https://cloud.google.com/iam/docs/creating-managing-service-account-keys, 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.

google-platform-console-analytics​​​​​

google-platform-console-analytics-REST-API-enabled​​​​​

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. https://lazypandatech.com/blog/NodeJS/13/Create-production-ready-REST-API-using-nodeJS-with-TypeScript/

 
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

undefined,

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

["https://www.googleapis.com/auth/analytics.readonly"],

undefined

);

 

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 - 

https://www.googleapis.com/analytics/v3/data/ga?ids=ga:214292350&metrics=ga:pageviews&start-date=2019-01-01&end-date=TODAY'S-DATE&max-results=1

import request from "request-promise";

 

private async totalPageView() {

const url = `https://www.googleapis.com/analytics/v3/data/ga?ids=ga:214292350&metrics=ga:pageviews&start-date=2019-01-01&end-date=${this.endDate}&max-results=1`;

 

const options = {

url,

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 - 

https://www.googleapis.com/analytics/v3/data/ga?ids=ga:214292350&dimensions=ga:pagePath&metrics=ga:pageviews&filters=ga:pagePath==PATH_OF_YOUR_BLOG_POST&start-date=2019-01-01&end-date=TODAY'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. 

https://www.googleapis.com/analytics/v3/data/ga?ids=ga:214292350&dimensions=ga:landingPagePath,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!