Add Umami tracking for quiz completion

This commit is contained in:
Jakob Kordež 2025-01-11 17:24:47 +01:00
parent b4f50581cf
commit 079ff0dcc2
2 changed files with 7 additions and 8 deletions

View File

@ -6,6 +6,7 @@ import { create } from 'zustand';
import QuestionCard from '@/components/question_card';
import { scrollToTop } from '@/components/scroll-to-top-button';
import { useEffect, useState } from 'react';
import { useUmami } from '@/components/umami-analytics';
enum QuizState {
Loading,
@ -55,6 +56,10 @@ const useStore = create<IzpitQuizStore>((set, get) => ({
.map((q, qi) => q.correct === answers![qi][0])
.reduce((acc, cur) => acc + (cur ? 1 : 0), 0);
useUmami()?.track('quiz-finished', {
correctCount: correctCount / questions!.length,
});
set({ state: QuizState.Finished, correctCount });
scrollToTop();
},

View File

@ -5,13 +5,10 @@ import Script from 'next/script';
const ANALYTICS_URL = process.env.NEXT_PUBLIC_ANALYTICS_URL;
const ANALYTICS_ID = process.env.NEXT_PUBLIC_ANALYTICS_ID;
export function umamiTrack(
eventName: string,
data?: { [key: string]: string | number },
) {
export function useUmami() {
if (typeof window === 'undefined') return;
const umami = (
return (
window as unknown as {
umami:
| {
@ -23,9 +20,6 @@ export function umamiTrack(
| undefined;
}
).umami;
console.log('umami', umami);
umami?.track(eventName, data);
}
export function UmamiAnalytics() {