Track search queries and click-through for relevance tuning
✓Works with OpenClaudeYou are a search analytics engineer. The user wants to track search queries, monitor click-through rates, and use that data to tune search relevance.
What to check first
- Verify your search engine has a logging mechanism enabled (Elasticsearch, Algolia, or custom solution)
- Check that your frontend captures click events on search results with
data-*attributes or event listeners - Confirm you have write access to an analytics database or time-series storage (MongoDB, ClickHouse, or cloud analytics service)
Steps
- Add a query logger on the search endpoint that records the exact query string, timestamp, user ID, and search context (filters applied, sort order)
- Attach a unique
search_session_idto each search request so you can correlate clicks back to specific queries - Instrument result click tracking on the frontend by listening to click events on result links and POST-ing the clicked result ID, position, and search_session_id to an analytics endpoint
- Store query and click events in separate collections/tables: queries table (query, timestamp, user_id, result_count, session_id) and clicks table (session_id, result_id, position, clicked_at, dwell_time)
- Calculate click-through rate (CTR) per query by grouping clicks by query string and dividing successful clicks by total impressions
- Identify zero-result queries and low-CTR queries (position 1 result gets <5% clicks) as relevance problems
- Export aggregated metrics (top queries, CTR by position, query volume trends) to a dashboard for weekly review
- Use CTR and position data to retrain relevance models or adjust ranking weights for underperforming query types
Code
// Search query logger (backend)
const express = require('express');
const { MongoClient } = require('mongodb');
const client = new MongoClient(process.env.MONGODB_URI);
const db = client.db('search_analytics');
const queriesCollection = db.collection('search_queries');
const clicksCollection = db.collection('search_clicks');
app.post('/api/search', async (req, res) => {
const { q, filters = {}, userId } = req.body;
const sessionId = crypto.randomUUID();
const startTime = Date.now();
const results = await performSearch(q, filters);
const duration = Date.now() - startTime;
// Log query event
await queriesCollection.insertOne({
query: q,
sessionId,
userId,
filters,
resultCount: results.length,
duration,
timestamp: new Date(),
});
// Return results with session ID embedded
res.json({
results: results.map((r, i) => ({ ...r, position: i + 1 })),
sessionId,
});
});
// Frontend click tracking (client-side)
document.addEventListener('click', async (e) => {
Note: this example was truncated in the source. See the GitHub repo for the latest full version.
Common Pitfalls
- Treating this skill as a one-shot solution — most workflows need iteration and verification
- Skipping the verification steps — you don't know it worked until you measure
- Applying this skill without understanding the underlying problem — read the related docs first
When NOT to Use This Skill
- When a simpler manual approach would take less than 10 minutes
- On critical production systems without testing in staging first
- When you don't have permission or authorization to make these changes
How to Verify It Worked
- Run the verification steps documented above
- Compare the output against your expected baseline
- Check logs for any warnings or errors — silent failures are the worst kind
Production Considerations
- Test in staging before deploying to production
- Have a rollback plan — every change should be reversible
- Monitor the affected systems for at least 24 hours after the change
Related Search Skills
Other Claude Code skills in the same category — free to download.
Elasticsearch Setup
Set up Elasticsearch with indexing and full-text search
Algolia Setup
Integrate Algolia instant search with facets and filters
Meilisearch Setup
Set up Meilisearch for fast typo-tolerant search
Postgres Full-Text
Implement full-text search with PostgreSQL tsvector
Search Autocomplete
Build search autocomplete with debounce and suggestions
Elasticsearch Index Mapping
Design Elasticsearch mappings that make queries fast and storage efficient
Want a Search skill personalized to YOUR project?
This is a generic skill that works for everyone. Our AI can generate one tailored to your exact tech stack, naming conventions, folder structure, and coding patterns — with 3x more detail.