PHP code example of ahmedash95 / laravel-analytics

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

    

ahmedash95 / laravel-analytics example snippets


use Spatie\Analytics\Period;

//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));

// config/app.php
'providers' => [
    ...
    Spatie\Analytics\AnalyticsServiceProvider::class,
    ...
];

// config/app.php
'aliases' => [
    ...
    'Analytics' => Spatie\Analytics\AnalyticsFacade::class,
    ...
];


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

    /*
     * Path to the json file with service account credentials. Take a look at the README of this package
     * to learn how to get this file.
     */
    'service_account_credentials_json' => storage_path('app/laravel-google-analytics/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 directory where the underlying Google_Client will store it's cache files.
     */
    'cache_location' => storage_path('app/laravel-google-analytics/google-cache/'),
];


$analyticsData = Analytics::fetchVisitorsAndPageViews(Period::days(7));

$startDate = Carbon::now()->subYear();
$endDate = Carbon::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 fetchTopBrowsers(Period $period, int $maxResults = 10): Collection

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

Analytics::getAnalyticsService();
 bash
php artisan vendor:publish --provider="Spatie\Analytics\AnalyticsServiceProvider"