Skip to content

Commit dc3640a

Browse files
committed
re-enqueue ga4 import if auth expires
1 parent 902f94c commit dc3640a

3 files changed

Lines changed: 43 additions & 0 deletions

File tree

lib/plausible/google/ga4/api.ex

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,9 @@ defmodule Plausible.Google.GA4.API do
170170
{:error, {:rate_limit_exceeded, details}} ->
171171
{:error, {:rate_limit_exceeded, details}}
172172

173+
{:error, {:authentication_failed, details}} ->
174+
{:error, {:authentication_failed, details}}
175+
173176
{:error, cause} ->
174177
if attempt >= @max_attempts do
175178
Logger.debug(

lib/plausible/google/ga4/http.ex

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,18 @@ defmodule Plausible.Google.GA4.HTTP do
6969
{:error,
7070
{:rate_limit_exceeded, dataset: report_request.dataset, offset: report_request.offset}}
7171

72+
{:error, %{reason: %{status: 401, body: body}} = error} ->
73+
log_ce_error("retrieving report for #{report_request.dataset}", error)
74+
75+
Logger.debug(
76+
"[#{inspect(__MODULE__)}:#{report_request.property}] Request failed for #{report_request.dataset} due to invalid auth."
77+
)
78+
79+
Sentry.Context.set_extra_context(%{ga_response: %{body: body, status: 401}})
80+
81+
{:error,
82+
{:authentication_failed, dataset: report_request.dataset, offset: report_request.offset}}
83+
7284
{:error, %{reason: %{status: status, body: body}} = error} ->
7385
log_ce_error("retrieving report for #{report_request.dataset}", error)
7486

lib/plausible/imported/google_analytics4.ex

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,34 @@ defmodule Plausible.Imported.GoogleAnalytics4 do
123123

124124
{:error, :rate_limit_exceeded, skip_purge?: true, skip_mark_failed?: true}
125125

126+
{:error, {:authentication_failed, details}} ->
127+
site_import = Repo.preload(site_import, [:site, :imported_by])
128+
dataset = Keyword.fetch!(details, :dataset)
129+
offset = Keyword.fetch!(details, :offset)
130+
{access_token, refresh_token, token_expires_at} = auth
131+
132+
resume_import_opts = [
133+
property: property,
134+
label: property,
135+
start_date: date_range.first,
136+
end_date: date_range.last,
137+
access_token: access_token,
138+
refresh_token: refresh_token,
139+
token_expires_at: token_expires_at,
140+
resume_from_import_id: site_import.id,
141+
resume_from_dataset: dataset,
142+
resume_from_offset: offset,
143+
job_opts: [schedule_in: {65, :minutes}, unique: nil]
144+
]
145+
146+
new_import(
147+
site_import.site,
148+
site_import.imported_by,
149+
resume_import_opts
150+
)
151+
152+
{:error, :authentication_failed, skip_purge?: true, skip_mark_failed?: true}
153+
126154
other ->
127155
other
128156
end

0 commit comments

Comments
 (0)