PHP code example of builtbyberry / laravel-site-analytics

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

    

builtbyberry / laravel-site-analytics example snippets


namespace App\Analytics;

use BuiltByBerry\SiteAnalytics\Contracts\AnalyticsEventCatalog;

final class WebsiteEventCatalog implements AnalyticsEventCatalog
{
    public function contains(string $event): bool
    {
        return in_array($event, ['page_view', 'reader_signup_requested'], true);
    }

    public function label(string $event): string
    {
        return match ($event) {
            'page_view' => 'Page view',
            'reader_signup_requested' => 'Signup request',
            default => $event,
        };
    }

    public function allowedDimensions(string $event): array
    {
        return [];
    }
}

// Application provider
$this->app->bind(
    \BuiltByBerry\SiteAnalytics\Contracts\AnalyticsEventCatalog::class,
    \App\Analytics\WebsiteEventCatalog::class,
);

// After applying the application's public-route/privacy policy:
$context = \BuiltByBerry\SiteAnalytics\AnalyticsContext::fromRequest(
    $request,
    app(\BuiltByBerry\SiteAnalytics\Contracts\TrafficExclusion::class),
    ['path' => '/writing/example'],
);
app(\BuiltByBerry\SiteAnalytics\Contracts\AnalyticsRecorder::class)
    ->record('page_view', $context);

$report = app(\BuiltByBerry\SiteAnalytics\BuildAnalyticsReport::class)->handle(
    days: 30,
    conversionEvent: 'reader_signup_requested',
    pageGroups: ['writing' => ['/writing/'], 'books' => ['/books/']],
);

Schedule::command('model:prune', [
    '--model' => [\BuiltByBerry\SiteAnalytics\Models\SiteEvent::class],
])->daily();
sh
php artisan vendor:publish --tag=site-analytics-migrations
php artisan migrate