From 9ab6edb0b8df12d1cebe33f03788cd40c3112974 Mon Sep 17 00:00:00 2001 From: Fernando Valverde Date: Mon, 17 Oct 2022 10:11:27 -0600 Subject: [PATCH] Opt in settings for Ahoy Tracking (#18575) * Opt in settings for Ahoy Tracking * Typo tweak --- app/lib/constants/settings/general.rb | 3 + app/models/settings/general.rb | 3 + app/views/admin/settings/forms/_ahoy.html.erb | 21 ++++++ app/views/admin/settings/show.html.erb | 1 + config/initializers/ahoy.rb | 22 ++++-- config/locales/lib/en.yml | 2 + config/locales/lib/fr.yml | 2 + .../adminFlows/config/ahoySection.spec.js | 68 +++++++++++++++++++ 8 files changed, 117 insertions(+), 5 deletions(-) create mode 100644 app/views/admin/settings/forms/_ahoy.html.erb create mode 100644 cypress/e2e/seededFlows/adminFlows/config/ahoySection.spec.js diff --git a/app/lib/constants/settings/general.rb b/app/lib/constants/settings/general.rb index 3f756a803..2c9ad7fb0 100644 --- a/app/lib/constants/settings/general.rb +++ b/app/lib/constants/settings/general.rb @@ -5,6 +5,9 @@ module Constants def self.details { + ahoy_tracking: { + description: I18n.t("lib.constants.settings.general.ahoy_tracking.description") + }, contact_email: { description: I18n.t("lib.constants.settings.general.contact_email.description"), placeholder: "hello@example.com" diff --git a/app/models/settings/general.rb b/app/models/settings/general.rb index 8187e0ee4..74325f929 100644 --- a/app/models/settings/general.rb +++ b/app/models/settings/general.rb @@ -25,6 +25,9 @@ module Settings setting :ga_tracking_id, type: :string, default: ApplicationConfig["GA_TRACKING_ID"] setting :ga_analytics_4_id, type: :string, default: ApplicationConfig["GA_ANALYTICS_4_ID"] + # Ahoy Tracking + setting :ahoy_tracking, type: :boolean, default: false + # Images setting :main_social_image, type: :string, diff --git a/app/views/admin/settings/forms/_ahoy.html.erb b/app/views/admin/settings/forms/_ahoy.html.erb new file mode 100644 index 000000000..ef789212e --- /dev/null +++ b/app/views/admin/settings/forms/_ahoy.html.erb @@ -0,0 +1,21 @@ +<%= form_for(Settings::General.new, + url: admin_settings_general_settings_path, + html: { data: { + action: "submit->config#updateConfigurationSettings", + testid: "ahoyAnalyticsForm" + } }) do |f| %> +
+ Ahoy Analytics + +
+
+
+ <%= f.check_box :ahoy_tracking, checked: Settings::General.ahoy_tracking, class: "crayons-checkbox" %> + <%= admin_config_label :ahoy_tracking %> +
+ <%= admin_config_description Constants::Settings::General.details[:ahoy_tracking][:description] %> +
+ <%= render "update_setting_button", f: f %> +
+
+<% end %> diff --git a/app/views/admin/settings/show.html.erb b/app/views/admin/settings/show.html.erb index d98826c3c..0f31f6f9b 100644 --- a/app/views/admin/settings/show.html.erb +++ b/app/views/admin/settings/show.html.erb @@ -14,6 +14,7 @@ <%= render partial: "forms/credits" %> <%= render partial: "forms/emails" %> <%= render partial: "forms/smtp" %> + <%= render partial: "forms/ahoy" %> <%= render partial: "forms/google_analytics" %> <%= render partial: "forms/images", locals: { logo_allowed_types: @logo_allowed_types, logo_max_file_size: @logo_max_file_size } %> <%= render partial: "forms/mascot" %> diff --git a/config/initializers/ahoy.rb b/config/initializers/ahoy.rb index ec007726b..d06d402f2 100644 --- a/config/initializers/ahoy.rb +++ b/config/initializers/ahoy.rb @@ -9,11 +9,23 @@ Ahoy.mask_ips = true # set to false to switch from cookies to anonymity sets Ahoy.cookies = false -# set to true for JavaScript tracking -Ahoy.api = true - # set to false to disable geocode tracking Ahoy.geocode = false -# only create visits server-side when needed for events and `visitable` -Ahoy.server_side_visits = :when_needed +# Settings::General.ahoy_tracking is only available after initialization +Rails.configuration.after_initialize do + # In local development the setting may have changed so this helps maintain + # consistency when running tests locally + ahoy_enabled = Settings::General.ahoy_tracking || Rails.env.test? + + # set to true for JavaScript tracking + Ahoy.api = ahoy_enabled + + # only create visits server-side when needed for events and `visitable` + Ahoy.server_side_visits = ahoy_enabled ? :when_needed : false +rescue StandardError + # When setting up the local environment there's a chance the DB won't be + # available or migrations haven't run yet + Rails.logger.warn "Warning: Ahoy didn't initialize correctly" + Rails.logger.warn "Omit this message if running a rake task (i.e. `rails db:create`)" +end diff --git a/config/locales/lib/en.yml b/config/locales/lib/en.yml index 70be36533..4909bbb29 100644 --- a/config/locales/lib/en.yml +++ b/config/locales/lib/en.yml @@ -94,6 +94,8 @@ en: description: Used in signup modal. placeholder: We're a place where coders share, stay up-to-date and grow their careers. general: + ahoy_tracking: + description: Track visits and events - data is stored in your database. A restart is required for changes to this configuration to take effect. contact_email: description: Used for contact links. Please provide an email address where users can get in touch with you or your team. credit: diff --git a/config/locales/lib/fr.yml b/config/locales/lib/fr.yml index ac594b390..3b5bf13c6 100644 --- a/config/locales/lib/fr.yml +++ b/config/locales/lib/fr.yml @@ -94,6 +94,8 @@ fr: description: Used in signup modal. placeholder: We're a place where coders share, stay up-to-date and grow their careers. general: + ahoy_tracking: + description: Track visits and events - data is stored in your database. A restart is required for changes to this configuration to take effect. contact_email: description: Used for contact links. Please provide an email address where users can get in touch with you or your team. credit: diff --git a/cypress/e2e/seededFlows/adminFlows/config/ahoySection.spec.js b/cypress/e2e/seededFlows/adminFlows/config/ahoySection.spec.js new file mode 100644 index 000000000..2ec2b1044 --- /dev/null +++ b/cypress/e2e/seededFlows/adminFlows/config/ahoySection.spec.js @@ -0,0 +1,68 @@ +describe('Ahoy Analytics Section', () => { + beforeEach(() => { + cy.testSetup(); + cy.fixture('users/adminUser.json').as('user'); + + cy.get('@user').then((user) => { + cy.loginUser(user).then(() => { + cy.updateAdminAuthConfig(user.username); + }); + }); + }); + + describe('ahoy analytics setting', () => { + it('persists the config when updated', () => { + cy.get('@user').then(() => { + cy.visit('/admin/customization/config'); + cy.findByTestId('ahoyAnalyticsForm').as('ahoyAnalyticsForm'); + + cy.get('@ahoyAnalyticsForm').findByText('Ahoy Analytics').click(); + + cy.get('#settings_general_ahoy_tracking').should('not.be.checked'); + + cy.get('@ahoyAnalyticsForm') + .get('#settings_general_ahoy_tracking') + .click(); + + cy.get('@ahoyAnalyticsForm').findByText('Update Settings').click(); + + cy.url().should('contains', '/admin/customization/config'); + + cy.findByTestId('snackbar').within(() => { + cy.findByRole('alert').should( + 'have.text', + 'Successfully updated settings.', + ); + }); + + cy.get('#settings_general_ahoy_tracking').should('be.checked'); + }); + }); + + it('generates error message when update fails', () => { + cy.intercept('POST', 'admin/settings/general_settings', { + error: 'some error msg', + }); + cy.get('@user').then(() => { + cy.visit('/admin/customization/config'); + cy.findByTestId('ahoyAnalyticsForm').as('ahoyAnalyticsForm'); + + cy.get('@ahoyAnalyticsForm').findByText('Ahoy Analytics').click(); + + cy.get('#settings_general_ahoy_tracking').should('not.be.checked'); + + cy.get('@ahoyAnalyticsForm') + .get('#settings_general_ahoy_tracking') + .click(); + + cy.get('@ahoyAnalyticsForm').findByText('Update Settings').click(); + + cy.url().should('contains', '/admin/customization/config'); + + cy.findByTestId('snackbar').within(() => { + cy.findByRole('alert').should('have.text', 'some error msg'); + }); + }); + }); + }); +});