Skip to content

Commit

Permalink
Merge pull request #39 from ConvivioTeam/GTD-58-search-ga-tracking
Browse files Browse the repository at this point in the history
Search Google Analytics tracking
  • Loading branch information
jonathanglassman authored Aug 2, 2018
2 parents 1def8cf + 530e02f commit de0c518
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 1 deletion.
12 changes: 12 additions & 0 deletions lib/assets/javascripts/_analytics.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
36 changes: 35 additions & 1 deletion lib/assets/javascripts/_modules/search.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
var $searchResultsWrapper;
var $searchResultsClose;
var results;
var query;
var queryTimer;
var maxSearchEntries = 10;

this.start = function start($element) {
Expand Down Expand Up @@ -50,12 +52,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
Expand All @@ -73,6 +79,20 @@
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'
});
});
}

// When selecting navigation link, close the search results.
$('.toc').on('click','a', function(e) {
hideResults();
Expand Down Expand Up @@ -182,6 +202,20 @@
$html.removeClass('has-search-results-open');
}

function sendQueryToAnalytics() {
if(query === '') {
return;
}
var stripped = window.stripPIIFromString(query)
ga('send', {
hitType: 'event',
eventCategory: 'Search query',
eventAction: 'type',
eventLabel: stripped,
transport: 'beacon'
});
}

function replaceStopWordFilter() {
// Replace the default stopWordFilter as it excludes useful words like
// 'get'
Expand Down

0 comments on commit de0c518

Please sign in to comment.