2019-11-19 07:30:42 +03:00
|
|
|
defmodule PlausibleWeb.Api.StatsController.ConversionsTest do
|
|
|
|
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
|
|
|
|
|
|
|
describe "GET /api/stats/:domain/conversions" 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 mixed conversions in ordered by count", %{conn: conn, site: site} do
|
2021-07-23 13:44:05 +03:00
|
|
|
populate_stats(site, [
|
|
|
|
build(:pageview, pathname: "/"),
|
|
|
|
build(:pageview, pathname: "/"),
|
|
|
|
build(:pageview, pathname: "/register"),
|
|
|
|
build(:pageview, pathname: "/register"),
|
|
|
|
build(:event, name: "Signup", "meta.key": ["variant"], "meta.value": ["A"]),
|
|
|
|
build(:event,
|
|
|
|
user_id: @user_id,
|
|
|
|
name: "Signup",
|
|
|
|
"meta.key": ["variant"],
|
|
|
|
"meta.value": ["A"]
|
|
|
|
),
|
|
|
|
build(:event,
|
|
|
|
user_id: @user_id,
|
|
|
|
name: "Signup",
|
|
|
|
"meta.key": ["variant"],
|
|
|
|
"meta.value": ["B"]
|
|
|
|
)
|
|
|
|
])
|
|
|
|
|
2020-05-18 12:44:52 +03:00
|
|
|
insert(:goal, %{domain: site.domain, page_path: "/register"})
|
2019-11-19 07:30:42 +03:00
|
|
|
insert(:goal, %{domain: site.domain, event_name: "Signup"})
|
|
|
|
|
2021-07-23 13:44:05 +03:00
|
|
|
conn = get(conn, "/api/stats/#{site.domain}/conversions?period=day")
|
2019-11-19 07:30:42 +03:00
|
|
|
|
|
|
|
assert json_response(conn, 200) == [
|
2020-11-03 12:20:11 +03:00
|
|
|
%{
|
|
|
|
"name" => "Signup",
|
2021-07-23 13:44:05 +03:00
|
|
|
"count" => 2,
|
2020-11-03 12:20:11 +03:00
|
|
|
"total_count" => 3,
|
|
|
|
"prop_names" => ["variant"],
|
2021-07-23 13:44:05 +03:00
|
|
|
"conversion_rate" => 33.3
|
2020-11-03 12:20:11 +03:00
|
|
|
},
|
|
|
|
%{
|
|
|
|
"name" => "Visit /register",
|
|
|
|
"count" => 2,
|
|
|
|
"total_count" => 2,
|
|
|
|
"prop_names" => nil,
|
2021-07-23 13:44:05 +03:00
|
|
|
"conversion_rate" => 33.3
|
2020-11-03 12:20:11 +03:00
|
|
|
}
|
2020-06-08 10:35:13 +03:00
|
|
|
]
|
2019-11-19 07:30:42 +03:00
|
|
|
end
|
|
|
|
end
|
2019-11-20 11:49:22 +03:00
|
|
|
|
|
|
|
describe "GET /api/stats/:domain/conversions - with goal filter" do
|
2021-07-23 13:44:05 +03:00
|
|
|
setup [:create_user, :log_in, :create_new_site]
|
2019-11-20 11:49:22 +03:00
|
|
|
|
|
|
|
test "returns only the conversion tha is filtered for", %{conn: conn, site: site} do
|
2021-07-23 13:44:05 +03:00
|
|
|
populate_stats(site, [
|
|
|
|
build(:pageview, pathname: "/"),
|
|
|
|
build(:pageview, pathname: "/"),
|
|
|
|
build(:pageview, pathname: "/register"),
|
|
|
|
build(:event, name: "Signup", "meta.key": ["variant"], "meta.value": ["A"]),
|
|
|
|
build(:event, name: "Signup", "meta.key": ["variant"], "meta.value": ["A"]),
|
|
|
|
build(:event, name: "Signup", "meta.key": ["variant"], "meta.value": ["B"])
|
|
|
|
])
|
|
|
|
|
2020-05-18 12:44:52 +03:00
|
|
|
insert(:goal, %{domain: site.domain, page_path: "/register"})
|
2019-11-20 11:49:22 +03:00
|
|
|
insert(:goal, %{domain: site.domain, event_name: "Signup"})
|
|
|
|
|
|
|
|
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}/conversions?period=day&filters=#{filters}"
|
2020-06-08 10:35:13 +03:00
|
|
|
)
|
2019-11-20 11:49:22 +03:00
|
|
|
|
|
|
|
assert json_response(conn, 200) == [
|
2020-11-03 12:20:11 +03:00
|
|
|
%{
|
|
|
|
"name" => "Signup",
|
|
|
|
"count" => 3,
|
|
|
|
"total_count" => 3,
|
|
|
|
"prop_names" => ["variant"],
|
2021-07-23 13:44:05 +03:00
|
|
|
"conversion_rate" => 50
|
2020-11-03 12:20:11 +03:00
|
|
|
}
|
|
|
|
]
|
2019-11-20 11:49:22 +03:00
|
|
|
end
|
|
|
|
end
|
2020-10-28 12:09:04 +03:00
|
|
|
|
2020-10-30 12:05:19 +03:00
|
|
|
describe "GET /api/stats/:domain/property/:key" do
|
2021-07-23 13:44:05 +03:00
|
|
|
setup [:create_user, :log_in, :create_new_site]
|
|
|
|
|
|
|
|
test "returns property breakdown for goal", %{conn: conn, site: site} do
|
|
|
|
populate_stats(site, [
|
|
|
|
build(:pageview, pathname: "/"),
|
|
|
|
build(:pageview, pathname: "/"),
|
|
|
|
build(:pageview, pathname: "/register"),
|
|
|
|
build(:event, name: "Signup", "meta.key": ["variant"], "meta.value": ["A"]),
|
|
|
|
build(:event, name: "Signup", "meta.key": ["variant"], "meta.value": ["B"]),
|
|
|
|
build(:event, name: "Signup", "meta.key": ["variant"], "meta.value": ["B"])
|
|
|
|
])
|
2020-10-28 12:09:04 +03:00
|
|
|
|
|
|
|
insert(:goal, %{domain: site.domain, event_name: "Signup"})
|
|
|
|
filters = Jason.encode!(%{goal: "Signup"})
|
2020-10-30 12:05:19 +03:00
|
|
|
prop_key = "variant"
|
2020-10-28 12:09:04 +03:00
|
|
|
|
|
|
|
conn =
|
|
|
|
get(
|
|
|
|
conn,
|
2021-07-23 13:44:05 +03:00
|
|
|
"/api/stats/#{site.domain}/property/#{prop_key}?period=day&filters=#{filters}"
|
|
|
|
)
|
|
|
|
|
|
|
|
assert json_response(conn, 200) == [
|
|
|
|
%{"count" => 2, "name" => "B", "total_count" => 2, "conversion_rate" => 33.3},
|
|
|
|
%{"count" => 1, "name" => "A", "total_count" => 1, "conversion_rate" => 16.7}
|
|
|
|
]
|
|
|
|
end
|
|
|
|
|
|
|
|
test "returns (none) values in property breakdown for goal", %{conn: conn, site: site} do
|
|
|
|
populate_stats(site, [
|
|
|
|
build(:event, name: "Signup"),
|
|
|
|
build(:event, name: "Signup"),
|
|
|
|
build(:event, name: "Signup", "meta.key": ["variant"], "meta.value": ["A"]),
|
|
|
|
build(:event, name: "Signup", "meta.key": ["variant"], "meta.value": ["B"]),
|
|
|
|
build(:event, name: "Signup", "meta.key": ["variant"], "meta.value": ["B"]),
|
|
|
|
build(:event, name: "Signup", "meta.key": ["variant"], "meta.value": ["B"])
|
|
|
|
])
|
|
|
|
|
|
|
|
insert(:goal, %{domain: site.domain, event_name: "Signup"})
|
|
|
|
filters = Jason.encode!(%{goal: "Signup"})
|
|
|
|
prop_key = "variant"
|
|
|
|
|
|
|
|
conn =
|
|
|
|
get(
|
|
|
|
conn,
|
|
|
|
"/api/stats/#{site.domain}/property/#{prop_key}?period=day&filters=#{filters}"
|
|
|
|
)
|
|
|
|
|
|
|
|
assert json_response(conn, 200) == [
|
|
|
|
%{"count" => 3, "name" => "B", "total_count" => 3, "conversion_rate" => 50.0},
|
|
|
|
%{"count" => 2, "name" => "(none)", "total_count" => 2, "conversion_rate" => 33.3},
|
|
|
|
%{"count" => 1, "name" => "A", "total_count" => 1, "conversion_rate" => 16.7}
|
|
|
|
]
|
|
|
|
end
|
|
|
|
|
|
|
|
test "property breakdown with prop filter", %{conn: conn, site: site} do
|
|
|
|
populate_stats(site, [
|
|
|
|
build(:event, name: "Signup"),
|
|
|
|
build(:event, name: "Signup"),
|
|
|
|
build(:event, name: "Signup", "meta.key": ["variant"], "meta.value": ["A"]),
|
|
|
|
build(:event, name: "Signup", "meta.key": ["variant"], "meta.value": ["B"]),
|
|
|
|
build(:event, name: "Signup", "meta.key": ["variant"], "meta.value": ["B"]),
|
|
|
|
build(:event, name: "Signup", "meta.key": ["variant"], "meta.value": ["B"])
|
|
|
|
])
|
|
|
|
|
|
|
|
insert(:goal, %{domain: site.domain, event_name: "Signup"})
|
|
|
|
filters = Jason.encode!(%{goal: "Signup", props: %{"variant" => "B"}})
|
|
|
|
prop_key = "variant"
|
|
|
|
|
|
|
|
conn =
|
|
|
|
get(
|
|
|
|
conn,
|
|
|
|
"/api/stats/#{site.domain}/property/#{prop_key}?period=day&filters=#{filters}"
|
2020-10-28 12:09:04 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
assert json_response(conn, 200) == [
|
2021-07-23 13:44:05 +03:00
|
|
|
%{"count" => 3, "name" => "B", "total_count" => 3, "conversion_rate" => 100.0}
|
2020-11-03 12:20:11 +03:00
|
|
|
]
|
2020-10-28 12:09:04 +03:00
|
|
|
end
|
|
|
|
end
|
2021-03-02 15:53:03 +03:00
|
|
|
|
|
|
|
describe "GET /api/stats/:domain/conversions - with glob goals" do
|
|
|
|
setup [:create_user, :log_in, :create_site]
|
|
|
|
|
|
|
|
test "returns correct and sorted glob goal counts", %{conn: conn, site: site} do
|
|
|
|
insert(:goal, %{domain: site.domain, page_path: "/register"})
|
|
|
|
insert(:goal, %{domain: site.domain, page_path: "/reg*"})
|
|
|
|
insert(:goal, %{domain: site.domain, page_path: "/*/register"})
|
|
|
|
insert(:goal, %{domain: site.domain, page_path: "/billing**/success"})
|
|
|
|
insert(:goal, %{domain: site.domain, page_path: "/billing*/success"})
|
|
|
|
insert(:goal, %{domain: site.domain, page_path: "/signup"})
|
|
|
|
insert(:goal, %{domain: site.domain, page_path: "/signup/*"})
|
|
|
|
insert(:goal, %{domain: site.domain, page_path: "/signup/**"})
|
|
|
|
insert(:goal, %{domain: site.domain, page_path: "/*"})
|
|
|
|
insert(:goal, %{domain: site.domain, page_path: "/**"})
|
|
|
|
|
|
|
|
conn =
|
|
|
|
get(
|
|
|
|
conn,
|
|
|
|
"/api/stats/#{site.domain}/conversions?period=day&date=2019-07-01"
|
|
|
|
)
|
|
|
|
|
|
|
|
assert json_response(conn, 200) == [
|
|
|
|
%{
|
|
|
|
"conversion_rate" => 100.0,
|
|
|
|
"count" => 8,
|
|
|
|
"name" => "Visit /**",
|
|
|
|
"total_count" => 8,
|
|
|
|
"prop_names" => nil
|
|
|
|
},
|
|
|
|
%{
|
|
|
|
"conversion_rate" => 37.5,
|
|
|
|
"count" => 3,
|
|
|
|
"name" => "Visit /*",
|
|
|
|
"total_count" => 3,
|
|
|
|
"prop_names" => nil
|
|
|
|
},
|
|
|
|
%{
|
|
|
|
"conversion_rate" => 37.5,
|
|
|
|
"count" => 3,
|
|
|
|
"name" => "Visit /signup/**",
|
|
|
|
"total_count" => 3,
|
|
|
|
"prop_names" => nil
|
|
|
|
},
|
|
|
|
%{
|
|
|
|
"conversion_rate" => 25.0,
|
|
|
|
"count" => 2,
|
|
|
|
"name" => "Visit /billing**/success",
|
|
|
|
"total_count" => 2,
|
|
|
|
"prop_names" => nil
|
|
|
|
},
|
|
|
|
%{
|
|
|
|
"conversion_rate" => 25.0,
|
|
|
|
"count" => 2,
|
|
|
|
"name" => "Visit /reg*",
|
|
|
|
"total_count" => 2,
|
|
|
|
"prop_names" => nil
|
|
|
|
},
|
|
|
|
%{
|
|
|
|
"conversion_rate" => 12.5,
|
|
|
|
"count" => 1,
|
|
|
|
"name" => "Visit /billing*/success",
|
|
|
|
"total_count" => 1,
|
|
|
|
"prop_names" => nil
|
|
|
|
},
|
|
|
|
%{
|
|
|
|
"conversion_rate" => 12.5,
|
|
|
|
"count" => 1,
|
|
|
|
"name" => "Visit /register",
|
|
|
|
"total_count" => 1,
|
|
|
|
"prop_names" => nil
|
|
|
|
},
|
|
|
|
%{
|
|
|
|
"conversion_rate" => 12.5,
|
|
|
|
"count" => 1,
|
|
|
|
"name" => "Visit /signup/*",
|
|
|
|
"total_count" => 1,
|
|
|
|
"prop_names" => nil
|
|
|
|
}
|
|
|
|
]
|
|
|
|
end
|
|
|
|
end
|
2019-11-19 07:30:42 +03:00
|
|
|
end
|