From 7e5a90ccb7a748ec80281da2ef9b29827bd4949a Mon Sep 17 00:00:00 2001 From: kr8n3r Date: Fri, 1 Nov 2019 15:23:58 +0000 Subject: [PATCH 1/2] Custom meta tag to track spelling suggestions For finders where we show spelling suggestions, we want to track their page view impressions. First part of the work is in static https://github.com/alphagov/static/pull/1927 Here we insert the meta tag into the document head for analytics script to pick up. --- app/views/finders/_spelling_suggestion.html.erb | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/app/views/finders/_spelling_suggestion.html.erb b/app/views/finders/_spelling_suggestion.html.erb index c98551f60..506d21d5e 100644 --- a/app/views/finders/_spelling_suggestion.html.erb +++ b/app/views/finders/_spelling_suggestion.html.erb @@ -1,3 +1,5 @@ +<% # we want an empty string if there are no suggestions on page load %> +<% _spelling_suggestion = '' %> <% if @spelling_suggestion_presenter.suggestions.any? %>

Did you mean <% @spelling_suggestion_presenter.suggestions.each do |suggestion| %> @@ -5,6 +7,10 @@ class: "govuk-link govuk-!-font-weight-bold", :data => suggestion[:data_attributes] %> + <% _spelling_suggestion = suggestion[:keywords] %> <% end %>

-<% end %> \ No newline at end of file +<% end %> +<% content_for :head do %> + +<% end %> From 05e469b5c352fef7e6849a8025566325423cb872 Mon Sep 17 00:00:00 2001 From: kr8n3r Date: Fri, 1 Nov 2019 15:24:34 +0000 Subject: [PATCH 2/2] Update spelling suggestion meta string dynamically Update the way we track what spellign suggestions are being shown. As we're adding a meta tag for GA tracking, we now need to only update the meta tag content where there is and isn't a spelling suggestion. --- app/assets/javascripts/live_search.js | 13 +++++-------- 1 file changed, 5 insertions(+), 8 deletions(-) diff --git a/app/assets/javascripts/live_search.js b/app/assets/javascripts/live_search.js index 1161e57df..4e9723b9b 100644 --- a/app/assets/javascripts/live_search.js +++ b/app/assets/javascripts/live_search.js @@ -38,11 +38,6 @@ window.ga('set', 'transport', 'beacon') } - // track impressions of spelling suggestions - if (this.$suggestionsBlock) { - this.trackSpellingSuggestionsImpressions(this.$suggestionsBlock) - } - if (GOVUK.support.history()) { this.saveState() @@ -151,9 +146,11 @@ } LiveSearch.prototype.trackSpellingSuggestionsImpressions = function trackSpellingSuggestionsImpressions ($suggestions) { - $($suggestions).find('a').each(function () { - GOVUK.SearchAnalytics.setDimension(81, $(this).data('track-options').dimension81) - }) + var $spellingSuggestionMetaTag = $("meta[name='govuk:spelling-suggestion']") + // currently there's ever only one suggestion + var spellingSuggestionAvailable = this.$suggestionsBlock.find('a').length > 0 + var suggestion = spellingSuggestionAvailable ? this.$suggestionsBlock.find('a').data('track-options').dimension81 : '' + $spellingSuggestionMetaTag.attr('content', suggestion) } /**