Answer the question
In order to leave comments, you need to log in
Is it possible to pull data using Google Analytics?
Hello. I want to make a visitor counter for the entire time on the site, as well as display the percentage of users visiting from the continents. Is it possible to collect data from Analytics and display it on my website?
Answer the question
In order to leave comments, you need to log in
The google-api-php-client-2.2.0
library helped .
1) Connect it to the site
2) Create the necessary keys in https://console.developers.google.com
3) Write all the keys to a file
4) Enjoy the result
require_once 'libs/google-api-php-client-2.2.0/vendor/autoload.php';
$client = new Google_Client(); $client->setApplicationName(""); // имя проекта
$client->setAuthConfig(''); // путь на файл json с данными для автоматической аунтификации
$client->addScope('https://www.googleapis.com/auth/analytics.readonly');
$GA_VIEW_ID = 'ga:XXXXX'; // Ваш View_ID
$service = new Google_Service_Analytics($client);
try {
$result = $service->data_ga->get( $GA_VIEW_ID, '100daysAgo', 'today','ga:uniquePageviews');
$count = $result->totalsForAllResults['ga:uniquePageviews'];
echo $count;
} catch(Exception $e) {
var_dump($e);
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question