Set up tracking of site search for Google Analytics

This commit is contained in:
squidfunk
2016-02-27 12:20:49 +01:00
parent f6195de306
commit c829555313
7 changed files with 30 additions and 10 deletions

View File

@@ -253,7 +253,7 @@ document.addEventListener('DOMContentLoaded', function() {
/* Close search and jump to anchor when on same page */
var parts = link.href.split('#');
if (parts[0] == window.location.href.split('#')[0]) {
if (parts[0] == document.location.href.split('#')[0]) {
link.addEventListener('click', function(e) {
document.body.classList.remove('toggle-search');
document.body.classList.remove('locked');

View File

@@ -208,8 +208,8 @@
{% include "footer.html" %}
</footer>
{% endblock %}
</article>
</div>
</div>
</article>
<!-- Search results -->
<div class="results" role="status" aria-live="polite">
@@ -251,13 +251,22 @@
/* Track outbound links */
var buttons = document.querySelectorAll('a');
Array.prototype.map.call(buttons, function(item) {
if (item.host != window.location.host) {
if (item.host != document.location.host) {
item.addEventListener('click', function() {
var action = item.getAttribute('data-action') || 'follow';
ga('send', 'event', 'outbound', action, item.href);
});
}
});
/* Register handler to log search on blur */
var query = document.querySelector('.query');
query.addEventListener('blur', function() {
if (this.value) {
var path = document.location.pathname;
ga('send', 'pageview', path + '?q=' + this.value);
}
});
</script>
{% endif %}
</body>