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:
$client = new Google_Client();
The next step is to configure our new object. The following code sets some important attributes:
$app_name = 'MyAnalyticsApp'; $analytics_client_id = '123456789123.apps.googleusercontent.com'; $analytics_client_secret = 'jsiId93-ncUs0d92d5vF9sOk'; $analytics_developerToken = 'ldoCnenfiU93-oeuFJoI84'; $redirect_uri = "http://".$_SERVER['HTTP_HOST']."/script.php"; $scope = "https://www.googleapis.com/auth/analytics.readonly"; $client->setApplicationName($app_name); $client->setClientId($analytics_client_id); $client->setClientSecret($analytics_client_secret); $client->setDeveloperKey($analytics_developerToken); $client->setRedirectUri($redirect_uri); $client->setScopes(array($scope));
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.
if (isset($_GET['code'])) { $client->authenticate(); $_SESSION['token'] = $client->getAccessToken(); $redirect = 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF']; header('Location: ' . filter_var($redirect, FILTER_SANITIZE_URL)); } if (isset($_SESSION['token'])) { $client->setAccessToken($_SESSION['token']); } if ($client->getAccessToken()) { $_SESSION['token'] = $client->getAccessToken(); } else { $loginUrl = sprintf("https://accounts.google.com/o/oauth2/auth? scope=%s&state=%s&redirect_uri=%s&response_type=code&client_id=%s&access_type=%s",$scope,$state,$redirect_uri,$analytics_client_id,$access_type); header('Location: '.$loginUrl); }
After successful login procedure, we can use our client object to create an AnalyticsService object to request data from the API:
$analytics = new Google_AnalyticsService($client);
I show you how to request Analytics data in the second part!