PhoenixKitWebAnalytics.Web.Filters (PhoenixKitWebAnalytics v0.6.1)

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.

Two switches widen what is counted: flagged shows the traffic the settings leave out (the site's own people and networks — see PhoenixKitWebAnalytics.TrafficFlags), bots shows bot traffic. Either one reads the period from raw events.

Summary

Functions

Reads period, site, path, flagged and bots 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). Traffic flagged with a bit of excluded_flags (the settings' mask by default) isn't counted.

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

Whether the filter reads its whole period from raw events because it counts in traffic the rollups don't hold — bots, or a TrafficFlags bit (by the report's switch or a setting).

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, path, flagged and bots 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, excluded_flags \\ nil)

@spec online(String.t() | nil, non_neg_integer() | 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). Traffic flagged with a bit of excluded_flags (the settings' mask by default) isn't counted.

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.

raw?(filter)

Whether the filter reads its whole period from raw events because it counts in traffic the rollups don't hold — bots, or a TrafficFlags bit (by the report's switch or a setting).

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.