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 73cc9ff commit b0b3bdd
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 18 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

0 comments on commit b0b3bdd

Please sign in to comment.