Action Coding
Action Coding

Reputation: 830

Google Analytics API using PHP with service account

I have been searching and looking this up for two days now and can't figure out where I am going wrong. I am attempting to pull data from my google analytics account using php. I have followed all the steps to set up a service account and downloaded the JSON file. Here is the code I am using:

// Load the Google API PHP Client Library
require_once ('/google-api-php-client-1-master/src/Google/autoload.php');
$KEY_FILE_LOCATION = '/inc/ac_key.json';
// Create and configure a new client object.
$client = new Google_Client();
$client->setApplicationName("Hello Analytics Reporting");
$client->setAuthConfig($KEY_FILE_LOCATION);
$client->setScopes(['https://www.googleapis.com/auth/analytics.readonly']);
$analytics = new Google_Service_Analytics($client);
function getResults($analytics, $profileId) {
  // Calls the Core Reporting API and queries for the number of sessions
  // for the last seven days.
   return $analytics->data_ga->get(
       'ga:' . $profileId,
       '30daysAgo',
       'today',
       'ga:pageviews'
      );
}
$profile = $r['google_analytics'];
$results = getResults($analytics, $profile);
$month = date('n');
$year = date('Y');
$results->setMonth($month,$year);
$visits = $results->getVisitors();
$views = $results->getPageviews();
/* build tables */
if(count($visits)) {
    foreach($visits as $day=>$visit) { 
        $flot_datas_visits[] = '['.$day.','.$visit.']';
        $flot_datas_views[] = '['.$day.','.$views[$day].']';
    }
    $flot_data_visits = '['.implode(',',$flot_datas_visits).']';
    $flot_data_views = '['.implode(',',$flot_datas_views).']';
}

I am getting an error for an invalid client_secret.json file but I am attempting to authenticate using a service account so I am not sure what is going on here. I am then attempting to plot the data in a flot table but I am not worried about that part yet as I am still trying to get through this first hurdle. Any help would be appreciated!

Upvotes: 1

Views: 1792

Answers (1)

Eike Pierstorff
Eike Pierstorff

Reputation: 32780

I'm not quite up-to-date with the Google PHP API client, but apparently there are different versions on offer - when I cloned the repo I got the version that you app use and I did not get it to work with my credentials file. I then installed the current version of the library via composer:

composer require google/apiclient:^2.0

I was then able to access a Google Analytics with the following code:

<?php
include_once __DIR__ . '/vendor/autoload.php';
date_default_timezone_set('America/Los_Angeles');
$client = new Google_Client();

$client->setAuthConfig('/path/to/credentials.json');

$client->setApplicationName("Client_Library_Examples");
$client->setScopes(['https://www.googleapis.com/auth/analytics.readonly']);
$service = new Google_Service_Analytics($client);

function getResults($service, $profileId) {
   return $service->data_ga->get(
       'ga:' . $profileId,
       '30daysAgo',
       'today',
       'ga:pageviews'
      );
}

$profile= "80493610";
$results = getResults($service, $profile);

print_r($results);

With a bit of poking around I managend to get an instance of the analytics service with the old version from your example. Apparently you have to use the loadServiceAccountJson function rather than setAuthConfig

<?php

// Load the Google API PHP Client Library
require_once ('google-api-php-client/src/Google/autoload.php');
$KEY_FILE_LOCATION =  '/path/to/credentials.json';
// Create and configure a new client object.
$client = new Google_Client();
$client->setApplicationName("Hello Analytics Reporting");
$scopes = ['https://www.googleapis.com/auth/analytics.readonly'];
$client->loadServiceAccountJson($KEY_FILE_LOCATION, $scopes);

$analytics = new Google_Service_Analytics($client);

print_r($analytics);

Upvotes: 0

Related Questions