Accessing your Google Analytic Data via API
To allow a third party module or application to view and display your Google Analytics data for your website. You need to get a few things organised.
1. You need to have a Google Analytic account with your website registered.
Go to: http://www.google.com/analytics/ and follow their instructions to set up your URL under an account that you can manage and access with Admin permissions. We are not going to go through these steps here as it is a given that you will have this. Seek help from Google if you can’t manage.
2. Set up an API for your Google Analytics Account at the Google Developers Portal.
Go to: https://developers.google.com/ and login with your account.
To get started using Google Analytics API, you need to first create or select a project in the Google Developers Console and enable the API. Using this link guides you through the process and activates the Google Analytics API automatically.
Alternatively, you can activate the Google Analytics API yourself in the Developers Console by doing the following:
- Go to the Google Developers Console.
- Select a project, or create a new one.
- In the sidebar on the left, expand APIs & auth. Next, click APIs. Select the Enabled APIs link in the API section to see a list of all your enabled APIs. Make sure that the Google Analytics API is on the list of enabled APIs. If you have not enabled it, select the API from the list of APIs, then select the Enable API button for the API.
- In the sidebar on the left, select Credentials.
In either case, you end up on the Credentials page and can create your project’s credentials from here.
Create a client ID
From the Credentials page, click Create new Client ID under the OAuth heading to create your OAuth 2.0 credentials.
- For the APPLICATION TYPE select Service account.
- Click Create Client ID.
- For the KEY TYPE select P12 key. (The system will download a .P12 file. You will need this file to upload to the module)
- A dialog box appears. To proceed, click Okay, got it.
3. Add service account to Google Analytics account
The newly created service account will have an email address,
<projectId>-<uniqueId>@developer.gserviceaccount.com; Use this email address to add a user to the Google analytics account you want to access via the API. For this tutorial only Read & Analyzepermissions are needed.
Select User Management (in the Analytics Admin)
Enter the weird email address from the API credentials step above to give Read & Analyze permissions.
- You have created a Google Developer Account.
- Created an API and Given Permission to “Analytics API”
- You have Downloaded a P12 Credentials file.
- You have Authorised the associated weird google email address from the P12 account to have read permissions on your Analytics Account.
If you get all that right, then the module we use, will work to access your Google Analytics data from within your module.