File size: 1,526 Bytes
5c1dcf0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""Optional PostHog analytics. Loads only when POSTHOG_KEY is configured.

POSTHOG_KEY is a public (client-side) project key, not a secret; set it as a
Space *variable*. CTA clicks fire `hf_loreify_cta_clicked` with placement,
generated_before_click, and device_type (wired in ctas.py).
"""

import json
import os


def analytics_head() -> str:
    key = os.environ.get("POSTHOG_KEY", "")
    if not key:
        return ""
    host = os.environ.get("POSTHOG_HOST", "https://us.i.posthog.com")
    return (
        "<script>"
        "!function(t,e){var o,n,p,r;e.__SV||(window.posthog=e,e._i=[],e.init=function(i,s,a)"
        "{function g(t,e){var o=e.split('.');2==o.length&&(t=t[o[0]],e=o[1]),t[e]=function()"
        "{t.push([e].concat(Array.prototype.slice.call(arguments,0)))}}(p=t.createElement('script'))"
        ".type='text/javascript',p.crossOrigin='anonymous',p.async=!0,p.src=s.api_host+'/static/array.js',"
        "(r=t.getElementsByTagName('script')[0]).parentNode.insertBefore(p,r);var u=e;for(void 0!==a?"
        "u=e[a]=[]:a='posthog',u.people=u.people||[],u.toString=function(t){var e='posthog';return"
        "'posthog'!==a&&(e+='.'+a),t||(e+=' (stub)'),e},u.people.toString=function(){return u.toString(1)"
        "+'.people (stub)'},o='init capture identify'.split(' '),n=0;n<o.length;n++)g(u,o[n]);"
        "e._i.push([i,s,a])},e.__SV=1)}(document,window.posthog||[]);"
        f"posthog.init({json.dumps(key)},{{api_host:{json.dumps(host)},person_profiles:'never'}});"
        "</script>"
    )