Answer the question
In order to leave comments, you need to log in
How to make a simple php script to work with Google Analytics API?
Hello!
Help to run the simplest script for working with the Google Analytics API.
What I do:
1. Downloaded the archive from https://github.com/google/google-api-php-client/
2. Expanded it to the test/google-api-php-client-master/ directory
3. Created the file test/ index.php with this code:
<?php
require_once('google-api-php-client-master/src/Google/Client.php');
require_once('google-api-php-client-master/src/Google/Service/Analytics.php');
?>
Fatal error: Class 'Google_Service' not found in X:\home\foo\www\test\google-api-php-client-master\src\Google\Service\Analytics.php on line 32
<?php
require_once('google-api-php-client-master/src/Google/autoload.php');
define ('APP_NAME', 'Analytics Example');
define ('APP_EMAIL', '650831266*********@developer.gserviceaccount.com');
define ('PATH_TO_PRIVATE_KEY_FILE', 'API Project-7d819********.p12');
define ('CLIENT_ID', '********.apps.googleusercontent.com');
$client = new Google_Client();
$client->setApplicationName(APP_NAME);
$client->setClientId(CLIENT_ID);
$client->setAccessType( 'offline_access');
$client->setAssertionCredentials(
new Google_Auth_AssertionCredentials(
APP_EMAIL,
array('https://www.googleapis.com/auth/analytics.readonly'),
file_get_contents(PATH_TO_PRIVATE_KEY_FILE)
)
);
$service = new Google_Service_Analytics($client);
//print_r($service);
$data = $service->data_ga->get(
'ga:***',
'2015-03-26',
'2015-03-26',
'ga:users'
);
print_r($data);
?>
[totalsForAllResults] => Array
(
[ga:users] => 0
)
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question