diff --git a/lib/plausible/google/api.ex b/lib/plausible/google/api.ex index 797cadc5e6b1..44372da25048 100644 --- a/lib/plausible/google/api.ex +++ b/lib/plausible/google/api.ex @@ -203,7 +203,7 @@ defmodule Plausible.Google.Api do { "imported_pages", ["ga:date", "ga:hostname", "ga:pagePath"], - ["ga:users", "ga:pageviews", "ga:timeOnPage"] + ["ga:users", "ga:pageviews", "ga:exits", "ga:timeOnPage"] }, { "imported_entry_pages", diff --git a/lib/plausible/imported/site.ex b/lib/plausible/imported/site.ex index a418cb78e1fb..d137fb667656 100644 --- a/lib/plausible/imported/site.ex +++ b/lib/plausible/imported/site.ex @@ -72,11 +72,12 @@ defmodule Plausible.Imported do defp new_from_google_analytics(site_id, "imported_pages", %{ "dimensions" => [date, hostname, page], - "metrics" => [%{"values" => [visitors, pageviews, time_on_page]}] + "metrics" => [%{"values" => [visitors, pageviews, exits, time_on_page]}] }) do page = URI.parse(page).path {visitors, ""} = Integer.parse(visitors) {pageviews, ""} = Integer.parse(pageviews) + {exits, ""} = Integer.parse(exits) {time_on_page, _} = Integer.parse(time_on_page) %{ @@ -86,6 +87,7 @@ defmodule Plausible.Imported do page: page, visitors: visitors, pageviews: pageviews, + exits: exits, time_on_page: time_on_page } end diff --git a/lib/plausible/stats/breakdown.ex b/lib/plausible/stats/breakdown.ex index c9c0a77f7723..ccef6a49a72d 100644 --- a/lib/plausible/stats/breakdown.ex +++ b/lib/plausible/stats/breakdown.ex @@ -288,7 +288,7 @@ defmodule Plausible.Stats.Breakdown do where: i.page in ^pages, select: %{ page: i.page, - pageviews: sum(i.pageviews), + pageviews: fragment("sum(?) - sum(?)", i.pageviews, i.exits), time_on_page: sum(i.time_on_page) } ) @@ -297,8 +297,9 @@ defmodule Plausible.Stats.Breakdown do {restime, resviews} = Map.get(res, page, {0, 0}) Map.put(res, page, {restime + time, resviews + pageviews}) end) - |> Enum.map(fn {page, {time, pageviews}} -> - {page, time / pageviews} + |> Enum.map(fn + {page, {_, 0}} -> {page, nil} + {page, {time, pageviews}} -> {page, time / pageviews} end) |> Enum.into(%{}) else diff --git a/priv/clickhouse_repo/migrations/20211112130238_create_imported_tables.exs b/priv/clickhouse_repo/migrations/20211112130238_create_imported_tables.exs index 5abc84a957bf..33bc8b23c4d4 100644 --- a/priv/clickhouse_repo/migrations/20211112130238_create_imported_tables.exs +++ b/priv/clickhouse_repo/migrations/20211112130238_create_imported_tables.exs @@ -39,6 +39,7 @@ defmodule Plausible.ClickhouseRepo.Migrations.CreateImportedVisitors do add(:page, :string) add(:visitors, :UInt64) add(:pageviews, :UInt64) + add(:exits, :UInt64) add(:time_on_page, :UInt64) end diff --git a/test/plausible/imported/imported_test.exs b/test/plausible/imported/imported_test.exs index 90bf1d66408f..d211d2215e0e 100644 --- a/test/plausible/imported/imported_test.exs +++ b/test/plausible/imported/imported_test.exs @@ -325,15 +325,15 @@ defmodule Plausible.ImportedTest do [ %{ "dimensions" => ["20210101", "host-a.com", "/"], - "metrics" => [%{"values" => ["1", "1", "700"]}] + "metrics" => [%{"values" => ["1", "1", "0", "700"]}] }, %{ "dimensions" => ["20210101", "host-b.com", "/some-other-page"], - "metrics" => [%{"values" => ["1", "1", "60"]}] + "metrics" => [%{"values" => ["1", "2", "1", "60"]}] }, %{ "dimensions" => ["20210101", "host-b.com", "/some-other-page?wat=wot"], - "metrics" => [%{"values" => ["1", "1", "60"]}] + "metrics" => [%{"values" => ["1", "1", "0", "60"]}] } ], site.id, @@ -363,7 +363,7 @@ defmodule Plausible.ImportedTest do "bounce_rate" => nil, "time_on_page" => 60, "visitors" => 3, - "pageviews" => 3, + "pageviews" => 4, "name" => "/some-other-page" }, %{ @@ -403,7 +403,7 @@ defmodule Plausible.ImportedTest do [ %{ "dimensions" => ["20210101", "host-a.com", "/page2"], - "metrics" => [%{"values" => ["2", "4", "10"]}] + "metrics" => [%{"values" => ["2", "4", "0", "10"]}] } ], site.id, diff --git a/test/support/factory.ex b/test/support/factory.ex index 747d888a66ac..30ca268069f1 100644 --- a/test/support/factory.ex +++ b/test/support/factory.ex @@ -211,6 +211,7 @@ defmodule Plausible.Factory do page: "", visitors: 1, pageviews: 1, + exits: 0, time_on_page: 10 } end