Skip to content

Commit

Permalink
fix: Match config to old format (#26600)
Browse files Browse the repository at this point in the history
Co-authored-by: github-actions <41898282+github-actions[bot]@users.noreply.github.com>
  • Loading branch information
benjackwhite and github-actions[bot] authored Dec 4, 2024
1 parent 30ac3ea commit bf32a66
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 35 deletions.
22 changes: 11 additions & 11 deletions posthog/models/remote_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,10 +68,10 @@ def build_config(self):
# NOTE: Let's try and keep this tidy! Follow the styling of the values already here...
config = {
"token": team.api_token,
"supported_compression": ["gzip", "gzip-js"],
"has_feature_flags": FeatureFlag.objects.filter(team=team, active=True, deleted=False).count() > 0,
"capture_dead_clicks": bool(team.capture_dead_clicks),
"capture_performance": (
"supportedCompression": ["gzip", "gzip-js"],
"hasFeatureFlags": FeatureFlag.objects.filter(team=team, active=True, deleted=False).count() > 0,
"captureDeadClicks": bool(team.capture_dead_clicks),
"capturePerformance": (
{
"network_timing": bool(team.capture_performance_opt_in),
"web_vitals": bool(team.autocapture_web_vitals_opt_in),
Expand All @@ -81,7 +81,7 @@ def build_config(self):
else False
),
"autocapture_opt_out": bool(team.autocapture_opt_out),
"autocapture_exceptions": (
"autocaptureExceptions": (
{
"endpoint": "/e/",
}
Expand All @@ -92,7 +92,7 @@ def build_config(self):
}

if str(team.id) not in (settings.ELEMENT_CHAIN_AS_STRING_EXCLUDED_TEAMS or []):
config["elements_chain_as_string"] = True
config["elementsChainAsString"] = True

# MARK: Session Recording
session_recording_config_response: bool | dict = False
Expand Down Expand Up @@ -136,7 +136,7 @@ def build_config(self):
"canvasQuality": "0.4" if record_canvas else None,
}
)
config["session_recording"] = session_recording_config_response
config["sessionRecording"] = session_recording_config_response

# MARK: Quota limiting
if settings.EE_AVAILABLE:
Expand All @@ -151,16 +151,16 @@ def build_config(self):
)

if team.api_token in limited_tokens_recordings:
config["quota_limited"] = ["recordings"]
config["session_recording"] = False
config["quotaLimited"] = ["recordings"]
config["sessionRecording"] = False

config["surveys"] = True if team.surveys_opt_in else False
config["heatmaps"] = True if team.heatmaps_opt_in else False
try:
default_identified_only = team.pk >= int(settings.DEFAULT_IDENTIFIED_ONLY_TEAM_ID_MIN)
except Exception:
default_identified_only = False
config["default_identified_only"] = bool(default_identified_only)
config["defaultIdentifiedOnly"] = bool(default_identified_only)

# MARK: Site apps - we want to eventually inline the JS but that will come later
site_apps = []
Expand All @@ -171,7 +171,7 @@ def build_config(self):
except Exception:
pass

config["site_apps"] = site_apps
config["siteApps"] = site_apps

return config

Expand Down
3 changes: 2 additions & 1 deletion posthog/models/test/test_hog_function.py
Original file line number Diff line number Diff line change
Expand Up @@ -278,7 +278,8 @@ def test_hog_functions_reload_on_team_saved(self):
{"key": "$pageview", "operator": "regex", "value": "test"},
]
# 1 update team, 1 load hog functions, 1 update hog functions
with self.assertNumQueries(3):
# 4 unrelated due to RemoteConfig refresh
with self.assertNumQueries(3 + 4):
self.team.save()
hog_function_1.refresh_from_db()
hog_function_2.refresh_from_db()
Expand Down
42 changes: 19 additions & 23 deletions posthog/models/test/test_remote_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,26 +37,22 @@ def test_creates_remote_config_immediately(self):
"token": "phc_12345",
"surveys": False,
"heatmaps": False,
"siteApps": [],
"analytics": {"endpoint": "/i/v0/e/"},
"site_apps": [],
"has_feature_flags": False,
"session_recording": False,
"hasFeatureFlags": False,
"sessionRecording": False,
"captureDeadClicks": False,
"capturePerformance": {"web_vitals": False, "network_timing": True, "web_vitals_allowed_metrics": None},
"autocapture_opt_out": False,
"capture_dead_clicks": False,
"capture_performance": {
"web_vitals": False,
"network_timing": True,
"web_vitals_allowed_metrics": None,
},
"supported_compression": ["gzip", "gzip-js"],
"autocapture_exceptions": False,
"default_identified_only": False,
"elements_chain_as_string": True,
"supportedCompression": ["gzip", "gzip-js"],
"autocaptureExceptions": False,
"defaultIdentifiedOnly": False,
"elementsChainAsString": True,
}
)

def test_indicates_if_feature_flags_exist(self):
assert not self.remote_config.config["has_feature_flags"]
assert not self.remote_config.config["hasFeatureFlags"]

flag = FeatureFlag.objects.create(
team=self.team,
Expand All @@ -67,29 +63,29 @@ def test_indicates_if_feature_flags_exist(self):
deleted=True,
)

