Teevio

PHP Google Analytics API

January 29th, 2016

Yesterday, I was wanting to pull specific Google Analytics data with their API. I started following their Hello Analytics API: PHP QuickStart guide and quickly found out that their quick start does not use the latest Client PHP Library. After struggling a while to get the new stuff working, I finally got it all set up correctly. If you have never used their Analytics API you ought to.

After getting everything worked out I decided to take their current docs and update them with the latest API info. Most of what’s below is their documentation with some critical changes made by me. Enjoy!

Step 1: Enable the Analytics API

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:

  1. Open the Credentials page.

In either case, you end up on the Credentials page and can create your project’s credentials from here.

Create a client ID

  1. Open the Service accounts section of the Developers Console’s Permissions page.
  2. Click Create service account.
  3. In the Create service account window, type a name for the service account and select Furnish a new private key. If you want to grant Google Apps domain-wide authority to the service account, also select Enable Google Apps Domain-wide Delegation. Then, click Create.

When prompted for the Key type select JSON, select your Service Account and then click Create. Once you create the service account a JSON file will be downloaded. Your new public/private key pair is generated and downloaded to your machine; it serves as the only copy of this key. You are responsible for storing it securely.

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 & Analyze permissions are needed.

Step 2: Install the Google Client Library

The preferred method is via Composer. Follow the installation instructions if you do not already have Composer installed.

Once Composer is installed, execute the following command in your project root to install the PHP API library:

composer require google/apiclient:^2.0.0@RC

Finally, be sure to include the autoloader:

require_once '/path/to/your-project/vendor/autoload.php';

Step 3: Setup the sample

You’ll need to create a single file named HelloAnalytics.php, which will contain the sample code below. The only difference in this code and the original is seen in the getService function.

Move the previously downloaded .json within the same directory as the sample code and make sure you update the filename for this line putenv('GOOGLE_APPLICATION_CREDENTIALS=file.json');

Step 4: Run the sample

After you have enabled the Analytics API, installed the Google APIs client library for PHP, and set up the sample source code the sample is ready to run.

Run the sample using:

php HelloAnalytics.php

When you finish these steps, the sample outputs the name of the authorized user’s first Google Analytics view (profile) and the number of sessions for the last seven days.

With the authorized Analytics service object you can now run any of code samples found in the Management API reference docs. For example you could try changing the code to use the accountSummaries.list method.

If you have any questions feel free to comment on the Gist