Test Google referrer drilldown

This commit is contained in:
Uku Taht 2019-11-20 16:42:45 +08:00
parent 9feecf6741
commit f71f98a521
5 changed files with 30 additions and 3 deletions

View File

@ -42,7 +42,8 @@ config :ref_inspector,
database_path: "priv/ref_inspector"
config :plausible,
paddle_api: Plausible.Billing.PaddleApi
paddle_api: Plausible.Billing.PaddleApi,
google_api: Plausible.Google.Api
# Import environment specific config. This must remain at the bottom
# of this file so it overrides the configuration defined above.

View File

@ -24,4 +24,5 @@ config :plausible, Plausible.Mailer,
adapter: Bamboo.TestAdapter
config :plausible,
paddle_api: Plausible.PaddleApi.Mock
paddle_api: Plausible.PaddleApi.Mock,
google_api: Plausible.Google.Api.Mock

View File

@ -32,12 +32,15 @@ defmodule PlausibleWeb.Api.StatsController do
json(conn, Stats.top_referrers(site, query, params["limit"] || 5))
end
@google_api Application.fetch_env!(:plausible, :google_api)
def referrer_drilldown(conn, %{"referrer" => "Google"} = params) do
site = conn.assigns[:site] |> Repo.preload(:google_auth)
query = Stats.Query.from(site.timezone, params)
search_terms = if site.google_auth && site.google_auth.property && !query.filters["goal"] do
Plausible.Google.Api.fetch_stats(site.google_auth, query)
@google_api.fetch_stats(site.google_auth, query)
end
case search_terms do

View File

@ -84,5 +84,19 @@ defmodule PlausibleWeb.Api.StatsController.ReferrersTest do
]
}
end
test "gets keywords from Google", %{conn: conn, user: user, site: site} do
insert(:google_auth, user: user, user: user,site: site, property: "sc-domain:example.com")
insert(:pageview, hostname: site.domain, referrer: "google.com", referrer_source: "Google", timestamp: ~N[2019-01-01 01:00:00])
insert(:pageview, hostname: site.domain, referrer: "google.com", referrer_source: "Google", timestamp: ~N[2019-01-01 02:00:00])
conn = get(conn, "/api/stats/#{site.domain}/referrers/Google?period=day&date=2019-01-01")
{:ok, terms} = Plausible.Google.Api.Mock.fetch_stats(nil, nil)
assert json_response(conn, 200) == %{
"total_visitors" => 2,
"search_terms" => terms
}
end
end
end

View File

@ -0,0 +1,8 @@
defmodule Plausible.Google.Api.Mock do
def fetch_stats(_auth, _query) do
{:ok, [
%{"name" => "simple web analytics", "count" => 6},
%{"name" => "open-source analytics", "count" => 2},
]}
end
end