Skip to content
This repository has been archived by the owner on Nov 2, 2021. It is now read-only.

Commit

Permalink
Add analytics code trigger
Browse files Browse the repository at this point in the history
- note that this change depends upon analytics code being in the components gem, which at time of writing is not complete
- analytics code is initialised on every page, if a previously set consent cookie is found, pageviews are sent
- the registration flow includes a yes/no radio for cookie consent, choosing 'yes' sets the cookie and immediately reinitialises the analytics code, firing a pageview
- if the user chooses 'no' that cookie is deleted
- this preference is not currently tied to the record in the user profile although this may change
  • Loading branch information
andysellick committed Oct 22, 2020
1 parent 1c92d49 commit e580764
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 22 deletions.
23 changes: 23 additions & 0 deletions app/assets/javascripts/analytics.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
//= require govuk_publishing_components/analytics

var linkedDomains = ['www.gov.uk']

$(document).ready(function() {
if (typeof window.GOVUK.analyticsInit !== 'undefined') {
window.GOVUK.analyticsInit(linkedDomains)

$('.js-cookie-consent .govuk-radios__input').on('click', function() {
var response = $(this).val()

if (response === 'yes') {
window.GOVUK.approveAllCookieTypes()
window.GOVUK.cookie('cookies_preferences_set', 'true', { days: 365 })
window.GOVUK.analyticsInit(linkedDomains)
}

else {
window.GOVUK.deleteCookie('cookies_policy')
}
})
}
})
2 changes: 2 additions & 0 deletions app/assets/javascripts/application.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,5 @@
//= require govuk_publishing_components/components/checkboxes
//= require govuk_publishing_components/components/error-summary
//= require govuk_publishing_components/components/radio

//= require ./analytics
38 changes: 20 additions & 18 deletions app/views/devise/registrations/your_information.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -19,24 +19,26 @@

<%= form_with(url: new_user_registration_your_information_path(registration_state_id: @registration_state_id), method: :post) do %>

<%= render "govuk_publishing_components/components/radio", {
name: "cookie_consent",
heading: t("devise.registrations.your_information.fields.cookie_consent.heading"),
heading_size: "s",
error_message: devise_error_items(:cookie_consent, @resource_error_messages),
items: [
{
value: "yes",
text: t("devise.registrations.your_information.fields.cookie_consent.yes"),
checked: @consents.fetch(:cookie_consent_decision, nil) == "yes"
},
{
value: "no",
text: t("devise.registrations.your_information.fields.cookie_consent.no"),
checked: @consents.fetch(:cookie_consent_decision, nil) == "no"
}
]
} %>
<div class="js-cookie-consent">
<%= render "govuk_publishing_components/components/radio", {
name: "cookie_consent",
heading: t("devise.registrations.your_information.fields.cookie_consent.heading"),
heading_size: "s",
error_message: devise_error_items(:cookie_consent, @resource_error_messages),
items: [
{
value: "yes",
text: t("devise.registrations.your_information.fields.cookie_consent.yes"),
checked: @consents.fetch(:cookie_consent_decision, nil) == "yes"
},
{
value: "no",
text: t("devise.registrations.your_information.fields.cookie_consent.no"),
checked: @consents.fetch(:cookie_consent_decision, nil) == "no"
}
]
} %>
</div>

<%= render "govuk_publishing_components/components/radio", {
name: "feedback_consent",
Expand Down
4 changes: 2 additions & 2 deletions spec/feature/registration_from_another_application_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -166,8 +166,8 @@ def start_journey
end

def i_consent_my_information_being_used
within(".govuk-form-group:first-of-type") { choose "Yes" }
within(".govuk-form-group:last-of-type") { choose "Yes" }
find(:css, "input[name='cookie_consent'][value='yes']").set(true)
find(:css, "input[name='feedback_consent'][value='yes']").set(true)
click_on I18n.t("devise.registrations.your_information.fields.submit.label")
end

Expand Down
4 changes: 2 additions & 2 deletions spec/feature/registration_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -244,8 +244,8 @@ def enter_incorrect_mfa
end

def provide_consent
within(".govuk-form-group:first-of-type") { choose "Yes" }
within(".govuk-form-group:last-of-type") { choose "No" }
find(:css, "input[name='cookie_consent'][value='yes']").set(true)
find(:css, "input[name='feedback_consent'][value='no']").set(true)
click_on I18n.t("devise.registrations.your_information.fields.submit.label")
end
end

0 comments on commit e580764

Please sign in to comment.