Google Analytics PHP API - A Simple Example

I am trying to install some basic example of using Google Analytics with this library: https://github.com/google/google-api-php-client

For starter, I have:

<?php require_once 'Google/Client.php'; require_once 'Google/Service/Analytics.php'; $client = new Google_Client(); $client->setApplicationName("Client_Library_Examples"); $client->setDeveloperKey("MY_SECRET_API"); //security measures $service = new Google_Service_Analytics($client); $results = $service->data_ga; echo '<pre>'; print_r($results); echo '</pre>'; 

Q: How to get data from Google Analytics from this query?

 /* https://www.googleapis.com/analytics/v3/data/ ga?ids=ga%123456 &dimensions=ga%3Acampaign &metrics=ga%3Atransactions &start-date=2013-12-25 &end-date=2014-01-08 &max-results=50 */ 
+7
api php google-analytics google-analytics-api
source share
4 answers
 $client->setDeveloperKey("MY_SECRET_API"); 

First of all, since, as I understand it, this will not work for authentication, you will need to use OAuth2 authentication. There are two options for this: using a client identifier for a web application or using a service account. Api authorization

After that you can make such a call. (Here I use the service account)

First authenticate:

 $key = file_get_contents($key_file_location); $cred = new Google_Auth_AssertionCredentials( $service_account_name, array('https://www.googleapis.com/auth/analytics.readonly'), $key ); $client->setAssertionCredentials($cred); 

Call:

 $ids = 'ga:123456'; //your id $startDate = '2013-12-25'; $endDate = '2014-01-08'; $metrics = 'ga:transactions'; $optParams = array( 'dimensions' => 'ga:campaign', 'max-results' => '50' ); $results = $service->data_ga->get($ids, $startDate, $endDate, $metrics, $optParams); //Dump results echo "<h3>Results Of Call:</h3>"; echo "dump of results"; var_dump($results); echo "results['totalsForAllResults']"; var_dump($results['totalsForAllResults']); echo "results['rows']"; foreach ($results['rows'] as $item) { var_dump($item); } 
+1
source share

You will need to do http to get the information from the url.

http://www.php.net/manual/en/function.http-get.php

Remember that you still have to add the Oauth2 authentication code to the string before you can send this request. This link may help if you no longer have auth code. https://developers.google.com/analytics/solutions/articles/hello-analytics-api#authorize_access

0
source share

what you can do is create a new function ...

 function ga_campaign_transactions($gaEmail, $gaPass, $gProfile, $limit) { require_once('classes/google-analytics/gapi.class.php'); $gDimensions = array('campaign'); $gMetrics = array('transactions'); $gSortMetric = NULL; $gFilter = ''; $gSegment = ''; $gStartDate = '2013-12-25'; $gEndDate = '2014-01-08'; $gStartIndex = 1; $gMaxResults = $limit; $ga = new gapi($gaEmail, $gaPass); $ga->requestReportData($gProfile, $gDimensions, $gMetrics, $gSortMetric, $gFilter, $gSegment, $gStartDate, $gEndDate, $gStartIndex, $gMaxResults); $gAnalytics_results = $ga->getResults(); //RETURN RESULTS return $gAnalytics_results; } $gProfile = '123456'; // The Profile ID for the account, NOT GA: $gaEmail = 'YOUR GOOGLE EMAIL'; // Google Email address. $gaPass = 'YOUR GOOGLE PASSWORD'; // Google Password. // NOTE: if 2 step login is turned on, create an application password. $limit = 50; $ga_campaign_transactions = ga_campaign_transactions($gaEmail, $gaPass, $gProfile, $limit) //OUTPUT if(!empty($ga_campaign_transactions)) { $counter=0; $gaCampResults= array(); // CREATE ARRAY TO STORE ALL RESULTS foreach($ga_campaign_transactions as $row) { $dim_list = $row->getDimesions(); $met_list = $row->getMetrics(); $gaCampResults[$counter]['campaign'] = $dim_list['campaign']; $gaCampResults[$counter]['transactions'] = $met_list['transactions']; $counter++; } } if(!empty($gaCampResults)) { $totalCampTransactions = count($gaCampResults); ?> <h2>We Found ( <?php echo number_format($totalCampTransactions,0);?> ) Results</h2> <ul> <?php foreach($gaCampResults as $gaRow){ echo "<li>Campaign:".$gaRow['campaign']." | Transactions: ".$gaRow['transactions']."</li>"; } ?> </ul> <?php } 

find google analytics profile id

Create password for Google app

We hope you are on the right track :) this is unchecked, but it looks like I used ...

Marty

0
source share

If you want to integrate the Google Analytics API with your application, visit Integration and the Google Analytics API Example

0
source share

All Articles