Google Analytics API – Part 1

Google offers different APIs for different Google Apps. For SEM data there is AdWords. For SEO relevant data you can use Analytics API. With this API you can get a lot of data, which can be seen with Googles Analytics online tool. As for AdWords, Google also offers a library for many different programming languages. Here is a list of theses different languages:

  • PHP
  • Java
  • Python
  • JavaScript

The current version is called Core Reporting API v3.

Google client object

The first step is to create a Google client object. With PHP this will work as follows:

The next step is to configure our new object. The following code sets some important attributes:

Here is a list of all used parameters with description:

  • ApplicationName
    you can give your application a specified name.
  • ClientId
    You can create a new client Id at Google API Console. At the API Console you will see a list with many different Google APIs. We only want to set Analytics login. There you can activate Google APIs OAuth Logins.
  • ClientSecret
    If you successfully created a new OAuth Login, then you will find a new password, which is needed for login.
  • DeveloperKey
    You can get a developer key from Google if you request one.
  • RedirectUri
    This is a very important param. Yo have to set the URL to a script or site which is redirected after Google granted you the login for the Google Account.
  • Scopes
    You can set different scopes. For your example we only want to read values, so we only need read access.

The next step is to create code, that handles login and uses this Google client object. We place the code after initialization of the object.

If the user is not yet logged in, we have to make a redirect to Google authentication. After login, the user is redirected (RedirectUri) back to our script. So we check our GET param. If there is an AccessToken, we successfully logged in.

After successful login procedure, we can use our client object to create an AnalyticsService object to request data from the API:

I show you how to request Analytics data in the second part!

Part 2 | Part 3

(Visited 481 times, 1 visits today)

Leave a Reply

Your email address will not be published. Required fields are marked *