PHP code example of sergeybruhin / laravel-analytics

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

    

sergeybruhin / laravel-analytics example snippets


// config/queue.php
'connections' => [
    // ...
    'redis_analytics' => [
        'driver' => 'redis',
        'connection' => env('REDIS_QUEUE_CONNECTION', 'default'),
        'queue' => env('ANALYTICS_QUEUE', 'analytics'),
        'retry_after' => (int) env('REDIS_ANALYTICS_RETRY_AFTER', 60),
        'block_for' => null,
        'after_commit' => false,
    ],
],

'supervisor-analytics' => [
    'connection' => 'redis_analytics',
    'queue' => ['analytics'],
    'balance' => 'auto',
    'minProcesses' => 1,
    'maxProcesses' => 1,
    'memory' => 128,
    'tries' => 2,
    'timeout' => 30,
],

// bootstrap/app.php
->withMiddleware(function (Middleware $middleware) {
    $middleware->validateCsrfTokens(except: [
        'api/analytics/collect',
    ]);
})

use SergeyBruhin\Analytics\Analytics;

app(Analytics::class)->track(
    visitorId: $order->visitor_id,
    site: config('laravel-analytics.site'),
    name: 'purchase',
    properties: ['order_id' => $order->id],
    value: (float) $order->grand_total,
    dedupKey: "order-{$order->id}",
);

// routes/console.php
Schedule::command('analytics:prune')->daily();

use SergeyBruhin\Analytics\Drivers\Postgres\Nova\AnalyticsVisit;
use SergeyBruhin\Analytics\Drivers\Postgres\Metrics\PageviewsTrend;
use SergeyBruhin\Analytics\Drivers\Postgres\Metrics\TopTrafficSources;

app(\SergeyBruhin\Analytics\Drivers\DriverManager::class)
    ->extend('clickhouse', fn ($app) => new ClickHouseStore(/* ... */));
bash
php artisan vendor:publish --tag=analytics-config
php artisan vendor:publish --tag=analytics-migrations
php artisan migrate --database=analytics
bash
php artisan analytics:install
bash
php artisan analytics:prune