Skip to content

Commit

Permalink
Export and import custom events via CSV
Browse files Browse the repository at this point in the history
  • Loading branch information
zoldar committed May 10, 2024
1 parent a6dcd19 commit 6d20385
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 0 deletions.
33 changes: 33 additions & 0 deletions lib/plausible/exports.ex
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ defmodule Plausible.Exports do
"""

use Plausible
use Plausible.Stats.Fragments
import Ecto.Query

@doc "Schedules CSV export job to S3 storage"
Expand Down Expand Up @@ -236,6 +237,8 @@ defmodule Plausible.Exports do
filename.("imported_pages") => export_pages_q(site_id, timezone, date_range),
filename.("imported_entry_pages") => export_entry_pages_q(site_id, timezone, date_range),
filename.("imported_exit_pages") => export_exit_pages_q(site_id, timezone, date_range),
filename.("imported_custom_events") =>
export_custom_events_q(site_id, timezone, date_range),
filename.("imported_locations") => export_locations_q(site_id, timezone, date_range),
filename.("imported_devices") => export_devices_q(site_id, timezone, date_range),
filename.("imported_browsers") => export_browsers_q(site_id, timezone, date_range),
Expand Down Expand Up @@ -467,6 +470,36 @@ 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),
where: e.name != "pageview",
group_by: [
selected_as(:date),
e.name,
selected_as(:link_url),
selected_as(:path)
],
order_by: selected_as(:date),
select: [
date(e.timestamp, ^timezone),
e.name,
selected_as(
fragment("if(? in ?, ?, '')", e.name, @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")),
:path
),
visitors(e),
selected_as(fragment("toUInt64(round(count()*any(_sample_factor)))"), :events)
]
end

defp export_locations_q(site_id, timezone, date_range) do
from s in sampled("sessions_v2"),
where: ^export_filter(site_id, date_range),
Expand Down
2 changes: 2 additions & 0 deletions lib/plausible/imported/csv_importer.ex
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,8 @@ defmodule Plausible.Imported.CSVImporter do
"date Date, entry_page String, visitors UInt64, entrances UInt64, visit_duration UInt64, bounces UInt32, pageviews UInt64",
"imported_exit_pages" =>
"date Date, exit_page String, visitors UInt64, visit_duration UInt64, exits UInt64, bounces UInt32, pageviews UInt64",
"imported_custom_events" =>
"date Date, name String, link_url String, path String, visitors UInt64, events UInt64",
"imported_locations" =>
"date Date, country String, region String, city UInt64, visitors UInt64, visits UInt64, visit_duration UInt64, bounces UInt32, pageviews UInt64",
"imported_operating_systems" =>
Expand Down
1 change: 1 addition & 0 deletions lib/plausible/imported/custom_event.ex
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ defmodule Plausible.Imported.CustomEvent do
field :date, :date
field :name, :string
field :link_url, :string
field :path, :string
field :visitors, Ch, type: "UInt64"
field :events, Ch, type: "UInt64"
end
Expand Down
3 changes: 3 additions & 0 deletions test/plausible/exports_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ defmodule Plausible.ExportsTest do

assert Map.keys(queries) == [
"imported_browsers.csv",
"imported_custom_events.csv",
"imported_devices.csv",
"imported_entry_pages.csv",
"imported_exit_pages.csv",
Expand All @@ -31,6 +32,7 @@ defmodule Plausible.ExportsTest do

assert Map.keys(queries) == [
"imported_browsers_20230101_20240312.csv",
"imported_custom_events_20230101_20240312.csv",
"imported_devices_20230101_20240312.csv",
"imported_entry_pages_20230101_20240312.csv",
"imported_exit_pages_20230101_20240312.csv",
Expand All @@ -50,6 +52,7 @@ defmodule Plausible.ExportsTest do

assert Map.keys(queries) == [
"imported_browsers.ch",
"imported_custom_events.ch",
"imported_devices.ch",
"imported_entry_pages.ch",
"imported_exit_pages.ch",
Expand Down
1 change: 1 addition & 0 deletions test/plausible/imported/csv_importer_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ defmodule Plausible.Imported.CSVImporterTest do
test "parses job args properly", %{user: user, site: site} do
tables = [
"imported_browsers",
"imported_custom_events",
"imported_devices",
"imported_entry_pages",
"imported_exit_pages",
Expand Down

0 comments on commit 6d20385

Please sign in to comment.