Hi Kiran,
Service Account
Using server side authorization with the Google Python Client API you can use this demo to get access in google analytics data and charts for every user without login.
oAuth for Google APP
The other way is to use oAuth. But in this case you need a payable Google Apps for work Account Google Apps You can read here how to combine Apps and oAuth for access without login.
I have added here the working code for the new (2016) Google Client PHP API Beta for anonymous access using json - including an amChart.
Additionally the renewing process for the json credential files can be automated - this is not done in this code example.
A ClientLogin token can last for 2 weeks from the issue date, but this limit is service-specific and can be shorter. You can change lifetime in Google_AssertionCredentials.php(24) though this is a security risk (for your money - if someone calls the site automatically you run out of the allowed duty free volume)
class Google_AssertionCredentials {
const MAX_TOKEN_LIFETIME_SECS = 360000;
To make the autoload.php work correctly, you have to install the Client PHP API ressources by composer.phar into htdocs(Apache) or wwwroot(IIS) and place this code in the folder "vendor".
I did not use dataLoader and commented it out, for amChart stucks in my environment on load. Therefore I used the dataprovider, which works reliable.
/*
"dataLoader": {
"url": "data.php",
"format": "json"
},
*/
<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1" />
<title>Gimba - Google Analytics - GimbaChartAll</title>
<script src="https://www.amcharts.com/lib/3/amcharts.js"></script>
<script src="https://www.amcharts.com/lib/3/serial.js"></script>
<script src="https://www.amcharts.com/lib/3/plugins/dataloader/dataloader.min.js"></script>
<style>
body, html {
font-family: Verdana;
font-size: 10px;
}
#chartdiv {
width: 1100px;
height: 700px;
margin-left:auto;
margin-right:auto;
}
</style>
<script>
var dataJS = <?php echo dataGA(); ?>;
var chart = AmCharts.makeChart("chartdiv", {
"type": "serial",
"theme": "light",
/*
"dataLoader": {
"url": "data.php",
"format": "json"
},
*/
"dataProvider": dataJS ,
"categoryField": "country",
"categoryAxis": {
"gridColor": "#0000FF",
"gridAlpha": 0.07,
"title": "Country"
},
"creditsPosition": "top-right",
"categoryField": "country",
"categoryAxis": {
"gridAlpha": 0.07,
"gridPosition": "start",
"tickPosition": "start",
"title": "Country"
},
"valueAxes": [ {
"id": "v1",
"gridAlpha": 0.1,
"axisColor": "#0000ff",
"title": "Users/Sessions"
}, {
"id": "v2",
"gridAlpha": 0,
"axisColor": "#0000ff",
"position": "right",
"title": "Page views"
} ],
"graphs": [ {
"startDuration": 3,
"type": "column",
"title": "Sessions",
"valueField": "sessions",
"fillColors": "#0000ff" ,
"lineAlpha": 0,
"fillAlphas": 0.6
}, {
"type": "column",
"title": "Users",
"valueField": "users",
"fillColors": "#0000ff" ,
"lineAlpha": 0,
"fillAlphas": 0.2
}, {
"type": "line",
"valueAxis": "v2",
"title": "Page views",
"valueField": "pageviews",
"lineColor": "#0000ff" ,
"lineThickness": 1,
"bullet": "round"
} ],
"legend": {}
} );
</script>
</head>
<body>
<div id="chartdiv"></div>
</body>
</html>
<?php
//dataGA();
function dataGA()
{
require_once 'autoload.php';
$google_account = array(
'email' => '[email protected]',
'key' => file_get_contents(__DIR__ . '/OAuthClientServiceAccount1.json'),
'profile' => 'xxxxxxxxx'
);
// Creates and returns the Analytics service object.
// Load the Google API PHP Client Library.
// Create and configure a new client object.
$client = new Google_Client();
$client->setApplicationName( 'Gimba3' );
$analytics = new Google_Service_Analytics($client);
$scopes = array('https://www.googleapis.com/auth/analytics.readonly');
$client->setScopes($scopes);
try{
$client->setAuthConfigFile(__DIR__ . '/OAuthClientServiceAccount1.json');
}
catch(Exception $e){
echo "Key NOT OK<br>";
echo $e->getMessage()."<br>";
}
try{
if( $client->isAccessTokenExpired() ) {
$client->refreshTokenWithAssertion($client->setAuthConfigFile(__DIR__ . '/OAuthClientServiceAccount2.json'));
}
}
catch(Exception $e){
echo "RefreshKey NOT OK<br>";
echo $e->getMessage()."<br>";
}
$projectId = '123464155';
$results = $analytics->data_ga->get(
'ga:'.$projectId,
'30daysAgo',
'today',
'ga:sessions,ga:users,ga:pageviews',
array(
'dimensions' => 'ga:country',
'sort' => '-ga:sessions',
'max-results' => 10
));
$rows = $results->getRows();
//var_dump($rows);
$dataGA = array();
foreach( $rows as $row ) {
$dataGA[] = array(
'country' => $row[0],
'sessions' => $row[1],
'users' => $row[2],
'pageviews' => $row[3]
);
}
$res = json_encode($dataGA);
return $res;
}
?>
Best regards
Axel Arnold Bangert - Herzogenrath 2016