Skip to content
Merged
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
14 changes: 14 additions & 0 deletions assets/static/js/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,14 @@ const setLine = (id: string, value: string): void => {
el.hidden = value.length === 0
}

// Send a GA4 event via the gtag stub in index.html. Guarded so the app keeps
// working when Analytics is blocked, offline, or absent (common on signage).
type Gtag = (...args: unknown[]) => void
const track = (event: string, params: Record<string, unknown>): void => {
const gtag = (window as unknown as { gtag?: Gtag }).gtag
if (typeof gtag === 'function') gtag('event', event, params)
}

// Human-readable target datetime for the sub-line, rendered in the chosen zone.
const formatTarget = (targetMs: number, tz: string): string => {
try {
Expand Down Expand Up @@ -86,6 +94,12 @@ const render = (): void => {
document.documentElement.dataset.state = targetMs === null ? 'invalid' : 'ready'
if (targetMs === null) {
setLine('target-line', 'Set a target date to start the timer.')
} else {
// Report once per load whether this timer is counting down to a future
// target or up from a past one — the page reloads periodically, so each
// load re-reports the current mode.
const { direction } = computeState(targetMs, Date.now())
track('timer_direction', { direction: direction === 'down' ? 'countdown' : 'countup' })
}
setLine('message', '')
// paint() fills the target line and digits for a valid target and keeps them
Expand Down
10 changes: 10 additions & 0 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,16 @@
<html lang="en">
<head>
<meta charset="utf-8" />
<!-- Google tag (gtag.js) -->
<script async src="https://www.googletagmanager.com/gtag/js?id=G-3V92EN0SBN"></script>
<script>
window.dataLayer = window.dataLayer || []
function gtag() {
dataLayer.push(arguments)
}
gtag('js', new Date())
gtag('config', 'G-3V92EN0SBN')
</script>
<title>Timer</title>
<meta name="viewport" content="width=device-width, initial-scale=1" />
<meta
Expand Down
Loading