Skip to content

Commit 14d2a29

Browse files
committed
export/import/count pageleave_visitors
1 parent ed6eac8 commit 14d2a29

File tree

8 files changed

+55
-25
lines changed

8 files changed

+55
-25
lines changed

lib/plausible/exports.ex

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -445,7 +445,8 @@ defmodule Plausible.Exports do
445445
"if(isNull(sum(?)), NULL, toUInt64(sum(?)))",
446446
p.max_scroll_depth,
447447
p.max_scroll_depth
448-
)
448+
),
449+
pageleave_visitors: count(p.user_id)
449450
},
450451
group_by: [:date, :page]
451452
)
@@ -465,7 +466,8 @@ defmodule Plausible.Exports do
465466
),
466467
visitors(e),
467468
selected_as(fragment("toUInt64(round(count()*any(_sample_factor)))"), :pageviews),
468-
selected_as(fragment("any(?)", s.scroll_depth), :scroll_depth)
469+
selected_as(fragment("any(?)", s.scroll_depth), :scroll_depth),
470+
selected_as(fragment("any(?)", s.pageleave_visitors), :pageleave_visitors)
469471
]
470472
)
471473
|> group_by([e], [selected_as(:date), selected_as(:page)])

lib/plausible/imported/csv_importer.ex

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,7 @@ defmodule Plausible.Imported.CSVImporter do
161161
"imported_operating_systems" =>
162162
"date Date, operating_system String, operating_system_version String, visitors UInt64, visits UInt64, visit_duration UInt64, bounces UInt32, pageviews UInt64",
163163
"imported_pages" =>
164-
"date Date, hostname String, page String, visits UInt64, visitors UInt64, pageviews UInt64, scroll_depth Nullable(UInt64)",
164+
"date Date, hostname String, page String, visits UInt64, visitors UInt64, pageviews UInt64, scroll_depth Nullable(UInt64), pageleave_visitors UInt64",
165165
"imported_sources" =>
166166
"date Date, source String, referrer String, utm_source String, utm_medium String, utm_campaign String, utm_content String, utm_term String, pageviews UInt64, visitors UInt64, visits UInt64, visit_duration UInt64, bounces UInt32",
167167
"imported_visitors" =>

lib/plausible/imported/page.ex

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,5 +16,6 @@ defmodule Plausible.Imported.Page do
1616
field :exits, Ch, type: "UInt64"
1717
field :time_on_page, Ch, type: "UInt64"
1818
field :scroll_depth, Ch, type: "Nullable(UInt64)"
19+
field :pageleave_visitors, Ch, type: "UInt64"
1920
end
2021
end

lib/plausible/stats/imported/sql/expression.ex

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ defmodule Plausible.Stats.Imported.SQL.Expression do
118118
defp select_metric(:scroll_depth, "imported_pages") do
119119
wrap_alias([i], %{
120120
scroll_depth_sum: sum(i.scroll_depth),
121-
total_visitors: fragment("sumIf(?, isNotNull(?))", i.visitors, i.scroll_depth)
121+
total_visitors: sum(i.pageleave_visitors)
122122
})
123123
end
124124

test/plausible/imported/csv_importer_test.exs

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1190,14 +1190,19 @@ defmodule Plausible.Imported.CSVImporterTest do
11901190
select: %{
11911191
date: i.date,
11921192
page: i.page,
1193-
scroll_depth: i.scroll_depth
1193+
scroll_depth: i.scroll_depth,
1194+
pageleave_visitors: i.pageleave_visitors
11941195
}
11951196
)
11961197
|> Plausible.IngestRepo.all()
11971198

1198-
assert %{date: ~D[2020-01-01], page: "/another", scroll_depth: 50} in imported_data
1199-
assert %{date: ~D[2020-01-01], page: "/blog", scroll_depth: 180} in imported_data
1200-
assert %{date: ~D[2020-01-02], page: "/blog", scroll_depth: nil} in imported_data
1199+
assert %{date: ~D[2020-01-01], page: "/", scroll_depth: 20, pageleave_visitors: 1} in imported_data
1200+
1201+
assert %{date: ~D[2020-01-01], page: "/another", scroll_depth: 50, pageleave_visitors: 2} in imported_data
1202+
1203+
assert %{date: ~D[2020-01-01], page: "/blog", scroll_depth: 180, pageleave_visitors: 3} in imported_data
1204+
1205+
assert %{date: ~D[2020-01-02], page: "/blog", scroll_depth: nil, pageleave_visitors: 0} in imported_data
12011206

12021207
# assert via stats queries that scroll_depth from imported
12031208
# data matches the scroll_depth from native data

