module Matomo

Constants

VERSION

Public Class Methods

top_pages_url() click to toggle source
# File lib/matomo.rb, line 107
def self.top_pages_url
  base_portal_url+"&category=General_Actions&subcategory=General_Pages"
end
top_referrers_url() click to toggle source
# File lib/matomo.rb, line 111
def self.top_referrers_url
  base_portal_url+"&category=Referrers_Referrers&subcategory=Referrers_WidgetGetAll"
end
visits_graph_url() click to toggle source
# File lib/matomo.rb, line 95
def self.visits_graph_url
  base_url + "?" + default_api_params.merge({
    method: "ImageGraph.get",
    apiModule: "VisitsSummary",
    apiAction: "get",
    token_auth: "anonymous",
    width: 800,
    height: 400,
    period: "day",
  }).to_query
end

Private Class Methods

base_portal_url() click to toggle source

## Gnarly base url for finding pages on the Matomo web portal

# File lib/matomo.rb, line 132
def self.base_portal_url
  "#{base_url}/index.php?module=CoreHome&action=index&"\
  "idSite=#{site_id}&period=#{default_api_params[:period]}&date=#{default_api_params[:date]}&updated=1#?"\
  "idSite=#{site_id}&period=#{default_api_params[:period]}&date=#{default_api_params[:date]}"\
end
base_url() click to toggle source
# File lib/matomo.rb, line 122
def self.base_url
  ENV["MATOMO_BASE_URL"] || "https://anon-stats.eff.org"
end
date_range_params(start_date, end_date) click to toggle source
# File lib/matomo.rb, line 149
def self.date_range_params(start_date, end_date)
  date_format = "%Y-%m-%d"
  end_date = end_date || Date.today
  start_date = start_date || end_date - 30.days
  {
    date: start_date.strftime(date_format) + "," + end_date.strftime(date_format)
  }
end
default_api_params() click to toggle source
# File lib/matomo.rb, line 138
def self.default_api_params
  {
    module: "API",
    idSite: site_id,
    format: "JSON",
    period: "range",
    date: "last30",
    filter_limit: 5
  }
end
get(params) click to toggle source
# File lib/matomo.rb, line 117
def self.get(params)
  url = base_url + "?" + default_api_params.merge(params).to_query
  HTTParty.get(url)
end
site_id() click to toggle source
# File lib/matomo.rb, line 126
def self.site_id
  ENV["MATOMO_SITE_ID"]
end
tracked_site_url() click to toggle source

The full url of a tracked page is sometimes required by the API, eg. when scoping by page. We can load the base url from the environment or by making an API call.

# File lib/matomo.rb, line 162
def self.tracked_site_url
  return ENV["MATOMO_TRACKED_SITE_URL"] if ENV["MATOMO_TRACKED_SITE_URL"]
  return @tracked_site_url if @tracked_site_url
  resp = get({
    method: "SitesManager.getSiteUrlsFromId"
  })
  return nil if resp.response.code != "200" || resp.length == 0
  @tracked_site_url = resp[0]
end