Skip to content

A platform for community discussion. Free, open, simple.

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

David Taylor2386ad12f26b
diff --git a/app/models/admin_dashboard_data.rb b/app/models/admin_dashboard_data.rb
index 9ed9100df11..2b85dc6b5c0 100644
--- a/app/models/admin_dashboard_data.rb
+++ b/app/models/admin_dashboard_data.rb
@@ -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
diff --git a/config/locales/server.en.yml b/config/locales/server.en.yml
index d0336bab9bf..5cfd329a445 100644
--- a/config/locales/server.en.yml
+++ b/config/locales/server.en.yml
@@ -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"
diff --git a/config/site_settings.yml b/config/site_settings.yml
index 08e85af2317..6cdca72ada2 100644
--- a/config/site_settings.yml
+++ b/config/site_settings.yml
@@ -150,7 +150,7 @@ basic:
max: 36500
ga_version:
type: enum
- default: v3_analytics
+ default: v4_gtag
choices:
- v3_analytics
- v4_gtag
diff --git a/db/migrate/20230403094936_change_google_analytics_default.rb b/db/migrate/20230403094936_change_google_analytics_default.rb
new file mode 100644
index 00000000000..230c6580c84
--- /dev/null
+++ b/db/migrate/20230403094936_change_google_analytics_default.rb
@@ -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
diff --git a/spec/lib/content_security_policy_spec.rb b/spec/lib/content_security_policy_spec.rb
index 2d36edfbdcf..87ea6713836 100644
--- a/spec/lib/content_security_policy_spec.rb
+++ b/spec/lib/content_security_policy_spec.rb
@@ -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")