View Source Plausible.Stats.Comparisons (Plausible v0.0.1)

This module provides functions for comparing query periods.

It allows you to compare a given period with a previous period or with the same period from the previous year. For example, you can compare this month's main graph with last month or with the same month from last year.

Summary

Functions

Generates a comparison query based on the source query and comparison mode.

Types

@type mode() :: String.t() | nil

Functions

Link to this function

compare(site, source_query, mode, opts \\ [])

View Source
@spec compare(Plausible.Site.t(), Plausible.Stats.Query.t(), mode(), [option()]) ::
  {:ok, Plausible.Stats.Query.t()}
  | {:error, :not_supported}
  | {:error, :invalid_dates}

Generates a comparison query based on the source query and comparison mode.

The mode parameter specifies the type of comparison and can be one of the following:

  • "previous_period" - shifts back the query by the same number of days the source query has.

  • "year_over_year" - shifts back the query by 1 year.

  • "custom" - compares the query using a custom date range. See options for more details.

The comparison query returned by the function has its end date restricted to the current day. This can be overriden by the now option, described below.

Options

  • :now - a NaiveDateTime struct with the current date and time. This is optional and used for testing purposes.

  • :from - a ISO-8601 date string used when mode is "custom".

  • :to - a ISO-8601 date string used when mode is "custom". Must be after from.

  • :match_day_of_week? - determines whether the comparison query should be adjusted to match the day of the week of the source query. When this option is set to true, the comparison query is shifted to start on the same day of the week as the source query, rather than on the exact same date. For example, if the source query starts on Sunday, January 1st, 2023 and the year_over_year comparison query is configured to match_day_of_week?, it will be shifted to start on Sunday, January 2nd, 2022 instead of January 1st. Defaults to false.