GA:PI() – Google Analytics PHP Interface and OAuth2

Go to Google Console

Enable Google Analytics API

Create new project

google-console

Go to APIs & auth > Credentials
Click on Create new Client ID, and select Service account and P12 key. Then click on Create Client ID to download it.

ga-admin

Go to Google Analytics Admin and give permissions for read & analyze to the new email that looks something like 381198546241-kwuf1jrlpqbpq1523sx1k0h37tidybhe@developer.gserviceaccount.com

Download gapi-google-analytics-php-interface

And finally some php code:


define('ga_profile_id','YOUR_PROFILE_ID');
$ga = new gapi("381198546241-kwuf1jrlpqbpq1523sx1k0h37tidybhe@developer.gserviceaccount.com","PATH_TO_YOUR_P12_KEY.p12");
$ga->requestReportData(ga_profile_id, $dimensions, $metrics, $sort_metric, $filter, $start_date, $end_date, $start_index=1, $maxResults=30);

This are the basic steps