From 7bce643b565f15aea3aff37ce8c26d550f2b5352 Mon Sep 17 00:00:00 2001 From: lewisnyman Date: Mon, 9 Jul 2018 14:43:04 +0100 Subject: [PATCH 1/4] GTD-58: Added tracking of clicks on search results if google analytics is enabled --- lib/assets/javascripts/_modules/search.js | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/lib/assets/javascripts/_modules/search.js b/lib/assets/javascripts/_modules/search.js index 02b5f0d9..0233a4bf 100644 --- a/lib/assets/javascripts/_modules/search.js +++ b/lib/assets/javascripts/_modules/search.js @@ -67,6 +67,20 @@ $searchInput.focus(); hideResults(); }); + + // Attach analytics events to search result clicks + if(window.ga) { + $searchResults.on('click', '.search-result__title a', function() { + var href = $(this).attr('href'); + ga('send', { + hitType: 'event', + eventCategory: 'Search result', + eventAction: 'click', + eventLabel: href, + transport: 'beacon' + }); + }); + } } function getResults(query) { From 43a26f658c8ebee505c616985ed92ecf0704bd8d Mon Sep 17 00:00:00 2001 From: lewisnyman Date: Mon, 9 Jul 2018 16:12:37 +0100 Subject: [PATCH 2/4] GTD-58: Send a search query event to GA if the searcher hasn't typed for one second --- lib/assets/javascripts/_modules/search.js | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/lib/assets/javascripts/_modules/search.js b/lib/assets/javascripts/_modules/search.js index 0233a4bf..4d3a1578 100644 --- a/lib/assets/javascripts/_modules/search.js +++ b/lib/assets/javascripts/_modules/search.js @@ -15,6 +15,8 @@ var $searchResultsWrapper; var $searchResultsClose; var results; + var query; + var queryTimer; var maxSearchEntries = 10; this.start = function start($element) { @@ -46,12 +48,16 @@ // Search functionality on search text input $searchInput.on('input', function (e) { e.preventDefault(); - var query = $(this).val(); + query = $(this).val(); s.search(query, function(r) { results = r; renderResults(query); updateTitle(); }); + if(window.ga) { + window.clearTimeout(queryTimer); + queryTimer = window.setTimeout(sendQueryToAnalytics, 1000); + } }); // Set focus on the first search result instead of submiting the search @@ -181,6 +187,16 @@ .attr('aria-hidden', 'true'); $html.removeClass('has-search-results-open'); } + + function sendQueryToAnalytics() { + ga('send', { + hitType: 'event', + eventCategory: 'Search query', + eventAction: 'type', + eventLabel: query, + transport: 'beacon' + }); + } }; // Polyfill includes From 915757384767609dcb243c6d2f6870f8aa0841ee Mon Sep 17 00:00:00 2001 From: lewisnyman Date: Wed, 1 Aug 2018 13:41:57 +0100 Subject: [PATCH 3/4] GTD-58: Check for an empty query before sending a blank event label --- lib/assets/javascripts/_modules/search.js | 3 +++ 1 file changed, 3 insertions(+) diff --git a/lib/assets/javascripts/_modules/search.js b/lib/assets/javascripts/_modules/search.js index 6f3a13cc..9a49c958 100644 --- a/lib/assets/javascripts/_modules/search.js +++ b/lib/assets/javascripts/_modules/search.js @@ -197,6 +197,9 @@ } function sendQueryToAnalytics() { + if(query === '') { + return; + } ga('send', { hitType: 'event', eventCategory: 'Search query', From 4b0237c183dd0edfa847a2da1c0f89260470975b Mon Sep 17 00:00:00 2001 From: lewisnyman Date: Wed, 1 Aug 2018 14:26:19 +0100 Subject: [PATCH 4/4] GTD-58: Strip email, postcode, and date PPI from text search events --- lib/assets/javascripts/_analytics.js | 12 ++++++++++++ lib/assets/javascripts/_modules/search.js | 3 ++- 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/lib/assets/javascripts/_analytics.js b/lib/assets/javascripts/_analytics.js index 852569d7..59e9eb44 100644 --- a/lib/assets/javascripts/_analytics.js +++ b/lib/assets/javascripts/_analytics.js @@ -42,5 +42,17 @@ $('.header a').on('click', linkTrackingEventHandler('topNavigationClick')); $('.toc a').on('click', linkTrackingEventHandler('tableOfContentsNavigationClick')); catchBrokenFragmentLinks(); + + // Borrowed from: + // https://github.com/alphagov/govuk_frontend_toolkit/blob/master/javascripts/govuk/analytics/analytics.js + window.stripPIIFromString = function (string) { + var EMAIL_PATTERN = /[^\s=/?&]+(?:@|%40)[^\s=/?&]+/g + var POSTCODE_PATTERN = /[A-PR-UWYZ][A-HJ-Z]?[0-9][0-9A-HJKMNPR-Y]?(?:[\s+]|%20)*[0-9][ABD-HJLNPQ-Z]{2}/gi + var DATE_PATTERN = /\d{4}(-?)\d{2}(-?)\d{2}/g + var stripped = string.replace(EMAIL_PATTERN, '[email]') + .replace(DATE_PATTERN, '[date]') + .replace(POSTCODE_PATTERN, '[postcode]'); + return stripped + } }); })(jQuery); diff --git a/lib/assets/javascripts/_modules/search.js b/lib/assets/javascripts/_modules/search.js index 9a49c958..d54f3c53 100644 --- a/lib/assets/javascripts/_modules/search.js +++ b/lib/assets/javascripts/_modules/search.js @@ -200,11 +200,12 @@ if(query === '') { return; } + var stripped = window.stripPIIFromString(query) ga('send', { hitType: 'event', eventCategory: 'Search query', eventAction: 'type', - eventLabel: query, + eventLabel: stripped, transport: 'beacon' }); }