Open source, privacy-first web analytics. Lightweight, cookie-free Google Analytics alternative. Self-hosted or cloud.
TypeScript analyticsanalytics-dashboardclickhouseelixirgoogle-analyticsmarketingopen-source-analyticsphoenixplausible-analyticspostgresqlprivacyprivacy-friendlyself-hostedsimple-analyticsstatisticstailwindcssweb-analyticswebsite-analyticswebsite-statswebsite-tracking
Google Search Terms error message: too recent period (#5212)
RobertJoonas08d865b21484
diff --git a/assets/js/dashboard/stats/sources/index.js b/assets/js/dashboard/stats/sources/index.jsindex 5cb2bc95cb..12bb54edf1 100644--- a/assets/js/dashboard/stats/sources/index.js+++ b/assets/js/dashboard/stats/sources/index.js@@ -1,16 +1,15 @@import React from 'react';-import SearchTerms from './search-terms';+import { SearchTerms } from './search-terms';import SourceList from './source-list';import ReferrerList from './referrer-list';import { getFiltersByKeyPrefix, isFilteringOnFixedValue } from '../../util/filters';-import { useQueryContext } from '../../query-context';-import { useSiteContext } from '../../site-context';+import { useQueryContext } from '../../query-context'export default function Sources() {const { query } = useQueryContext();- const site = useSiteContext();+if (isFilteringOnFixedValue(query, 'source', 'Google')) {- return <SearchTerms query={query} site={site} />+ return <SearchTerms />} else if (isFilteringOnFixedValue(query, 'source')) {const [[_operation, _filterKey, clauses]] = getFiltersByKeyPrefix(query, "source")return <ReferrerList source={clauses[0]} />diff --git a/assets/js/dashboard/stats/sources/search-terms.js b/assets/js/dashboard/stats/sources/search-terms.jsdeleted file mode 100644index cf38a5edb3..0000000000--- a/assets/js/dashboard/stats/sources/search-terms.js+++ /dev/null@@ -1,134 +0,0 @@-import React from 'react';-import FadeIn from '../../fade-in'-import Bar from '../bar'-import MoreLink from '../more-link'-import { numberShortFormatter } from '../../util/number-formatter'-import RocketIcon from '../modals/rocket-icon'-import * as api from '../../api'-import LazyLoader from '../../components/lazy-loader'-import { referrersGoogleRoute } from '../../router';--export function ConfigureSearchTermsCTA({site}) {- return (- <>- <div>Configure the integration to view search terms</div>- <a href={`/${encodeURIComponent(site.domain)}/settings/integrations`} className="button mt-4">Connect with Google</a>- </>- )-}--export default class SearchTerms extends React.Component {- constructor(props) {- super(props)- this.state = { loading: true, errorPayload: null }- this.onVisible = this.onVisible.bind(this)- this.fetchSearchTerms = this.fetchSearchTerms.bind(this)- }-- onVisible() {- this.fetchSearchTerms()- if (this.props.query.period === 'realtime') {- document.addEventListener('tick', this.fetchSearchTerms)- }- }-- componentDidUpdate(prevProps) {- if (this.props.query !== prevProps.query) {- this.setState({ loading: true, terms: null })- this.fetchSearchTerms()- }- }-- componentWillUnmount() {- document.removeEventListener('tick', this.fetchSearchTerms)- }-- fetchSearchTerms() {- api.get(`/api/stats/${encodeURIComponent(this.props.site.domain)}/referrers/Google`, this.props.query)- .then((res) => this.setState({- loading: false,- searchTerms: res.results,- errorPayload: null- })).catch((error) => {- this.setState({ loading: false, searchTerms: [], errorPayload: error.payload })- })- }-- renderSearchTerm(term) {- return (- <div className="flex items-center justify-between my-1 text-sm" key={term.name}>- <Bar- count={term.visitors}- all={this.state.searchTerms}- bg="bg-blue-50 dark:bg-gray-500 dark:bg-opacity-15"- maxWidthDeduction="4rem"- >- <span className="flex px-2 py-1.5 dark:text-gray-300 z-9 relative break-all">- <span className="md:truncate block">- {term.name}- </span>- </span>- </Bar>- <span className="font-medium dark:text-gray-200">{numberShortFormatter(term.visitors)}</span>- </div>- )- }-- renderList() {- if (this.state.errorPayload) {- const {reason, is_admin, error} = this.state.errorPayload-- return (- <div className="text-center text-gray-700 dark:text-gray-300 text-sm mt-20">- <RocketIcon />- <div>{error}</div>- {reason === 'not_configured' && is_admin && <ConfigureSearchTermsCTA site={this.props.site}/> }- </div>- )- } else if (this.state.searchTerms.length > 0) {- const valLabel = this.props.query.period === 'realtime' ? 'Current visitors' : 'Visitors'-- return (- <React.Fragment>- <div className="flex items-center mt-3 mb-2 justify-between text-gray-500 dark:text-gray-400 text-xs font-bold tracking-wide">- <span>Search term</span>- <span>{valLabel}</span>- </div>-- {this.state.searchTerms.map(this.renderSearchTerm.bind(this))}- </React.Fragment>- )- } else {- return (- <div className="text-center text-gray-700 dark:text-gray-300 ">- <div className="mt-44 mx-auto font-medium text-gray-500 dark:text-gray-400">No data yet</div>- </div>- )- }- }-- renderContent() {- if (this.state.searchTerms) {- return (- <React.Fragment>- <h3 className="font-bold dark:text-gray-100">Search Terms</h3>- {this.renderList()}- <MoreLink list={this.state.searchTerms} linkProps={{ path: referrersGoogleRoute.path, search: (search) => search }} className="w-full pb-4 absolute bottom-0 left-0" />- </React.Fragment>- )- }- }-- render() {- return (- <div>- {this.state.loading && <div className="loading mt-44 mx-auto"><div></div></div>}- <FadeIn show={!this.state.loading} className="flex-grow">- <LazyLoader onVisible={this.onVisible}>- {this.renderContent()}- </LazyLoader>- </FadeIn>- </div>- )- }-}diff --git a/assets/js/dashboard/stats/sources/search-terms.tsx b/assets/js/dashboard/stats/sources/search-terms.tsxnew file mode 100644index 0000000000..258258ba4b--- /dev/null+++ b/assets/js/dashboard/stats/sources/search-terms.tsx@@ -0,0 +1,211 @@+/** @format */++import React, { useEffect, useCallback } from 'react'+import FadeIn from '../../fade-in'+import Bar from '../bar'+import MoreLink from '../more-link'+import { numberShortFormatter } from '../../util/number-formatter'+import RocketIcon from '../modals/rocket-icon'+import * as api from '../../api'+import LazyLoader from '../../components/lazy-loader'+import { referrersGoogleRoute } from '../../router'+import { useQueryContext } from '../../query-context'+import { PlausibleSite, useSiteContext } from '../../site-context'++interface SearchTerm {+ name: string+ visitors: number+}++type ErrorCode = 'not_configured' | 'unsupported_filters' | 'period_too_recent'++interface ErrorPayload {+ error_code: ErrorCode+ is_admin: boolean+}++function ErrorMessage({ code }: { code: ErrorCode }): JSX.Element {+ if (code === 'not_configured') {+ return <div>The site is not connected to Google Search Keywords</div>+ } else if (code === 'unsupported_filters') {+ return (+ <div>+ Unable to fetch keyword data from Search Console because it does not+ support the current set of filters+ </div>+ )+ } else if (code === 'period_too_recent') {+ return (+ <div>+ No search terms were found for this period. Please adjust or extend your+ time range. Check{' '}+ <a+ href="https://plausible.io/docs/google-search-console-integration#i-dont-see-google-search-query-data-in-my-dashboard"+ target="_blank"+ rel="noreferrer"+ className="hover:underline text-indigo-700 dark:text-indigo-500"+ >+ our documentation+ </a>{' '}+ for more details.+ </div>+ )+ } else {+ return <div>Unable to fetch keyword data from Search Console</div>+ }+}++function ConfigureSearchTermsCTA({+ site+}: {+ site: PlausibleSite+}): JSX.Element {+ return (+ <>+ <div>Configure the integration to view search terms</div>+ <a+ href={`/${encodeURIComponent(site.domain)}/settings/integrations`}+ className="button mt-4"+ >+ Connect with Google+ </a>+ </>+ )+}++export function SearchTerms() {+ const site = useSiteContext()+ const { query } = useQueryContext()++ const [loading, setLoading] = React.useState(true)+ const [errorPayload, setErrorPayload] = React.useState<null | ErrorPayload>(+ null+ )+ const [searchTerms, setSearchTerms] = React.useState<null | SearchTerm[]>(+ null+ )+ const [visible, setVisible] = React.useState(false)++ const fetchSearchTerms = useCallback(() => {+ api+ .get(+ `/api/stats/${encodeURIComponent(site.domain)}/referrers/Google`,+ query+ )+ .then((res) => {+ setLoading(false)+ setSearchTerms(res.results)+ setErrorPayload(null)+ })+ .catch((error) => {+ setLoading(false)+ setSearchTerms(null)+ setErrorPayload(error.payload)+ })+ }, [query, site.domain])++ useEffect(() => {+ if (visible) {+ setLoading(true)+ setSearchTerms([])+ fetchSearchTerms()+ }+ }, [query, fetchSearchTerms, visible])++ const onVisible = () => {+ setVisible(true)+ }++ const renderList = () => {+ if (searchTerms && searchTerms.length > 0) {+ return (+ <React.Fragment>+ <div className="flex items-center mt-3 mb-2 justify-between text-gray-500 dark:text-gray-400 text-xs font-bold tracking-wide">+ <span>Search term</span>+ <span>Visitors</span>+ </div>+ {searchTerms &&+ searchTerms.map((term: SearchTerm) => (+ <div+ className="flex items-center justify-between my-1 text-sm"+ key={term.name}+ >+ <Bar+ count={term.visitors}+ all={searchTerms}+ bg="bg-blue-50 dark:bg-gray-500 dark:bg-opacity-15"+ maxWidthDeduction="4rem"+ >+ <span className="flex px-2 py-1.5 dark:text-gray-300 z-9 relative break-all">+ <span className="md:truncate block">{term.name}</span>+ </span>+ </Bar>+ <span className="font-medium dark:text-gray-200">+ {numberShortFormatter(term.visitors)}+ </span>+ </div>+ ))}+ <MoreLink+ list={searchTerms}+ linkProps={{+ path: referrersGoogleRoute.path,+ search: (search: Record<string, unknown>) => search+ }}+ className="w-full pb-4 absolute bottom-0 left-0"+ onClick={undefined}+ />+ </React.Fragment>+ )+ }+ }++ const renderNoDataYet = () => {+ if (searchTerms && searchTerms.length === 0) {+ return (+ <div className="text-center text-gray-700 dark:text-gray-300 ">+ <div className="mt-44 mx-auto font-medium text-gray-500 dark:text-gray-400">+ No data yet+ </div>+ </div>+ )+ }+ }++ const renderError = () => {+ if (errorPayload) {+ const { is_admin, error_code } = errorPayload++ return (+ <div className="text-center text-gray-700 dark:text-gray-300 text-sm mt-20">+ <RocketIcon />+ <ErrorMessage code={error_code} />+ {error_code === 'not_configured' && is_admin && (+ <ConfigureSearchTermsCTA site={site} />+ )}+ </div>+ )+ }+ }++ return (+ <div className="flex flex-col h-full">+ <h3 className="font-bold dark:text-gray-100">Search Terms</h3>+ <div className="relative flex-grow">+ {loading && (+ <div className="absolute inset-0 flex justify-center items-center">+ <div className="loading">+ <div />+ </div>+ </div>+ )}+ <FadeIn show={!loading} className="flex-grow">+ <LazyLoader onVisible={onVisible}>+ {searchTerms && searchTerms.length > 0 && renderList()}+ {searchTerms && searchTerms.length === 0 && renderNoDataYet()}+ {errorPayload && renderError()}+ </LazyLoader>+ </FadeIn>+ </div>+ </div>+ )+}diff --git a/lib/plausible_web/controllers/api/stats_controller.ex b/lib/plausible_web/controllers/api/stats_controller.exindex bd8c0adbf0..f5af00a821 100644--- a/lib/plausible_web/controllers/api/stats_controller.ex+++ b/lib/plausible_web/controllers/api/stats_controller.ex@@ -789,34 +789,48 @@ defmodule PlausibleWeb.Api.StatsController dosearch = params["search"] || ""+ # TODO: stop returning `error` and `reason` (not needed anymore)not_configured_error_payload =%{+ error_code: :not_configured,error: "The site is not connected to Google Search Keywords",reason: :not_configured,is_admin: is_admin}+ # TODO: stop returning `error` and `reason` (not needed anymore)unsupported_filters_error_payload = %{+ error_code: :unsupported_filters,error:"Unable to fetch keyword data from Search Console because it does not support the current set of filters",reason: :unsupported_filters}- case google_api().fetch_stats(site, query, pagination, search) do- {:error, :google_property_not_configured} ->+ search_terms = google_api().fetch_stats(site, query, pagination, search)+ period_too_recent? = DateTime.diff(query.now, query.utc_time_range.first, :hour) < 72++ case {search_terms, period_too_recent?} do+ {{:error, :google_property_not_configured}, _} ->conn|> put_status(422)|> json(not_configured_error_payload)- {:error, :unsupported_filters} ->+ {{:error, :unsupported_filters}, _} ->conn|> put_status(422)|> json(unsupported_filters_error_payload)- {:ok, terms} ->+ {{:ok, []}, _period_too_recent? = true} ->+ # We consider this an error case because Google Search Console+ # data is usually delayed 1-3 days.+ conn+ |> put_status(422)+ |> json(%{error_code: :period_too_recent})++ {{:ok, terms}, _} ->json(conn, %{results: terms})- {:error, error} ->+ {{:error, error}, _} ->Logger.error("Plausible.Google.API.fetch_stats failed with error: `#{inspect(error)}`")conndiff --git a/test/plausible_web/controllers/api/stats_controller/sources_test.exs b/test/plausible_web/controllers/api/stats_controller/sources_test.exsindex a6704340d3..38f7ce275e 100644--- a/test/plausible_web/controllers/api/stats_controller/sources_test.exs+++ b/test/plausible_web/controllers/api/stats_controller/sources_test.exs@@ -1,5 +1,6 @@defmodule PlausibleWeb.Api.StatsController.SourcesTest douse PlausibleWeb.ConnCase+ use Plausible.Teams.Test@user_id Enum.random(1000..9999)@@ -1736,6 +1737,86 @@ defmodule PlausibleWeb.Api.StatsController.SourcesTest doendend+ describe "GET /api/stats/:domain/referrer-drilldown (Google Search Terms)" do+ setup [:create_user, :log_in, :create_site]++ test "gets keywords from Google", %{conn: conn, site: site} do+ conn = get(conn, "/api/stats/#{site.domain}/referrers/Google?period=day")++ assert %{+ "results" => [+ %{"name" => "simple web analytics", "count" => 6},+ %{"name" => "open-source analytics", "count" => 2}+ ]+ } = json_response(conn, 200)+ end++ test "returns 200 with empty keywords list when no data returned from last 30d", %{+ conn: conn,+ site: site+ } do+ filters = Jason.encode!([[:is, "event:page", ["/empty"]]])++ conn = get(conn, "/api/stats/#{site.domain}/referrers/Google?period=30d&filters=#{filters}")++ assert json_response(conn, 200) == %{"results" => []}+ end++ test "returns 422 with error when no data returned and queried range is too recent", %{+ conn: conn,+ site: site+ } do+ filters = Jason.encode!([[:is, "event:page", ["/empty"]]])++ conn = get(conn, "/api/stats/#{site.domain}/referrers/Google?period=day&filters=#{filters}")++ assert json_response(conn, 422) == %{"error_code" => "period_too_recent"}+ end++ test "returns 422 with error when Google account not connected (admin)", %{+ conn: conn,+ site: site+ } do+ filters = Jason.encode!([[:is, "event:page", ["/not-configured"]]])++ conn = get(conn, "/api/stats/#{site.domain}/referrers/Google?period=day&filters=#{filters}")++ assert %{"error_code" => "not_configured", "is_admin" => true} = json_response(conn, 422)+ end++ test "returns 422 with error when Google account not connected (non-admin)", %{conn: conn} do+ site = new_site(public: true)++ filters = Jason.encode!([[:is, "event:page", ["/not-configured"]]])++ conn = get(conn, "/api/stats/#{site.domain}/referrers/Google?period=day&filters=#{filters}")++ %{+ "error_code" => "not_configured",+ "is_admin" => false+ } = json_response(conn, 422)+ end++ test "returns 422 with error when unsupported filters used", %{conn: conn, site: site} do+ filters = Jason.encode!([[:is, "event:page", ["/unsupported-filters"]]])++ conn = get(conn, "/api/stats/#{site.domain}/referrers/Google?period=day&filters=#{filters}")++ assert %{"error_code" => "unsupported_filters"} = json_response(conn, 422)+ end++ test "returns 502 when Google API responds with an unexpected error", %{+ conn: conn,+ site: site+ } do+ filters = Jason.encode!([[:is, "event:page", ["/unexpected-error"]]])++ conn = get(conn, "/api/stats/#{site.domain}/referrers/Google?period=day&filters=#{filters}")++ assert %{"error_code" => "not_configured"} = json_response(conn, 502)+ end+ end+describe "GET /api/stats/:domain/referrer-drilldown" dosetup [:create_user, :log_in, :create_site]@@ -1858,28 +1939,6 @@ defmodule PlausibleWeb.Api.StatsController.SourcesTest do]end- test "gets keywords from Google", %{conn: conn, site: site} do- populate_stats(site, [- build(:pageview,- referrer_source: "DuckDuckGo",- referrer: "duckduckgo.com"- ),- 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")- {:ok, terms} = Plausible.Google.API.Mock.fetch_stats(nil, nil, nil, nil)-- assert json_response(conn, 200) == %{"results" => terms}- end-test "returns top referring urls for a custom goal", %{conn: conn, site: site} dopopulate_stats(site, [build(:pageview,diff --git a/test/support/google_api_mock.ex b/test/support/google_api_mock.exindex 84329a6547..7d17f2c05d 100644--- a/test/support/google_api_mock.ex+++ b/test/support/google_api_mock.ex@@ -3,11 +3,30 @@ defmodule Plausible.Google.API.Mock doMock of API to Google services."""- def fetch_stats(_auth, _query, _pagination, _search) do- {:ok,- [- %{"name" => "simple web analytics", "count" => 6},- %{"name" => "open-source analytics", "count" => 2}- ]}+ @doc """+ This function uses the event:page filter (which can be passed as a query+ parameter into StatsController) as a hack to mock different responses.+ """+ def fetch_stats(_auth, query, _pagination, _search) do+ case query.filters do+ [[:is, "event:page", ["/empty"]]] ->+ {:ok, []}++ [[:is, "event:page", ["/unsupported-filters"]]] ->+ {:error, :unsupported_filters}++ [[:is, "event:page", ["/not-configured"]]] ->+ {:error, :google_property_not_configured}++ [[:is, "event:page", ["/unexpected-error"]]] ->+ {:error, :some_unexpected_error}++ _ ->+ {:ok,+ [+ %{"name" => "simple web analytics", "count" => 6},+ %{"name" => "open-source analytics", "count" => 2}+ ]}+ endendend