Skip to content

Repository files navigation

Usage Statistics Client

Reusable PHP client for applications that submit opt-in usage statistics using Usage Statistics Protocol v1.

Requirements

  • PHP 8.2 or newer
  • Composer

Install

composer require librecodecoop/usage-statistics-client

Example

use LibreCode\UsageStatistics\Client;
use LibreCode\UsageStatistics\ConsentState;
use LibreCode\UsageStatistics\Endpoint;
use LibreCode\UsageStatistics\InstallationId;
use LibreCode\UsageStatistics\Metric;
use LibreCode\UsageStatistics\Report;
use LibreCode\UsageStatistics\ReportingPeriod;
use LibreCode\UsageStatistics\Transport\StreamTransport;

$report = new Report(
    application: 'libresign',
    installationId: (string) InstallationId::derive('libresign', $localInstallationIdentifier),
    schemaVersion: 1,
    period: ReportingPeriod::monthContaining(new DateTimeImmutable()),
    metrics: [
        Metric::string('environment', 'version', '12.0.0'),
        Metric::integer('usage', 'requests_completed', 72),
    ],
);

$client = new Client(
    new StreamTransport(),
    new Endpoint('https://statistics.example/api/v1/reports'),
);

$client->submit($report, ConsentState::Enabled);

Applications define their own metrics, consent UI and persistence, endpoint, scheduling and logging policy. The client validates and serializes Protocol v1 reports and submits them when consent is enabled.

Documentation

License

AGPL-3.0-or-later. See COPYING and LICENSES/AGPL-3.0-or-later.txt.

About

Reusable PHP client for Usage Statistics Protocol v1, with typed reports, consent gating, privacy-preserving installation IDs and HTTP transport.

Topics

Resources

Contributing

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Used by

Contributors

Languages