class Matomo::Page

Attributes

hits[RW]
path[RW]
visits[RW]

Public Class Methods

get_subtables() click to toggle source
# File lib/matomo.rb, line 81
def self.get_subtables
  # Get a mapping from resource paths to Matomo page view subtable ids
  resp = Matomo.get({
    method: "Actions.getPageUrls",
    filter_limit: 50,
  })
  if resp.response.code == "200"
    @subtables = resp.map{|x| [x["label"], x["idsubdatatable"]]}.to_h
  else
    @subtables = {}
  end
end
group_by_day(path, **args) click to toggle source

Return format eg: { “2018-10-03”: <Matomo::Page> }

# File lib/matomo.rb, line 69
def self.group_by_day(path, **args)
  params = {
    method: "Actions.getPageUrls",
    segment: "pageUrl==#{Matomo.tracked_site_url}#{path}",
    period: "day"
  }.merge(Matomo.date_range_params(args[:start_date], args[:end_date]))

  resp = Matomo.get(params)
  return {} if resp.response.code != "200"
  resp.transform_values{ |v| new(path, v.first || {}) }
end
new(path, params = {}) click to toggle source
# File lib/matomo.rb, line 41
def initialize(path, params = {})
  @path = path
  @label = params["label"]
  @hits = params["nb_hits"] || 0
  @visits = params["nb_visits"] || 0
end
under_path(base_path, **args) click to toggle source
# File lib/matomo.rb, line 53
def self.under_path(base_path, **args)
  get_subtables unless @subtables
  # Remove leading and trailing slashes to match Matomo label format.
  base_path = base_path.gsub(/^\/|\/$/, "")
  return [] unless @subtables[base_path]

  resp = Matomo.get({
    method: "Actions.getPageUrls",
    idSubtable: @subtables[base_path]
  })
  return [] if resp.response.code != "200"
  resp.map{ |x| new("/#{base_path}#{x["label"]}", x) }
end

Public Instance Methods

label() click to toggle source
# File lib/matomo.rb, line 48
def label
  return nil unless @label
  @label.sub(/^\//, "").sub(/\?.*$/, "")
end