File size: 6,820 Bytes
8da2481 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 | defmodule Plausible.Stats.Imported.Base do
@moduledoc """
A module for building the base of an imported stats query
"""
import Ecto.Query
alias Plausible.Imported
alias Plausible.Stats.Query
import Plausible.Stats.Filters,
only: [dimensions_used_in_filters: 1, dimensions_used_in_filters: 2]
@property_to_table_mappings %{
"visit:source" => "imported_sources",
"visit:channel" => "imported_sources",
"visit:referrer" => "imported_sources",
"visit:utm_source" => "imported_sources",
"visit:utm_medium" => "imported_sources",
"visit:utm_campaign" => "imported_sources",
"visit:utm_term" => "imported_sources",
"visit:utm_content" => "imported_sources",
"visit:entry_page" => "imported_entry_pages",
"visit:exit_page" => "imported_exit_pages",
"visit:country" => "imported_locations",
"visit:region" => "imported_locations",
"visit:city" => "imported_locations",
"visit:country_name" => "imported_locations",
"visit:region_name" => "imported_locations",
"visit:city_name" => "imported_locations",
"visit:device" => "imported_devices",
"visit:browser" => "imported_browsers",
"visit:browser_version" => "imported_browsers",
"visit:os" => "imported_operating_systems",
"visit:os_version" => "imported_operating_systems",
"event:page" => "imported_pages",
"event:name" => "imported_custom_events",
# NOTE: these dimensions can be only filtered by
"visit:screen" => "imported_devices",
"event:hostname" => "imported_pages",
# NOTE: These dimensions are only used in group by
"time:month" => "imported_visitors",
"time:week" => "imported_visitors",
"time:day" => "imported_visitors",
"time:hour" => "imported_visitors"
}
@queriable_time_dimensions ["time:month", "time:week", "time:day", "time:hour"]
@imported_custom_props Imported.imported_custom_props()
def property_to_table_mappings(), do: @property_to_table_mappings
def query_imported(site, query) do
[table] = decide_tables(query)
query_imported(table, site, query)
end
def query_imported(table, site, query) do
import_ids = Imported.complete_import_ids(site)
# Assumption: dates in imported table are in user-local timezone.
%{first: date_from, last: date_to} = Query.date_range(query)
from(i in table,
where: i.site_id == ^site.id,
where: i.import_id in ^import_ids,
where: i.date >= ^date_from,
where: i.date <= ^date_to,
where: ^Plausible.Stats.Imported.SQL.WhereBuilder.build(query),
select: %{}
)
end
def decide_tables(query) do
behavioral_filters = dimensions_used_in_filters(query.filters, behavioral_filters: :only)
cond do
# Behavioral filters cannot be emulated via aggregated imported stats
length(behavioral_filters) > 0 ->
[]
custom_prop_query?(query) ->
do_decide_custom_prop_table(query)
true ->
do_decide_tables(query)
end
end
defp custom_prop_query?(query) do
dimensions_used_in_filters(query.filters)
|> Enum.concat(query.dimensions)
|> Enum.any?(&(&1 in @imported_custom_props))
end
defp do_decide_custom_prop_table(%{dimensions: [dimension]} = query)
when dimension in @imported_custom_props do
do_decide_custom_prop_table(query, dimension)
end
@queriable_custom_prop_dimensions ["event:goal", "event:name"] ++ @queriable_time_dimensions
defp do_decide_custom_prop_table(%{dimensions: dimensions} = query) do
if dimensions == [] or
(length(dimensions) == 1 and hd(dimensions) in @queriable_custom_prop_dimensions) do
custom_prop_filters =
dimensions_used_in_filters(query.filters)
|> Enum.filter(&(&1 in @imported_custom_props))
|> Enum.uniq()
case custom_prop_filters do
[custom_prop_filter] ->
do_decide_custom_prop_table(query, custom_prop_filter)
_ ->
[]
end
else
[]
end
end
defp do_decide_custom_prop_table(query, property) do
"event:props:" <> prop_key = property
has_required_event_or_goal_name_filter? =
query.filters
|> Enum.flat_map(fn
[:is, "event:name", event_names | _rest] -> event_names
[:is, "event:goal", goal_names | _rest] -> goal_names
_ -> []
end)
|> Enum.any?(fn event_or_goal_name ->
event_or_goal_name in Plausible.Event.SystemEvents.special_events_for_prop_key(prop_key)
end)
has_unsupported_filters? =
query.filters
|> dimensions_used_in_filters()
|> Enum.any?(&(&1 not in [property, "event:name", "event:goal"]))
if has_required_event_or_goal_name_filter? and
not has_unsupported_filters? do
["imported_custom_events"]
else
[]
end
end
defp do_decide_tables(%Query{filters: [], dimensions: []}), do: ["imported_visitors"]
defp do_decide_tables(%Query{filters: [], dimensions: ["event:goal"]}) do
["imported_pages", "imported_custom_events"]
end
defp do_decide_tables(%Query{dimensions: ["event:goal"]} = query) do
filter_dimensions = dimensions_used_in_filters(query.filters)
filter_goals = query.preloaded_goals.matching_toplevel_filters
any_event_goals? = Enum.any?(filter_goals, fn goal -> Plausible.Goal.type(goal) == :event end)
any_pageview_goals? =
Enum.any?(filter_goals, fn goal -> Plausible.Goal.type(goal) == :page end)
any_event_name_filters? = "event:name" in filter_dimensions or any_event_goals?
any_page_filters? = "event:page" in filter_dimensions or any_pageview_goals?
any_other_filters? =
Enum.any?(filter_dimensions, &(&1 not in ["event:page", "event:name", "event:goal"]))
cond do
any_other_filters? -> []
any_event_name_filters? and not any_page_filters? -> ["imported_custom_events"]
any_page_filters? and not any_event_name_filters? -> ["imported_pages"]
true -> []
end
end
defp do_decide_tables(query) do
table_candidates =
dimensions_used_in_filters(query.filters)
|> Enum.concat(query.dimensions)
|> Enum.reject(&(&1 in @queriable_time_dimensions or &1 == "event:goal"))
|> Enum.flat_map(fn
"visit:screen" -> ["visit:device"]
dimension -> [dimension]
end)
|> Enum.map(&@property_to_table_mappings[&1])
filter_goal_table_candidates =
query.preloaded_goals.matching_toplevel_filters
|> Enum.map(&Plausible.Goal.type/1)
|> Enum.map(fn
:event -> "imported_custom_events"
:page -> "imported_pages"
:scroll -> nil
end)
case Enum.uniq(table_candidates ++ filter_goal_table_candidates) do
[] -> ["imported_visitors"]
[nil] -> []
[candidate] -> [candidate]
_ -> []
end
end
end
|