2020-09-28 11:29:24 +03:00
|
|
|
defmodule PlausibleWeb.Api.StatsController.SourcesTest do
|
2019-11-19 07:30:42 +03:00
|
|
|
use PlausibleWeb.ConnCase
|
|
|
|
import Plausible.TestUtils
|
|
|
|
|
2020-09-28 11:29:24 +03:00
|
|
|
describe "GET /api/stats/:domain/sources" do
|
2019-11-19 07:30:42 +03:00
|
|
|
setup [:create_user, :log_in, :create_site]
|
|
|
|
|
2020-09-28 11:29:24 +03:00
|
|
|
test "returns top sources by unique user ids", %{conn: conn, site: site} do
|
|
|
|
conn = get(conn, "/api/stats/#{site.domain}/sources?period=day&date=2019-01-01")
|
2019-11-19 07:30:42 +03:00
|
|
|
|
|
|
|
assert json_response(conn, 200) == [
|
2020-06-08 10:35:13 +03:00
|
|
|
%{"name" => "10words", "count" => 2, "url" => "10words.com"},
|
|
|
|
%{"name" => "Bing", "count" => 1, "url" => ""}
|
|
|
|
]
|
2019-11-19 07:30:42 +03:00
|
|
|
end
|
2019-11-20 11:31:49 +03:00
|
|
|
|
2020-09-28 11:29:24 +03:00
|
|
|
test "calculates bounce rate and visit duration for sources", %{conn: conn, site: site} do
|
2020-06-08 10:35:13 +03:00
|
|
|
conn =
|
|
|
|
get(
|
|
|
|
conn,
|
2020-09-28 11:29:24 +03:00
|
|
|
"/api/stats/#{site.domain}/sources?period=day&date=2019-01-01&include=bounce_rate,visit_duration"
|
2020-06-08 10:35:13 +03:00
|
|
|
)
|
2020-01-06 16:51:43 +03:00
|
|
|
|
|
|
|
assert json_response(conn, 200) == [
|
2020-06-08 10:35:13 +03:00
|
|
|
%{
|
|
|
|
"name" => "10words",
|
|
|
|
"count" => 2,
|
|
|
|
"bounce_rate" => 50.0,
|
2020-07-17 11:09:54 +03:00
|
|
|
"visit_duration" => 50,
|
2020-06-08 10:35:13 +03:00
|
|
|
"url" => "10words.com"
|
|
|
|
},
|
2020-07-21 09:58:00 +03:00
|
|
|
%{
|
|
|
|
"name" => "Bing",
|
|
|
|
"count" => 1,
|
|
|
|
"bounce_rate" => 0,
|
|
|
|
"visit_duration" => 100,
|
|
|
|
"url" => ""
|
|
|
|
}
|
2020-06-08 10:35:13 +03:00
|
|
|
]
|
2020-01-06 16:51:43 +03:00
|
|
|
end
|
2020-07-14 16:52:26 +03:00
|
|
|
|
2020-09-28 11:29:24 +03:00
|
|
|
test "returns top sources in realtime report", %{conn: conn, site: site} do
|
|
|
|
conn = get(conn, "/api/stats/#{site.domain}/sources?period=realtime")
|
2020-07-14 16:52:26 +03:00
|
|
|
|
|
|
|
assert json_response(conn, 200) == [
|
|
|
|
%{"name" => "10words", "count" => 2, "url" => "10words.com"},
|
2020-07-30 11:18:28 +03:00
|
|
|
%{"name" => "Bing", "count" => 1, "url" => "bing.com"}
|
2020-07-14 16:52:26 +03:00
|
|
|
]
|
|
|
|
end
|
2020-09-10 10:42:43 +03:00
|
|
|
|
|
|
|
test "can paginate the results", %{conn: conn, site: site} do
|
2020-09-28 11:29:24 +03:00
|
|
|
conn = get(conn, "/api/stats/#{site.domain}/sources?period=day&date=2019-01-01&limit=1&page=2")
|
2020-09-10 10:42:43 +03:00
|
|
|
|
|
|
|
assert json_response(conn, 200) == [
|
|
|
|
%{"name" => "Bing", "count" => 1, "url" => ""}
|
|
|
|
]
|
|
|
|
end
|
2020-01-16 16:40:06 +03:00
|
|
|
end
|
|
|
|
|
2020-09-28 11:29:24 +03:00
|
|
|
describe "GET /api/stats/:domain/utm_mediums" do
|
|
|
|
setup [:create_user, :log_in, :create_site]
|
|
|
|
|
|
|
|
test "returns top utm_mediums by unique user ids", %{conn: conn, site: site} do
|
|
|
|
conn = get(conn, "/api/stats/#{site.domain}/utm_mediums?period=day&date=2019-01-01")
|
|
|
|
|
|
|
|
assert json_response(conn, 200) == [
|
|
|
|
%{"name" => "listing", "count" => 2, "bounce_rate" => 50.0, "visit_duration" => 50.0},
|
|
|
|
%{"name" => "search", "count" => 1, "bounce_rate" => 0.0, "visit_duration" => 100.0}
|
|
|
|
]
|
|
|
|
end
|
|
|
|
end
|
2020-09-10 10:42:43 +03:00
|
|
|
|
2020-01-16 16:40:06 +03:00
|
|
|
describe "GET /api/stats/:domain/goal/referrers" do
|
|
|
|
setup [:create_user, :log_in, :create_site]
|
2020-01-06 16:51:43 +03:00
|
|
|
|
2020-01-16 16:40:06 +03:00
|
|
|
test "returns top referrers for a custom goal", %{conn: conn, site: site} do
|
2019-11-20 11:31:49 +03:00
|
|
|
filters = Jason.encode!(%{goal: "Signup"})
|
2020-06-08 10:35:13 +03:00
|
|
|
|
|
|
|
conn =
|
|
|
|
get(
|
|
|
|
conn,
|
2020-09-28 11:29:24 +03:00
|
|
|
"/api/stats/#{site.domain}/sources?period=day&date=2019-01-01&filters=#{filters}"
|
2020-06-08 10:35:13 +03:00
|
|
|
)
|
2019-11-20 11:31:49 +03:00
|
|
|
|
|
|
|
assert json_response(conn, 200) == [
|
2020-06-08 10:35:13 +03:00
|
|
|
%{"name" => "10words", "count" => 2, "url" => "10words.com"}
|
|
|
|
]
|
2019-11-20 11:31:49 +03:00
|
|
|
end
|
2019-11-20 11:33:47 +03:00
|
|
|
|
2020-01-16 16:40:06 +03:00
|
|
|
test "returns top referrers for a pageview goal", %{conn: conn, site: site} do
|
2019-11-20 11:33:47 +03:00
|
|
|
filters = Jason.encode!(%{goal: "Visit /register"})
|
2020-06-08 10:35:13 +03:00
|
|
|
|
|
|
|
conn =
|
|
|
|
get(
|
|
|
|
conn,
|
2020-09-28 11:29:24 +03:00
|
|
|
"/api/stats/#{site.domain}/sources?period=day&date=2019-01-01&filters=#{filters}"
|
2020-06-08 10:35:13 +03:00
|
|
|
)
|
2019-11-20 11:33:47 +03:00
|
|
|
|
|
|
|
assert json_response(conn, 200) == [
|
2020-06-08 10:35:13 +03:00
|
|
|
%{"name" => "10words", "count" => 2, "url" => "10words.com"}
|
|
|
|
]
|
2019-11-20 11:33:47 +03:00
|
|
|
end
|
2019-11-19 07:30:42 +03:00
|
|
|
end
|
|
|
|
|
|
|
|
describe "GET /api/stats/:domain/referrer-drilldown" do
|
|
|
|
setup [:create_user, :log_in, :create_site]
|
|
|
|
|
|
|
|
test "returns top referrers for a particular source", %{conn: conn, site: site} do
|
2020-08-05 14:45:20 +03:00
|
|
|
filters = Jason.encode!(%{source: "10words"})
|
|
|
|
conn = get(conn, "/api/stats/#{site.domain}/referrers/10words?period=day&date=2019-01-01&filters=#{filters}")
|
2019-11-19 07:30:42 +03:00
|
|
|
|
|
|
|
assert json_response(conn, 200) == %{
|
2020-09-28 11:29:24 +03:00
|
|
|
"total_visitors" => 6,
|
2020-06-08 10:35:13 +03:00
|
|
|
"referrers" => [
|
2020-07-30 11:18:28 +03:00
|
|
|
%{"name" => "10words.com/page1", "url" => "10words.com", "count" => 2}
|
2020-06-08 10:35:13 +03:00
|
|
|
]
|
|
|
|
}
|
2019-11-19 07:30:42 +03:00
|
|
|
end
|
2019-11-20 11:42:45 +03:00
|
|
|
|
2020-07-17 11:25:20 +03:00
|
|
|
test "calculates bounce rate and visit duration for referrer urls", %{conn: conn, site: site} do
|
2020-08-05 14:45:20 +03:00
|
|
|
filters = Jason.encode!(%{source: "10words"})
|
2020-06-08 10:35:13 +03:00
|
|
|
conn =
|
|
|
|
get(
|
|
|
|
conn,
|
2020-08-05 14:45:20 +03:00
|
|
|
"/api/stats/#{site.domain}/referrers/10words?period=day&date=2019-01-01&filters=#{filters}&include=bounce_rate,visit_duration"
|
2020-06-08 10:35:13 +03:00
|
|
|
)
|
2020-01-06 16:51:43 +03:00
|
|
|
|
|
|
|
assert json_response(conn, 200) == %{
|
2020-09-28 11:29:24 +03:00
|
|
|
"total_visitors" => 6,
|
2020-06-08 10:35:13 +03:00
|
|
|
"referrers" => [
|
2020-07-21 09:58:00 +03:00
|
|
|
%{
|
|
|
|
"name" => "10words.com/page1",
|
2020-07-30 11:18:28 +03:00
|
|
|
"url" => "10words.com",
|
2020-07-21 09:58:00 +03:00
|
|
|
"count" => 2,
|
|
|
|
"bounce_rate" => 50.0,
|
|
|
|
"visit_duration" => 50.0
|
|
|
|
}
|
2020-06-08 10:35:13 +03:00
|
|
|
]
|
|
|
|
}
|
2020-01-06 16:51:43 +03:00
|
|
|
end
|
|
|
|
|
2019-11-20 11:42:45 +03:00
|
|
|
test "gets keywords from Google", %{conn: conn, user: user, site: site} do
|
2020-06-08 10:35:13 +03:00
|
|
|
insert(:google_auth, user: user, user: user, site: site, property: "sc-domain:example.com")
|
2020-05-18 12:44:52 +03:00
|
|
|
conn = get(conn, "/api/stats/#{site.domain}/referrers/Google?period=day&date=2019-02-01")
|
2020-07-30 11:18:28 +03:00
|
|
|
{:ok, terms} = Plausible.Google.Api.Mock.fetch_stats(nil, nil, nil)
|
2019-11-20 11:42:45 +03:00
|
|
|
|
|
|
|
assert json_response(conn, 200) == %{
|
2020-06-08 10:35:13 +03:00
|
|
|
"total_visitors" => 2,
|
|
|
|
"search_terms" => terms
|
|
|
|
}
|
2019-11-20 11:42:45 +03:00
|
|
|
end
|
2020-01-16 16:13:03 +03:00
|
|
|
|
|
|
|
test "enriches twitter referrers with tweets if available", %{conn: conn, site: site} do
|
|
|
|
insert(:tweet, link: "t.co/some-link", text: "important tweet")
|
|
|
|
|
2020-05-18 12:44:52 +03:00
|
|
|
conn = get(conn, "/api/stats/#{site.domain}/referrers/Twitter?period=day&date=2019-03-01")
|
2020-01-16 16:13:03 +03:00
|
|
|
|
|
|
|
res = json_response(conn, 200)
|
|
|
|
assert res["total_visitors"] == 3
|
|
|
|
assert [tweet1, tweet2] = res["referrers"]
|
2020-06-08 10:35:13 +03:00
|
|
|
|
|
|
|
assert %{
|
|
|
|
"name" => "t.co/some-link",
|
|
|
|
"count" => 2,
|
|
|
|
"tweets" => [%{"text" => "important tweet"}]
|
|
|
|
} = tweet1
|
|
|
|
|
2020-01-16 16:13:03 +03:00
|
|
|
assert %{"name" => "t.co/nonexistent-link", "count" => 1, "tweets" => nil} = tweet2
|
|
|
|
end
|
2019-11-19 07:30:42 +03:00
|
|
|
end
|
2020-01-16 16:40:06 +03:00
|
|
|
|
|
|
|
describe "GET /api/stats/:domain/goal/referrers/:referrer" do
|
|
|
|
setup [:create_user, :log_in, :create_site]
|
|
|
|
|
|
|
|
test "returns top referring urls for a custom goal", %{conn: conn, site: site} do
|
|
|
|
filters = Jason.encode!(%{goal: "Signup"})
|
2020-06-08 10:35:13 +03:00
|
|
|
|
|
|
|
conn =
|
|
|
|
get(
|
|
|
|
conn,
|
|
|
|
"/api/stats/#{site.domain}/goal/referrers/10words?period=day&date=2019-01-01&filters=#{
|
|
|
|
filters
|
|
|
|
}"
|
|
|
|
)
|
2020-01-16 16:40:06 +03:00
|
|
|
|
|
|
|
assert json_response(conn, 200) == %{
|
2020-06-08 10:35:13 +03:00
|
|
|
"total_visitors" => 2,
|
|
|
|
"referrers" => [
|
|
|
|
%{"name" => "10words.com/page1", "count" => 2}
|
|
|
|
]
|
|
|
|
}
|
2020-01-16 16:40:06 +03:00
|
|
|
end
|
|
|
|
|
|
|
|
test "returns top referring urls for a pageview goal", %{conn: conn, site: site} do
|
|
|
|
filters = Jason.encode!(%{goal: "Visit /register"})
|
2020-06-08 10:35:13 +03:00
|
|
|
|
|
|
|
conn =
|
|
|
|
get(
|
|
|
|
conn,
|
|
|
|
"/api/stats/#{site.domain}/goal/referrers/10words?period=day&date=2019-01-01&filters=#{
|
|
|
|
filters
|
|
|
|
}"
|
|
|
|
)
|
2020-01-16 16:40:06 +03:00
|
|
|
|
2020-02-12 12:11:02 +03:00
|
|
|
assert json_response(conn, 200) == %{
|
2020-06-08 10:35:13 +03:00
|
|
|
"total_visitors" => 2,
|
|
|
|
"referrers" => [
|
|
|
|
%{"name" => "10words.com/page1", "count" => 2}
|
|
|
|
]
|
|
|
|
}
|
2020-01-16 16:40:06 +03:00
|
|
|
end
|
|
|
|
end
|
2019-11-19 07:30:42 +03:00
|
|
|
end
|