Edit analytics

Signed-off-by: Hadi <112569860+anotherhadi@users.noreply.github.com>
This commit is contained in:
Hadi
2026-03-16 23:24:08 +01:00
parent 0cb4f515ce
commit 5b4ca98f70
2 changed files with 38 additions and 6 deletions

View File

@@ -76,7 +76,16 @@
totalPages = data.pagination.totalPages;
totalResults = data.pagination.totalResults;
if (typeof window !== "undefined" && (window as any).umami) {
(window as any).umami.track("search", { query: lastQuery, results: totalResults });
(window as any).umami.track("search", {
query: lastQuery,
results: totalResults,
hasResults: totalResults > 0,
});
if (totalResults === 0) {
(window as any).umami.track("search_no_results", {
query: lastQuery,
});
}
}
} catch (e) {
console.error("Search error:", e);

View File

@@ -112,7 +112,7 @@ export const GET: APIRoute = async ({ url, request }) => {
// Server-side tracking for search queries, respecting DNT/GPC
if (query && dnt) {
await trackSearchServerSide(query, filtered.length > 0);
await trackSearchServerSide(query, filtered.length);
}
const totalResults = filtered.length;
@@ -141,7 +141,7 @@ export const GET: APIRoute = async ({ url, request }) => {
);
};
async function trackSearchServerSide(query: string, hasResults: boolean) {
async function trackSearchServerSide(query: string, results: number) {
const umamiUrl = process.env.UMAMI_URL;
const umamiId = process.env.UMAMI_WEBSITE_ID;
@@ -163,12 +163,35 @@ async function trackSearchServerSide(query: string, hasResults: boolean) {
name: "search",
data: {
query,
hasResults,
results,
hasResults: results > 0,
source: "server",
},
},
}),
});
if (results === 0) {
await fetch(`${umamiUrl}/api/send`, {
method: "POST",
headers: {
"Content-Type": "application/json",
"User-Agent": "Mozilla/5.0 (compatible; default-creds-server/1.0)",
},
body: JSON.stringify({
type: "event",
payload: {
website: umamiId,
hostname: "default-creds.hadi.diy",
url: "/api/search",
name: "search_no_results",
data: {
query,
source: "server",
},
},
}),
});
}
} catch (e) {
console.error("Umami server-side tracking failed:", e);
}