PHP code example of p1ho / google-analytics-api

1. Go to this page and download the library: Download p1ho/google-analytics-api library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

p1ho / google-analytics-api example snippets




1ho\GoogleAnalyticsAPI\Client;

$googleAnalytics = new Client('path/to/your/credentials/json');

// this is the view id on your Google Analytics page that represents the website.
$viewId = '12345678';

// dates can be anything that can be parsed by strtotime()
$startDate = 'yesterday';
$endDate = 'today';

/*
For complete listing of dimensions and metrics,
see https://developers.google.com/analytics/devguides/reporting/core/dimsmets
 */

// no longer than 7
$dimensions = [
  'ga:city',
  'ga:cityId'
];
// no longer than 50
$metrics = [
  'ga:users',
  'ga:sessions'
];

// queries and get organized data
$report = $googleAnalytics->getData($viewId, $startDate, $endDate, $dimensions, $metrics);



// your code from before

$filtersExp = "your-filter-expression-here";
$report = $googleAnalytics
            ->getData($viewId, $startDate, $endDate, $dimensions, $metrics, $filtersExp);