Google Analytics API via PHP
The Google Analytics API is really great once you have it setup. The process to get it up and running can be a bit complicated as there a number of different steps involved.
As I've gone through the process a number of times I thought it would be useful to throw up a step by step guide. I've also include some sample PHP code that shows you how to connect and extract Analytics using the Google API PHP client.
API Access Setup
1. Create a new Project
The first thing that you need to do is to create a new project. Navigate to the projects page via this url: https://console.developers.google.com/project
Go ahead and click on the 'Create Project' button, you need to give your project a name. A project id will be automatically generated for you but you can overwrite it with your own unique id if you so wish.
2. API Access
Once you've created your project you need to activate the various Google APIs that you wish to access. Navigate to 'APIs' within the 'APIs and Auth' side menu. There are a number of APIs already activated by default, go ahead and activate the 'Analytics API'.
3. Authorisation
Now that we have a project setup with the Analytics API enabled we will now need to create the security credentials that will make is possible to access the API.
Navigate to the 'Credentials' link within the 'APIs & Auth' side menu, here you will be presented with the option to create a new oAuth client id or public access key.
We are interested in the OAuth client id so go ahead and click the 'Create new Client ID' button then select 'Service account'. Read more about service accounts
Your service account has it's own unique email address and client id, take note of both of these are they will be required later. A json file with your credentials will automatically be downloaded to your computer.
Now that your service account has been activated, click on the 'Generate new P12 key' and download your key cert and store it somewhere safe.
Setup Analytics for API Access
Now that you've created your new service account you will need to add this 'user' to your Google Analytics account.
1. Add API User
Login to analytics and then go to the admin dashboard section.
Under the first column labeled 'Account' select 'User Management'. Now go ahead and add the email address that is associated with your service account. It should be in the format [email protected].
As we will only be using this account to extract data from your Analytics account you can set the user privileges to 'Read & Analyze'.
2. Get your View ID
Go back to your Analytics admin dashboard and navigate to the 'View Settings' link, located in the third column 'View'. Take a note of the 'View ID', this will be required later.
That's pretty much it from the setup point of view. Next up is a simple example of how to access the Analytics API using PHP.
Access Google Analytics via PHP
We are going to be using composer to get started with this simple example. If you haven't used composer before have a read about what it is and how to use it over on the Composer website.
1. Install the Google PHP client library
You can install the Google API PHP client library by adding it as a dependency to your composer.json.
2. Simple PHP example
The official documentation goes into more detail on the various requests along with what metrics/dimensions you can use.
That should be enough to get you up and running, I hope it helps! If you've any questions just let me know!
- Ciaran Maher @ciaranmaher
Square1 are a creative online and mobile software development company based in Dublin, Ireland.








