Dotazy na štatistiky
Odporúčaný spôsob čítania štatistík je fluentný builder. Začnite ho cez for($site) — alebo query() pre nakonfigurovaný predvolený web — pridajte voľby a ukončite terminálnym volaním, ktoré vráti typovaný výsledok, takže JSON už nemusíte parsovať ručne. Enumy dávajú typovo bezpečné metriky, obdobia, intervaly a vlastnosti; bežné reťazce fungujú všade tiež.
use RoundlyConsulting\Plausible\Facades\Plausible;
Plausible::stats()->for('my-site.com'); // a fluent PendingStatsQuery for one site
Plausible::stats()->query(); // the same, for the configured PLAUSIBLE_SITENávštevníci v reálnom čase
$live = Plausible::stats()->for('my-site.com')->realtime(); // int — visitors on the site right nowSúhrn (aggregate)
aggregate() vráti AggregateResult. metric() dá hodnotu metriky (0, ak chýba), change() rozdiel oproti predchádzajúcemu obdobiu pri zapnutom compare() a has() to, či je metrika v odpovedi:
use RoundlyConsulting\Plausible\Enums\Metric;
// Aggregate metrics for a custom range, compared with the previous period
$result = Plausible::stats()
->for('my-site.com')
->metrics(Metric::Visitors, Metric::Pageviews, Metric::BounceRate)
->between(now()->subDays(7), now()) // auto-sets period=custom
->compare()
->aggregate(); // -> AggregateResult
$visitors = $result->metric(Metric::Visitors); // typed int
$change = $result->change(Metric::Visitors); // comparison delta (or null)
$result->has(Metric::BounceRate); // bool
$result->results; // ['visitors' => ['value' => 2001, 'change' => 30], …]
$raw = $result->response(); // escape hatch to the ResponseČasový rad (timeseries)
timeseries() vráti TimeseriesResult s objektmi TimeseriesPoint — každý má dátum ako Carbon a svoje metriky:
use RoundlyConsulting\Plausible\Enums\Interval;
use RoundlyConsulting\Plausible\Enums\Metric;
use RoundlyConsulting\Plausible\Enums\Period;
// Day-by-day trend you can chart
$points = Plausible::stats()
->for('my-site.com')
->metrics(Metric::Visitors, Metric::Pageviews)
->period(Period::ThirtyDays)
->interval(Interval::Date) // minute, hour, date, week or month
->timeseries() // -> TimeseriesResult
->points(); // list<TimeseriesPoint>
foreach ($points as $point) {
$point->date; // Carbon
$point->metric(Metric::Visitors); // int|float
}Rozpad (breakdown)
breakdown() potrebuje vlastnosť — odovzdajte ju terminálnemu volaniu alebo ju nastavte skôr cez property(). Riadky zachovávajú kľúče z odpovede Plausible, takže dimenziu čítate cez get() (page pre event:page) a metriky cez metric():
use RoundlyConsulting\Plausible\Enums\Metric;
use RoundlyConsulting\Plausible\Enums\Property;
// Top pages, ranked and paginated
$rows = Plausible::stats()
->for('my-site.com')
->metrics(Metric::Visitors)
->last30Days()
->perPage(20) // default 100
->breakdown(Property::EventPage) // -> BreakdownResult
->rows(); // list<BreakdownRow>
foreach ($rows as $row) {
$row->get('page'); // "/pricing" — the dimension, by its response key
$row->metric(Metric::Visitors); // int|float
}
// Or set the property first
Plausible::stats()->for('my-site.com')->property(Property::VisitSource)->breakdown();Obdobia a rozsahy dátumov
Pre bežné rozsahy použite pomenované skratky namiesto enumu, prípadne zadajte ľubovoľné Period alebo explicitný rozsah:
use RoundlyConsulting\Plausible\Enums\Period;
Plausible::stats()->for('my-site.com')->today()->aggregate();
Plausible::stats()->for('my-site.com')->yesterday()->aggregate(); // custom range
Plausible::stats()->for('my-site.com')->last7Days()->timeseries();
Plausible::stats()->for('my-site.com')->last30Days()->aggregate();
Plausible::stats()->for('my-site.com')->last6Months()->timeseries();
Plausible::stats()->for('my-site.com')->last12Months()->timeseries();
Plausible::stats()->for('my-site.com')->thisMonth()->aggregate();
Plausible::stats()->for('my-site.com')->lastMonth()->aggregate(); // custom range
Plausible::stats()->for('my-site.com')->thisYear()->aggregate();
Plausible::stats()->for('my-site.com')->allTime()->aggregate();
// Any Period, or an explicit range
Plausible::stats()->for('my-site.com')->period(Period::SixMonths)->aggregate();
Plausible::stats()->for('my-site.com')->between(now()->subDays(14), now())->aggregate();
Plausible::stats()->for('my-site.com')->since(now()->startOfYear())->aggregate(); // year to date — through today| Skratka | Odošle |
|---|---|
today() | period=day |
yesterday() | vlastný rozsah — včerajšok od začiatku do konca dňa |
last7Days() | period=7d |
last30Days() | period=30d |
last6Months() | period=6mo |
last12Months() | period=12mo |
thisMonth() | period=month |
lastMonth() | vlastný rozsah — predchádzajúci kalendárny mesiac |
thisYear() | period=year |
allTime() | period=all |
between(Carbon $start, Carbon $end) aj since(Carbon $start) za vás nastavia period=custom s uzavretým rozsahom: terminály v1 pošlú date=start,end a run() pošle date_range: [start, end]. since() siaha po dnešok v časovom pásme aplikácie, určený v momente volania. Nenastavené voľby majú predvolené hodnoty buildera: metrika visitors, obdobie 30d, interval date, strana 1 a 100 riadkov na stranu.
Prejavte lásku k open source
Tento balík je zadarmo pod licenciou MIT. Ak vám šetrí čas, jednorazový príspevok alebo členstvo na Patreone nám pomôže ho ďalej udržiavať, testovať a dokumentovať.
Ďalšie spôsoby podpory vrátane kryptomienOdoslaním daru súhlasíte s našimi podmienkami prijímania darov.
Chcete to zabudovať do svojho produktu?
Naše balíky integrujeme do zákazkových Laravel a AI riešení. Napíšte nám, na čom pracujete, a ozveme sa do 48 hodín.