Fix missing click analytics on /search/ results page#397
Conversation
The standalone /search/ InstantSearch page reported searches to Algolia
but never sent click/conversion events, so Click & Conversion analytics
showed little/no click data (CTR/CVR mostly N/A or 0%). Only the
autocomplete dropdown (algolia-script.hbs) was instrumented.
Changes:
- Enable `insights: true` on the InstantSearch instance. It reuses the
global `aa` (search-insights) already initialised in algolia-script.hbs
and attaches a queryID with clickAnalytics on each query.
- Wire `sendEvent('click', hit, 'Hit Clicked')` into the custom hit
template links. Custom templates don't get automatic click tracking,
so this is required for clicks to be recorded and attributed.
- Suppress empty "root" queries from analytics via `analytics: false`.
These fire on initial page load and facet-only browsing and were being
logged as "<empty search>" (~33% of searches), dominating the report
and skewing average CTR. Results still return; they're just not counted
as a searched term.
Note: the "{search_term_string}" query seen in analytics is the
sitelinks-searchbox SearchAction template in head-structured-data.hbs
being crawled literally by bots; it's expected SEO behavior and left
as-is.
Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
✅ Deploy Preview for docs-ui ready!
To edit notification comments on pull requests, go to your Netlify project configuration. |
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: Organization UI Review profile: CHILL Plan: Pro Run ID: 📒 Files selected for processing (1)
📝 WalkthroughWalkthroughThe change updates the Algolia search-page template's search request wrapper to always normalize requests, disabling analytics for empty queries and conditionally applying exact-match overrides. InstantSearch initialization now enables insights. The hits rendering template adds a click handler using sendEvent, wiring it into both external and internal hit link markup for click-through attribution. Estimated code review effort: 2 (Simple) | ~10 minutes Sequence Diagram(s)sequenceDiagram
participant User
participant HitsTemplate
participant InstantSearch
participant AlgoliaAnalytics
User->>HitsTemplate: clicks hit link
HitsTemplate->>HitsTemplate: onHitClick(hit)
HitsTemplate->>InstantSearch: sendEvent('click', hit)
InstantSearch->>AlgoliaAnalytics: report click event (insights: true)
Related Issues: None linked in the provided summary. Related PRs: None linked in the provided summary. Suggested labels: search, analytics, enhancement Suggested reviewers: None specified. 🐰 A search bar clicks, a hit gets tracked, 🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
✨ Finishing Touches🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |

Problem
Algolia Click & Conversion analytics showed little/no click data for docs search (CTR/CVR mostly N/A or 0%), and the top queries were dominated by two odd rows:
<empty search>(~33% of searches) and{search_term_string}.Root cause: we have two Algolia surfaces and only one was instrumented.
src/partials/algolia-script.hbs) — fully wired:search-insights,aa('init'),insights: true,clickAnalytics: true, andclickedObjectIDsAfterSearchwithqueryID. ✅/search/results page (src/layouts/search.hbs) — InstantSearch with no insights, noclickAnalytics, and no click events sent. Every click from the full results page was invisible to Algolia. ❌Since the bulk of "real" searching happens on
/search/, most click data was missing and CVR sat at 0%.Changes (
src/layouts/search.hbs)insights: trueon the InstantSearch instance. It reuses the globalaa(search-insights) already initialised inalgolia-script.hbswith matchingALGOLIA_APP_ID/ALGOLIA_API_KEY, setsclickAnalytics, and attaches aqueryIDto each query.sendEvent('click', hit, 'Hit Clicked')into the custom hit-template links. Customhitstemplates don't get automatic click tracking, so this is required for clicks to record and attribute to the query.sendEventusesnavigator.sendBeacon, so it survives the navigation the link triggers.analytics: false. These fire on initial page load and during facet-only browsing (no term typed) and were logged as<empty search>(~33% of searches), dominating the report and skewing average CTR. Results still return — they're just not counted as a searched term. Usinganalytics: false(rather than blocking the request) preserves initial load and facet-only browsing.Not changed
The
{search_term_string}query (low volume, no clicks) is the sitelinks-searchboxSearchActiontemplate insrc/partials/head-structured-data.hbs(/search/?q={search_term_string}) being crawled literally by Google/bots. That's expected SEO behavior — left as-is; filter it out in the dashboard view if desired.Testing
gulp lintpasses./search/, click a result, and confirm aHit Clickedevent appears in Algolia (Events debugger / Click analytics), and that a blank/search/load no longer records an<empty search>.🤖 Generated with Claude Code