PhoenixKitWebAnalytics.Web.Filters (PhoenixKitWebAnalytics v0.5.0)

Copy Markdown View Source

Shared period/site filter handling for the report LiveViews.

Every report page answers the same two questions — when and which site — and keeps both in the URL so a filtered view can be bookmarked, shared, and survives a refresh. A third, optional path narrows every report to one page (set by clicking a path on the Pages report). This module owns that plumbing so the five pages don't each reimplement it.

Summary

Functions

Reads period, site and path from the URL params and assigns :filter, :period, :site, :path, :sites, and :bucket.

Applies the current filter's params to another report path.

How many people are on the site now: the larger of the pages open over a LiveView socket (exact) and the visitors with a hit in the last five minutes (covers pages without a LiveView).

Builds the URL to patch to when the filter form changes.

Re-reads the online count for the page's current site filter.

The current filter as URL params, for links between report pages that should keep the selected window.

Assigns :online and, once connected, refreshes it every 30 seconds by sending the LiveView :refresh_online — which it hands back to refresh_online/1. Every report page shows the same badge this way.

Functions

assign_filter(socket, params)

Reads period, site and path from the URL params and assigns :filter, :period, :site, :path, :sites, and :bucket.

Unknown period values fall back to the default rather than erroring — these come from a URL anyone can edit.

online(site)

@spec online(String.t() | nil) :: non_neg_integer()

How many people are on the site now: the larger of the pages open over a LiveView socket (exact) and the visitors with a hit in the last five minutes (covers pages without a LiveView).

patch_to(path, params)

@spec patch_to(String.t(), map()) :: String.t()

Builds the URL to patch to when the filter form changes.

Empty values are dropped, so the default view has a clean URL.

refresh_online(socket)

Re-reads the online count for the page's current site filter.

to_params(filter)

@spec to_params(PhoenixKitWebAnalytics.Reports.filter()) :: map()

The current filter as URL params, for links between report pages that should keep the selected window.

track_online(socket)

Assigns :online and, once connected, refreshes it every 30 seconds by sending the LiveView :refresh_online — which it hands back to refresh_online/1. Every report page shows the same badge this way.