Skip to content

Commit

Permalink
Make events with properties logic DRY and fix missed cloaked link
Browse files Browse the repository at this point in the history
  • Loading branch information
zoldar committed May 14, 2024
1 parent b410777 commit fbb0419
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 13 deletions.
17 changes: 12 additions & 5 deletions lib/plausible/exports.ex
Original file line number Diff line number Diff line change
Expand Up @@ -468,9 +468,6 @@ defmodule Plausible.Exports do
]
end

@events_with_url ["Outbound Link: Click", "Cloaked Link: Click", "File Download"]
@events_with_path ["404"]

defp export_custom_events_q(site_id, timezone, date_range) do
from e in sampled("events_v2"),
where: ^export_filter(site_id, date_range),
Expand All @@ -486,11 +483,21 @@ defmodule Plausible.Exports do
date(e.timestamp, ^timezone),
e.name,
selected_as(
fragment("if(? in ?, ?, '')", e.name, @events_with_url, get_by_key(e, :meta, "url")),
fragment(
"if(? in ?, ?, '')",
e.name,
^Plausible.Imported.events_with_url(),
get_by_key(e, :meta, "url")
),
:link_url
),
selected_as(
fragment("if(? in ?, ?, '')", e.name, @events_with_path, get_by_key(e, :meta, "path")),
fragment(
"if(? in ?, ?, '')",
e.name,
^Plausible.Imported.events_with_path(),
get_by_key(e, :meta, "path")
),
:path
),
visitors(e),
Expand Down
15 changes: 15 additions & 0 deletions lib/plausible/imported.ex
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,11 @@ defmodule Plausible.Imported do
# Maximum number of complete imports to account for when querying stats
@max_complete_imports 5

# Events which can be filtered by url property
@events_with_url ["Outbound Link: Click", "Cloaked Link: Click", "File Download"]
# Events which can be filtered by path property
@events_with_path ["404"]

@spec schemas() :: [module()]
def schemas, do: @tables

Expand All @@ -49,6 +54,16 @@ defmodule Plausible.Imported do
@max_complete_imports
end

@spec events_with_url() :: [String.t()]
def events_with_url() do
@events_with_url
end

@spec events_with_path() :: [String.t()]
def events_with_path() do
@events_with_path
end

@spec load_import_data(Site.t()) :: Site.t()
def load_import_data(%{import_data_loaded: true} = site), do: site

Expand Down
18 changes: 11 additions & 7 deletions lib/plausible/stats/imported.ex
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ defmodule Plausible.Stats.Imported do
@no_ref "Direct / None"
@not_set "(not set)"
@none "(none)"
@events_with_url Plausible.Imported.events_with_url()
@events_with_path Plausible.Imported.events_with_path()

@property_to_table_mappings %{
"visit:source" => "imported_sources",
Expand Down Expand Up @@ -50,14 +52,15 @@ defmodule Plausible.Stats.Imported do
filters: %{"event:goal" => {:is, {:event, event}}},
property: "event:props:url"
})
when event in ["Outbound Link: Click", "Cloaked Link: Click", "File Download"] do
when event in @events_with_url do
true
end

defp supports_single_filter?(%Query{
filters: %{"event:goal" => {:is, {:event, "404"}}},
filters: %{"event:goal" => {:is, {:event, event}}},
property: "event:props:path"
}) do
})
when event in @events_with_path do
true
end

Expand Down Expand Up @@ -229,17 +232,18 @@ defmodule Plausible.Stats.Imported do
"event:props:url",
_dim
)
when event_name in ["Outbound Link: Click", "File Download"] do
when event_name in @events_with_url do
where(q, [i], i.name == ^event_name)
end

defp maybe_apply_filter(
q,
%{"event:goal" => {:is, {:event, "404"}}},
%{"event:goal" => {:is, {:event, event_name}}},
"event:props:path",
_dim
) do
where(q, [i], i.name == "404")
)
when event_name in @events_with_path do
where(q, [i], i.name == ^event_name)
end

defp maybe_apply_filter(q, filters, property, dim) do
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3164,7 +3164,7 @@ defmodule PlausibleWeb.Api.ExternalStatsController.BreakdownTest do
assert %{"source" => "Google", "events" => 1} = breakdown_and_first.("visit:source")
end

for goal_name <- ["Outbound Link: Click", "File Download"] do
for goal_name <- ["Outbound Link: Click", "Cloaked Link: Click", "File Download"] do
test "returns url breakdown for #{goal_name} goal", %{conn: conn, site: site} do
insert(:goal, event_name: unquote(goal_name), site: site)
site_import = insert(:site_import, site: site)
Expand Down

0 comments on commit fbb0419

Please sign in to comment.