Посмотрите, поможет ли это.(метод с использованием учетной записи службы):
Как создать учетную запись службы: https://developers.google.com/analytics/devguides/reporting/core/v3/quickstart/service-php?hl=pt-br
<?php
require_once __DIR__ . '/vendor/autoload.php';
$KEY_FILE_LOCATION = __DIR__ . '/service-account-credentials.json'; // <-- Service account credentials
$client = new Google_Client();
$client->setAuthConfig($KEY_FILE_LOCATION);
$client->setAccessType('offline');
$client->setScopes(['https://www.googleapis.com/auth/analytics.readonly']);
$client->refreshTokenWithAssertion();
$analytics = new Google_Service_Analytics($client);
$token = $client->getAccessToken();
$accounts = $analytics->management_accounts->listManagementAccounts();
$items = $accounts->getItems();
$total_contas = count($items);
if($total_contas > 0){
$firstAccountId = $items[0]->getId();
$properties = $analytics->management_webproperties->listManagementWebproperties($firstAccountId);
$items = $properties->getItems();
$firstPropertyId = $items[0]->getId();
$profiles = $analytics->management_profiles->listManagementProfiles($firstAccountId, $firstPropertyId);
$items = $profiles->getItems();
}
?>
<canvas id="grafico-analytics" height="300px"></canvas>
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.7.3/Chart.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.10.2/moment.min.js"></script>
<script>
(function(w,d,s,g,js,fs){
g=w.gapi||(w.gapi={});g.analytics={q:[],ready:function(f){this.q.push(f);}};
js=d.createElement(s);fs=d.getElementsByTagName(s)[0];
js.src='https://apis.google.com/js/platform.js';
fs.parentNode.insertBefore(js,fs);js.onload=function(){g.load('analytics');};
}(window,document,'script'));
gapi.analytics.ready(function() {
gapi.analytics.auth.authorize({
'serverAuth': {
'access_token': '<?php echo $token['access_token']; ?>' // Do not modify
}
});
Promise.all([query({
'ids': 'ga:<?php echo $items[0]->id; ?>', // Do not modify
'dimensions': 'ga:date,ga:nthDay',
'metrics': 'ga:pageviews',
'start-date': '2018-12-01',
'end-date': '2018-12-12'
})]).then(function(results) {
var data1 = results[0].rows.map(function(row) { return +row[2]; });
var labels = results[0].rows.map(function(row) { return +row[0]; });
labels = labels.map(function(label) {
return moment(label, 'YYYYMMDD').format('d MMM D YYYY');
});
var ctx = document.getElementById("grafico-analytics").getContext('2d');
var myChart = new Chart(ctx, {
type: 'line',
data: {
labels : labels,
datasets : [
{
label: 'PageViews',
backgroundColor: 'rgba(51,0,204, 0.2)',
borderColor: 'rgba(51,0,204, 1)',
borderWidth: 3,
data : data1
}
]
},
options:{
responsive: false,
scales:{
xAxes: [{
display: false,
}],
}
}
});
});
function query(params) {
return new Promise(function(resolve, reject) {
var data = new gapi.analytics.report.Data({query: params});
data.once('success', function(response) { resolve(response); })
.once('error', function(response) { reject(response); })
.execute();
});
}
});
</script>