diff --git a/frontend/src/actions/analytics.ts b/frontend/src/actions/analytics.ts new file mode 100644 index 0000000..da0d9a6 --- /dev/null +++ b/frontend/src/actions/analytics.ts @@ -0,0 +1,24 @@ +"use server"; + +import { getMongoClientPromise } from "@/lib/mongodb"; + +const COLLECTION = "job_apply_clicks"; + +export async function trackApplyClick(job: { + jobId: string; + jobTitle: string; + companyName: string; +}) { + try { + const client = await getMongoClientPromise(); + const db = client.db(); + await db.collection(COLLECTION).insertOne({ + jobId: job.jobId, + jobTitle: job.jobTitle, + companyName: job.companyName, + clickedAt: new Date(), + }); + } catch { + // Non-critical — don't surface tracking errors to the user + } +} diff --git a/frontend/src/components/jobs/job-details.tsx b/frontend/src/components/jobs/job-details.tsx index 781b85f..076d353 100644 --- a/frontend/src/components/jobs/job-details.tsx +++ b/frontend/src/components/jobs/job-details.tsx @@ -17,6 +17,8 @@ import JobDetailsLoading from "@/components/layout/job-details-loading"; import JobSummary from "@/components/jobs/job-summary"; import { upsertLocalStartedApplication } from "@/lib/local-applications"; import { addApplication } from "@/app/my-applications/actions"; +import { trackApplyClick } from "@/actions/analytics"; +import { sendGAEvent } from "@next/third-parties/google"; import Link from "next/link"; import { useSession } from "next-auth/react"; @@ -50,6 +52,18 @@ export default function JobDetails() { const handleApplyClick = () => { window.open(selectedJob.application_url, "_blank"); + trackApplyClick({ + jobId: selectedJob.id, + jobTitle: selectedJob.title, + companyName: selectedJob.company?.name || "Unknown", + }); + + sendGAEvent("event", "apply_click", { + job_id: selectedJob.id, + job_title: selectedJob.title, + company: selectedJob.company?.name || "Unknown", + }); + if (session?.user) { addApplication(selectedJob.id, { jobId: selectedJob.id,