Skip to content

Commit

Permalink
add tracking
Browse files Browse the repository at this point in the history
  • Loading branch information
stephtr committed Apr 2, 2023
1 parent fbe2e3a commit 80aa2b7
Show file tree
Hide file tree
Showing 7 changed files with 94 additions and 7 deletions.
2 changes: 2 additions & 0 deletions website/src/lib/calculatorWidget.svelte
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<script lang="ts">
import { slide } from 'svelte/transition';
import { trackEvent } from '../routes/tracking';
import type { Calculator } from './calculator';
import { setOption } from './calculatorModule';
import {
Expand Down Expand Up @@ -63,6 +64,7 @@
}
return;
}
trackEvent('calculator', 'submit', submittedByBlur ? 'by blur' : 'by enter');
if (submitOnBlur) {
calculator.submitCalculation(currentInput);
currentInput = '';
Expand Down
5 changes: 5 additions & 0 deletions website/src/lib/history.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { trackEvent } from '../routes/tracking';
import type { Calculation } from './calculator';

const tutorialCalculations: Calculation[] = [
Expand Down Expand Up @@ -60,6 +61,7 @@ export class History {
this.entries = this.entries.filter((c) => c.id !== id);
this.save();
this.notifyChangeListeners();
trackEvent('history', 'delete');
}

private doWithEntry(
Expand All @@ -79,12 +81,14 @@ export class History {
entry.isBookmarked = true;
entry.bookmarkName = name;
});
trackEvent('history', 'bookmark');
}

renameBookmark(id: string, name?: string) {
this.doWithEntry(id, (entry) => {
entry.bookmarkName = name;
});
trackEvent('history', 'renameBookmark');
}

removeBookmark(id: string) {
Expand Down Expand Up @@ -154,6 +158,7 @@ export class History {
];
this.save();
this.notifyChangeListeners();
trackEvent('history', 'clear');
}

isEmpty() {
Expand Down
16 changes: 13 additions & 3 deletions website/src/lib/historyItem.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
} from '@fortawesome/free-solid-svg-icons';
import type { Calculation } from './calculator';
import type { History } from './history';
import { trackEvent } from '../routes/tracking';
export let calculation: Calculation;
export let history: History;
Expand All @@ -26,12 +27,13 @@
let copyAnimated = false;
function copyClick() {
navigator.clipboard.writeText(
decodeHTMLentities(calculation.output.replace(/<[^>]*>/g, '')),
decodeHTMLentities(calculation.output.replace(/<[^>]*>/g, ''))!,
);
copyAnimated = true;
setTimeout(() => {
copyAnimated = false;
}, 1000);
trackEvent('history', 'copy');
}
function deleteClick() {
Expand Down Expand Up @@ -179,7 +181,10 @@
<button
class="response"
on:mousedown={() => onabouttoselect()}
on:click={() => onselectcalculation(calculation.rawInput)}
on:click={() => {
onselectcalculation(calculation.rawInput);
trackEvent('history', 'select');
}}
style={`transform: translateX(${shiftX}px)`}
>
{#if calculation.bookmarkName}
Expand Down Expand Up @@ -208,7 +213,12 @@
{/if}
</button>
<div class="mouseActions" style={`transform: translateX(${shiftX}px)`}>
<button title="Copy result" class="copyButton" class:animated={copyAnimated} on:click={copyClick}>
<button
title="Copy result"
class="copyButton"
class:animated={copyAnimated}
on:click={copyClick}
>
<FontAwesomeIcon icon={faClone} />
</button>
<button title="Remove" class="deleteButton" on:click={deleteClick}>
Expand Down
4 changes: 4 additions & 0 deletions website/src/lib/settings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ export class Settings {

additionalOptions = '';

sendUsageStatistics = true;

load() {
if (typeof window === 'undefined') return;
const savedSettings = window.localStorage?.getItem(
Expand All @@ -45,6 +47,7 @@ export class Settings {
this.useDecimalPoint =
settings.useDecimalPoint ?? getLocaleDecimalPoint() === '.';
this.additionalOptions = settings.additionalOptions ?? false;
this.sendUsageStatistics = settings.sendUsageStatistics ?? true;
}

save() {
Expand All @@ -56,6 +59,7 @@ export class Settings {
useUnitPrefixes: this.useUnitPrefixes,
useDecimalPoint: this.useDecimalPoint,
additionalOptions: this.additionalOptions,
sendUsageStatistics: this.sendUsageStatistics,
}),
);
}
Expand Down
14 changes: 14 additions & 0 deletions website/src/lib/settingsWidget.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,15 @@
/>
</div>

<label>
<input
type="checkbox"
bind:checked={settings.sendUsageStatistics}
/>
Send anonymous usage statistics<br />
<span class="damped">(We don't record your calculations)</span>
</label>

<style>
label,
.setting {
Expand Down Expand Up @@ -185,4 +194,9 @@
.furtherInfoLink {
font-size: 0.9em;
}
.damped {
font-size: 0.8em;
opacity: 0.8;
}
</style>
8 changes: 4 additions & 4 deletions website/src/routes/+layout.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,8 @@
import LoadingIndicator from '$lib/loadingIndicator.svelte';
import { config } from '@fortawesome/fontawesome-svg-core';
import '@fortawesome/fontawesome-svg-core/styles.css';
import {
addNewsReadLister,
newsAvailable,
} from './news/version';
import { addNewsReadLister, newsAvailable } from './news/version';
import { setupTracking } from './tracking';
config.autoAddCss = false;
Expand Down Expand Up @@ -80,6 +78,8 @@
addNewsReadLister(() => {
newsUpdateAvailable = false;
});
setupTracking(calculator.settings.sendUsageStatistics);
</script>

<svelte:window on:touchstart={touchstart} />
Expand Down
52 changes: 52 additions & 0 deletions website/src/routes/tracking.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/* eslint-disable @typescript-eslint/naming-convention */
/* eslint-disable no-underscore-dangle */

// eslint-disable-next-line import/no-unresolved, import/extensions
import { page } from '$app/stores';

const isClient = typeof window !== 'undefined';
let trackingEnabled = false;

const _paq: (string | number)[][] = (isClient && (window as any)._paq) || [];
if (isClient) (window as any)._paq = _paq;

export function setupTracking(enable: boolean) {
if (enable && isClient && process.env.NODE_ENV !== 'development') {
trackingEnabled = true;
_paq.push(['enableLinkTracking']);
(function createTracking() {
const u = '//analytics.tonalio.com/';
_paq.push(['setTrackerUrl', `${u}matomo.php`]);
_paq.push(['setSiteId', '2']);
const d = document;
const g = d.createElement('script');
const s = d.getElementsByTagName('script')[0];
g.async = true;
g.src = `${u}matomo.js`;
s.parentNode!.insertBefore(g, s);
})();
page.subscribe(({ url }) => {
_paq.push(['setCustomUrl', url.pathname]);
_paq.push(['setDocumentTitle', document.title]);
_paq.push(['trackPageView']);
_paq.push(['enableLinkTracking']);
});
}
}

export function trackEvent(
category: string,
action: string,
name?: string,
value?: number,
) {
if (!trackingEnabled) return;
const eventArgs: (string | number)[] = [category, action];
if (name) {
eventArgs.push(name);
if (value) {
eventArgs.push(value);
}
}
_paq.push(['trackEvent', ...eventArgs]);
}

0 comments on commit 80aa2b7

Please sign in to comment.