Listing stats
A marketplace gives each seller a stats page for their listing: a chart of the views per day over the past 30 days, the number of people who saw it, how the views compare with the 30 days before, and where they came from.
The pieces
Section titled “The pieces”| File | Role |
|---|---|
Listing.php |
The viewable model |
ListingStats.php |
Counts the views and caches each count |
ListingReport.php |
The numbers the page shows |
create_listings_table.php |
The listings table |
ListingStatsTest.php |
The behaviour below, as tests |
Views are recorded on the listing page the usual way; see the trending articles sample for a controller that does so with a cooldown. Build the report and render it:
$report = app(ListingStats::class)->for($listing);<p> {{ $report->totalViews() }} views by {{ $report->visitors }} people @if ($report->trend->percent !== null) ({{ sprintf('%+.1f', $report->trend->percent) }}% on the 30 days before) @endif</p>
@foreach ($report->views as $day) <div title="{{ $day->start->toFormattedDayDateString() }}" style="height: {{ $day->count }}px"></div>@endforeach
<ul> @foreach ($report->sources->all() as $source => $views) <li>{{ $source }}: {{ $views }}</li> @endforeach @if ($report->sources->other() > 0) <li>Other sites: {{ $report->sources->other() }}</li> @endif</ul>The sources come from the source dimension, so list it and add its column first:
'dimensions' => [ 'definitions' => [ 'source' => CyrildeWit\EloquentViewable\Dimensions\Source::class, ],],php artisan views:dimensionsphp artisan migrateDecisions
Section titled “Decisions”One query per chart, not one per day. countByInterval(Granularity::Day) groups the views by day in the database
and fills the days without views with zero, so the chart always has 30 bars and needs one query. Looping over the days
and calling count() for each would run 30.
Run the window from midnight to midnight. remember() builds its cache key from the period, and an absolute period
is identified by its timestamps. A window that starts 30 days before now gets a new key every second, so nothing would
ever be read back from the cache. Running from midnight 29 days ago to midnight tonight keeps the key the same all day
and gives a new one at midnight, which is exactly when the chart should gain a bar for the new day. A Period::since()
would chart the same days, but compare() needs both bounds to know how far to step back.
Let compare() find the window before. compare() counts the window and Period::previous(), the 30 days that
end where the window starts, and returns both with the delta and the percentage. The page needs no date arithmetic of
its own, and the percentage is null when the 30 days before had no views, because growth from nothing has none. The
comparison is not quite like for like: today so far is compared with a full day 30 days ago. Comparing up to the same
time of day would bring back the moving key.
Count the people once. A person who views the listing on three days is one of the people who saw it, but a
visitor on each of those days. visitors is a unique() count over the whole window; visitorsPerDay is the unique
series. Their numbers differ, so the page should not show the sum of the chart as the number of people.
Cache for ten minutes. The window ends with today, which keeps changing, so its counts are cached for ten minutes.
compare() remembers the previous window for as long, under a key of its own. Views are recorded at the current time,
so the previous window cannot change and could be cached until midnight, but only by building and counting it
separately again. One extra count every ten minutes is the price of letting compare() do it.
Name five sources and add up the rest. A listing shared around gets views from many small sites, and a seller
cares about the few that matter. countBy('source', limit: 5) keeps the five largest and counts the rest in
other(), in one grouped query. Views recorded before the dimension was listed have no source, which none() counts,
so the list never adds up to more than totalViews().
remember() does the caching. Unlike the ranking in the trending articles sample, count(),
countByInterval() and compare() go through remember(), so ListingStats needs no cache of its own.
Where to take it next
Section titled “Where to take it next”- Let the seller click a bar to see that day by the hour.
$day->period()is the bucket as a period, soviews($listing)->period($day->period())->countByInterval(Granularity::Hour)adds up to the bar. - Offer a year view with
Granularity::WeekorGranularity::Month. A finer granularity over a long range can produce more buckets thanmax_intervalsallows, which throwsInvalidIntervalbefore the query runs. - Narrow the chart to one source with
whereDimension('source', 'Google'), or break the views down bydevicetoo. - Keep the sources past the 30 days the views table holds by listing
sourceunderretention.rollups.dimensions.