Skip to content

Commit

Permalink
Umami Events
Browse files Browse the repository at this point in the history
  • Loading branch information
ZonianMidian committed Nov 1, 2024
1 parent 49e1e06 commit ddfa3f7
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 1 deletion.
9 changes: 9 additions & 0 deletions src/lib/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,12 @@ export type Category = {
export type LanguageData = {
[key: string]: string;
};

export type UmamiEvent = {
mode: string;
time?: number;
percentTry: boolean;
percentEnd: boolean;
answersEnd: boolean;
soundEffects: boolean;
};
35 changes: 34 additions & 1 deletion src/routes/+page.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@
import { checkMatch, formatTime, loadCountryData } from '$utils/utils';
import { countriesStore, categoriesStore } from '$stores/store';
import { getLocaleFromNavigator, t } from 'svelte-i18n';
import type { LanguageData, UmamiEvent } from '$types';
import { effectSounds } from '$utils/soundEffects';
import Spinner from '$components/Spinner.svelte';
import type { LanguageData } from '$types';
import { browser } from '$app/environment';
import { onMount } from 'svelte';
import '../app.css';
Expand Down Expand Up @@ -38,6 +39,12 @@
let playSoundEffects: boolean = true;
let effectsVolume: number = 0.1;
//Umami
let umami: any;
if (browser) {
umami = (window as any)?.umami;
}
async function loadLanguage() {
currentLang = getLocaleFromNavigator()?.slice(0, 2) ?? 'en';
const langFile = await import(`$data/languages/${currentLang}.json`);
Expand Down Expand Up @@ -84,6 +91,18 @@
}
function startGame() {
if (umami) {
const eventData: UmamiEvent = {
mode,
percentTry: showPercentagePerTry,
percentEnd: showPercentageAtEnd,
answersEnd: showAnswersAtEnd,
soundEffects: playSoundEffects
};
if (mode === 'Timer') eventData.time = timeLimit;
umami.track('start', eventData);
}
gameStarted = true;
isStopEnabled = true;
isStartEnabled = false;
Expand Down Expand Up @@ -118,6 +137,12 @@
}
function endGame() {
if (umami) {
umami.track('end', {
result: `${guessedCountries.length}/${filteredCountries.length}`
});
}
gameStarted = false;
isStopEnabled = false;
isStartEnabled = false;
Expand All @@ -137,11 +162,19 @@
}
function stopGame() {
if (umami) {
umami.track('stop');
}
endGame();
disableStartButton(true);
}
function restartGame() {
if (umami) {
umami.track('restart');
}
guessedCountries = [];
percentages = {};
actualTime = 0;
Expand Down

0 comments on commit ddfa3f7

Please sign in to comment.