Integrate Google Analytics Api to fetch Page views count
My use case was to show count of Page Views and Unique Page Views using Google analytics. Though there is already enough documentation provided with detailed explanation, it takes time to understand where to get required parameters to call those APIs, due to nested structure of Analytics account.
We can refer here to get detailed idea on Hierarchy of accounts, users, properties, and views for Google Analytics
To fetch Page views and Unique Page views count it’s a Profile Id that we need to know, not a Analytics account Id.
Below are required api url with scenarios and purpose it is for:
Scenario – A single Google Account can have more than Analytics account
Api : https://www.googleapis.com/analytics/v3/management/accounts
Purpose : To fetch all Analytics accounts associated with a particular Google Account
Reference : https://developers.google.com/analytics/devguides/config/mgmt/v3/mgmtReference
/management/accounts/list
Scenario – A single Analytics account can have more than one Webproperty
Api : https://www.googleapis.com/analytics/v3/management/accounts/accountId/webproperties
Purpose : To fetch all web properties associated with a specific Analytics account.
Reference : https://developers.google.com/analytics/devguides/config/mgmt/v3/mgmtReference
/management/webproperties/list
Scenario – A single Webproperty can have more than one Profiles
Api : https://www.googleapis.com/analytics/v3/management/accounts/accountId/webproperties
/webPropertyId/profiles
Purpose : To fetch all Profiles associated under a webproperty
Reference : https://developers.google.com/analytics/devguides/config/mgmt/v3/mgmtReference
/management/profiles/list
Once we have desired profile id, we can use below api to fetch Page views and visitors count.
Api : https://www.googleapis.com/analytics/v3/data/ga?ids=ga:{profileId}&start-date={yyyy-mm- dd}&end-date={yyyy-mm-dd}&metrics=ga:pageviews,ga:uniquePageviews – Reference
Hope it helps 🙂