:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.
Python ab-testingai-analyticsanalyticscdpdata-warehouseexperimentsfeature-flagsjavascriptproduct-analyticspythonreactsession-replaysurveystypescriptweb-analytics
fix(cdp): guard meta ads fbc against malformed fbclid person properties (#73897)
Harley Alexandercabc92d6ee06
diff --git a/posthog/cdp/templates/meta_ads/template_meta_ads.py b/posthog/cdp/templates/meta_ads/template_meta_ads.pyindex 9822145c7905..4944215204cd 100644--- a/posthog/cdp/templates/meta_ads/template_meta_ads.py+++ b/posthog/cdp/templates/meta_ads/template_meta_ads.py@@ -180,7 +180,7 @@ if (res.status >= 400) {"em": "{sha256Hex(lower(person.properties.email))}","fn": "{sha256Hex(lower(person.properties.first_name))}","ln": "{sha256Hex(lower(person.properties.last_name))}",- "fbc": "{not empty(person.properties.fbclid ?? person.properties.$initial_fbclid) ? f'fb.1.{toUnixTimestampMilli(now())}.{person.properties.fbclid ?? person.properties.$initial_fbclid}' : ''}",+ "fbc": "{match(person.properties.fbclid ?? person.properties.$initial_fbclid ?? '', '^fb[.][0-9]+[.][0-9]+[.][A-Za-z0-9_-]+$') ? person.properties.fbclid ?? person.properties.$initial_fbclid : (match(person.properties.fbclid ?? person.properties.$initial_fbclid ?? '', '^[A-Za-z0-9_-]+$') ? f'fb.1.{toUnixTimestampMilli(now())}.{person.properties.fbclid ?? person.properties.$initial_fbclid}' : '')}","client_user_agent": "{event.properties.$raw_user_agent}",},"secret": False,diff --git a/posthog/cdp/templates/meta_ads/test_template_meta_ads.py b/posthog/cdp/templates/meta_ads/test_template_meta_ads.pyindex 21e70f1b58c8..b67deacd5a59 100644--- a/posthog/cdp/templates/meta_ads/test_template_meta_ads.py+++ b/posthog/cdp/templates/meta_ads/test_template_meta_ads.py@@ -1,6 +1,37 @@+import re++from django.test import SimpleTestCase++from parameterized import parameterized++from posthog.hogql.compiler.bytecode import create_bytecode+from posthog.hogql.parser import parse_string_template+from posthog.cdp.templates.helpers import BaseHogFunctionTemplateTestfrom posthog.cdp.templates.meta_ads.template_meta_ads import template as template_meta_ads+from common.hogvm.python.execute import execute_bytecode+++class TestMetaAdsFbcDefault(SimpleTestCase):+ @parameterized.expand(+ [+ ("clean_fbclid", {"fbclid": "IwAR2F4-dbP0l7Mn1"}, r"fb\.1\.\d+\.IwAR2F4-dbP0l7Mn1"),+ ("initial_fallback", {"$initial_fbclid": "AbC_123-x"}, r"fb\.1\.\d+\.AbC_123-x"),+ # a full _fbc cookie value stored in the property must pass through, not get wrapped again+ ("full_fbc_cookie", {"fbclid": "fb.1.1752830400000.IwAR123"}, r"fb\.1\.1752830400000\.IwAR123"),+ # values Meta would reject (subcode 2804001) are skipped rather than sent+ ("url_encoded_junk", {"fbclid": "IwAR%3D123 456"}, r""),+ ("prefixed_but_malformed", {"fbclid": "fb.junk"}, r""),+ ("missing_fbclid", {}, r""),+ ]+ )+ def test_fbc_default_expression(self, _name, person_properties, expected_pattern):+ expr = next(i for i in template_meta_ads.inputs_schema if i["key"] == "userData")["default"]["fbc"]+ bytecode = create_bytecode(parse_string_template(expr)).bytecode+ result = execute_bytecode(bytecode, {"person": {"properties": person_properties}}).result+ assert re.fullmatch(expected_pattern, result), f"got {result!r}"+class TestTemplateMetaAds(BaseHogFunctionTemplateTest):template = template_meta_ads