From c4ac50a65cde1e3f4ab1032baf23ec850b8da75b Mon Sep 17 00:00:00 2001 From: Adrian Gruntkowski Date: Tue, 28 May 2024 22:18:58 +0200 Subject: [PATCH] Make views per visit metric work for import entry and exit pages --- lib/plausible/stats/imported/imported.ex | 26 ++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/lib/plausible/stats/imported/imported.ex b/lib/plausible/stats/imported/imported.ex index 3a6704a4c80f2..0c16bf397dafa 100644 --- a/lib/plausible/stats/imported/imported.ex +++ b/lib/plausible/stats/imported/imported.ex @@ -587,6 +587,32 @@ defmodule Plausible.Stats.Imported do |> select_imported_metrics(rest) end + defp select_imported_metrics( + %Ecto.Query{from: %Ecto.Query.FromExpr{source: {"imported_entry_pages", _}}} = q, + [:views_per_visit | rest] + ) do + q + |> where([i], i.pageviews > 0) + |> select_merge([i], %{ + pageviews: sum(i.pageviews), + __internal_visits: sum(i.entrances) + }) + |> select_imported_metrics(rest) + end + + defp select_imported_metrics( + %Ecto.Query{from: %Ecto.Query.FromExpr{source: {"imported_exit_pages", _}}} = q, + [:views_per_visit | rest] + ) do + q + |> where([i], i.pageviews > 0) + |> select_merge([i], %{ + pageviews: sum(i.pageviews), + __internal_visits: sum(i.exits) + }) + |> select_imported_metrics(rest) + end + defp select_imported_metrics(q, [:views_per_visit | rest]) do q |> where([i], i.pageviews > 0)