!async function(w, document, host) { // look for bots, bail out of fishy userAgent if (w.navigator.userAgent.search(/(bot|spider|crawl|preview)/gi) > -1) { return } let event let session const get = async (url) => { let r = await fetch(url) return await r.text() } const cache = async () => { return get(`${host}/cache`) } session = await cache() const params = data => Object.keys(data) .map(key => `${key}=${encodeURIComponent(data[key])}`) .join('&') const send = async (type, data) => { let url = `${host}/ping?t=${type}&${params(data)}&p=${event}&s=urn:uuid:${session}` return await get(url) } const getData = (node) => node .getAttributeNames() .filter(ns => ns.startsWith('pb:') || ns === 'url') .reduce((o, key) => ({ ...o, [key]: node.getAttribute(key)}), {}) const pageview = async (node) => { event = await send('View', getData(node)) } const checkNode = document => { document .querySelectorAll('push-broom') .forEach(node => { pageview(node) }) } const domchange = (arr) => { checkNode(document) } const dom = new MutationObserver(domchange) dom.observe(document.body, {subtree: true, childList: true}) checkNode(document) }(window, document, "https://pushbroom.co")