Update default ga_version to v4 and add warning message for v3 (#20936)

Sites which are already using ga3 will stay on that version, and will be shown a warning in the admin panel until they update.

https://meta.discourse.org/t/upgrade-to-google-analytics-4-before-july-2023/260498
This commit is contained in:
David Taylor 2023-04-04 13:14:20 +01:00 committed by GitHub
parent 6e2fd7a451
commit 2386ad12f2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 40 additions and 2 deletions

View File

@ -205,7 +205,8 @@ class AdminDashboardData
:email_polling_errored_recently,
:out_of_date_themes,
:unreachable_themes,
:watched_words_check
:watched_words_check,
:google_analytics_version_check
register_default_scheduled_problem_checks
@ -380,6 +381,10 @@ class AdminDashboardData
I18n.t("dashboard.subfolder_ends_in_slash") if Discourse.base_path =~ %r{/\z}
end
def google_analytics_version_check
I18n.t("dashboard.v3_analytics_deprecated") if SiteSetting.ga_version == "v3_analytics"
end
def email_polling_errored_recently
errors = Jobs::PollMailbox.errors_in_past_24_hours
if errors > 0

View File

@ -1475,6 +1475,7 @@ en:
out_of_date_themes: "Updates are available for the following themes:"
unreachable_themes: "We were unable to check for updates on the following themes:"
watched_word_regexp_error: "The regular expression for '%{action}' watched words is invalid. Please check your <a href='%{base_path}/admin/customize/watched_words'>Watched Word settings</a>, or disable the 'watched words regular expressions' site setting."
v3_analytics_deprecated: "Your Discourse is currently using Google Analytics 3, which will no longer be supported after July 2023. <a href='https://meta.discourse.org/t/260498'>Upgrade to Google Analytics 4</a> now to continue receiving valuable insights and analytics for your website's performance."
site_settings:
allow_bulk_invite: "Allow bulk invites by uploading a CSV file"

View File

@ -150,7 +150,7 @@ basic:
max: 36500
ga_version:
type: enum
default: v3_analytics
default: v4_gtag
choices:
- v3_analytics
- v4_gtag

View File

@ -0,0 +1,30 @@
# frozen_string_literal: true
class ChangeGoogleAnalyticsDefault < ActiveRecord::Migration[7.0]
def up
should_persist_old_default =
Migration::Helpers.existing_site? && tracking_code && current_db_version != "v4_gtag"
return if !should_persist_old_default
execute <<~SQL
INSERT INTO site_settings(name, data_type, value, created_at, updated_at)
VALUES ('ga_version', 7, 'v3_analytics', now(), now())
ON CONFLICT DO NOTHING
SQL
end
def down
raise ActiveRecord::IrreversibleMigration
end
def tracking_code
DB.query_single("SELECT value FROM site_settings WHERE name='ga_universal_tracking_code'")[
0
].presence
end
def current_db_version
DB.query_single("SELECT value FROM site_settings WHERE name='ga_version'")[0].presence
end
end

View File

@ -85,6 +85,8 @@ RSpec.describe ContentSecurityPolicy do
before { SiteSetting.ga_universal_tracking_code = "UA-12345678-9" }
it "allowlists Google Analytics v3 when integrated" do
SiteSetting.ga_version = "v3_analytics"
script_srcs = parse(policy)["script-src"]
expect(script_srcs).to include("https://www.google-analytics.com/analytics.js")
expect(script_srcs).not_to include("https://www.googletagmanager.com/gtag/js")