summaryrefslogtreecommitdiff
path: root/src/clicks.ts
blob: 35471f5337aca7608d105b0f51b3da10c094e717 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
const CLICKS_WSURL: string | undefined = (window as any).clicksWsUrl

class Session {
    private wss: WebSocketStream
    private queue: string[] = []
    private writer: WritableStreamDefaultWriter | undefined
    private url: string | undefined
    private header: string | undefined

    constructor(url: string, header: string) {
        this.wss = new WebSocketStream(url)
        this.header = header
    }

    async connect() {
        const openInfo = await this.wss.opened
        this.writer = openInfo.writable.getWriter()
        for (const event of this.queue) {
            void this.writer.write(event)
        }
    }

    reportEvent(event: string) {
        // Could do this with NavigateEvent instead,
        // but then we would report all navigations,
        // even if there are no clicks, which is unnecessary.
        if (window.location.href !== this.url) {
            if (this.header !== undefined) {
                this.pushEvent(this.header)
                this.header = undefined
            }
            this.url = window.location.href
            this.pushEvent(`u|${this.url}`)
        }

        this.pushEvent(event)
    }

    private pushEvent(event: string) {
        if (this.writer === undefined) {
            this.queue.push(event)
        } else {
            void this.writer.write(event)
        }
    }
}

function startSession(data: string): Session | undefined {
    if (CLICKS_WSURL === undefined) return undefined
    const session = new Session(CLICKS_WSURL, data)
    void session.connect()
    return session
}

document.addEventListener("DOMContentLoaded", () => {
    const ua = navigator.userAgentData?.brands
        ?.map((brand) => `${brand.brand}:${brand.version}`)
        .join(",")
    const session = startSession(
        `${document.documentElement.clientWidth}|${document.documentElement.clientHeight}|${ua}`
    )
    if (session !== undefined) {
        document.documentElement.addEventListener(
            "click",
            (e: MouseEvent) => {
                const target =
                    e.target instanceof HTMLElement ? e.target.tagName : ""
                const x = e.pageX / document.documentElement.clientWidth
                // clientWidth is NOT a typo, using width as scale,
                // to not have to know height of rendered document when
                // showing.
                const y = e.pageY / document.documentElement.clientWidth
                session.reportEvent(`${x}|${y}|${target}`)
            },
            {
                capture: true,
            }
        )
    }
})