Class: Forem::Analytics
- Inherits:
-
APIResource
- Object
- ForemObject
- APIResource
- Forem::Analytics
- Defined in:
- lib/forem/resources/analytics.rb
Overview
Provides access to analytics data for the authenticated user's content.
All analytics endpoints require authentication. They return analytics for
the calling user (or an organization, when :organization_id is passed).
The Analytics resource exposes four read-only reporting endpoints: cumulative totals, day-by-day historical data, yesterday's aggregates, and traffic referrer breakdowns.
Response shapes
The Forem analytics endpoints don't all return the same kind of object, so this resource preserves each endpoint's natural shape rather than forcing them into a uniform list:
- Analytics.totals — single nested-stats object
- Analytics.historical —
Hashkeyed by"YYYY-MM-DD"date string - Analytics.past_day —
Hashkeyed by"YYYY-MM-DD"(typically 1–2 entries) - Analytics.referrers —
Arrayof referrer objects (extracted fromdomains)
Constant Summary collapse
- OBJECT_NAME =
"analytics"- RESOURCE_PATH =
"/api/analytics"
Instance Attribute Summary
Attributes inherited from ForemObject
Class Method Summary collapse
-
.historical(params = {}, opts = {}) ⇒ Hash{String => Forem::ForemObject}
Return day-by-day historical analytics for a given date range.
-
.past_day(params = {}, opts = {}) ⇒ Hash{String => Forem::ForemObject}
Return aggregated analytics for the previous calendar day(s).
-
.referrers(params = {}, opts = {}) ⇒ Array<Forem::ForemObject>
Return the breakdown of traffic referrers for the authenticated user's content.
-
.totals(params = {}, opts = {}) ⇒ Forem::ForemObject
Return cumulative analytics totals for the authenticated user (or org).
Methods inherited from APIResource
#refresh, resource_path, #resource_url
Methods inherited from ForemObject
#==, #[], #[]=, construct_from, cursor_list, #initialize, #inspect, #method_missing, paginated_list, #respond_to_missing?, #to_hash
Methods included from Forem::APIOperations::Request
Constructor Details
This class inherits a constructor from Forem::ForemObject
Dynamic Method Handling
This class handles dynamic methods through the method_missing method in the class Forem::ForemObject
Class Method Details
.historical(params = {}, opts = {}) ⇒ Hash{String => Forem::ForemObject}
Return day-by-day historical analytics for a given date range.
The response is a Hash keyed by the calendar date (+"YYYY-MM-DD"+).
Each value is a stats object with the same nested shape as totals.
83 84 85 86 87 |
# File 'lib/forem/resources/analytics.rb', line 83 def self.historical(params = {}, opts = {}) requestor = opts[:requestor] resp = request(:get, "/api/analytics/historical", params, opts) grouped_by_day(resp.parsed_body, requestor) end |
.past_day(params = {}, opts = {}) ⇒ Hash{String => Forem::ForemObject}
Return aggregated analytics for the previous calendar day(s).
The response shape mirrors historical: a Hash keyed by date.
Forem typically returns one or two entries (yesterday plus today's
partial), so callers usually want either the most recent date or
iteration with #each.
106 107 108 109 110 |
# File 'lib/forem/resources/analytics.rb', line 106 def self.past_day(params = {}, opts = {}) requestor = opts[:requestor] resp = request(:get, "/api/analytics/past_day", params, opts) grouped_by_day(resp.parsed_body, requestor) end |
.referrers(params = {}, opts = {}) ⇒ Array<Forem::ForemObject>
Return the breakdown of traffic referrers for the authenticated user's content.
The endpoint wraps the data in a {"domains" => [...]} envelope. This
method unwraps that envelope and returns the inner array directly so
callers can iterate without an extra hop.
130 131 132 133 134 135 136 |
# File 'lib/forem/resources/analytics.rb', line 130 def self.referrers(params = {}, opts = {}) requestor = opts[:requestor] resp = request(:get, "/api/analytics/referrers", params, opts) body = resp.parsed_body domains = body.is_a?(Hash) ? (body["domains"] || []) : [] domains.map { |item| Forem::ForemObject.construct_from(item, requestor: requestor) } end |
.totals(params = {}, opts = {}) ⇒ Forem::ForemObject
Return cumulative analytics totals for the authenticated user (or org).
The response groups totals into nested buckets — comments, follows,
reactions, and page_views — each with its own sub-fields. To read
a count, navigate one level deeper than you might expect (e.g.
totals.reactions.total, not totals.reactions).
59 60 61 62 63 |
# File 'lib/forem/resources/analytics.rb', line 59 def self.totals(params = {}, opts = {}) requestor = opts[:requestor] resp = request(:get, "/api/analytics/totals", params, opts) Forem::ForemObject.construct_from(resp.parsed_body, requestor: requestor) end |