Stats API (#679)
* WIP * Add ability to filter by anything * Add API keys * Add version to api endpoint * Fix API test route * Fix API tests * Allow 'date' parameter in '6mo' and '12mo' * Rename session -> visit in API filters * Filter expressions in the API * Implement filters in aggregate call * Add `compare` option to aggregate call * Add way to manage API keys through the UI * Authenticate with API key * Use API key in testspull/703/head
parent
60b99102a6
commit
5acb5b7039
@ -0,0 +1,45 @@
|
||||
defmodule Plausible.Auth.ApiKey do
|
||||
use Ecto.Schema
|
||||
import Ecto.Changeset
|
||||
|
||||
@required [:user_id, :key, :name]
|
||||
schema "api_keys" do
|
||||
field :name, :string
|
||||
field :key, :string, virtual: true
|
||||
field :key_hash, :string
|
||||
field :key_prefix, :string
|
||||
|
||||
belongs_to :user, Plausible.Auth.User
|
||||
|
||||
timestamps()
|
||||
end
|
||||
|
||||
def changeset(schema, attrs \\ %{}) do
|
||||
schema
|
||||
|> cast(attrs, @required)
|
||||
|> validate_required(@required)
|
||||
|> process_key
|
||||
end
|
||||
|
||||
def do_hash(key) do
|
||||
:crypto.hash(:sha256, [secret_key_base(), key])
|
||||
|> Base.encode16()
|
||||
|> String.downcase()
|
||||
end
|
||||
|
||||
def process_key(%{errors: [], changes: changes} = changeset) do
|
||||
prefix = binary_part(changes[:key], 0, 6)
|
||||
|
||||
change(changeset,
|
||||
key_hash: do_hash(changes[:key]),
|
||||
key_prefix: prefix
|
||||
)
|
||||
end
|
||||
|
||||
def process_key(changeset), do: changeset
|
||||
|
||||
defp secret_key_base() do
|
||||
Application.get_env(:plausible, PlausibleWeb.Endpoint)
|
||||
|> Keyword.fetch!(:secret_key_base)
|
||||
end
|
||||
end
|
||||
@ -0,0 +1,239 @@
|
||||
defmodule Plausible.Stats do
|
||||
use Plausible.ClickhouseRepo
|
||||
alias Plausible.Stats.Query
|
||||
@no_ref "Direct / None"
|
||||
|
||||
def timeseries(site, query) do
|
||||
steps = buckets(query)
|
||||
|
||||
groups =
|
||||
from(e in base_event_query(site, query),
|
||||
group_by: fragment("bucket"),
|
||||
order_by: fragment("bucket")
|
||||
)
|
||||
|> select_bucket(site, query)
|
||||
|> ClickhouseRepo.all()
|
||||
|> Enum.into(%{})
|
||||
|
||||
plot = Enum.map(steps, fn step -> groups[step] || 0 end)
|
||||
labels = Enum.map(steps, fn step -> Timex.format!(step, "{ISOdate}") end)
|
||||
|
||||
{plot, labels}
|
||||
end
|
||||
|
||||
@event_metrics ["visitors", "pageviews"]
|
||||
@session_metrics ["bounce_rate", "visit_duration"]
|
||||
|
||||
def aggregate(site, query, metrics) do
|
||||
event_metrics = Enum.filter(metrics, &(&1 in @event_metrics))
|
||||
event_task = Task.async(fn -> aggregate_events(site, query, event_metrics) end)
|
||||
session_metrics = Enum.filter(metrics, &(&1 in @session_metrics))
|
||||
session_task = Task.async(fn -> aggregate_sessions(site, query, session_metrics) end)
|
||||
|
||||
Map.merge(
|
||||
Task.await(event_task),
|
||||
Task.await(session_task)
|
||||
)
|
||||
|> Enum.map(fn {metric, value} ->
|
||||
{metric, %{value: value}}
|
||||
end)
|
||||
|> Enum.into(%{})
|
||||
end
|
||||
|
||||
defp aggregate_events(_, _, []), do: %{}
|
||||
|
||||
defp aggregate_events(site, query, metrics) do
|
||||
q = from(e in base_event_query(site, query), select: %{})
|
||||
|
||||
Enum.reduce(metrics, q, &select_event_metric/2)
|
||||
|> ClickhouseRepo.one()
|
||||
end
|
||||
|
||||
defp select_event_metric("pageviews", q) do
|
||||
from(e in q, select_merge: %{pageviews: fragment("count(*)")})
|
||||
end
|
||||
|
||||
defp select_event_metric("visitors", q) do
|
||||
from(e in q, select_merge: %{visitors: fragment("uniq(?)", e.user_id)})
|
||||
end
|
||||
|
||||
defp aggregate_sessions(_, _, []), do: %{}
|
||||
|
||||
defp aggregate_sessions(site, query, metrics) do
|
||||
q = from(e in query_sessions(site, query), select: %{})
|
||||
|
||||
Enum.reduce(metrics, q, &select_session_metric/2)
|
||||
|> ClickhouseRepo.one()
|
||||
end
|
||||
|
||||
defp select_session_metric("bounce_rate", q) do
|
||||
from(s in q,
|
||||
select_merge: %{bounce_rate: fragment("round(sum(is_bounce * sign) / sum(sign) * 100)")}
|
||||
)
|
||||
end
|
||||
|
||||
defp select_session_metric("visit_duration", q) do
|
||||
from(s in q, select_merge: %{visit_duration: fragment("round(avg(duration * sign))")})
|
||||
end
|
||||
|
||||
@session_props [
|
||||
"source",
|
||||
"referrer",
|
||||
"utm_medium",
|
||||
"utm_source",
|
||||
"utm_campaign",
|
||||
"device",
|
||||
"browser",
|
||||
"browser_version",
|
||||
"os",
|
||||
"os_version",
|
||||
"country"
|
||||
]
|
||||
|
||||
defp base_event_query(site, query) do
|
||||
events_q = query_events(site, query)
|
||||
|
||||
if Enum.any?(@session_props, &query.filters["visit:" <> &1]) do
|
||||
sessions_q =
|
||||
from(
|
||||
s in query_sessions(site, query),
|
||||
select: %{session_id: s.session_id}
|
||||
)
|
||||
|
||||
from(
|
||||
e in events_q,
|
||||
join: sq in subquery(sessions_q),
|
||||
on: e.session_id == sq.session_id
|
||||
)
|
||||
else
|
||||
events_q
|
||||
end
|
||||
end
|
||||
|
||||
defp query_events(site, query) do
|
||||
{first_datetime, last_datetime} = utc_boundaries(query, site.timezone)
|
||||
|
||||
q =
|
||||
from(e in "events",
|
||||
where: e.domain == ^site.domain,
|
||||
where: e.timestamp >= ^first_datetime and e.timestamp < ^last_datetime
|
||||
)
|
||||
|
||||
q =
|
||||
if query.filters["event:page"] do
|
||||
page = query.filters["event:page"]
|
||||
from(e in q, where: e.pathname == ^page)
|
||||
else
|
||||
q
|
||||
end
|
||||
|
||||
if query.filters["props"] do
|
||||
[{key, val}] = query.filters["props"] |> Enum.into([])
|
||||
|
||||
if val == "(none)" do
|
||||
from(
|
||||
e in q,
|
||||
where: fragment("not has(meta.key, ?)", ^key)
|
||||
)
|
||||
else
|
||||
from(
|
||||
e in q,
|
||||
inner_lateral_join: meta in fragment("meta as m"),
|
||||
as: :meta,
|
||||
where: meta.key == ^key and meta.value == ^val
|
||||
)
|
||||
end
|
||||
else
|
||||
q
|
||||
end
|
||||
end
|
||||
|
||||
defp query_sessions(site, query) do
|
||||
{first_datetime, last_datetime} = utc_boundaries(query, site.timezone)
|
||||
|
||||
sessions_q =
|
||||
from(s in "sessions",
|
||||
where: s.domain == ^site.domain,
|
||||
where: s.timestamp >= ^first_datetime and s.start < ^last_datetime
|
||||
)
|
||||
|
||||
sessions_q =
|
||||
if query.filters["event:page"] do
|
||||
page = query.filters["event:page"]
|
||||
from(e in sessions_q, where: e.entry_page == ^page)
|
||||
else
|
||||
sessions_q
|
||||
end
|
||||
|
||||
Enum.reduce(@session_props, sessions_q, fn prop_name, sessions_q ->
|
||||
prop_val = query.filters["visit:" <> prop_name]
|
||||
prop_name = if prop_name == "source", do: "referrer_source", else: prop_name
|
||||
prop_name = if prop_name == "device", do: "screen_size", else: prop_name
|
||||
prop_name = if prop_name == "os", do: "operating_system", else: prop_name
|
||||
prop_name = if prop_name == "os_version", do: "operating_system_version", else: prop_name
|
||||
prop_name = if prop_name == "country", do: "country_code", else: prop_name
|
||||
|
||||
prop_val =
|
||||
if prop_name == "referrer_source" && prop_val == @no_ref do
|
||||
""
|
||||
else
|
||||
prop_val
|
||||
end
|
||||
|
||||
if prop_val do
|
||||
where_target = [{String.to_existing_atom(prop_name), prop_val}]
|
||||
from(s in sessions_q, where: ^where_target)
|
||||
else
|
||||
sessions_q
|
||||
end
|
||||
end)
|
||||
end
|
||||
|
||||
defp buckets(%Query{interval: "month"} = query) do
|
||||
n_buckets = Timex.diff(query.date_range.last, query.date_range.first, :months)
|
||||
|
||||
Enum.map(n_buckets..0, fn shift ->
|
||||
query.date_range.last
|
||||
|> Timex.beginning_of_month()
|
||||
|> Timex.shift(months: -shift)
|
||||
end)
|
||||
end
|
||||
|
||||
defp buckets(%Query{interval: "date"} = query) do
|
||||
Enum.into(query.date_range, [])
|
||||
end
|
||||
|
||||
defp select_bucket(q, site, %Query{interval: "month"}) do
|
||||
from(
|
||||
e in q,
|
||||
select:
|
||||
{fragment("toStartOfMonth(toTimeZone(?, ?)) as bucket", e.timestamp, ^site.timezone),
|
||||
fragment("uniq(?)", e.user_id)}
|
||||
)
|
||||
end
|
||||
|
||||
defp select_bucket(q, site, %Query{interval: "date"}) do
|
||||
from(
|
||||
e in q,
|
||||
select:
|
||||
{fragment("toDate(toTimeZone(?, ?)) as bucket", e.timestamp, ^site.timezone),
|
||||
fragment("uniq(?)", e.user_id)}
|
||||
)
|
||||
end
|
||||
|
||||
defp utc_boundaries(%Query{date_range: date_range}, timezone) do
|
||||
{:ok, first} = NaiveDateTime.new(date_range.first, ~T[00:00:00])
|
||||
|
||||
first_datetime =
|
||||
Timex.to_datetime(first, timezone)
|
||||
|> Timex.Timezone.convert("UTC")
|
||||
|
||||
{:ok, last} = NaiveDateTime.new(date_range.last |> Timex.shift(days: 1), ~T[00:00:00])
|
||||
|
||||
last_datetime =
|
||||
Timex.to_datetime(last, timezone)
|
||||
|> Timex.Timezone.convert("UTC")
|
||||
|
||||
{first_datetime, last_datetime}
|
||||
end
|
||||
end
|
||||
@ -0,0 +1,79 @@
|
||||
defmodule PlausibleWeb.Api.ExternalStatsController do
|
||||
use PlausibleWeb, :controller
|
||||
use Plausible.Repo
|
||||
use Plug.ErrorHandler
|
||||
alias Plausible.Stats.Query
|
||||
|
||||
def realtime_visitors(conn, _params) do
|
||||
site = conn.assigns[:site]
|
||||
query = Query.from(site.timezone, %{"period" => "realtime"})
|
||||
json(conn, Plausible.Stats.Clickhouse.current_visitors(site, query))
|
||||
end
|
||||
|
||||
def aggregate(conn, params) do
|
||||
site = conn.assigns[:site]
|
||||
query = Query.from(site.timezone, params)
|
||||
|
||||
metrics =
|
||||
params["metrics"]
|
||||
|> String.split(",")
|
||||
|> Enum.map(&String.trim/1)
|
||||
|
||||
result =
|
||||
if params["compare"] == "previous_period" do
|
||||
prev_query = Query.shift_back(query)
|
||||
|
||||
[prev_result, curr_result] =
|
||||
Task.await_many([
|
||||
Task.async(fn -> Plausible.Stats.aggregate(site, prev_query, metrics) end),
|
||||
Task.async(fn -> Plausible.Stats.aggregate(site, query, metrics) end)
|
||||
])
|
||||
|
||||
Enum.map(curr_result, fn {metric, %{value: current_val}} ->
|
||||
%{value: prev_val} = prev_result[metric]
|
||||
|
||||
{metric,
|
||||
%{
|
||||
value: current_val,
|
||||
change: percent_change(prev_val, current_val)
|
||||
}}
|
||||
end)
|
||||
|> Enum.into(%{})
|
||||
else
|
||||
Plausible.Stats.aggregate(site, query, metrics)
|
||||
end
|
||||
|
||||
json(conn, result)
|
||||
end
|
||||
|
||||
def timeseries(conn, params) do
|
||||
site = conn.assigns[:site]
|
||||
query = Query.from(site.timezone, params)
|
||||
|
||||
{plot, labels} = Plausible.Stats.timeseries(site, query)
|
||||
|
||||
graph =
|
||||
Enum.zip(labels, plot)
|
||||
|> Enum.map(fn {label, val} -> %{date: label, value: val} end)
|
||||
|> Enum.into([])
|
||||
|
||||
json(conn, graph)
|
||||
end
|
||||
|
||||
def handle_errors(conn, %{kind: kind, reason: reason}) do
|
||||
json(conn, %{error: Exception.format_banner(kind, reason)})
|
||||
end
|
||||
|
||||
defp percent_change(old_count, new_count) do
|
||||
cond do
|
||||
old_count == 0 and new_count > 0 ->
|
||||
100
|
||||
|
||||
old_count == 0 and new_count == 0 ->
|
||||
0
|
||||
|
||||
true ->
|
||||
round((new_count - old_count) / old_count * 100)
|
||||
end
|
||||
end
|
||||
end
|
||||
@ -0,0 +1,46 @@
|
||||
defmodule PlausibleWeb.AuthorizeApiStatsPlug do
|
||||
import Plug.Conn
|
||||
use Plausible.Repo
|
||||
alias Plausible.Auth.ApiKey
|
||||
|
||||
def init(options) do
|
||||
options
|
||||
end
|
||||
|
||||
def call(conn, _opts) do
|
||||
site = Repo.get_by(Plausible.Site, domain: conn.params["site_id"])
|
||||
api_key = get_bearer_token(conn)
|
||||
|
||||
if !(site && api_key) do
|
||||
not_found(conn)
|
||||
else
|
||||
hashed_key = ApiKey.do_hash(api_key)
|
||||
found_key = Repo.get_by(ApiKey, key_hash: hashed_key)
|
||||
can_access = found_key && Plausible.Sites.is_owner?(found_key.user_id, site)
|
||||
|
||||
if !can_access do
|
||||
not_found(conn)
|
||||
else
|
||||
assign(conn, :site, site)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
defp get_bearer_token(conn) do
|
||||
authorization_header =
|
||||
Plug.Conn.get_req_header(conn, "authorization")
|
||||
|> List.first()
|
||||
|
||||
case authorization_header do
|
||||
"Bearer " <> token -> String.trim(token)
|
||||
_ -> nil
|
||||
end
|
||||
end
|
||||
|
||||
defp not_found(conn) do
|
||||
conn
|
||||
|> put_status(404)
|
||||
|> Phoenix.Controller.json(%{error: "Not found"})
|
||||
|> halt()
|
||||
end
|
||||
end
|
||||
@ -0,0 +1,21 @@
|
||||
<%= form_for @changeset, "/settings/api-keys", [class: "w-full max-w-md mx-auto bg-white dark:bg-gray-800 shadow-md rounded px-8 py-6 mt-8"], fn f -> %>
|
||||
<h1 class="text-xl font-black dark:text-gray-100">Create new API key</h1>
|
||||
<div class="my-4 mt-8">
|
||||
<%= label f, :name, class: "block text-sm font-medium text-gray-700" %>
|
||||
<div class="mt-1">
|
||||
<%= text_input f, :name, class: "shadow-sm focus:ring-indigo-500 focus:border-indigo-500 block w-full sm:text-sm border-gray-300 rounded-md" %>
|
||||
</div>
|
||||
<%= error_tag f, :name %>
|
||||
</div>
|
||||
<div class="my-4">
|
||||
<%= label f, :key, class: "block text-sm font-medium text-gray-700" %>
|
||||
<div class="relative mt-1">
|
||||
<%= text_input f, :key, id: "key-input", class: "shadow-sm bg-gray-50 focus:ring-indigo-500 focus:border-indigo-500 block w-full sm:text-sm border-gray-300 rounded-md pr-16", readonly: "readonly" %>
|
||||
<a onclick="var textarea = document.getElementById('key-input'); textarea.focus(); textarea.select(); document.execCommand('copy');" href="javascript:void(0)" class="absolute flex items-center text-xs font-medium text-indigo-600 no-underline hover:underline" style="top: 12px; right: 12px;">
|
||||
<svg class="pr-1 text-indigo-600" xmlns="http://www.w3.org/2000/svg" width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><rect x="9" y="9" width="13" height="13" rx="2" ry="2"></rect><path d="M5 15H4a2 2 0 0 1-2-2V4a2 2 0 0 1 2-2h9a2 2 0 0 1 2 2v1"></path></svg>COPY
|
||||
</a>
|
||||
<p class="mt-1 text-xs text-gray-500">Make sure to store the key in a secure place. Once created, we will not be able to show it again.</p>
|
||||
</div>
|
||||
</div>
|
||||
<%= submit "Continue", class: "button mt-4 w-full" %>
|
||||
<% end %>
|
||||
@ -0,0 +1,14 @@
|
||||
defmodule Plausible.Repo.Migrations.CreateApiKeys do
|
||||
use Ecto.Migration
|
||||
|
||||
def change do
|
||||
create table(:api_keys) do
|
||||
add :user_id, references(:users, on_delete: :delete_all), null: false
|
||||
add :name, :string, null: false
|
||||
add :key_prefix, :string, null: false
|
||||
add :key_hash, :string, null: false
|
||||
|
||||
timestamps()
|
||||
end
|
||||
end
|
||||
end
|
||||
@ -0,0 +1,567 @@
|
||||
defmodule PlausibleWeb.Api.ExternalStatsController.AggregateTest do
|
||||
use PlausibleWeb.ConnCase
|
||||
import Plausible.TestUtils
|
||||
|
||||
setup [:create_user, :create_new_site, :create_api_key, :use_api_key]
|
||||
@user_id 123
|
||||
|
||||
test "aggregates a single metric", %{conn: conn, site: site} do
|
||||
populate_stats([
|
||||
build(:pageview, user_id: @user_id, domain: site.domain, timestamp: ~N[2021-01-01 00:00:00]),
|
||||
build(:pageview, user_id: @user_id, domain: site.domain, timestamp: ~N[2021-01-01 00:25:00]),
|
||||
build(:pageview, domain: site.domain, timestamp: ~N[2021-01-01 00:00:00])
|
||||
])
|
||||
|
||||
conn =
|
||||
get(conn, "/api/v1/stats/aggregate", %{
|
||||
"site_id" => site.domain,
|
||||
"period" => "day",
|
||||
"date" => "2021-01-01",
|
||||
"metrics" => "pageviews"
|
||||
})
|
||||
|
||||
assert json_response(conn, 200) == %{
|
||||
"pageviews" => %{"value" => 3}
|
||||
}
|
||||
end
|
||||
|
||||
test "aggregates visitors, pageviews, bounce rate and visit duration", %{conn: conn, site: site} do
|
||||
populate_stats([
|
||||
build(:pageview, user_id: @user_id, domain: site.domain, timestamp: ~N[2021-01-01 00:00:00]),
|
||||
build(:pageview, user_id: @user_id, domain: site.domain, timestamp: ~N[2021-01-01 00:25:00]),
|
||||
build(:pageview, domain: site.domain, timestamp: ~N[2021-01-01 00:00:00])
|
||||
])
|
||||
|
||||
conn =
|
||||
get(conn, "/api/v1/stats/aggregate", %{
|
||||
"site_id" => site.domain,
|
||||
"period" => "day",
|
||||
"date" => "2021-01-01",
|
||||
"metrics" => "pageviews,visitors,bounce_rate,visit_duration"
|
||||
})
|
||||
|
||||
assert json_response(conn, 200) == %{
|
||||
"pageviews" => %{"value" => 3},
|
||||
"visitors" => %{"value" => 2},
|
||||
"bounce_rate" => %{"value" => 50},
|
||||
"visit_duration" => %{"value" => 750}
|
||||
}
|
||||
end
|
||||
|
||||
describe "comparisons" do
|
||||
test "compare period=day with previous period", %{conn: conn, site: site} do
|
||||
populate_stats([
|
||||
build(:pageview, domain: site.domain, timestamp: ~N[2020-12-31 00:00:00]),
|
||||
build(:pageview,
|
||||
user_id: @user_id,
|
||||
domain: site.domain,
|
||||
timestamp: ~N[2021-01-01 00:00:00]
|
||||
),
|
||||
build(:pageview,
|
||||
user_id: @user_id,
|
||||
domain: site.domain,
|
||||
timestamp: ~N[2021-01-01 00:25:00]
|
||||
),
|
||||
build(:pageview, domain: site.domain, timestamp: ~N[2021-01-01 00:00:00])
|
||||
])
|
||||
|
||||
conn =
|
||||
get(conn, "/api/v1/stats/aggregate", %{
|
||||
"site_id" => site.domain,
|
||||
"period" => "day",
|
||||
"date" => "2021-01-01",
|
||||
"metrics" => "pageviews,visitors,bounce_rate,visit_duration",
|
||||
"compare" => "previous_period"
|
||||
})
|
||||
|
||||
assert json_response(conn, 200) == %{
|
||||
"pageviews" => %{"value" => 3, "change" => 200},
|
||||
"visitors" => %{"value" => 2, "change" => 100},
|
||||
"bounce_rate" => %{"value" => 50, "change" => -50},
|
||||
"visit_duration" => %{"value" => 750, "change" => 100}
|
||||
}
|
||||
end
|
||||
end
|
||||
|
||||
describe "filters" do
|
||||
test "can filter by source", %{conn: conn, site: site} do
|
||||
populate_stats([
|
||||
build(:pageview,
|
||||
referrer_source: "Google",
|
||||
user_id: @user_id,
|
||||
domain: site.domain,
|
||||
timestamp: ~N[2021-01-01 00:00:00]
|
||||
),
|
||||
build(:pageview,
|
||||
user_id: @user_id,
|
||||
domain: site.domain,
|
||||
timestamp: ~N[2021-01-01 00:25:00]
|
||||
),
|
||||
build(:pageview, domain: site.domain, timestamp: ~N[2021-01-01 00:00:00])
|
||||
])
|
||||
|
||||
conn =
|
||||
get(conn, "/api/v1/stats/aggregate", %{
|
||||
"site_id" => site.domain,
|
||||
"period" => "day",
|
||||
"date" => "2021-01-01",
|
||||
"metrics" => "pageviews,visitors,bounce_rate,visit_duration",
|
||||
"filters" => "visit:source==Google"
|
||||
})
|
||||
|
||||
assert json_response(conn, 200) == %{
|
||||
"pageviews" => %{"value" => 2},
|
||||
"visitors" => %{"value" => 1},
|
||||
"bounce_rate" => %{"value" => 0},
|
||||
"visit_duration" => %{"value" => 1500}
|
||||
}
|
||||
end
|
||||
|
||||
test "can filter by no source/referrer", %{conn: conn, site: site} do
|
||||
populate_stats([
|
||||
build(:pageview,
|
||||
user_id: @user_id,
|
||||
domain: site.domain,
|
||||
timestamp: ~N[2021-01-01 00:00:00]
|
||||
),
|
||||
build(:pageview,
|
||||
user_id: @user_id,
|
||||
domain: site.domain,
|
||||
timestamp: ~N[2021-01-01 00:25:00]
|
||||
),
|
||||
build(:pageview,
|
||||
referrer_source: "Google",
|
||||
domain: site.domain,
|
||||
timestamp: ~N[2021-01-01 00:00:00]
|
||||
)
|
||||
])
|
||||
|
||||
conn =
|
||||
get(conn, "/api/v1/stats/aggregate", %{
|
||||
"site_id" => site.domain,
|
||||
"period" => "day",
|
||||
"date" => "2021-01-01",
|
||||
"metrics" => "pageviews,visitors,bounce_rate,visit_duration",
|
||||
"filters" => "visit:source==Direct / None"
|
||||
})
|
||||
|
||||
assert json_response(conn, 200) == %{
|
||||
"pageviews" => %{"value" => 2},
|
||||
"visitors" => %{"value" => 1},
|
||||
"bounce_rate" => %{"value" => 0},
|
||||
"visit_duration" => %{"value" => 1500}
|
||||
}
|
||||
end
|
||||
|
||||
test "can filter by referrer", %{conn: conn, site: site} do
|
||||
populate_stats([
|
||||
build(:pageview,
|
||||
referrer: "https://facebook.com",
|
||||
user_id: @user_id,
|
||||
domain: site.domain,
|
||||
timestamp: ~N[2021-01-01 00:00:00]
|
||||
),
|
||||
build(:pageview,
|
||||
user_id: @user_id,
|
||||
domain: site.domain,
|
||||
timestamp: ~N[2021-01-01 00:25:00]
|
||||
),
|
||||
build(:pageview, domain: site.domain, timestamp: ~N[2021-01-01 00:00:00])
|
||||
])
|
||||
|
||||
conn =
|
||||
get(conn, "/api/v1/stats/aggregate", %{
|
||||
"site_id" => site.domain,
|
||||
"period" => "day",
|
||||
"date" => "2021-01-01",
|
||||
"metrics" => "pageviews,visitors,bounce_rate,visit_duration",
|
||||
"filters" => "visit:referrer==https://facebook.com"
|
||||
})
|
||||
|
||||
assert json_response(conn, 200) == %{
|
||||
"pageviews" => %{"value" => 2},
|
||||
"visitors" => %{"value" => 1},
|
||||
"bounce_rate" => %{"value" => 0},
|
||||
"visit_duration" => %{"value" => 1500}
|
||||
}
|
||||
end
|
||||
|
||||
test "can filter by utm_medium", %{conn: conn, site: site} do
|
||||
populate_stats([
|
||||
build(:pageview,
|
||||
utm_medium: "social",
|
||||
user_id: @user_id,
|
||||
domain: site.domain,
|
||||
timestamp: ~N[2021-01-01 00:00:00]
|
||||
),
|
||||
build(:pageview,
|
||||
user_id: @user_id,
|
||||
domain: site.domain,
|
||||
timestamp: ~N[2021-01-01 00:25:00]
|
||||
),
|
||||
build(:pageview, domain: site.domain, timestamp: ~N[2021-01-01 00:00:00])
|
||||
])
|
||||
|
||||
conn =
|
||||
get(conn, "/api/v1/stats/aggregate", %{
|
||||
"site_id" => site.domain,
|
||||
"period" => "day",
|
||||
"date" => "2021-01-01",
|
||||
"metrics" => "pageviews,visitors,bounce_rate,visit_duration",
|
||||
"filters" => "visit:utm_medium==social"
|
||||
})
|
||||
|
||||
assert json_response(conn, 200) == %{
|
||||
"pageviews" => %{"value" => 2},
|
||||
"visitors" => %{"value" => 1},
|
||||
"bounce_rate" => %{"value" => 0},
|
||||
"visit_duration" => %{"value" => 1500}
|
||||
}
|
||||
end
|
||||
|
||||
test "can filter by utm_source", %{conn: conn, site: site} do
|
||||
populate_stats([
|
||||
build(:pageview,
|
||||
utm_source: "Twitter",
|
||||
user_id: @user_id,
|
||||
domain: site.domain,
|
||||
timestamp: ~N[2021-01-01 00:00:00]
|
||||
),
|
||||
build(:pageview,
|
||||
user_id: @user_id,
|
||||
domain: site.domain,
|
||||
timestamp: ~N[2021-01-01 00:25:00]
|
||||
),
|
||||
build(:pageview, domain: site.domain, timestamp: ~N[2021-01-01 00:00:00])
|
||||
])
|
||||
|
||||
conn =
|
||||
get(conn, "/api/v1/stats/aggregate", %{
|
||||
"site_id" => site.domain,
|
||||
"period" => "day",
|
||||
"date" => "2021-01-01",
|
||||
"metrics" => "pageviews,visitors,bounce_rate,visit_duration",
|
||||
"filters" => "visit:utm_source==Twitter"
|
||||
})
|
||||
|
||||
assert json_response(conn, 200) == %{
|
||||
"pageviews" => %{"value" => 2},
|
||||
"visitors" => %{"value" => 1},
|
||||
"bounce_rate" => %{"value" => 0},
|
||||
"visit_duration" => %{"value" => 1500}
|
||||
}
|
||||
end
|
||||
|
||||
test "can filter by utm_campaign", %{conn: conn, site: site} do
|
||||
populate_stats([
|
||||
build(:pageview,
|
||||
utm_campaign: "profile",
|
||||
user_id: @user_id,
|
||||
domain: site.domain,
|
||||
timestamp: ~N[2021-01-01 00:00:00]
|
||||
),
|
||||
build(:pageview,
|
||||
user_id: @user_id,
|
||||
domain: site.domain,
|
||||
timestamp: ~N[2021-01-01 00:25:00]
|
||||
),
|
||||
build(:pageview, domain: site.domain, timestamp: ~N[2021-01-01 00:00:00])
|
||||
])
|
||||
|
||||
conn =
|
||||
get(conn, "/api/v1/stats/aggregate", %{
|
||||
"site_id" => site.domain,
|
||||
"period" => "day",
|
||||
"date" => "2021-01-01",
|
||||
"metrics" => "pageviews,visitors,bounce_rate,visit_duration",
|
||||
"filters" => "visit:utm_campaign==profile"
|
||||
})
|
||||
|
||||
assert json_response(conn, 200) == %{
|
||||
"pageviews" => %{"value" => 2},
|
||||
"visitors" => %{"value" => 1},
|
||||
"bounce_rate" => %{"value" => 0},
|
||||
"visit_duration" => %{"value" => 1500}
|
||||
}
|
||||
end
|
||||
|
||||
test "can filter by device type", %{conn: conn, site: site} do
|
||||
populate_stats([
|
||||
build(:pageview,
|
||||
screen_size: "Desktop",
|
||||
user_id: @user_id,
|
||||
domain: site.domain,
|
||||
timestamp: ~N[2021-01-01 00:00:00]
|
||||
),
|
||||
build(:pageview,
|
||||
user_id: @user_id,
|
||||
domain: site.domain,
|
||||
timestamp: ~N[2021-01-01 00:25:00]
|
||||
),
|
||||
build(:pageview, domain: site.domain, timestamp: ~N[2021-01-01 00:00:00])
|
||||
])
|
||||
|
||||
conn =
|
||||
get(conn, "/api/v1/stats/aggregate", %{
|
||||
"site_id" => site.domain,
|
||||
"period" => "day",
|
||||
"date" => "2021-01-01",
|
||||
"metrics" => "pageviews,visitors,bounce_rate,visit_duration",
|
||||
"filters" => "visit:device==Desktop"
|
||||
})
|
||||
|
||||
assert json_response(conn, 200) == %{
|
||||
"pageviews" => %{"value" => 2},
|
||||
"visitors" => %{"value" => 1},
|
||||
"bounce_rate" => %{"value" => 0},
|
||||
"visit_duration" => %{"value" => 1500}
|
||||
}
|
||||
end
|
||||
|
||||
test "can filter by browser", %{conn: conn, site: site} do
|
||||
populate_stats([
|
||||
build(:pageview,
|
||||
browser: "Chrome",
|
||||
user_id: @user_id,
|
||||
domain: site.domain,
|
||||
timestamp: ~N[2021-01-01 00:00:00]
|
||||
),
|
||||
build(:pageview,
|
||||
user_id: @user_id,
|
||||
domain: site.domain,
|
||||
timestamp: ~N[2021-01-01 00:25:00]
|
||||
),
|
||||
build(:pageview, domain: site.domain, timestamp: ~N[2021-01-01 00:00:00])
|
||||
])
|
||||
|
||||
conn =
|
||||
get(conn, "/api/v1/stats/aggregate", %{
|
||||
"site_id" => site.domain,
|
||||
"period" => "day",
|
||||
"date" => "2021-01-01",
|
||||
"metrics" => "pageviews,visitors,bounce_rate,visit_duration",
|
||||
"filters" => "visit:browser==Chrome"
|
||||
})
|
||||
|
||||
assert json_response(conn, 200) == %{
|
||||
"pageviews" => %{"value" => 2},
|
||||
"visitors" => %{"value" => 1},
|
||||
"bounce_rate" => %{"value" => 0},
|
||||
"visit_duration" => %{"value" => 1500}
|
||||
}
|
||||
end
|
||||
|
||||
test "can filter by browser version", %{conn: conn, site: site} do
|
||||
populate_stats([
|
||||
build(:pageview,
|
||||
browser: "Chrome",
|
||||
browser_version: "56",
|
||||
user_id: @user_id,
|
||||
domain: site.domain,
|
||||
timestamp: ~N[2021-01-01 00:00:00]
|
||||
),
|
||||
build(:pageview,
|
||||
user_id: @user_id,
|
||||
domain: site.domain,
|
||||
timestamp: ~N[2021-01-01 00:25:00]
|
||||
),
|
||||
build(:pageview, domain: site.domain, timestamp: ~N[2021-01-01 00:00:00])
|
||||
])
|
||||
|
||||
conn =
|
||||
get(conn, "/api/v1/stats/aggregate", %{
|
||||
"site_id" => site.domain,
|
||||
"period" => "day",
|
||||
"date" => "2021-01-01",
|
||||
"metrics" => "pageviews,visitors,bounce_rate,visit_duration",
|
||||
"filters" => "visit:browser_version==56"
|
||||
})
|
||||
|
||||
assert json_response(conn, 200) == %{
|
||||
"pageviews" => %{"value" => 2},
|
||||
"visitors" => %{"value" => 1},
|
||||
"bounce_rate" => %{"value" => 0},
|
||||
"visit_duration" => %{"value" => 1500}
|
||||
}
|
||||
end
|
||||
|
||||
test "can filter by operating system", %{conn: conn, site: site} do
|
||||
populate_stats([
|
||||
build(:pageview,
|
||||
operating_system: "Mac",
|
||||
user_id: @user_id,
|
||||
domain: site.domain,
|
||||
timestamp: ~N[2021-01-01 00:00:00]
|
||||
),
|
||||
build(:pageview,
|
||||
user_id: @user_id,
|
||||
domain: site.domain,
|
||||
timestamp: ~N[2021-01-01 00:25:00]
|
||||
),
|
||||
build(:pageview, domain: site.domain, timestamp: ~N[2021-01-01 00:00:00])
|
||||
])
|
||||
|
||||
conn =
|
||||
get(conn, "/api/v1/stats/aggregate", %{
|
||||
"site_id" => site.domain,
|
||||
"period" => "day",
|
||||
"date" => "2021-01-01",
|
||||
"metrics" => "pageviews,visitors,bounce_rate,visit_duration",
|
||||
"filters" => "visit:os==Mac"
|
||||
})
|
||||
|
||||
assert json_response(conn, 200) == %{
|
||||
"pageviews" => %{"value" => 2},
|
||||
"visitors" => %{"value" => 1},
|
||||
"bounce_rate" => %{"value" => 0},
|
||||
"visit_duration" => %{"value" => 1500}
|
||||
}
|
||||
end
|
||||
|
||||
test "can filter by operating system version", %{conn: conn, site: site} do
|
||||
populate_stats([
|
||||
build(:pageview,
|
||||
operating_system_version: "10.5",
|
||||
user_id: @user_id,
|
||||
domain: site.domain,
|
||||
timestamp: ~N[2021-01-01 00:00:00]
|
||||
),
|
||||
build(:pageview,
|
||||
user_id: @user_id,
|
||||
domain: site.domain,
|
||||
timestamp: ~N[2021-01-01 00:25:00]
|
||||
),
|
||||
build(:pageview, domain: site.domain, timestamp: ~N[2021-01-01 00:00:00])
|
||||
])
|
||||
|
||||
conn =
|
||||
get(conn, "/api/v1/stats/aggregate", %{
|
||||
"site_id" => site.domain,
|
||||
"period" => "day",
|
||||
"date" => "2021-01-01",
|
||||
"metrics" => "pageviews,visitors,bounce_rate,visit_duration",
|
||||
"filters" => "visit:os_version==10.5"
|
||||
})
|
||||
|
||||
assert json_response(conn, 200) == %{
|
||||
"pageviews" => %{"value" => 2},
|
||||
"visitors" => %{"value" => 1},
|
||||
"bounce_rate" => %{"value" => 0},
|
||||
"visit_duration" => %{"value" => 1500}
|
||||
}
|
||||
end
|
||||
|
||||
test "can filter by country", %{conn: conn, site: site} do
|
||||
populate_stats([
|
||||
build(:pageview,
|
||||
country_code: "EE",
|
||||
user_id: @user_id,
|
||||
domain: site.domain,
|
||||
timestamp: ~N[2021-01-01 00:00:00]
|
||||
),
|
||||
build(:pageview,
|
||||
user_id: @user_id,
|
||||
domain: site.domain,
|
||||
timestamp: ~N[2021-01-01 00:25:00]
|
||||
),
|
||||
build(:pageview, domain: site.domain, timestamp: ~N[2021-01-01 00:00:00])
|
||||
])
|
||||
|
||||
conn =
|
||||
get(conn, "/api/v1/stats/aggregate", %{
|
||||
"site_id" => site.domain,
|
||||
"period" => "day",
|
||||
"date" => "2021-01-01",
|
||||
"metrics" => "pageviews,visitors,bounce_rate,visit_duration",
|
||||
"filters" => "visit:country==EE"
|
||||
})
|
||||
|
||||
assert json_response(conn, 200) == %{
|
||||
"pageviews" => %{"value" => 2},
|
||||
"visitors" => %{"value" => 1},
|
||||
"bounce_rate" => %{"value" => 0},
|
||||
"visit_duration" => %{"value" => 1500}
|
||||
}
|
||||
end
|
||||
|
||||
test "when filtering by page, session metrics treat is like entry_page", %{
|
||||
conn: conn,
|
||||
site: site
|
||||
} do
|
||||
populate_stats([
|
||||
build(:pageview,
|
||||
pathname: "/blogpost",
|
||||
user_id: @user_id,
|
||||
domain: site.domain,
|
||||
timestamp: ~N[2021-01-01 00:00:00]
|
||||
),
|
||||
build(:pageview,
|
||||
user_id: @user_id,
|
||||
domain: site.domain,
|
||||
timestamp: ~N[2021-01-01 00:25:00]
|
||||
),
|
||||
build(:pageview, domain: site.domain, timestamp: ~N[2021-01-01 00:00:00]),
|
||||
build(:pageview,
|
||||
pathname: "/blogpost",
|
||||
domain: site.domain,
|
||||
timestamp: ~N[2021-01-01 00:00:00]
|
||||
)
|
||||
])
|
||||
|
||||
conn =
|
||||
get(conn, "/api/v1/stats/aggregate", %{
|
||||
"site_id" => site.domain,
|
||||
"period" => "day",
|
||||
"date" => "2021-01-01",
|
||||
"metrics" => "pageviews,visitors,bounce_rate,visit_duration",
|
||||
"filters" => "event:page==/blogpost"
|
||||
})
|
||||
|
||||
assert json_response(conn, 200) == %{
|
||||
"pageviews" => %{"value" => 2},
|
||||
"visitors" => %{"value" => 2},
|
||||
"bounce_rate" => %{"value" => 50},
|
||||
"visit_duration" => %{"value" => 750}
|
||||
}
|
||||
end
|
||||
|
||||
test "combining filters", %{conn: conn, site: site} do
|
||||
populate_stats([
|
||||
build(:pageview,
|
||||
pathname: "/blogpost",
|
||||
country_code: "EE",
|
||||
user_id: @user_id,
|
||||
domain: site.domain,
|
||||
timestamp: ~N[2021-01-01 00:00:00]
|
||||
),
|
||||
build(:pageview,
|
||||
user_id: @user_id,
|
||||
domain: site.domain,
|
||||
timestamp: ~N[2021-01-01 00:25:00]
|
||||
),
|
||||
build(:pageview, domain: site.domain, timestamp: ~N[2021-01-01 00:00:00]),
|
||||
build(:pageview,
|
||||
pathname: "/blogpost",
|
||||
domain: site.domain,
|
||||
timestamp: ~N[2021-01-01 00:00:00]
|
||||
)
|
||||
])
|
||||
|
||||
conn =
|
||||
get(conn, "/api/v1/stats/aggregate", %{
|
||||
"site_id" => site.domain,
|
||||
"period" => "day",
|
||||
"date" => "2021-01-01",
|
||||
"metrics" => "pageviews,visitors,bounce_rate,visit_duration",
|
||||
"filters" => "event:page==/blogpost;visit:country==EE"
|
||||
})
|
||||
|
||||
assert json_response(conn, 200) == %{
|
||||
"pageviews" => %{"value" => 1},
|
||||
"visitors" => %{"value" => 1},
|
||||
"bounce_rate" => %{"value" => 0},
|
||||
"visit_duration" => %{"value" => 1500}
|
||||
}
|
||||
end
|
||||
end
|
||||
end
|
||||
@ -0,0 +1,397 @@
|
||||
defmodule PlausibleWeb.Api.ExternalStatsController.TimeseriesTest do
|
||||
use PlausibleWeb.ConnCase
|
||||
import Plausible.TestUtils
|
||||
|
||||
setup [:create_user, :create_new_site, :create_api_key, :use_api_key]
|
||||
|
||||
test "shows last 6 months of visitors", %{conn: conn, site: site} do
|
||||
populate_stats([
|
||||
build(:pageview, domain: site.domain, timestamp: ~N[2020-12-31 00:00:00]),
|
||||
build(:pageview, domain: site.domain, timestamp: ~N[2021-01-01 00:00:00]),
|
||||
build(:pageview, domain: site.domain, timestamp: ~N[2021-01-01 00:00:00])
|
||||
])
|
||||
|
||||
conn =
|
||||
get(conn, "/api/v1/stats/timeseries", %{
|
||||
"site_id" => site.domain,
|
||||
"period" => "6mo",
|
||||
"date" => "2021-01-01"
|
||||
})
|
||||
|
||||
assert json_response(conn, 200) == [
|
||||
%{"date" => "2020-08-01", "value" => 0},
|
||||
%{"date" => "2020-09-01", "value" => 0},
|
||||
%{"date" => "2020-10-01", "value" => 0},
|
||||
%{"date" => "2020-11-01", "value" => 0},
|
||||
%{"date" => "2020-12-01", "value" => 1},
|
||||
%{"date" => "2021-01-01", "value" => 2}
|
||||
]
|
||||
end
|
||||
|
||||
test "shows last 12 months of visitors", %{conn: conn, site: site} do
|
||||
populate_stats([
|
||||
build(:pageview, domain: site.domain, timestamp: ~N[2020-02-01 00:00:00]),
|
||||
build(:pageview, domain: site.domain, timestamp: ~N[2020-12-31 00:00:00]),
|
||||
build(:pageview, domain: site.domain, timestamp: ~N[2021-01-01 00:00:00]),
|
||||
build(:pageview, domain: site.domain, timestamp: ~N[2021-01-01 00:00:00])
|
||||
])
|
||||
|
||||
conn =
|
||||
get(conn, "/api/v1/stats/timeseries", %{
|
||||
"site_id" => site.domain,
|
||||
"period" => "12mo",
|
||||
"date" => "2021-01-01"
|
||||
})
|
||||
|
||||
assert json_response(conn, 200) == [
|
||||
%{"date" => "2020-02-01", "value" => 1},
|
||||
%{"date" => "2020-03-01", "value" => 0},
|
||||
%{"date" => "2020-04-01", "value" => 0},
|
||||
%{"date" => "2020-05-01", "value" => 0},
|
||||
%{"date" => "2020-06-01", "value" => 0},
|
||||
%{"date" => "2020-07-01", "value" => 0},
|
||||
%{"date" => "2020-08-01", "value" => 0},
|
||||
%{"date" => "2020-09-01", "value" => 0},
|
||||
%{"date" => "2020-10-01", "value" => 0},
|
||||
%{"date" => "2020-11-01", "value" => 0},
|
||||
%{"date" => "2020-12-01", "value" => 1},
|
||||
%{"date" => "2021-01-01", "value" => 2}
|
||||
]
|
||||
end
|
||||
|
||||
test "shows last 12 months of visitors with interval daily", %{conn: conn, site: site} do
|
||||
populate_stats([
|
||||
build(:pageview, domain: site.domain, timestamp: ~N[2020-02-01 00:00:00]),
|
||||
build(:pageview, domain: site.domain, timestamp: ~N[2020-12-31 00:00:00]),
|
||||
build(:pageview, domain: site.domain, timestamp: ~N[2021-01-01 00:00:00]),
|
||||
build(:pageview, domain: site.domain, timestamp: ~N[2021-01-01 00:00:00])
|
||||
])
|
||||
|
||||
conn =
|
||||
get(conn, "/api/v1/stats/timeseries", %{
|
||||
"site_id" => site.domain,
|
||||
"period" => "12mo",
|
||||
"interval" => "date"
|
||||
})
|
||||
|
||||
res = json_response(conn, 200)
|
||||
assert Enum.count(res) in [365, 366]
|
||||
end
|
||||
|
||||
test "shows a custom range with daily interval", %{conn: conn, site: site} do
|
||||
populate_stats([
|
||||
build(:pageview, domain: site.domain, timestamp: ~N[2021-01-01 00:00:00]),
|
||||
build(:pageview, domain: site.domain, timestamp: ~N[2021-01-01 00:00:00]),
|
||||
build(:pageview, domain: site.domain, timestamp: ~N[2021-01-02 00:00:00])
|
||||
])
|
||||
|
||||
conn =
|
||||
get(conn, "/api/v1/stats/timeseries", %{
|
||||
"site_id" => site.domain,
|
||||
"period" => "custom",
|
||||
"from" => "2021-01-01",
|
||||
"to" => "2021-01-02"
|
||||
})
|
||||
|
||||
assert json_response(conn, 200) == [
|
||||
%{"date" => "2021-01-01", "value" => 2},
|
||||
%{"date" => "2021-01-02", "value" => 1}
|
||||
]
|
||||
end
|
||||
|
||||
test "shows a custom range with monthly interval", %{conn: conn, site: site} do
|
||||
populate_stats([
|
||||
build(:pageview, domain: site.domain, timestamp: ~N[2020-12-01 00:00:00]),
|
||||
build(:pageview, domain: site.domain, timestamp: ~N[2021-01-01 00:00:00]),
|
||||
build(:pageview, domain: site.domain, timestamp: ~N[2021-01-02 00:00:00])
|
||||
])
|
||||
|
||||
conn =
|
||||
get(conn, "/api/v1/stats/timeseries", %{
|
||||
"site_id" => site.domain,
|
||||
"period" => "custom",
|
||||
"from" => "2020-12-01",
|
||||
"to" => "2021-01-02",
|
||||
"interval" => "month"
|
||||
})
|
||||
|
||||
assert json_response(conn, 200) == [
|
||||
%{"date" => "2020-12-01", "value" => 1},
|
||||
%{"date" => "2021-01-01", "value" => 2}
|
||||
]
|
||||
end
|
||||
|
||||
describe "filters" do
|
||||
test "can filter by source", %{conn: conn, site: site} do
|
||||
populate_stats([
|
||||
build(:pageview,
|
||||
referrer_source: "Google",
|
||||
domain: site.domain,
|
||||
timestamp: ~N[2021-01-01 00:00:00]
|
||||
),
|
||||
build(:pageview, domain: site.domain, timestamp: ~N[2021-01-01 00:00:00])
|
||||
])
|
||||
|
||||
conn =
|
||||
get(conn, "/api/v1/stats/timeseries", %{
|
||||
"site_id" => site.domain,
|
||||
"period" => "month",
|
||||
"date" => "2021-01-01",
|
||||
"filters" => "visit:source==Google"
|
||||
})
|
||||
|
||||
res = json_response(conn, 200)
|
||||
assert List.first(res) == %{"date" => "2021-01-01", "value" => 1}
|
||||
end
|
||||
|
||||
test "can filter by no source/referrer", %{conn: conn, site: site} do
|
||||
populate_stats([
|
||||
build(:pageview, domain: site.domain, timestamp: ~N[2021-01-01 00:00:00]),
|
||||
build(:pageview,
|
||||
referrer_source: "Google",
|
||||
domain: site.domain,
|
||||
timestamp: ~N[2021-01-01 00:00:00]
|
||||
)
|
||||
])
|
||||
|
||||
conn =
|
||||
get(conn, "/api/v1/stats/timeseries", %{
|
||||
"site_id" => site.domain,
|
||||
"period" => "month",
|
||||
"date" => "2021-01-01",
|
||||
"filters" => "visit:source==Direct / None"
|
||||
})
|
||||
|
||||
res = json_response(conn, 200)
|
||||
assert List.first(res) == %{"date" => "2021-01-01", "value" => 1}
|
||||
end
|
||||
|
||||
test "can filter by referrer", %{conn: conn, site: site} do
|
||||
populate_stats([
|
||||
build(:pageview,
|
||||
referrer: "https://facebook.com",
|
||||
domain: site.domain,
|
||||
timestamp: ~N[2021-01-01 00:00:00]
|
||||
),
|
||||
build(:pageview, domain: site.domain, timestamp: ~N[2021-01-01 00:00:00])
|
||||
])
|
||||
|
||||
conn =
|
||||
get(conn, "/api/v1/stats/timeseries", %{
|
||||
"site_id" => site.domain,
|
||||
"period" => "month",
|
||||
"date" => "2021-01-01",
|
||||
"filters" => "visit:referrer==https://facebook.com"
|
||||
})
|
||||
|
||||
res = json_response(conn, 200)
|
||||
assert List.first(res) == %{"date" => "2021-01-01", "value" => 1}
|
||||
end
|
||||
|
||||
test "can filter by utm_medium", %{conn: conn, site: site} do
|
||||
populate_stats([
|
||||
build(:pageview,
|
||||
utm_medium: "social",
|
||||
domain: site.domain,
|
||||
timestamp: ~N[2021-01-01 00:00:00]
|
||||
),
|
||||
build(:pageview, domain: site.domain, timestamp: ~N[2021-01-01 00:00:00])
|
||||
])
|
||||
|
||||
conn =
|
||||
get(conn, "/api/v1/stats/timeseries", %{
|
||||
"site_id" => site.domain,
|
||||
"period" => "month",
|
||||
"date" => "2021-01-01",
|
||||
"filters" => "visit:utm_medium==social"
|
||||
})
|
||||
|
||||
res = json_response(conn, 200)
|
||||
assert List.first(res) == %{"date" => "2021-01-01", "value" => 1}
|
||||
end
|
||||
|
||||
test "can filter by utm_source", %{conn: conn, site: site} do
|
||||
populate_stats([
|
||||
build(:pageview,
|
||||
utm_source: "Twitter",
|
||||
domain: site.domain,
|
||||
timestamp: ~N[2021-01-01 00:00:00]
|
||||
),
|
||||
build(:pageview, domain: site.domain, timestamp: ~N[2021-01-01 00:00:00])
|
||||
])
|
||||
|
||||
conn =
|
||||
get(conn, "/api/v1/stats/timeseries", %{
|
||||
"site_id" => site.domain,
|
||||
"period" => "month",
|
||||
"date" => "2021-01-01",
|
||||
"filters" => "visit:utm_source==Twitter"
|
||||
})
|
||||
|
||||
res = json_response(conn, 200)
|
||||
assert List.first(res) == %{"date" => "2021-01-01", "value" => 1}
|
||||
end
|
||||
|
||||
test "can filter by utm_campaign", %{conn: conn, site: site} do
|
||||
populate_stats([
|
||||
build(:pageview,
|
||||
utm_campaign: "profile",
|
||||
domain: site.domain,
|
||||
timestamp: ~N[2021-01-01 00:00:00]
|
||||
),
|
||||
build(:pageview, domain: site.domain, timestamp: ~N[2021-01-01 00:00:00])
|
||||
])
|
||||
|
||||
conn =
|
||||
get(conn, "/api/v1/stats/timeseries", %{
|
||||
"site_id" => site.domain,
|
||||
"period" => "month",
|
||||
"date" => "2021-01-01",
|
||||
"filters" => "visit:utm_campaign==profile"
|
||||
})
|
||||
|
||||
res = json_response(conn, 200)
|
||||
assert List.first(res) == %{"date" => "2021-01-01", "value" => 1}
|
||||
end
|
||||
|
||||
test "can filter by device type", %{conn: conn, site: site} do
|
||||
populate_stats([
|
||||
build(:pageview,
|
||||
screen_size: "Desktop",
|
||||
domain: site.domain,
|
||||
timestamp: ~N[2021-01-01 00:00:00]
|
||||
),
|
||||
build(:pageview, domain: site.domain, timestamp: ~N[2021-01-01 00:00:00])
|
||||
])
|
||||
|
||||
conn =
|
||||
get(conn, "/api/v1/stats/timeseries", %{
|
||||
"site_id" => site.domain,
|
||||
"period" => "month",
|
||||
"date" => "2021-01-01",
|
||||
"filters" => "visit:device==Desktop"
|
||||
})
|
||||
|
||||
res = json_response(conn, 200)
|
||||
assert List.first(res) == %{"date" => "2021-01-01", "value" => 1}
|
||||
end
|
||||
|
||||
test "can filter by browser", %{conn: conn, site: site} do
|
||||
populate_stats([
|
||||
build(:pageview,
|
||||
browser: "Chrome",
|
||||
browser_version: "56.1",
|
||||
domain: site.domain,
|
||||
timestamp: ~N[2021-01-01 00:00:00]
|
||||
),
|
||||
build(:pageview,
|
||||
browser: "Chrome",
|
||||
browser_version: "55",
|
||||
domain: site.domain,
|
||||
timestamp: ~N[2021-01-01 00:00:00]
|
||||
),
|
||||
build(:pageview, domain: site.domain, timestamp: ~N[2021-01-01 00:00:00])
|
||||
])
|
||||
|
||||
conn =
|
||||
get(conn, "/api/v1/stats/timeseries", %{
|
||||
"site_id" => site.domain,
|
||||
"period" => "month",
|
||||
"date" => "2021-01-01",
|
||||
"filters" => "visit:browser==Chrome;visit:browser_version==56.1"
|
||||
})
|
||||
|
||||
res = json_response(conn, 200)
|
||||
assert List.first(res) == %{"date" => "2021-01-01", "value" => 1}
|
||||
end
|
||||
|
||||
test "can filter by operating system", %{conn: conn, site: site} do
|
||||
populate_stats([
|
||||
build(:pageview,
|
||||
operating_system: "Mac",
|
||||
operating_system_version: "10.5",
|
||||
domain: site.domain,
|
||||
timestamp: ~N[2021-01-01 00:00:00]
|
||||
),
|
||||
build(:pageview,
|
||||
operating_system: "Something else",
|
||||
operating_system_version: "10.5",
|
||||
domain: site.domain,
|
||||
timestamp: ~N[2021-01-01 00:00:00]
|
||||
),
|
||||
build(:pageview,
|
||||
operating_system: "Mac",
|
||||
operating_system_version: "10.4",
|
||||
domain: site.domain,
|
||||
timestamp: ~N[2021-01-01 00:00:00]
|
||||
),
|
||||
build(:pageview, domain: site.domain, timestamp: ~N[2021-01-01 00:00:00])
|
||||
])
|
||||
|
||||
conn =
|
||||
get(conn, "/api/v1/stats/timeseries", %{
|
||||
"site_id" => site.domain,
|
||||
"period" => "month",
|
||||
"date" => "2021-01-01",
|
||||
"filters" => "visit:os == Mac;visit:os_version==10.5"
|
||||
})
|
||||
|
||||
res = json_response(conn, 200)
|
||||
assert List.first(res) == %{"date" => "2021-01-01", "value" => 1}
|
||||
end
|
||||
|
||||
test "can filter by country", %{conn: conn, site: site} do
|
||||
populate_stats([
|
||||
build(:pageview,
|
||||
country_code: "EE",
|
||||
operating_system_version: "10.5",
|
||||
domain: site.domain,
|
||||
timestamp: ~N[2021-01-01 00:00:00]
|
||||
),
|
||||
build(:pageview, domain: site.domain, timestamp: ~N[2021-01-01 00:00:00])
|
||||
])
|
||||
|
||||
conn =
|
||||
get(conn, "/api/v1/stats/timeseries", %{
|
||||
"site_id" => site.domain,
|
||||
"period" => "month",
|
||||
"date" => "2021-01-01",
|
||||
"filters" => "visit:country==EE"
|
||||
})
|
||||
|
||||
res = json_response(conn, 200)
|
||||
assert List.first(res) == %{"date" => "2021-01-01", "value" => 1}
|
||||
end
|
||||
|
||||
test "can filter by page", %{conn: conn, site: site} do
|
||||
populate_stats([
|
||||
build(:pageview,
|
||||
pathname: "/hello",
|
||||
domain: site.domain,
|
||||
timestamp: ~N[2021-01-01 00:00:00]
|
||||
),
|
||||
build(:pageview,
|
||||
pathname: "/hello",
|
||||
domain: site.domain,
|
||||
timestamp: ~N[2021-01-01 00:00:00]
|
||||
),
|
||||
build(:pageview,
|
||||
pathname: "/goobye",
|
||||
domain: site.domain,
|
||||
timestamp: ~N[2021-01-01 00:00:00]
|
||||
)
|
||||
])
|
||||
|
||||
conn =
|
||||
get(conn, "/api/v1/stats/timeseries", %{
|
||||
"site_id" => site.domain,
|
||||
"period" => "month",
|
||||
"date" => "2021-01-01",
|
||||
"filters" => "event:page==/hello"
|
||||
})
|
||||
|
||||
res = json_response(conn, 200)
|
||||
assert List.first(res) == %{"date" => "2021-01-01", "value" => 2}
|
||||
end
|
||||
end
|
||||
end
|
||||
Loading…
Reference in New Issue