PHP code example of hkyss / beacon

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

    

hkyss / beacon example snippets


use hkyss\Beacon\Throttle;
use hkyss\Beacon\Counter\PdoCounter;

new Throttle($config);                        // files in the system temp directory
new Throttle($config, new PdoCounter($pdo));  // one count for the whole site

echo Beacon::inject($html, $nonce);

use hkyss\Beacon\Storage\PdoStorage;

$storage = new PdoStorage($pdo);
$storage->migrate();

Schedule::command('beacon:prune')->daily();

// core/custom/config/app/providers/BeaconServiceProvider.php
 return \hkyss\Beacon\Integration\Evolution\BeaconServiceProvider::class;

\hkyss\Beacon\Beacon::migrate();

use hkyss\Beacon\{Agent, Config, Ingest, Throttle};
use hkyss\Beacon\Counter\PdoCounter;
use hkyss\Beacon\Integration\Psr15\BeaconMiddleware;
use hkyss\Beacon\Storage\PdoStorage;

$config = Config::fromEnv();
$storage = new PdoStorage($pdo);

$pipeline->pipe(new BeaconMiddleware(
    new Ingest($config, $storage),
    new Agent($config),
    $streamFactory,
    $responseFactory,
    $config->endpoint(),
    new Throttle($config, new PdoCounter($pdo)),
));

use hkyss\Beacon\{Beacon, Config};
use hkyss\Beacon\Storage\PdoStorage;

Beacon::boot(Config::fromEnv(), new PdoStorage($pdo));
Beacon::migrate(); // once, not on every request

// beacon.php — whatever BEACON_ENDPOINT points at
Beacon::receive();
http_response_code(204);

// every page
echo Beacon::inject($html);

use hkyss\Beacon\{Period, Report};

$report = new Report($storage, 'shop');

$traffic = $report->traffic(Period::days(30));
// ['views' => 12480, 'visitors' => 5133, 'timeline' => [...],
//  'pages' => [...], 'referrers' => [...], 'devices' => [...], ...]

$vitals = $report->vitals(Period::days(7));
// [['metric' => 'lcp', 'samples' => 4021, 'p50' => 1180.0,
//   'p75' => 2410.0, 'p95' => 5200.0, 'rating' => 'good'], ...]

$worst = $report->slowestPages(Period::days(7));
// the pages people land on most, worst rating first
bash
php artisan vendor:publish --tag=beacon-config
php artisan beacon:migrate