Skip to content

Commit

Permalink
feat: push queries to mongo for analytics in a fire and forget fashion
Browse files Browse the repository at this point in the history
  • Loading branch information
sznowicki committed Dec 26, 2023
1 parent f49e8ce commit fcac7dc
Showing 1 changed file with 8 additions and 13 deletions.
21 changes: 8 additions & 13 deletions functions/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,14 @@ import {trackQuery} from '../lib/mongo.js';

export const onRequestGet = async (context) => {
const startTime = Date.now();
// Save used queries for analytics
if (q) {
trackQuery(context.env, { q, hasResults })
.catch(error => {
console.error('Could not send mongo analytics', error);
});
}

const { request, env } = context;
const { searchParams } = new URL(request.url);
const { q} = Object.fromEntries(searchParams.entries());
Expand Down Expand Up @@ -70,19 +78,6 @@ export const onRequestGet = async (context) => {

const langs = [...facets.lang.values()].sort((a,b) => a.count > b.count ? -1 : 1);

// Save used queries for analytics
if (q) {
await context.env.KUKEI_QUERIES.put(
q,
JSON.stringify({
hasResults,
}),
{
expirationTtl: 86400, // 24h
}
);
await trackQuery(context.env, { q, hasResults });
}
const view = {
...viewDefaults,
q,
Expand Down

0 comments on commit fcac7dc

Please sign in to comment.