assert not self.remote_config.config["has_feature_flags"]
assert not self.remote_config.config["hasFeatureFlags"]
flag.active = False
flag.deleted = False
flag.save()
self.remote_config.refresh_from_db()
assert not self.remote_config.config["has_feature_flags"]
assert not self.remote_config.config["hasFeatureFlags"]
flag.active = True
flag.deleted = False
flag.save()
self.remote_config.refresh_from_db()
assert self.remote_config.config["has_feature_flags"]
assert self.remote_config.config["hasFeatureFlags"]

def test_capture_dead_clicks_toggle(self):
self.team.capture_dead_clicks = True
self.team.save()
self.remote_config.refresh_from_db()
assert self.remote_config.config["capture_dead_clicks"]
assert self.remote_config.config["captureDeadClicks"]

def test_capture_performance_toggle(self):
self.team.capture_performance_opt_in = True
self.team.save()
self.remote_config.refresh_from_db()
assert self.remote_config.config["capture_performance"]["network_timing"]
assert self.remote_config.config["capturePerformance"]["network_timing"]

def test_autocapture_opt_out_toggle(self):
self.team.autocapture_opt_out = True
Expand All @@ -101,14 +97,14 @@ def test_autocapture_exceptions_toggle(self):
self.team.autocapture_exceptions_opt_in = True
self.team.save()
self.remote_config.refresh_from_db()
assert self.remote_config.config["autocapture_exceptions"] == {"endpoint": "/e/"}
assert self.remote_config.config["autocaptureExceptions"] == {"endpoint": "/e/"}

def test_session_recording_sample_rate(self):
self.team.session_recording_opt_in = True
self.team.session_recording_sample_rate = Decimal("0.5")
self.team.save()
self.remote_config.refresh_from_db()
assert self.remote_config.config["session_recording"]["sampleRate"] == "0.50"
assert self.remote_config.config["sessionRecording"]["sampleRate"] == "0.50"


class TestRemoteConfigSync(_RemoteConfigBase):
Expand Down Expand Up @@ -143,7 +139,7 @@ def test_renders_js_including_config(self):
assert js == snapshot(
"""\
(function() {
window._POSTHOG_CONFIG = {"token": "phc_12345", "surveys": false, "heatmaps": false, "analytics": {"endpoint": "/i/v0/e/"}, "site_apps": [], "has_feature_flags": false, "session_recording": false, "autocapture_opt_out": false, "capture_dead_clicks": false, "capture_performance": {"web_vitals": false, "network_timing": true, "web_vitals_allowed_metrics": null}, "supported_compression": ["gzip", "gzip-js"], "autocapture_exceptions": false, "default_identified_only": false, "elements_chain_as_string": true};
window._POSTHOG_CONFIG = {"token": "phc_12345", "surveys": false, "heatmaps": false, "siteApps": [], "analytics": {"endpoint": "/i/v0/e/"}, "hasFeatureFlags": false, "sessionRecording": false, "captureDeadClicks": false, "capturePerformance": {"web_vitals": false, "network_timing": true, "web_vitals_allowed_metrics": null}, "autocapture_opt_out": false, "supportedCompression": ["gzip", "gzip-js"], "autocaptureExceptions": false, "defaultIdentifiedOnly": false, "elementsChainAsString": true};
window._POSTHOG_SITE_APPS = [];
})();\
"""
Expand Down Expand Up @@ -187,7 +183,7 @@ def test_renders_js_including_site_apps(self):
assert js == snapshot(
"""\
(function() {
window._POSTHOG_CONFIG = {"token": "phc_12345", "surveys": false, "heatmaps": false, "analytics": {"endpoint": "/i/v0/e/"}, "site_apps": [], "has_feature_flags": false, "session_recording": false, "autocapture_opt_out": false, "capture_dead_clicks": false, "capture_performance": {"web_vitals": false, "network_timing": true, "web_vitals_allowed_metrics": null}, "supported_compression": ["gzip", "gzip-js"], "autocapture_exceptions": false, "default_identified_only": false, "elements_chain_as_string": true};
window._POSTHOG_CONFIG = {"token": "phc_12345", "surveys": false, "heatmaps": false, "siteApps": [], "analytics": {"endpoint": "/i/v0/e/"}, "hasFeatureFlags": false, "sessionRecording": false, "captureDeadClicks": false, "capturePerformance": {"web_vitals": false, "network_timing": true, "web_vitals_allowed_metrics": null}, "autocapture_opt_out": false, "supportedCompression": ["gzip", "gzip-js"], "autocaptureExceptions": false, "defaultIdentifiedOnly": false, "elementsChainAsString": true};
window._POSTHOG_SITE_APPS = [{ token: 'tokentoken', load: function(posthog) { (function () { return { inject: (data) => console.log('injected!', data)}; })().inject({ config:{}, posthog:posthog }) } },{ token: 'tokentoken', load: function(posthog) { (function () { return { inject: (data) => console.log('injected 2!', data)}; })().inject({ config:{}, posthog:posthog }) } },{ token: 'tokentoken', load: function(posthog) { (function () { return { inject: (data) => console.log('injected but disabled!', data)}; })().inject({ config:{}, posthog:posthog }) } }];
})();\
"""
Expand Down

0 comments on commit bf32a66

Please sign in to comment.