test/plausible_web/controllers/api/stats_controller/main_graph_test.exs

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -648,9 +648,21 @@ defmodule PlausibleWeb.Api.StatsController.MainGraphTest do
648648
# 2020-01-02 - both imported and native data
649649
build(:pageview, user_id: 56, timestamp: ~N[2020-01-02 00:00:00]),
650650
build(:pageleave, user_id: 56, timestamp: ~N[2020-01-02 00:01:00], scroll_depth: 20),
651-
build(:imported_pages, date: ~D[2020-01-02], page: "/", visitors: 1, scroll_depth: 40),
651+
build(:imported_pages,
652+
date: ~D[2020-01-02],
653+
page: "/",
654+
visitors: 1,
655+
scroll_depth: 40,
656+
pageleave_visitors: 1
657+
),
652658
# 2020-01-03 - only imported data
653-
build(:imported_pages, date: ~D[2020-01-03], page: "/", visitors: 1, scroll_depth: 90),
659+
build(:imported_pages,
660+
date: ~D[2020-01-03],
661+
page: "/",
662+
visitors: 1,
663+
scroll_depth: 90,
664+
pageleave_visitors: 1
665+
),
654666
build(:imported_pages, date: ~D[2020-01-03], page: "/", visitors: 100, scroll_depth: nil)
655667
])
656668

test/plausible_web/controllers/api/stats_controller/pages_test.exs

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -666,7 +666,8 @@ defmodule PlausibleWeb.Api.StatsController.PagesTest do
666666
pageviews: 3,
667667
time_on_page: 90,
668668
page: "/blog",
669-
scroll_depth: 120
669+
scroll_depth: 120,
670+
pageleave_visitors: 3
670671
)
671672
])
672673

@@ -717,19 +718,21 @@ defmodule PlausibleWeb.Api.StatsController.PagesTest do
717718
),
718719
build(:imported_pages,
719720
date: ~D[2020-01-01],
720-
visitors: 3,
721-
pageviews: 3,
721+
visitors: 4,
722+
pageviews: 4,
722723
time_on_page: 180,
723724
page: "/native-and-imported",
724-
scroll_depth: 120
725+
scroll_depth: 120,
726+
pageleave_visitors: 3
725727
),
726728
build(:imported_pages,
727729
date: ~D[2020-01-01],
728-
visitors: 10,
729-
pageviews: 10,
730+
visitors: 20,
731+
pageviews: 30,
730732
time_on_page: 300,
731733
page: "/imported-only",
732-
scroll_depth: 100
734+
scroll_depth: 100,
735+
pageleave_visitors: 10
733736
)
734737
])
735738

@@ -742,10 +745,10 @@ defmodule PlausibleWeb.Api.StatsController.PagesTest do
742745
assert json_response(conn, 200)["results"] == [
743746
%{
744747
"name" => "/native-and-imported",
745-
"visitors" => 4,
746-
"pageviews" => 4,
748+
"visitors" => 5,
749+
"pageviews" => 5,
747750
"bounce_rate" => 0,
748-
"time_on_page" => 60,
751+
"time_on_page" => 48,
749752
"scroll_depth" => 50
750753
},
751754
%{
@@ -758,10 +761,10 @@ defmodule PlausibleWeb.Api.StatsController.PagesTest do
758761
},
759762
%{
760763
"name" => "/imported-only",
761-
"visitors" => 10,
762-
"pageviews" => 10,
764+
"visitors" => 20,
765+
"pageviews" => 30,
763766
"bounce_rate" => 0,
764-
"time_on_page" => 30.0,
767+
"time_on_page" => 10.0,
765768
"scroll_depth" => 10
766769
}
767770
]
@@ -778,7 +781,8 @@ defmodule PlausibleWeb.Api.StatsController.PagesTest do
778781
visitors: 10,
779782
pageviews: 10,
780783
page: "/blog",
781-
scroll_depth: 100
784+
scroll_depth: 100,
785+
pageleave_visitors: 10
782786
),
783787
build(:imported_pages,
784788
date: ~D[2020-01-01],

test/plausible_web/controllers/api/stats_controller/top_stats_test.exs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1000,7 +1000,13 @@ defmodule PlausibleWeb.Api.StatsController.TopStatsTest do
10001000
build(:pageleave, user_id: 123, timestamp: ~N[2021-01-01 00:00:20], scroll_depth: 60),
10011001
build(:pageview, user_id: 456, timestamp: ~N[2021-01-01 00:00:00]),
10021002
build(:pageleave, user_id: 456, timestamp: ~N[2021-01-01 00:00:10], scroll_depth: 80),
1003-
build(:imported_pages, page: "/", date: ~D[2021-01-01], visitors: 8, scroll_depth: 410),
1003+
build(:imported_pages,
1004+
page: "/",
1005+
date: ~D[2021-01-01],
1006+
visitors: 8,
1007+
scroll_depth: 410,
1008+
pageleave_visitors: 8
1009+
),
10041010
build(:imported_pages, page: "/", date: ~D[2021-01-02], visitors: 100, scroll_depth: nil)
10051011
])
10061012

0 commit comments

Comments
 (0)