Skip to content

:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.

2.0 KB
Raw Blame
import { ReplayPlugin } from 'posthog-js/rrweb'
import { EventType, IncrementalSource, eventWithTime } from 'posthog-js/rrweb-types'

type Node = {
    id: number
    type: number
    tagName: string
    childNodes: Node[]
    textContent?: string
}

export const WindowTitlePlugin = (cb: (windowId: string, title: string) => void): ReplayPlugin => {
    const titleElementIds = new Set<number>()

    const extractTitleTextEl = (node: Node): Node | undefined => {
        // Document node
        if (node.type === 0) {
            const el = node.childNodes.find((n) => n.type === 2) // element node

            if (el) {
                const headEl = el.childNodes.filter((n) => n.type === 2).find((n) => n.tagName === 'head')

                if (headEl) {
                    const titleEl = headEl.childNodes.filter((n) => n.type === 2).find((n) => n.tagName === 'title')

                    if (titleEl) {
                        const textEl = titleEl.childNodes.find((n) => n.type === 3) // text node
                        return textEl
                    }
                }
            }
        }
    }

    return {
        handler: async (e: eventWithTime, isSync) => {
            if ('windowId' in e && e.windowId && isSync) {
                const windowId = e.windowId as string
                if (e.type === EventType.FullSnapshot) {
                    titleElementIds.clear()
                    const el = extractTitleTextEl(e.data.node as Node)
                    if (windowId && el && el.textContent) {
                        titleElementIds.add(el.id)
                        cb(windowId, el.textContent)
                    }
                } else if (e.type === EventType.IncrementalSnapshot && e.data.source === IncrementalSource.Mutation) {
                    e.data.texts.forEach(({ id, value }) => {
                        if (titleElementIds.has(id) && value) {
                            cb(windowId, value)
                        }
                    })
                }
            }
        },
    }
}