{
  "version": 3,
  "sources": ["latent-pages.js"],
  "sourcesContent": ["import { config } from \"./config.js\";\nimport { latent } from \"./latent.js\";\n\n/**\n * Where visitors come from and how long they stay, for the pages of the\n * platform itself - the landing pages and the panel. A client's gallery has its\n * visits recorded by the server instead.\n */\nconst UTM_PARAMETERS = [\n    \"utm_source\",\n    \"utm_medium\",\n    \"utm_campaign\",\n    \"utm_term\",\n    \"utm_content\",\n];\n\n/** Routes whose address carries a secret token, to be left out of what is recorded. */\nconst TOKEN_ROUTES =\n    /^\\/(activate|email\\/confirm|landing-pass|legal\\/accept|newsletter\\/confirm|newsletter\\/unsubscribe)\\/[^/]+/;\n\n/** A path as it is recorded: the token of a route that has one is never in it. */\nexport function recordedPath(path) {\n    return path.replace(TOKEN_ROUTES, \"/$1/:token\");\n}\n\n/** The host a visitor came from, when it is not this one: never the page itself. */\nfunction referrerHost() {\n    try {\n        const { host } = new URL(document.referrer);\n        return host === window.location.host ? \"\" : host;\n    } catch {\n        return \"\";\n    }\n}\n\nfunction campaign() {\n    const query = new URLSearchParams(window.location.search);\n    return Object.fromEntries(\n        UTM_PARAMETERS.map((name) => [name, query.get(name)]).filter(\n            ([, value]) => value,\n        ),\n    );\n}\n\nlet visibleSince = document.visibilityState === \"visible\" ? performance.now() : null;\nlet visibleSeconds = 0;\nlet deepest = 0;\n\nfunction scrollDepth() {\n    const page = document.documentElement;\n    const scrollable = page.scrollHeight - window.innerHeight;\n    if (scrollable <= 0) {\n        return 100;\n    }\n    return Math.min(100, Math.round(((window.scrollY || 0) / scrollable) * 100));\n}\n\nfunction stopClock() {\n    if (visibleSince !== null) {\n        visibleSeconds += (performance.now() - visibleSince) / 1000;\n        visibleSince = null;\n    }\n}\n\nexport function trackPage() {\n    if (config.surface !== \"landing\" && config.surface !== \"panel\") {\n        return;\n    }\n    latent.record(\"page.viewed\", {\n        path: recordedPath(window.location.pathname),\n        referrer: referrerHost(),\n        language: navigator.language,\n        viewport: [window.innerWidth, window.innerHeight],\n        ...campaign(),\n    });\n    window.addEventListener(\n        \"scroll\",\n        () => {\n            deepest = Math.max(deepest, scrollDepth());\n        },\n        { passive: true },\n    );\n    document.addEventListener(\"visibilitychange\", () => {\n        if (document.visibilityState === \"hidden\") {\n            stopClock();\n            latent.record(\"page.left\", {\n                path: recordedPath(window.location.pathname),\n                seconds: Math.round(visibleSeconds),\n                scroll: Math.max(deepest, scrollDepth()),\n            });\n            visibleSeconds = 0;\n        } else {\n            visibleSince = performance.now();\n        }\n    });\n}\n"],
  "mappings": "AAAA,OAAS,UAAAA,MAAc,cACvB,OAAS,UAAAC,MAAc,cAOvB,MAAMC,EAAiB,CACnB,aACA,aACA,eACA,WACA,aACJ,EAGMC,EACF,6GAGG,gBAAS,aAAaC,EAAM,CAC/B,OAAOA,EAAK,QAAQD,EAAc,YAAY,CAClD,CAGA,SAASE,GAAe,CACpB,GAAI,CACA,KAAM,CAAE,KAAAC,CAAK,EAAI,IAAI,IAAI,SAAS,QAAQ,EAC1C,OAAOA,IAAS,OAAO,SAAS,KAAO,GAAKA,CAChD,MAAQ,CACJ,MAAO,EACX,CACJ,CAEA,SAASC,GAAW,CAChB,MAAMC,EAAQ,IAAI,gBAAgB,OAAO,SAAS,MAAM,EACxD,OAAO,OAAO,YACVN,EAAe,IAAKO,GAAS,CAACA,EAAMD,EAAM,IAAIC,CAAI,CAAC,CAAC,EAAE,OAClD,CAAC,CAAC,CAAEC,CAAK,IAAMA,CACnB,CACJ,CACJ,CAEA,IAAIC,EAAe,SAAS,kBAAoB,UAAY,YAAY,IAAI,EAAI,KAC5EC,EAAiB,EACjBC,EAAU,EAEd,SAASC,GAAc,CAEnB,MAAMC,EADO,SAAS,gBACE,aAAe,OAAO,YAC9C,OAAIA,GAAc,EACP,IAEJ,KAAK,IAAI,IAAK,KAAK,OAAQ,OAAO,SAAW,GAAKA,EAAc,GAAG,CAAC,CAC/E,CAEA,SAASC,GAAY,CACbL,IAAiB,OACjBC,IAAmB,YAAY,IAAI,EAAID,GAAgB,IACvDA,EAAe,KAEvB,CAEO,gBAAS,WAAY,CACpBX,EAAO,UAAY,WAAaA,EAAO,UAAY,UAGvDC,EAAO,OAAO,cAAe,CACzB,KAAM,aAAa,OAAO,SAAS,QAAQ,EAC3C,SAAUI,EAAa,EACvB,SAAU,UAAU,SACpB,SAAU,CAAC,OAAO,WAAY,OAAO,WAAW,EAChD,GAAGE,EAAS,CAChB,CAAC,EACD,OAAO,iBACH,SACA,IAAM,CACFM,EAAU,KAAK,IAAIA,EAASC,EAAY,CAAC,CAC7C,EACA,CAAE,QAAS,EAAK,CACpB,EACA,SAAS,iBAAiB,mBAAoB,IAAM,CAC5C,SAAS,kBAAoB,UAC7BE,EAAU,EACVf,EAAO,OAAO,YAAa,CACvB,KAAM,aAAa,OAAO,SAAS,QAAQ,EAC3C,QAAS,KAAK,MAAMW,CAAc,EAClC,OAAQ,KAAK,IAAIC,EAASC,EAAY,CAAC,CAC3C,CAAC,EACDF,EAAiB,GAEjBD,EAAe,YAAY,IAAI,CAEvC,CAAC,EACL",
  "names": ["config", "latent", "UTM_PARAMETERS", "TOKEN_ROUTES", "path", "referrerHost", "host", "campaign", "query", "name", "value", "visibleSince", "visibleSeconds", "deepest", "scrollDepth", "scrollable", "stopClock"]
}
