Skip to content

Commit bc0eb84

Browse files
committed
Track search no results into plausible
1 parent 79b3a43 commit bc0eb84

File tree

2 files changed

+69
-1
lines changed

2 files changed

+69
-1
lines changed
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
// No-results analytics only
2+
(() => {
3+
const MAX_WAIT_MS = 8000;
4+
const POLL_INTERVAL_MS = 200;
5+
let flushed = false;
6+
const queue = [];
7+
8+
const enqueueOrSend = (event, props) => {
9+
if (typeof window.plausible === 'function') {
10+
window.plausible(event, { props });
11+
flushed = true;
12+
} else {
13+
queue.push([event, props]);
14+
}
15+
};
16+
17+
const flushQueueIfReady = () => {
18+
if (flushed) return true;
19+
if (typeof window.plausible !== 'function') return false;
20+
while (queue.length) {
21+
const [e, p] = queue.shift();
22+
window.plausible(e, { props: p });
23+
}
24+
flushed = true;
25+
return true;
26+
};
27+
28+
const startPolling = () => {
29+
const start = Date.now();
30+
const tick = () => {
31+
if (flushQueueIfReady()) return;
32+
if (Date.now() - start >= MAX_WAIT_MS) return; // give up silently
33+
setTimeout(tick, POLL_INTERVAL_MS);
34+
};
35+
tick();
36+
};
37+
38+
document.addEventListener('DOMContentLoaded', () => {
39+
const el = document.getElementById('search_no_results');
40+
if (!el) return;
41+
42+
const query = el.dataset.query || '';
43+
44+
// Inject UTM params (only if not already set)
45+
try {
46+
const url = new URL(window.location.href);
47+
const alreadyTagged = url.searchParams.get('utm_source') === 'internal_search'
48+
&& url.searchParams.get('utm_medium') === 'no_results';
49+
if (!alreadyTagged) {
50+
url.searchParams.set('utm_source', 'internal_search');
51+
url.searchParams.set('utm_medium', 'no_results');
52+
url.searchParams.set('utm_campaign', 'search');
53+
url.searchParams.set('utm_term', query);
54+
window.history.replaceState({}, '', url);
55+
}
56+
} catch (_) {
57+
// Ignore URL manipulation errors
58+
}
59+
60+
enqueueOrSend('Search no results', { query });
61+
flushQueueIfReady();
62+
if (!flushed) startPolling();
63+
});
64+
65+
// Also attempt flush after full load as a fallback
66+
window.addEventListener('load', flushQueueIfReady);
67+
})();

app/views/searches/show.html.erb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
<h2 class="govuk-heading-m govuk-!-margin-bottom-9">Výsledky vyhľadávania pre &bdquo;<%= @q %>&ldquo;</h2>
1010

1111
<% if @searches.empty? %>
12-
<p class="govuk-body">
12+
<p class="govuk-body" id="search_no_results" data-query="<%= @q %>">
1313
Pre tento výraz sme nenašli žiadne výsledky.
1414
</p>
1515
<% end %>
@@ -41,4 +41,5 @@
4141
</div>
4242
</main>
4343
</div>
44+
<%= javascript_include_tag 'no_results_analytics.js', 'data-turbolinks-track': 'reload' %>
4445
<% end %>

0 commit comments

Comments
 (0)