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
|
2021-07-23 13:44:05 +03:00
|
|
|
@user_id 123
|
2019-11-19 07:30:42 +03:00
|
|
|
|
2020-09-28 11:29:24 +03:00
|
|
|
describe "GET /api/stats/:domain/sources" do
|
2021-07-23 13:44:05 +03:00
|
|
|
setup [:create_user, :log_in, :create_new_site]
|
2019-11-19 07:30:42 +03:00
|
|
|
|
2020-09-28 11:29:24 +03:00
|
|
|
test "returns top sources by unique user ids", %{conn: conn, site: site} do
|
2021-07-23 13:44:05 +03:00
|
|
|
populate_stats(site, [
|
|
|
|
build(:pageview,
|
|
|
|
referrer_source: "Google",
|
|
|
|
referrer: "google.com"
|
|
|
|
),
|
|
|
|
build(:pageview,
|
|
|
|
referrer_source: "Google",
|
|
|
|
referrer: "google.com"
|
|
|
|
),
|
|
|
|
build(:pageview,
|
|
|
|
referrer_source: "DuckDuckGo",
|
|
|
|
referrer: "duckduckgo.com"
|
|
|
|
)
|
|
|
|
])
|
|
|
|
|
|
|
|
conn = get(conn, "/api/stats/#{site.domain}/sources")
|
2019-11-19 07:30:42 +03:00
|
|
|
|
|
|
|
assert json_response(conn, 200) == [
|
2021-11-04 15:20:39 +03:00
|
|
|
%{"name" => "Google", "visitors" => 2},
|
|
|
|
%{"name" => "DuckDuckGo", "visitors" => 1}
|
2020-06-08 10:35:13 +03:00
|
|
|
]
|
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
|
2021-07-23 13:44:05 +03:00
|
|
|
populate_stats(site, [
|
|
|
|
build(:pageview,
|
|
|
|
referrer_source: "Google",
|
|
|
|
referrer: "google.com",
|
|
|
|
user_id: @user_id,
|
|
|
|
timestamp: ~N[2021-01-01 00:00:00]
|
|
|
|
),
|
|
|
|
build(:pageview,
|
|
|
|
referrer_source: "Google",
|
|
|
|
referrer: "google.com",
|
|
|
|
user_id: @user_id,
|
|
|
|
timestamp: ~N[2021-01-01 00:15:00]
|
|
|
|
),
|
|
|
|
build(:pageview,
|
|
|
|
referrer_source: "DuckDuckGo",
|
|
|
|
referrer: "duckduckgo.com",
|
|
|
|
timestamp: ~N[2021-01-01 00:00:00]
|
|
|
|
)
|
|
|
|
])
|
|
|
|
|
2020-06-08 10:35:13 +03:00
|
|
|
conn =
|
|
|
|
get(
|
|
|
|
conn,
|
2021-07-23 13:44:05 +03:00
|
|
|
"/api/stats/#{site.domain}/sources?period=day&date=2021-01-01&detailed=true"
|
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
|
|
|
%{
|
2021-07-23 13:44:05 +03:00
|
|
|
"name" => "Google",
|
2021-11-04 15:20:39 +03:00
|
|
|
"visitors" => 1,
|
2021-07-23 13:44:05 +03:00
|
|
|
"bounce_rate" => 0,
|
|
|
|
"visit_duration" => 900
|
2020-06-08 10:35:13 +03:00
|
|
|
},
|
2020-07-21 09:58:00 +03:00
|
|
|
%{
|
2021-07-23 13:44:05 +03:00
|
|
|
"name" => "DuckDuckGo",
|
2021-11-04 15:20:39 +03:00
|
|
|
"visitors" => 1,
|
2021-07-23 13:44:05 +03:00
|
|
|
"bounce_rate" => 100,
|
|
|
|
"visit_duration" => 0
|
2020-07-21 09:58:00 +03:00
|
|
|
}
|
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
|
2021-07-23 13:44:05 +03:00
|
|
|
populate_stats(site, [
|
|
|
|
build(:pageview,
|
|
|
|
referrer_source: "Google",
|
|
|
|
referrer: "google.com",
|
|
|
|
timestamp: relative_time(minutes: -3)
|
|
|
|
),
|
|
|
|
build(:pageview,
|
|
|
|
referrer_source: "Google",
|
|
|
|
referrer: "google.com",
|
|
|
|
timestamp: relative_time(minutes: -2)
|
|
|
|
),
|
|
|
|
build(:pageview,
|
|
|
|
referrer_source: "DuckDuckGo",
|
|
|
|
referrer: "duckduckgo.com",
|
|
|
|
timestamp: relative_time(minutes: -1)
|
|
|
|
)
|
|
|
|
])
|
|
|
|
|
2020-09-28 11:29:24 +03:00
|
|
|
conn = get(conn, "/api/stats/#{site.domain}/sources?period=realtime")
|
2020-07-14 16:52:26 +03:00
|
|
|
|
|
|
|
assert json_response(conn, 200) == [
|
2021-11-04 15:20:39 +03:00
|
|
|
%{"name" => "Google", "visitors" => 2},
|
|
|
|
%{"name" => "DuckDuckGo", "visitors" => 1}
|
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
|
2021-07-23 13:44:05 +03:00
|
|
|
populate_stats(site, [
|
|
|
|
build(:pageview,
|
|
|
|
referrer_source: "Google",
|
|
|
|
referrer: "google.com"
|
|
|
|
),
|
|
|
|
build(:pageview,
|
|
|
|
referrer_source: "Google",
|
|
|
|
referrer: "google.com"
|
|
|
|
),
|
|
|
|
build(:pageview,
|
|
|
|
referrer_source: "DuckDuckGo",
|
|
|
|
referrer: "duckduckgo.com"
|
|
|
|
)
|
|
|
|
])
|
|
|
|
|
|
|
|
conn = get(conn, "/api/stats/#{site.domain}/sources?limit=1&page=2")
|
2020-09-10 10:42:43 +03:00
|
|
|
|
|
|
|
assert json_response(conn, 200) == [
|
2021-11-04 15:20:39 +03:00
|
|
|
%{"name" => "DuckDuckGo", "visitors" => 1}
|
2020-09-10 10:42:43 +03:00
|
|
|
]
|
|
|
|
end
|
2021-08-31 11:13:09 +03:00
|
|
|
|
|
|
|
test "shows sources for a page", %{conn: conn, site: site} do
|
|
|
|
populate_stats(site, [
|
|
|
|
build(:pageview, pathname: "/page1", referrer_source: "Google"),
|
|
|
|
build(:pageview, pathname: "/page2", referrer_source: "Google"),
|
|
|
|
build(:pageview, user_id: 1, pathname: "/page2", referrer_source: "DuckDuckGo"),
|
|
|
|
build(:pageview, user_id: 1, pathname: "/page1", referrer_source: "DuckDuckGo")
|
|
|
|
])
|
|
|
|
|
|
|
|
filters = Jason.encode!(%{"page" => "/page1"})
|
|
|
|
conn = get(conn, "/api/stats/#{site.domain}/sources?filters=#{filters}")
|
|
|
|
|
|
|
|
assert json_response(conn, 200) == [
|
2021-11-04 15:20:39 +03:00
|
|
|
%{"name" => "Google", "visitors" => 1}
|
2021-08-31 11:13:09 +03:00
|
|
|
]
|
|
|
|
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
|
2021-07-23 13:44:05 +03:00
|
|
|
setup [:create_user, :log_in, :create_new_site]
|
2020-09-28 11:29:24 +03:00
|
|
|
|
|
|
|
test "returns top utm_mediums by unique user ids", %{conn: conn, site: site} do
|
2021-07-23 13:44:05 +03:00
|
|
|
populate_stats(site, [
|
|
|
|
build(:pageview,
|
|
|
|
utm_medium: "social",
|
|
|
|
user_id: @user_id,
|
|
|
|
timestamp: ~N[2021-01-01 00:00:00]
|
|
|
|
),
|
|
|
|
build(:pageview,
|
|
|
|
utm_medium: "social",
|
|
|
|
user_id: @user_id,
|
|
|
|
timestamp: ~N[2021-01-01 00:15:00]
|
|
|
|
),
|
|
|
|
build(:pageview,
|
|
|
|
utm_medium: "email",
|
|
|
|
timestamp: ~N[2021-01-01 00:00:00]
|
|
|
|
)
|
|
|
|
])
|
|
|
|
|
|
|
|
conn =
|
|
|
|
get(
|
|
|
|
conn,
|
|
|
|
"/api/stats/#{site.domain}/utm_mediums?period=day&date=2021-01-01"
|
|
|
|
)
|
|
|
|
|
|
|
|
assert json_response(conn, 200) == [
|
|
|
|
%{
|
|
|
|
"name" => "social",
|
2021-11-04 15:20:39 +03:00
|
|
|
"visitors" => 1,
|
2021-07-23 13:44:05 +03:00
|
|
|
"bounce_rate" => 0,
|
|
|
|
"visit_duration" => 900
|
|
|
|
},
|
|
|
|
%{
|
|
|
|
"name" => "email",
|
2021-11-04 15:20:39 +03:00
|
|
|
"visitors" => 1,
|
2021-07-23 13:44:05 +03:00
|
|
|
"bounce_rate" => 100,
|
|
|
|
"visit_duration" => 0
|
|
|
|
}
|
|
|
|
]
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe "GET /api/stats/:domain/utm_campaigns" do
|
|
|
|
setup [:create_user, :log_in, :create_new_site]
|
|
|
|
|
|
|
|
test "returns top utm_campaigns by unique user ids", %{conn: conn, site: site} do
|
|
|
|
populate_stats(site, [
|
|
|
|
build(:pageview,
|
|
|
|
utm_campaign: "profile",
|
|
|
|
user_id: @user_id,
|
|
|
|
timestamp: ~N[2021-01-01 00:00:00]
|
|
|
|
),
|
|
|
|
build(:pageview,
|
|
|
|
utm_campaign: "profile",
|
|
|
|
user_id: @user_id,
|
|
|
|
timestamp: ~N[2021-01-01 00:15:00]
|
|
|
|
),
|
|
|
|
build(:pageview,
|
|
|
|
utm_campaign: "august",
|
|
|
|
timestamp: ~N[2021-01-01 00:00:00]
|
|
|
|
),
|
|
|
|
build(:pageview,
|
|
|
|
utm_campaign: "august",
|
|
|
|
timestamp: ~N[2021-01-01 00:00:00]
|
|
|
|
)
|
|
|
|
])
|
|
|
|
|
|
|
|
conn =
|
|
|
|
get(
|
|
|
|
conn,
|
|
|
|
"/api/stats/#{site.domain}/utm_campaigns?period=day&date=2021-01-01"
|
|
|
|
)
|
2020-09-28 11:29:24 +03:00
|
|
|
|
|
|
|
assert json_response(conn, 200) == [
|
2020-11-03 12:20:11 +03:00
|
|
|
%{
|
2021-07-23 13:44:05 +03:00
|
|
|
"name" => "august",
|
2021-11-04 15:20:39 +03:00
|
|
|
"visitors" => 2,
|
2021-07-23 13:44:05 +03:00
|
|
|
"bounce_rate" => 100,
|
|
|
|
"visit_duration" => 0
|
2020-11-03 12:20:11 +03:00
|
|
|
},
|
|
|
|
%{
|
2021-07-23 13:44:05 +03:00
|
|
|
"name" => "profile",
|
2021-11-04 15:20:39 +03:00
|
|
|
"visitors" => 1,
|
2021-07-23 13:44:05 +03:00
|
|
|
"bounce_rate" => 0,
|
|
|
|
"visit_duration" => 900
|
2020-11-03 12:20:11 +03:00
|
|
|
}
|
2020-09-28 11:29:24 +03:00
|
|
|
]
|
|
|
|
end
|
|
|
|
end
|
2020-09-10 10:42:43 +03:00
|
|
|
|
2021-07-23 13:44:05 +03:00
|
|
|
describe "GET /api/stats/:domain/utm_sources" do
|
|
|
|
setup [:create_user, :log_in, :create_new_site]
|
|
|
|
|
|
|
|
test "returns top utm_sources by unique user ids", %{conn: conn, site: site} do
|
|
|
|
populate_stats(site, [
|
|
|
|
build(:pageview,
|
|
|
|
utm_source: "Twitter",
|
|
|
|
user_id: @user_id,
|
|
|
|
timestamp: ~N[2021-01-01 00:00:00]
|
|
|
|
),
|
|
|
|
build(:pageview,
|
|
|
|
utm_source: "Twitter",
|
|
|
|
user_id: @user_id,
|
|
|
|
timestamp: ~N[2021-01-01 00:15:00]
|
|
|
|
),
|
|
|
|
build(:pageview,
|
|
|
|
utm_source: "newsletter",
|
|
|
|
timestamp: ~N[2021-01-01 00:00:00]
|
|
|
|
),
|
|
|
|
build(:pageview,
|
|
|
|
utm_source: "newsletter",
|
|
|
|
timestamp: ~N[2021-01-01 00:00:00]
|
|
|
|
)
|
|
|
|
])
|
|
|
|
|
|
|
|
conn =
|
|
|
|
get(
|
|
|
|
conn,
|
|
|
|
"/api/stats/#{site.domain}/utm_sources?period=day&date=2021-01-01"
|
|
|
|
)
|
|
|
|
|
|
|
|
assert json_response(conn, 200) == [
|
|
|
|
%{
|
|
|
|
"name" => "newsletter",
|
2021-11-04 15:20:39 +03:00
|
|
|
"visitors" => 2,
|
2021-07-23 13:44:05 +03:00
|
|
|
"bounce_rate" => 100,
|
|
|
|
"visit_duration" => 0
|
|
|
|
},
|
|
|
|
%{
|
|
|
|
"name" => "Twitter",
|
2021-11-04 15:20:39 +03:00
|
|
|
"visitors" => 1,
|
2021-07-23 13:44:05 +03:00
|
|
|
"bounce_rate" => 0,
|
|
|
|
"visit_duration" => 900
|
|
|
|
}
|
|
|
|
]
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe "GET /api/stats/:domain/sources - with goal filter" do
|
|
|
|
setup [:create_user, :log_in, :create_new_site]
|
2020-01-06 16:51:43 +03:00
|
|
|
|
2021-09-20 17:17:11 +03:00
|
|
|
test "returns top referrers for a custom goal including conversion_rate", %{
|
|
|
|
conn: conn,
|
|
|
|
site: site
|
|
|
|
} do
|
2021-07-23 13:44:05 +03:00
|
|
|
populate_stats(site, [
|
|
|
|
build(:pageview,
|
|
|
|
referrer_source: "Twitter",
|
|
|
|
user_id: @user_id
|
|
|
|
),
|
|
|
|
build(:event,
|
|
|
|
name: "Signup",
|
|
|
|
user_id: @user_id
|
|
|
|
),
|
|
|
|
build(:pageview,
|
|
|
|
referrer_source: "Twitter"
|
|
|
|
)
|
|
|
|
])
|
|
|
|
|
2019-11-20 11:31:49 +03:00
|
|
|
filters = Jason.encode!(%{goal: "Signup"})
|
2020-06-08 10:35:13 +03:00
|
|
|
|
|
|
|
conn =
|
|
|
|
get(
|
|
|
|
conn,
|
2021-07-23 13:44:05 +03:00
|
|
|
"/api/stats/#{site.domain}/sources?period=day&filters=#{filters}"
|
2020-06-08 10:35:13 +03:00
|
|
|
)
|
2019-11-20 11:31:49 +03:00
|
|
|
|
|
|
|
assert json_response(conn, 200) == [
|
2021-09-29 14:28:29 +03:00
|
|
|
%{
|
|
|
|
"name" => "Twitter",
|
|
|
|
"total_visitors" => 2,
|
2021-11-04 15:20:39 +03:00
|
|
|
"visitors" => 1,
|
2021-09-29 14:28:29 +03:00
|
|
|
"conversion_rate" => 50.0
|
|
|
|
}
|
2020-06-08 10:35:13 +03:00
|
|
|
]
|
2019-11-20 11:31:49 +03:00
|
|
|
end
|
2019-11-20 11:33:47 +03:00
|
|
|
|
2021-09-20 17:17:11 +03:00
|
|
|
test "returns top referrers for a pageview goal including conversion_rate", %{
|
|
|
|
conn: conn,
|
|
|
|
site: site
|
|
|
|
} do
|
2021-07-23 13:44:05 +03:00
|
|
|
populate_stats(site, [
|
|
|
|
build(:pageview,
|
|
|
|
referrer_source: "Twitter",
|
|
|
|
user_id: @user_id
|
|
|
|
),
|
|
|
|
build(:pageview,
|
|
|
|
pathname: "/register",
|
|
|
|
user_id: @user_id
|
|
|
|
),
|
|
|
|
build(:pageview,
|
|
|
|
referrer_source: "Twitter"
|
|
|
|
)
|
|
|
|
])
|
|
|
|
|
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,
|
2021-07-23 13:44:05 +03:00
|
|
|
"/api/stats/#{site.domain}/sources?period=day&filters=#{filters}"
|
2020-06-08 10:35:13 +03:00
|
|
|
)
|
2019-11-20 11:33:47 +03:00
|
|
|
|
|
|
|
assert json_response(conn, 200) == [
|
2021-09-29 14:28:29 +03:00
|
|
|
%{
|
|
|
|
"name" => "Twitter",
|
|
|
|
"total_visitors" => 2,
|
2021-11-04 15:20:39 +03:00
|
|
|
"visitors" => 1,
|
2021-09-29 14:28:29 +03:00
|
|
|
"conversion_rate" => 50.0
|
|
|
|
}
|
2020-06-08 10:35:13 +03:00
|
|
|
]
|
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
|
2021-07-23 13:44:05 +03:00
|
|
|
setup [:create_user, :log_in, :create_new_site]
|
2019-11-19 07:30:42 +03:00
|
|
|
|
|
|
|
test "returns top referrers for a particular source", %{conn: conn, site: site} do
|
2021-07-23 13:44:05 +03:00
|
|
|
populate_stats(site, [
|
|
|
|
build(:pageview,
|
|
|
|
referrer_source: "10words",
|
|
|
|
referrer: "10words.com"
|
|
|
|
),
|
|
|
|
build(:pageview,
|
|
|
|
referrer_source: "10words",
|
|
|
|
referrer: "10words.com"
|
|
|
|
),
|
|
|
|
build(:pageview,
|
|
|
|
referrer_source: "10words",
|
|
|
|
referrer: "10words.com/page1"
|
|
|
|
),
|
|
|
|
build(:pageview,
|
|
|
|
referrer_source: "ignored",
|
|
|
|
referrer: "ignored"
|
|
|
|
)
|
|
|
|
])
|
2020-11-03 12:20:11 +03:00
|
|
|
|
|
|
|
conn =
|
|
|
|
get(
|
|
|
|
conn,
|
2021-07-23 13:44:05 +03:00
|
|
|
"/api/stats/#{site.domain}/referrers/10words?period=day"
|
2020-11-03 12:20:11 +03:00
|
|
|
)
|
2019-11-19 07:30:42 +03:00
|
|
|
|
|
|
|
assert json_response(conn, 200) == %{
|
2021-07-23 13:44:05 +03:00
|
|
|
"total_visitors" => 3,
|
2020-06-08 10:35:13 +03:00
|
|
|
"referrers" => [
|
2021-11-04 15:20:39 +03:00
|
|
|
%{"name" => "10words.com", "visitors" => 2},
|
|
|
|
%{"name" => "10words.com/page1", "visitors" => 1}
|
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
|
2021-07-23 13:44:05 +03:00
|
|
|
populate_stats(site, [
|
|
|
|
build(:pageview,
|
|
|
|
referrer_source: "10words",
|
|
|
|
referrer: "10words.com",
|
|
|
|
user_id: @user_id,
|
|
|
|
timestamp: ~N[2021-01-01 00:00:00]
|
|
|
|
),
|
|
|
|
build(:pageview,
|
|
|
|
referrer_source: "10words",
|
|
|
|
referrer: "10words.com",
|
|
|
|
user_id: @user_id,
|
|
|
|
timestamp: ~N[2021-01-01 00:15:00]
|
|
|
|
),
|
|
|
|
build(:pageview,
|
|
|
|
referrer_source: "10words",
|
|
|
|
referrer: "10words.com",
|
|
|
|
timestamp: ~N[2021-01-01 00:15:00]
|
|
|
|
),
|
|
|
|
build(:pageview,
|
|
|
|
referrer_source: "ignored",
|
|
|
|
referrer: "ignored",
|
|
|
|
timestamp: ~N[2021-01-01 00:00:00]
|
|
|
|
)
|
|
|
|
])
|
2020-11-03 12:20:11 +03:00
|
|
|
|
2020-06-08 10:35:13 +03:00
|
|
|
conn =
|
|
|
|
get(
|
|
|
|
conn,
|
2021-07-23 13:44:05 +03:00
|
|
|
"/api/stats/#{site.domain}/referrers/10words?period=day&date=2021-01-01&detailed=true"
|
2020-06-08 10:35:13 +03:00
|
|
|
)
|
2020-01-06 16:51:43 +03:00
|
|
|
|
|
|
|
assert json_response(conn, 200) == %{
|
2021-07-23 13:44:05 +03:00
|
|
|
"total_visitors" => 2,
|
2020-06-08 10:35:13 +03:00
|
|
|
"referrers" => [
|
2020-07-21 09:58:00 +03:00
|
|
|
%{
|
2021-07-23 13:44:05 +03:00
|
|
|
"name" => "10words.com",
|
2021-11-04 15:20:39 +03:00
|
|
|
"visitors" => 2,
|
2020-07-21 09:58:00 +03:00
|
|
|
"bounce_rate" => 50.0,
|
2021-07-23 13:44:05 +03:00
|
|
|
"visit_duration" => 450
|
2020-07-21 09:58:00 +03:00
|
|
|
}
|
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")
|
2021-07-23 13:44:05 +03:00
|
|
|
|
|
|
|
populate_stats(site, [
|
2021-09-21 11:53:21 +03:00
|
|
|
build(:pageview,
|
|
|
|
referrer_source: "DuckDuckGo",
|
|
|
|
referrer: "duckduckgo.com"
|
|
|
|
),
|
2021-07-23 13:44:05 +03:00
|
|
|
build(:pageview,
|
|
|
|
referrer_source: "Google",
|
|
|
|
referrer: "google.com"
|
|
|
|
),
|
|
|
|
build(:pageview,
|
|
|
|
referrer_source: "Google",
|
|
|
|
referrer: "google.com"
|
|
|
|
)
|
|
|
|
])
|
|
|
|
|
|
|
|
conn = get(conn, "/api/stats/#{site.domain}/referrers/Google?period=day")
|
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
|
|
|
|
2020-01-16 16:40:06 +03:00
|
|
|
test "returns top referring urls for a custom goal", %{conn: conn, site: site} do
|
2021-07-23 13:44:05 +03:00
|
|
|
populate_stats(site, [
|
|
|
|
build(:pageview,
|
|
|
|
referrer_source: "10words",
|
|
|
|
referrer: "10words.com"
|
|
|
|
),
|
|
|
|
build(:pageview,
|
|
|
|
referrer_source: "10words",
|
|
|
|
referrer: "10words.com",
|
|
|
|
user_id: @user_id
|
|
|
|
),
|
|
|
|
build(:event,
|
|
|
|
name: "Signup",
|
|
|
|
user_id: @user_id
|
|
|
|
),
|
|
|
|
build(:event,
|
|
|
|
name: "Signup"
|
|
|
|
)
|
|
|
|
])
|
|
|
|
|
2020-01-16 16:40:06 +03:00
|
|
|
filters = Jason.encode!(%{goal: "Signup"})
|
2020-06-08 10:35:13 +03:00
|
|
|
|
|
|
|
conn =
|
|
|
|
get(
|
|
|
|
conn,
|
2021-07-23 13:44:05 +03:00
|
|
|
"/api/stats/#{site.domain}/referrers/10words?period=day&filters=#{filters}"
|
2020-06-08 10:35:13 +03:00
|
|
|
)
|
2020-01-16 16:40:06 +03:00
|
|
|
|
|
|
|
assert json_response(conn, 200) == %{
|
2021-07-23 13:44:05 +03:00
|
|
|
"total_visitors" => 1,
|
2020-06-08 10:35:13 +03:00
|
|
|
"referrers" => [
|
2021-09-29 14:28:29 +03:00
|
|
|
%{
|
|
|
|
"name" => "10words.com",
|
|
|
|
"total_visitors" => 2,
|
|
|
|
"conversion_rate" => 50.0,
|
2021-11-04 15:20:39 +03:00
|
|
|
"visitors" => 1
|
2021-09-29 14:28:29 +03:00
|
|
|
}
|
2020-06-08 10:35:13 +03:00
|
|
|
]
|
|
|
|
}
|
2020-01-16 16:40:06 +03:00
|
|
|
end
|
|
|
|
|
|
|
|
test "returns top referring urls for a pageview goal", %{conn: conn, site: site} do
|
2021-07-23 13:44:05 +03:00
|
|
|
populate_stats(site, [
|
|
|
|
build(:pageview,
|
|
|
|
referrer_source: "10words",
|
|
|
|
referrer: "10words.com"
|
|
|
|
),
|
|
|
|
build(:pageview,
|
|
|
|
referrer_source: "10words",
|
|
|
|
referrer: "10words.com",
|
|
|
|
user_id: @user_id
|
|
|
|
),
|
|
|
|
build(:pageview,
|
|
|
|
pathname: "/register",
|
|
|
|
user_id: @user_id
|
|
|
|
),
|
|
|
|
build(:pageview,
|
|
|
|
pathname: "/register"
|
|
|
|
)
|
|
|
|
])
|
|
|
|
|
2020-01-16 16:40:06 +03:00
|
|
|
filters = Jason.encode!(%{goal: "Visit /register"})
|
2020-06-08 10:35:13 +03:00
|
|
|
|
|
|
|
conn =
|
|
|
|
get(
|
|
|
|
conn,
|
2021-07-23 13:44:05 +03:00
|
|
|
"/api/stats/#{site.domain}/referrers/10words?period=day&filters=#{filters}"
|
2020-06-08 10:35:13 +03:00
|
|
|
)
|
2020-01-16 16:40:06 +03:00
|
|
|
|
2020-02-12 12:11:02 +03:00
|
|
|
assert json_response(conn, 200) == %{
|
2021-07-23 13:44:05 +03:00
|
|
|
"total_visitors" => 1,
|
2020-06-08 10:35:13 +03:00
|
|
|
"referrers" => [
|
2021-09-29 14:28:29 +03:00
|
|
|
%{
|
|
|
|
"name" => "10words.com",
|
|
|
|
"total_visitors" => 2,
|
|
|
|
"conversion_rate" => 50.0,
|
2021-11-04 15:20:39 +03:00
|
|
|
"visitors" => 1
|
2021-09-29 14:28:29 +03:00
|
|
|
}
|
2020-06-08 10:35:13 +03:00
|
|
|
]
|
|
|
|
}
|
2020-01-16 16:40:06 +03:00
|
|
|
end
|
|
|
|
end
|
2019-11-19 07:30:42 +03:00
|
|
|
end
|