Skip to content

Commit

Permalink
Reapply "Add plausible error handling"
Browse files Browse the repository at this point in the history
This reverts commit a11e63a.
  • Loading branch information
HendrikSchmidt committed Jul 12, 2024
1 parent a11e63a commit fa5162c
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 13 deletions.
2 changes: 1 addition & 1 deletion packages/dito/app/routes/download.$fileName.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export async function loader({ params, request }: LoaderFunctionArgs) {
const filePath = path.join("public", "assets", fileName);
const pdfData = await fs.readFile(filePath);

trackCustomEvent(request, { name: "Download Dokumentation" });
void trackCustomEvent(request, { name: "Download Dokumentation" });
return new Response(pdfData, {
status: 200,
headers: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,10 @@ export async function action({ params, request }: ActionFunctionArgs) {
} else if (Object.values(answers).find((a) => a === "unsure")) {
result = "Unsicher";
}
trackCustomEvent(request, { name: "Download Vorprüfung", props: { result } });
void trackCustomEvent(request, {
name: "Download Vorprüfung",
props: { result },
});

return new Response(pdfData, {
status: 200,
Expand Down
5 changes: 4 additions & 1 deletion packages/dito/app/routes/vorpruefung.ergebnis/route.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,10 @@ export async function loader({ request }: LoaderFunctionArgs) {
} else if (unsureQuestions.length > 0) {
result = "Unsicher";
}
trackCustomEvent(request, { name: "Vorprüfung Resultat", props: { result } });
void trackCustomEvent(request, {
name: "Vorprüfung Resultat",
props: { result },
});

return json({ positiveQuestions, unsureQuestions, negativeQuestions });
}
Expand Down
27 changes: 17 additions & 10 deletions packages/dito/app/utils/trackCustomEvent.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,28 @@
const plausibleUrl = "https://plausible.io/api/event";
const plausibleDomain = "digitalcheck-dito.prod.ds4g.net";

export default function trackCustomEvent(
export default async function trackCustomEvent(
request: Request,
event: { name: string; props?: Record<string, string> },
) {
if (process.env.NODE_ENV === "development") {
console.log("TRACKING", event);
return;
}
void fetch(plausibleUrl, {
method: "POST",
body: JSON.stringify({
domain: plausibleDomain,
url: request.url,
referrer: request.referrer,
...event,
}),
});
try {
const response = await fetch(plausibleUrl, {
method: "POST",
body: JSON.stringify({
domain: plausibleDomain,
url: request.url,
referrer: request.referrer,
...event,
}),
});
if (!response.ok) {
throw new Error(`Error tracking event: ${response.status}`);
}
} catch (error) {
console.error("Error tracking event", event, error);
}
}

0 comments on commit fa5162c

Please sign in to comment.