analytics/test/plausible_web/controllers/api/stats_controller/pages_test.exs
Uku Taht 7dbbc8ba22
Configurable site id (#30)
* Use site id instead of hostname for events

* Use site id in domain status check

* Revert change to tracking module

* Catch more places where link generation needed updating

* Rename site_id to domain

* Drop hostname index from events
2020-02-04 15:44:13 +02:00

38 lines
1.7 KiB
Elixir

defmodule PlausibleWeb.Api.StatsController.PagesTest do
use PlausibleWeb.ConnCase
import Plausible.TestUtils
describe "GET /api/stats/:domain/pages" do
setup [:create_user, :log_in, :create_site]
test "returns top pages sources by pageviews", %{conn: conn, site: site} do
insert(:pageview, domain: site.domain, pathname: "/", timestamp: ~N[2019-01-01 01:00:00])
insert(:pageview, domain: site.domain, pathname: "/", timestamp: ~N[2019-01-01 01:00:00])
insert(:pageview, domain: site.domain, pathname: "/contact", timestamp: ~N[2019-01-01 01:00:00])
conn = get(conn, "/api/stats/#{site.domain}/pages?period=day&date=2019-01-01")
assert json_response(conn, 200) == [
%{"name" => "/", "count" => 2},
%{"name" => "/contact", "count" => 1},
]
end
test "calculates bounce rate for pages", %{conn: conn, site: site} do
insert(:pageview, domain: site.domain, pathname: "/", timestamp: ~N[2019-01-01 02:00:00])
insert(:pageview, domain: site.domain, pathname: "/", timestamp: ~N[2019-01-01 02:00:00])
insert(:pageview, domain: site.domain, pathname: "/contact", timestamp: ~N[2019-01-01 02:00:00])
insert(:session, domain: site.domain, entry_page: "/", is_bounce: true, start: ~N[2019-01-01 02:00:00])
insert(:session, domain: site.domain, entry_page: "/", is_bounce: false, start: ~N[2019-01-01 02:00:00])
conn = get(conn, "/api/stats/#{site.domain}/pages?period=day&date=2019-01-01&include=bounce_rate")
assert json_response(conn, 200) == [
%{"name" => "/", "count" => 2, "bounce_rate" => 50},
%{"name" => "/contact", "count" => 1, "bounce_rate" => nil},
]
end
end
end