PHP code example of mstroink / cakephp-google-analytics

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

    

mstroink / cakephp-google-analytics example snippets


use MStroink\Analytics\Analytics;
use MStroink\Analytics\Period;

// In src/Controller/DashboardController.php
class DashboardController extends AppController
{
    public function view(Analytics $analytics)
    {
        //fetch the most visited pages for today and the past week
        $analytics->fetchMostVisitedPages(Period::days(7));

        //fetch visitors and page views for the past week
        $analytics->fetchVisitorsAndPageViews(Period::days(7));
    }
}

return [

    /*
     * The view id of which you want to display data.
     */
    'view_id' => env('ANALYTICS_VIEW_ID'),

    /*
     * Path to the client secret json file. Take a look at the README of this package
     * to learn how to get this file. You can also pass the credentials as an array
     * instead of a file path.
     */
    'service_account_credentials_json' => ROOT . DS . 'config' . DS . 'service-account-credentials.json',

    /*
     * The amount of minutes the Google API responses will be cached.
     * If you set this to zero, the responses won't be cached at all.
     */
    'cache_lifetime_in_minutes' => 60 * 24,

    /*
     * The name of the cache config engine
     */
    'cache' => [
        'analytics' => 'google_analytics',
        'auth' => 'google_auth',
    ],
];

'analytics_results' => [
    'className' => FileEngine::class,
    'duration' => '+1 year', //note that lifetime is managed by setting cache_lifetime_in_minutes
],

//retrieve visitors and pageview data for the current day and the last seven days
$analyticsData = $analytics->fetchVisitorsAndPageViews(Period::days(7));

//retrieve visitors and pageviews since the 6 months ago
$analyticsData = $analytics->fetchVisitorsAndPageViews(Period::months(6));

//retrieve sessions and pageviews with yearMonth dimension since 1 year ago
$analyticsData = $analytics->performQuery(
    Period::years(1),
    'ga:sessions',
    [
        'metrics' => 'ga:sessions, ga:pageviews',
        'dimensions' => 'ga:yearMonth'
    ]
);

$startDate = Chronos::now()->subYear();
$endDate = Chronos::now();

Period::create($startDate, $endDate);

public function fetchVisitorsAndPageViews(Period $period): Collection

public function fetchTotalVisitorsAndPageViews(Period $period): Collection

public function fetchMostVisitedPages(Period $period, int $maxResults = 20): Collection

public function fetchTopReferrers(Period $period, int $maxResults = 20): Collection

public function fetchUserTypes(Period $period): Collection

public function fetchTopBrowsers(Period $period, int $maxResults = 10): Collection

public function performQuery(Period $period, string $metrics, array $others = [])

$analytics->getAnalyticsService();