Skip to content

: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.

fix(marketing-analytics): resolve default source aliases in utm audit matching (#74052)

Javier Bahamondes3e8918f6c420
diff --git a/products/marketing_analytics/backend/services/test_utm_audit.py b/products/marketing_analytics/backend/services/test_utm_audit.py
index 696b78a5aff5..f2bd5e830b61 100644
--- a/products/marketing_analytics/backend/services/test_utm_audit.py
+++ b/products/marketing_analytics/backend/services/test_utm_audit.py
@@ -52,7 +52,7 @@ class TestCrossReference:
def test_campaign_with_source_mismatch(self):
campaigns = [Campaign("Brand Campaign", "789", "google", 200.0, 80, 2000)]
- utm_events = {("brand campaign", "adwords"): 30}
+ utm_events = {("brand campaign", "newsletter"): 30}
results = _cross_reference(campaigns, utm_events, NO_MAPPINGS, DEFAULT_KNOWN_SOURCES)
@@ -62,6 +62,48 @@ class TestCrossReference:
assert results[0].issues[0].field == "utm_source"
assert results[0].issues[0].severity == UtmIssueSeverity.WARNING
+ @pytest.mark.parametrize(
+ "campaign_source,utm_source",
+ [
+ ("meta", "facebook"),
+ ("meta", "fb"),
+ ("meta", "instagram"),
+ ("google", "adwords"),
+ ("google", "google_maps"),
+ ],
+ )
+ def test_default_alias_counts_as_exact_match(self, campaign_source, utm_source):
+ campaigns = [Campaign("Spring Sale", "123", campaign_source, 100.0, 50, 1000)]
+ utm_events = {("spring sale", utm_source): 42}
+
+ results = _cross_reference(campaigns, utm_events, NO_MAPPINGS, DEFAULT_KNOWN_SOURCES)
+
+ assert len(results) == 1
+ assert results[0].has_utm_events is True
+ assert results[0].event_count == 42
+ assert len(results[0].issues) == 0
+
+ def test_custom_mapping_wins_over_default_alias(self):
+ # Team remapped 'facebook' to google, so it must not count for the meta campaign.
+ campaigns = [
+ Campaign("Spring Sale", "1", "meta", 100.0, 50, 1000),
+ Campaign("Spring Sale", "2", "google", 100.0, 50, 1000),
+ ]
+ utm_events = {("spring sale", "facebook"): 42}
+ mappings = TeamMappings(
+ source_to_integration={"facebook": "google"},
+ campaign_aliases={},
+ field_preferences={},
+ )
+
+ results = _cross_reference(campaigns, utm_events, mappings, _build_known_sources(mappings))
+
+ google = next(r for r in results if r.source_name == "google")
+ meta = next(r for r in results if r.source_name == "meta")
+ assert google.has_utm_events is True
+ assert google.event_count == 42
+ assert meta.has_utm_events is False
+
def test_case_insensitive_matching(self):
campaigns = [Campaign("WINTER Sale", "101", "Google", 150.0, 60, 1500)]
utm_events = {("winter sale", "google"): 25}
@@ -418,7 +460,7 @@ class TestBuildAllUtmEvents:
def test_campaign_auto_source_none(self):
campaigns = [Campaign("brand", "1", "google", 0, 0, 0)]
- utm_events = {("brand", "adwords"): 30}
+ utm_events = {("brand", "newsletter"): 30}
result = _build_all_utm_events(campaigns, utm_events, NO_MAPPINGS)
@@ -427,6 +469,16 @@ class TestBuildAllUtmEvents:
assert result[0].source_match == "none"
assert result[0].matched_campaign == "brand"
+ def test_default_alias_source_auto(self):
+ campaigns = [Campaign("brand", "1", "meta", 0, 0, 0)]
+ utm_events = {("brand", "facebook"): 30}
+
+ result = _build_all_utm_events(campaigns, utm_events, NO_MAPPINGS)
+
+ assert len(result) == 1
+ assert result[0].campaign_match == "auto"
+ assert result[0].source_match == "auto"
+
def test_source_auto_campaign_none(self):
campaigns = [Campaign("brand", "1", "google", 0, 0, 0)]
utm_events = {("unknown_campaign", "google"): 30}
diff --git a/products/marketing_analytics/backend/services/utm_audit.py b/products/marketing_analytics/backend/services/utm_audit.py
index 245adb00faf2..588a3a3bc0d6 100644
--- a/products/marketing_analytics/backend/services/utm_audit.py
+++ b/products/marketing_analytics/backend/services/utm_audit.py
@@ -1,4 +1,5 @@
from dataclasses import dataclass
+from functools import cache
from django.utils import timezone
@@ -20,7 +21,12 @@ from products.marketing_analytics.backend.hogql_queries.constants import (
INTEGRATION_DEFAULT_SOURCES,
INTEGRATION_PRIMARY_SOURCE,
)
-from products.marketing_analytics.backend.services.native_integrations import KEY_TO_NATIVE, iter_custom_source_mappings
+from products.marketing_analytics.backend.services.native_integrations import (
+ KEY_TO_NATIVE,
+ canonical_source_aliases,
+ iter_custom_source_mappings,
+ normalize,
+)
from products.marketing_analytics.backend.services.types import (
AlternativeSource,
Campaign,
@@ -280,9 +286,26 @@ def _get_utm_events(team: Team, date_range: QueryDateRange, *, user: User | None
return utm_map
+@cache
+def _default_alias_to_primary() -> dict[str, str]:
+ """Normalized default utm_source aliases ('facebook', 'adwords') -> the
+ integration's primary source name ('meta', 'google')."""
+ out: dict[str, str] = {}
+ for alias, target_key in canonical_source_aliases().items():
+ primary = INTEGRATION_PRIMARY_SOURCE.get(KEY_TO_NATIVE[target_key])
+ if primary:
+ out[alias] = str(primary).lower().strip()
+ return out
+
+
def _resolve_source(utm_source: str, mappings: TeamMappings) -> str:
- """Resolve a utm_source to its integration source using custom mappings."""
- return mappings.source_to_integration.get(utm_source, utm_source)
+ """Resolve a utm_source to its integration's primary source: team custom
+ mappings first (they win, matching `build_combined_alias_map`), then the
+ platform default aliases so e.g. 'facebook' resolves to 'meta'."""
+ custom = mappings.source_to_integration.get(utm_source)
+ if custom is not None:
+ return custom
+ return _default_alias_to_primary().get(normalize(utm_source), utm_source)
def _get_match_value(campaign: Campaign, mappings: TeamMappings) -> str: