Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Track Lunr search queries and search-result clicks #1519

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 44 additions & 13 deletions layouts/partials/javascript.html
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@
"use strict";
let index;
let debounceTimer = null;
const _debug = false;
const debugLog = _debug ? console.log : () => { };
const currentUrl = window.location.pathname;
const currentLocation = String(currentUrl).split("/");
const currentVersion = currentLocation[2];
Expand Down Expand Up @@ -139,6 +141,7 @@
}
});
search(query);
trackSearchEvent(query);
}

function search(keywords) {
Expand Down Expand Up @@ -169,23 +172,51 @@
// show the matched result
results.forEach(function (result) {
const doc = parse[result.ref];
const element = template.content.cloneNode(true);
element.querySelector(".title").textContent = doc.title;
element.querySelector(".subtitle").textContent = doc.summary;
element.querySelector(".subtitle").setAttribute("href", doc.href);
element
.querySelector(".is-read-more")
.setAttribute("href", doc.href);
element.querySelector(".search-path").textContent = doc.path;
element
.querySelector(".search-path")
.setAttribute("href", doc.href);

target.appendChild(element);
const searchResultItem = template.content.cloneNode(true)
.querySelector('.search-result-item');

const searchPath = searchResultItem.querySelector('a.search-path');
searchPath.setAttribute('href', doc.href);
searchPath.textContent = doc.path;

const title = searchResultItem.querySelector('a.title');
title.setAttribute('href', doc.href);
title.textContent = doc.title;

const subtitle = searchResultItem.querySelector('a.subtitle');
subtitle.setAttribute('href', doc.href);
subtitle.textContent = doc.summary;

searchResultItem.addEventListener('click',
() => trackSearchResultClick(doc.href, query));
target.appendChild(searchResultItem);
}, this);
}
}

const lunrSearchEventCategory = 'Lunr Search';

function trackSearchEvent(searchTerm) {
debugLog('trackSearchEvent: searchTerm = ' + searchTerm);
if (typeof gtag !== 'function') return;
gtag('event', 'search', {
event_category: lunrSearchEventCategory,
event_label: 'Search Input',
search_term: searchTerm,
});
}

function trackSearchResultClick(searchResultURL, searchTerm) {
debugLog(`trackSearchResultClick: searchTerm = ${searchTerm}, link = ${searchResultURL}`);
if (typeof gtag !== 'function') return;
gtag('event', 'click', {
event_category: lunrSearchEventCategory,
event_label: 'Search-result Click',
search_term: searchTerm,
value: searchResultURL,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

from the documentation value can be a number only, so it cannot be set to string
https://developers.google.com/tag-platform/devguides/events#gtag.js_1

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For a preliminary analysis, see:

});
}

// click outside the search modal to close
document.addEventListener("click", () => {
searchPopover.style.display = "none";
Expand Down
2 changes: 1 addition & 1 deletion layouts/partials/search-results.html
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ <h6 id="search-results" class="title is-5"></h6>
<template id="is-search-template">
<article class="search-result-item">
<a class="search-path"></a>
<h6 class="search-title ">
<h6 class="search-title">
<a class="title is-5 is-read-more"></a>
</h6>
<p>
Expand Down