PHP code example of ozankurt / google-analytics

1. Go to this page and download the library: Download ozankurt/google-analytics 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/ */

    

ozankurt / google-analytics example snippets


composer 

'scopes' => [
	'https://www.googleapis.com/auth/analytics.readonly',
],

    /**
     * View ID can be found in `http://google.com/analytics` under the `Admin` tab on navigation.
     *
     * Select `Account`, `Property` and `View`. You will see a `View Settings` link.
     */
    'analytics' => [
        'viewId' => 'ga:12345678',
    ],

use Kurt\Google\Analytics\Analytics as GoogleAnalytics;

class GoogleController extends Controller
{
    private $ga;

    function __construct(GoogleAnalytics $ga) {
        $this->ga = $ga;
    }

    public function index()
    {
        $results = $this->ga->getUsersAndPageviewsOverTime();

        var_dump($results);
    }
}



urt\Google\Core\Core;
use Kurt\Google\Analytics\Analytics;

$googleCore = new Core([
    'applicationName'       => 'Google API Wrapper Demo',
    'jsonFilePath'          => 'Google API Wrapper Demo-174e172143a9.json',
    'scopes' => [
        Google_Service_Analytics::ANALYTICS_READONLY,
    ],
    'analytics' => [
        'viewId' => 'ga:97783314'
    ],
]);

$analytics = new Analytics($googleCore);

$results = $analytics->getUsersAndPageviewsOverTime();

var_dump($results);

array (size=2)
    'cols' => 
        array (size=2)
            0 => string 'ga:sessions' (length=11)
            1 => string 'ga:pageviews' (length=12)
    'rows' => 
        array (size=1)
            0 => array (size=2)
                'ga:sessions' => string '100' (length=3)
                'ga:pageviews' => string '250' (length=3)