Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Gather analytics on the usage of the application #150

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -110,3 +110,9 @@ model VerificationToken {
@@unique([identifier, token])
}

model UsageMetrics {
id String @id @default(cuid())
action String
createdAt DateTime @default(now())
}
4 changes: 3 additions & 1 deletion src/app/find-the-expert/[role]/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import ButtonSkeleton from "~/components/loading/button-loader";
import { Login } from "~/components/login";
import ShowDataTable from "~/components/show-data-table";
import { getServerAuthSession } from "~/server/auth";
import { api } from "~/trpc/server";
import {
extractUniqueIds,
fetchUserAnswersForRole,
Expand Down Expand Up @@ -39,7 +40,8 @@ const ContentSection = () => (

const FindTheExpertPage = async () => {
const session = await getServerAuthSession();

await api.usageMetricLogger.logUsageMetric.mutate({logMessage: 'find-the-expert-page-filtered-on-role'});

return (
<div className="container flex flex-col items-center justify-center gap-12 px-4 py-16">
<h1 className="text-center text-5xl font-extrabold tracking-tight">
Expand Down
1 change: 0 additions & 1 deletion src/app/result/[role]/page.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import React, { Suspense } from "react";
import { db } from "~/server/db";
import {
type Role,
type QuestionResult,
type TransformedData,
} from "~/models/types";
Expand Down
13 changes: 11 additions & 2 deletions src/components/additional-buttons-homepage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,15 @@ import { Login } from "./login";
import { Button } from "./ui/button";
import { ArrowRightDarkModeFriendly } from "./svg";
import { signIn } from "next-auth/react";
import { api } from "~/trpc/react";

const Buttons = ({ session }: { session: Session | null }) => {
const {mutate: logUsageMetric} = api.usageMetricLogger.logUsageMetric.useMutation();

const handleLogging = () => {
logUsageMetric({logMessage:'find-the-expert-page-accessed'});
}

return (
<div className="mt-5 flex justify-center">
<div className="mt-5 flex flex-col items-center gap-6 md:flex-row">
Expand All @@ -24,8 +31,10 @@ const Buttons = ({ session }: { session: Session | null }) => {
<ArrowRightDarkModeFriendly />
</Button>
<Button
onClick={() =>
signIn("azure-ad", { callbackUrl: "/find-the-expert/general" })
onClick={async () => {
handleLogging();
await signIn("azure-ad", { callbackUrl: "/find-the-expert/general" });
}
}
variant="outline"
className="border-2 border-[#bed62f]"
Expand Down
6 changes: 6 additions & 0 deletions src/components/select-role.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ export default function SelectRoles({
} = api.survey.setRole.useMutation();
const { mutate: setDefaultRoleMutate, isSuccess: setDefaultRoleIsSuccess } =
api.survey.setDefaultRole.useMutation();
const {mutate: logUsageMetric} = api.usageMetricLogger.logUsageMetric.useMutation();

// Show a toast notification on role mutation error
useEffect(() => {
Expand Down Expand Up @@ -85,6 +86,10 @@ export default function SelectRoles({
roleIds: selectedRoles,
});
};

const handleLogging = () => {
logUsageMetric({logMessage:'find-the-expert-page-accessed'});
}

const [communicationMethodIsLoading, setCommunicationMethodIsLoading] =
useState(false);
Expand Down Expand Up @@ -168,6 +173,7 @@ export default function SelectRoles({
</Link>
<Link href="/find-the-expert/general">
<SpinnerButton
onClick={handleLogging}
state={communicationMethodIsLoading}
variant="outline"
className="border-2 border-[#bed62f]"
Expand Down
2 changes: 2 additions & 0 deletions src/server/api/root.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { surveyRouter } from "~/server/api/routers/survey";
import { createTRPCRouter } from "~/server/api/trpc";
import { usageMetricLogger } from "../log";

/**
* This is the primary router for your server.
Expand All @@ -8,6 +9,7 @@ import { createTRPCRouter } from "~/server/api/trpc";
*/
export const appRouter = createTRPCRouter({
survey: surveyRouter,
usageMetricLogger: usageMetricLogger
});

// export type definition of API
Expand Down
16 changes: 16 additions & 0 deletions src/server/log.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { createTRPCRouter, publicProcedure } from "./api/trpc";
import { z } from "zod";
import { db } from "./db"


export const usageMetricLogger = createTRPCRouter({
logUsageMetric: publicProcedure
.input(z.object({logMessage: z.string()}))
.mutation(async ({input}) => {
await db.usageMetrics.create({
data: {
action: input.logMessage
}
});
})
})