discourse/db/migrate/20230403094936_change_google_analytics_default.rb
David Taylor 2386ad12f2
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
2023-04-04 13:14:20 +01:00

31 lines
815 B
Ruby

# 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