How to Use Google API to Get Google Analytics Data with PHP

To fetch the google analytics data through google api, you need to enable the google analytics api from google developer console panel.

Use this link to access google api console. Find the analytics api and enable it.

google api console

To enable this api, first you have to create a project in it. Just click on Create Project button. After that fill the project name in form and submit it. Now your analytics api get enabled.

create google project

You can access this api via Oauth procedure. We are showing an simple way to do this.

For this you required “analytics.class.php” file. In this file all analytics method are defined. We just call that method to show data. For that we need your gmail account access and Google Analytics Profile Id (View ID).

How to get Profile/View Id:

Signin your analytics account. Go to Admin section -> View Settings -> View Id

google profile id

Use this below code to view the analytics data.

This will show you visitors info of your website and number of page views. Their are many more function defined in analytics.class.php file. You can use as per your requirement.

Leave a Comment