Opt in settings for Ahoy Tracking (#18575)

* Opt in settings for Ahoy Tracking

* Typo tweak
This commit is contained in:
Fernando Valverde 2022-10-17 10:11:27 -06:00 committed by GitHub
parent ffb41dfdf9
commit 9ab6edb0b8
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
8 changed files with 117 additions and 5 deletions

View file

@ -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"

View file

@ -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,

View file

@ -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| %>
<details class="crayons-card">
<summary class="crayons-subtitle-2 p-6">Ahoy Analytics</summary>
<div class="p-6 pt-0">
<fieldset class="grid gap-4">
<div class="crayons-field crayons-field--checkbox">
<%= f.check_box :ahoy_tracking, checked: Settings::General.ahoy_tracking, class: "crayons-checkbox" %>
<%= admin_config_label :ahoy_tracking %>
</div>
<%= admin_config_description Constants::Settings::General.details[:ahoy_tracking][:description] %>
</fieldset>
<%= render "update_setting_button", f: f %>
</div>
</details>
<% end %>

View file

@ -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" %>

View file

@ -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

View file

@ -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:

View file

@ -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:

View file

@ -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');
});
});
});
});
});