You might have a need to display data from Google Analytics on your website like number of product page views in your webshop CMS. To achieve that, you would request the page views and maybe sessions (unique visits) for a certain URL from Google Analytics. If you’re using PHP, Google created Google API PHP client to help you do that. In this tutorial we’re going to use Server to Server Application authentication mechanism called two-legged OAuth.
Installing the PHP client
Download code from https://github.com/google/google-api-php-client or, better, use composer and
composer require google/apiclient @stable
Creating a Service Account
Go to Google Developers Console and create a project if you don’t have one for your app. Next, enable the API you want to use, we’re going to use Analytics API in this tutorial. After that, go to Credentials and go and create new Client ID. In the modal, select Service Account. New account will be created and a p12 key file will be downloaded. Store that file in a secure place, readable by your application.
Service account has Client ID and email address: put those in your configuration for future use.
Setting up authorization and Google_Client
To authorize, we’re going to use
// OAuth2 service account p12 key file $p12FilePath = '/path/to/key.p12'; // OAuth2 service account ClientId $serviceClientId = 'some-letters-and-numbers.apps.googleusercontent.com'; // OAuth2 service account email address $serviceAccountName = 'firstname.lastname@example.org'; // Scopes we're going to use, only analytics for this tutorial $scopes = array( 'https://www.googleapis.com/auth/analytics.readonly' ); $googleAssertionCredentials = new Google_Auth_AssertionCredentials( $serviceAccountName, $scopes, file_get_contents($p12FilePath) );
With this authorization credentials, create the client:
$client = new Google_Client(); $client->setAssertionCredentials($googleAssertionCredentials); $client->setClientId($serviceClientId); $client->setApplicationName("Project");
Calling Analytics API
First, add that service account email address to analytics account view User Management. Next thing is to request analytics data. We’re going to request
pagePath dimension with
pageviews metrics for a specific analytics view. To get the Analytics View ID, go to analytics account -> Admin -> View settings. Data will be sorted by page views in descending order. We are also going to filter the data by pagePath, as we only want products pages.
// Create Google Service Analytics object with our preconfigured Google_Client $analytics = new Google_Service_Analytics($client); // Add Analytics View ID, prefixed with "ga:" $analyticsViewId = 'ga:SOMENUMBER'; $startDate = '2015-01-01'; $endDate = '2015-01-15'; $metrics = 'ga:sessions,ga:pageviews'; $data = $analytics->data_ga->get($analyticsViewId, $startDate, $endDate, $metrics, array( 'dimensions' => 'ga:pagePath', 'filters' => 'ga:pagePath==/url/to/product/', 'sort' => '-ga:pageviews', )); // Data $items = $data->getRows();
$items array now holds one item, with URL, sessions and page views data. Hope this tutorial helped you, and that you will be able to put various Google Analytics data to good use.