//// Showing Data in tabular form extarcted from Google Analytics Using Google API(PHP) service account
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Google Analytics is simply the best traffic-tracking tool for any business size it tell you everything you want to know about your website, how many visitors entered your website in a specific time-span,how your visitors locate you where those visitors came from and how they interact with your site.It help you in understanding and monitoring the traffic flow.
Quick glance at the code for extracting data from Google Analytics Using Google API(PHP) using service account
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//// CODE
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
<?php////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/**********************************************
* Load Google API library first
**********************************************/
require_once 'google-api-php-client/vendor/autoload.php';
/**********************************************
* Start session to store authorization data
**********************************************/
session_start();
/**********************************************
* Set Google service account details here
**********************************************/
/**********************************************
*OAuth2 service account ClientId
**********************************************/
$client_id = 'YOUR CLIENT ID GENERATED FOR YOU';
/**********************************************
*OAuth2 service account email address
**********************************************/
$Email_address = 'YOUR SERVICE ACCOUNT EMAIL ADDRESS GENERATED FOR YOU';
/**********************************************
*OAuth2 service account JSON key file Location
**********************************************/
$key_file_location = 'YOUR KEY FILE LOCATION';
/**********************************************
* Create and configure a new client object
**********************************************/
$client = new Google_Client();
$client->setApplicationName("Client_Library_Examples");
$key = file_get_contents($key_file_location);
/**********************************************
* We are going to use only analytics here
**********************************************/
$scopes ="https://www.googleapis.com/auth/analytics.readonly";
$client->setAuthConfig('YOUR KEY FILE LOCATION');
$client->setScopes(['https://www.googleapis.com/auth/analytics.readonly']);
/**********************************************
* Adding Dimensions
**********************************************/
$dim = array('dimensions' => 'ga:userType,ga:date,ga:browser,ga:country,ga:city');
/**********************************************
* Adding Metrics
**********************************************/
$metrics = "ga:users,ga:sessions,ga:hits,ga:bounces";
putenv('GOOGLE_APPLICATION_CREDENTIALS=YOUR KEY FILE LOCATION');
$client->useApplicationDefaultCredentials();
$service = new Google_Service_Analytics($client);
/**********************************************
* Add Analytics View ID, prefixed with "ga:"
**********************************************/
$ViewId = 'ga:YOURVIEWID';
/**********************************************
* Query the Analytics data
**********************************************/
$data = $service->data_ga->get($ViewId , "7daysAgo", "today", $metrics, $dim );
?>
/**********************************************
* Extracting the detail obtained and displit in tabular form
**********************************************/
<html>
Displaing Results from Google Analytics<br>
<table border="1">
<tr>
<?php
/**********************************************
* Printing Headers
**********************************************/
foreach($data->getColumnHeaders() as $header){
print "<td><b>".$header['name']."</b></td>";
}
?>
</tr>
<?php
/**********************************************
* Printing inner Rows
**********************************************/
foreach ($data->getRows() as $row) {
print "<tr><td>".$row[0]."</td><td>".$row[1]."</td><td>".$row[2]."</td><td>".$row[3]."</td><td>".$row[4]."</td><td>".$row[5]."</td><td>".$row[6]."</td><td>".$row[7]."</td><td>".$row[8]."</td></tr>";
}
?>
</table>
</html>
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//// Detailed explanation of code
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
2.How to extract Custom Data from the Google Analytics Using Google API
How to extract Custom Data from the Google Analytics Using Google API
How to extract Custom Data from the Google Analytics Using Google API
How to extract Custom Data from the Google Analytics Using Google API
No comments:
Post a Comment