Skip to content

Commit

Permalink
it actually works
Browse files Browse the repository at this point in the history
  • Loading branch information
mariusandra committed Nov 13, 2024
1 parent 4c36deb commit 40c35a7
Showing 1 changed file with 18 additions and 1 deletion.
19 changes: 18 additions & 1 deletion posthog/api/site_app.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@
from statshog.defaults.django import statsd

from posthog.exceptions import generate_exception_response
from posthog.hogql import ast
from posthog.hogql.compiler.javascript import create_javascript
from posthog.hogql.parser import parse_string_template
from posthog.logging.timing import timed
from posthog.plugins.site import get_site_config_from_schema, get_transpiled_site_source

Expand All @@ -22,7 +25,21 @@ def get_site_app(request: HttpRequest, id: int, token: str, hash: str) -> HttpRe
id = source_file.id
source = source_file.source
config = get_site_config_from_schema(source_file.config_schema, source_file.config)
response = f"{source}().inject({{config:{json.dumps(config)},posthog:window['__$$ph_site_app_{id}']}})"

response = f"globalConfig = {json.dumps(config)};"
response += "function get_global(key) {return globalConfig[key]}"

extra = ""
for key, value in config.items():
if isinstance(value, str) and "{" in value:
stmt = ast.ReturnStatement(expr=parse_string_template(value))
code = create_javascript(
ast.Function(name=f"__get_config_{key}", body=ast.Block(declarations=[stmt]), params=["event"])
).code
extra += code + "\n"
response += extra

response += f"{source}().inject({{config:globalConfig,posthog:window['__$$ph_site_app_{id}']}});"

statsd.incr(f"posthog_cloud_raw_endpoint_success", tags={"endpoint": "site_app"})
return HttpResponse(content=response, content_type="application/javascript")
Expand Down

0 comments on commit 40c35a7

Please sign in to comment.