Enable pageview tracking when VITE_UMAMI_WEBSITE_ID is set at frontend build time (Forgejo secret + deploy workflow), with SPA route updates and no script in local dev. Document setup in docs/umami-analytics.md, extend integritetspolicy, and add admin Webbstatistik link in prod builds. Co-authored-by: Cursor <cursoragent@cursor.com>
72 lines
2.4 KiB
TypeScript
72 lines
2.4 KiB
TypeScript
import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest'
|
|
import { createRouter, createMemoryHistory } from 'vue-router'
|
|
import {
|
|
getUmamiConfig,
|
|
initUmamiAnalytics,
|
|
trackUmamiPageview,
|
|
} from '@/utils/umami'
|
|
|
|
describe('umami', () => {
|
|
beforeEach(() => {
|
|
document.head.innerHTML = ''
|
|
delete window.umami
|
|
})
|
|
|
|
afterEach(() => {
|
|
vi.unstubAllEnvs()
|
|
})
|
|
|
|
it('returns null when website id is unset', () => {
|
|
vi.stubEnv('VITE_UMAMI_WEBSITE_ID', '')
|
|
expect(getUmamiConfig()).toBeNull()
|
|
})
|
|
|
|
it('returns config when website id is set', () => {
|
|
vi.stubEnv('VITE_UMAMI_WEBSITE_ID', '11111111-2222-3333-4444-555555555555')
|
|
vi.stubEnv('VITE_UMAMI_SCRIPT_URL', '')
|
|
expect(getUmamiConfig()).toEqual({
|
|
websiteId: '11111111-2222-3333-4444-555555555555',
|
|
scriptUrl: 'https://analytics.bilhej.se/script.js',
|
|
})
|
|
})
|
|
|
|
it('uses custom script url when provided', () => {
|
|
vi.stubEnv('VITE_UMAMI_WEBSITE_ID', 'test-id')
|
|
vi.stubEnv('VITE_UMAMI_SCRIPT_URL', 'https://example.test/script.js')
|
|
expect(getUmamiConfig()?.scriptUrl).toBe('https://example.test/script.js')
|
|
})
|
|
|
|
it('does not inject script when website id is unset', () => {
|
|
vi.stubEnv('VITE_UMAMI_WEBSITE_ID', '')
|
|
const router = createRouter({
|
|
history: createMemoryHistory(),
|
|
routes: [{ path: '/', component: { template: '<div />' } }],
|
|
})
|
|
initUmamiAnalytics(router)
|
|
expect(document.querySelector('script[data-website-id]')).toBeNull()
|
|
})
|
|
|
|
it('injects script with auto-track disabled when configured', () => {
|
|
vi.stubEnv('VITE_UMAMI_WEBSITE_ID', 'test-id')
|
|
const router = createRouter({
|
|
history: createMemoryHistory(),
|
|
routes: [{ path: '/', component: { template: '<div />' } }],
|
|
})
|
|
initUmamiAnalytics(router)
|
|
const script = document.querySelector('script[data-website-id]')
|
|
expect(script?.getAttribute('data-website-id')).toBe('test-id')
|
|
expect(script?.getAttribute('data-auto-track')).toBe('false')
|
|
expect(script?.getAttribute('src')).toContain('script.js')
|
|
})
|
|
|
|
it('trackUmamiPageview forwards url to umami', () => {
|
|
const track = vi.fn()
|
|
window.umami = { track }
|
|
trackUmamiPageview('/orders')
|
|
expect(track).toHaveBeenCalledOnce()
|
|
const mapper = track.mock.calls[0][0] as (
|
|
props: Record<string, unknown>,
|
|
) => Record<string, unknown>
|
|
expect(mapper({ referrer: 'x' })).toEqual({ referrer: 'x', url: '/orders' })
|
|
})
|
|
})
|