prokodo (GA4) icon

prokodo (GA4)

Query Google Analytics 4 (Data API) with a Service Account. Includes helpers for best post times and quick lists (landing pages, referrers).

Page Hours TopK (Helper)

AI-generated

Summary

Compute and return the top hourly time slots aggregated by a specified metric for page traffic matching given filters in a GA4 property, projecting these time buckets into upcoming ISO datetimes with optional recurrence and quota info.

Inputs

  • Property ID (required) — The numeric GA4 Property ID for which to analyze traffic (not the Measurement ID), e.g., 412345678.
  • Domain (hostName filter) — Optional hostName filter to isolate a particular domain (e.g., 'www.example.com'). Leave empty if only one domain is tracked.
  • Path Contains — Substring filter on pagePath to restrict to pages containing this path segment (default '/' for Page Hours).
  • Lookback (days) — Number of past days to aggregate data from, between 7 and 365 (default 60).
  • Top K Buckets — Number of top hour×weekday buckets to return, e.g., the 3 best time slots (default 3).
  • Horizon (days ahead) — Number of forthcoming days over which to map the best buckets to actual ISO datetimes (default 14).
  • Metric for Timing — The metric to aggregate by hour and weekday (Page Views, Sessions, or Engaged Sessions). Default is Page Views (screenPageViews).
  • Label Time Zone (IANA) — IANA time zone to use when labeling the suggested time slots (default 'Europe/Berlin').
  • Occurrence Mode — Whether to return only the next upcoming slot per top bucket (first) or expand weekly repeats within the horizon (expand).
  • Max Occurrences per Bucket — When expanding weekly occurrences, limits how many repeats per time bucket can be returned (1–50, default 9).
  • Return Property Quota — Include GA4 property quota information about remaining tokens to monitor API usage (default true).

Output shape

A single JSON object containing operation details, a sorted list of candidate ISO datetimes for the top hourly buckets, human-readable labels, bucket ranking and scores, metric used, lookback and horizon details, time zone, occurrence mode, top K count, and optionally propertyQuota info from GA4.

The operation filters GA4 data by domain and page path substring, aggregates specified metric by hour and weekday over the lookback days, identifies the top K buckets, maps them to upcoming datetimes within the horizon (optionally expanding repeats weekly), and returns detailed diagnostic data including the raw API response. Time zone affects label formatting. If 'Return Property Quota' is enabled, quota information is included.

Discussion