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

Go to Google Console

Enable Google Analytics API

Create new project


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.


Go to Google Analytics Admin and give permissions for read & analyze to the new email that looks something like

Download gapi-google-analytics-php-interface

And finally some php code:

$ga = new gapi("","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