5.8.7
Analytics
See what visitors search for and what they open, by passing the search's events on to your own analytics.
Search sends nothing anywhere. It has no tracking of its own and no third-party calls. Instead, it fires three browser events that your own analytics can listen for. With them you can see what people search for, which searches find nothing, and which results they open.
The events work the same on plain Astro, on EmDash and on the embed. The dialog fires them everywhere. The results page fires them on EmDash and on the embed. The plain Astro SearchPage is a simple list and fires none, so listeners stay silent there.
The events
Each event bubbles out of the dialog, and out of its shadow root, up to document. Listen there.
| Event | When it fires | detail |
|---|---|---|
search:query |
Once typing stops for 600 ms, for each new query or type tab. A query still waiting fires at once when a result is opened or the dialog closes. | { query, index, locale, total, type } |
search:noresults |
Right after search:query, when that query found nothing. |
{ query, index } |
search:select |
When a result or a suggested link is opened: with Enter, a click, or in a new tab. | { query, index, id, url, type, position } |
Nothing fires for an empty box, or while an input method is still composing text.
The detail fields
| Field | What it holds |
|---|---|
query |
The query as typed, trimmed. For a suggested link opened from an empty box, "". |
index |
The index name, such as default. |
locale |
The language of the loaded index, or null. |
total |
How many pages match, not only the ones shown. |
type |
On search:query, the active type tab (null for all). On search:select, the result’s type, or null. |
id |
The result’s record id. null for a suggested link. |
url |
The result’s URL. |
position |
Where the result was on screen, starting at 1. |
search:noresults is the most useful of the three. It lists the words people expect to find on your site and do not. Add those words to the right page as keywords: searchKeywords in a page’s frontmatter on plain Astro, or a search_keywords field on EmDash. On plain Astro, synonyms help too.
Where to put the listener
Add a small script that runs on every page, after your analytics script.
- Plain Astro: a
<script>in your base layout. - EmDash: a
<script>in your theme’s base layout, the one that renders<EmDashBodyEnd page={page} />.
Plausible
document.addEventListener("search:query", (e) => {
const { query, total } = e.detail;
window.plausible?.("Search", { props: { query, results: String(total) } });
});
document.addEventListener("search:noresults", (e) => {
window.plausible?.("Search: no results", { props: { query: e.detail.query } });
});
document.addEventListener("search:select", (e) => {
window.plausible?.("Search: open", { props: { query: e.detail.query, url: e.detail.url ?? "", position: String(e.detail.position) } });
});
In Plausible, add the three names as custom events (goals) to see them in your dashboard.
Google Analytics 4
GA4 has a recommended search event. The other two are your own:
document.addEventListener("search:query", (e) => {
window.gtag?.("event", "search", { search_term: e.detail.query, results: e.detail.total });
});
document.addEventListener("search:noresults", (e) => {
window.gtag?.("event", "search_no_results", { search_term: e.detail.query });
});
document.addEventListener("search:select", (e) => {
const { query, url, position } = e.detail;
window.gtag?.("event", "select_search_result", { search_term: query, link_url: url, position });
});
PostHog
document.addEventListener("search:query", (e) => {
window.posthog?.capture("search", { query: e.detail.query, results: e.detail.total, type: e.detail.type });
});
document.addEventListener("search:noresults", (e) => {
window.posthog?.capture("search no results", { query: e.detail.query });
});
document.addEventListener("search:select", (e) => {
const { query, url, position } = e.detail;
window.posthog?.capture("search result opened", { query, url, position });
});
Turn the events off
The default dialog always fires them. If you build your own dialog on the browser layer, mountSearch({ events: false }) turns them off. See Look and text.
Privacy
The events carry what a visitor typed. A query can hold a name, an email address or other personal text. Before you send queries on, check what your analytics tool keeps and what your privacy policy says.