: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
feat(marketing-analytics): show one organic rule per search (#128404)
Javier Bahamondesb870be51521e
diff --git a/products/marketing_analytics/backend/hogql_queries/marketing_recommendations.py b/products/marketing_analytics/backend/hogql_queries/marketing_recommendations.pyindex 524d0514adfa..5d47f5987b79 100644--- a/products/marketing_analytics/backend/hogql_queries/marketing_recommendations.py+++ b/products/marketing_analytics/backend/hogql_queries/marketing_recommendations.py@@ -17,6 +17,8 @@ from posthog.dataclasses import frozenPER_RULE_LIMIT = 10# Share of impressions that become clicks once a page reaches the top 3 organic results. A rough industry figure.TOP3_CTR = 0.15+# Below this, reaching the top 3 brings too few clicks to be worth editing the page.+STRIKING_MIN_MONTHLY_IMPRESSIONS = 100# Below this, someone else's ad often takes the first spot on your own brand searches.BRAND_DEFENSE_ABSOLUTE_TOP = 0.9INACTIVE_STATUSES = frozenset({"PAUSED", "ENDED", "REMOVED"})@@ -553,6 +555,7 @@ class _Recommender:and term.conversions >= 2and term.cost / term.conversions <= cpaand (not term.organic or term.organic.position > 10)+ and not self._in_striking_distance(term, competitors)]rules[MarketingAnalyticsRecommendationRule.SPEND_WITHOUT_CONVERSIONS] = [self._term_recommendation(@@ -592,11 +595,7 @@ class _Recommender:steps=self._improve_page_steps(term.term),)for term in terms- if term.organic- and term.term not in self.brand_terms- and 5 <= term.organic.position <= 20- and term.clicks >= 5- and term.term not in competitors+ if term.organic and self._in_striking_distance(term, competitors)]ordered: list[MarketingAnalyticsRecommendation] = []@@ -955,6 +954,16 @@ class _Recommender:linkText="Campaigns",)+ def _in_striking_distance(self, term: _Term, competitors: set[str]) -> bool:+ return bool(+ term.organic+ and term.term not in self.brand_terms+ and 5 <= term.organic.position <= 20+ and term.organic.impressions * self.per_month >= STRIKING_MIN_MONTHLY_IMPRESSIONS+ and term.clicks >= 5+ and term.term not in competitors+ )+def _content_gap_steps(self, term: _Term) -> tuple[Step, ...]:keep_ad = Step(text="Keep the ad running until the page reaches the top 3, then test pausing it.")if term.organic:diff --git a/products/marketing_analytics/backend/hogql_queries/test_marketing_recommendations.py b/products/marketing_analytics/backend/hogql_queries/test_marketing_recommendations.pyindex 233d20267387..3191b09ee776 100644--- a/products/marketing_analytics/backend/hogql_queries/test_marketing_recommendations.py+++ b/products/marketing_analytics/backend/hogql_queries/test_marketing_recommendations.py@@ -196,6 +196,27 @@ class TestMarketingRecommendations(SimpleTestCase):assert steps[0].text.startswith("Create a page")assert all(step.linkText and step.linkText in step.text for step in steps if step.url)+ @parameterized.expand(+ [+ ("page_two_is_only_striking_distance", 15.0, 2000, {"striking_distance"}),+ ("page_two_with_few_impressions_is_a_content_gap", 15.0, 20, {"content_gap"}),+ ("page_one_with_few_impressions_is_neither", 8.0, 20, set()),+ ]+ )+ def test_a_ranking_search_gets_one_organic_rule(+ self, _name: str, position: float, impressions: float, expected: set[str]+ ) -> None:+ terms = (+ _term("funnel analysis", "c1", cost=100, conversions=5),+ _term("session replay", "c1", cost=900, conversions=5),+ )+ organic = (OrganicQueryStats(query="funnel analysis", clicks=1, impressions=impressions, position=position),)++ recs = recommend(_inputs((_campaign(),), terms, organic))++ rules = {rec.rule for rec in recs if rec.subject == "funnel analysis"} & {"content_gap", "striking_distance"}+ assert rules == expected+@parameterized.expand([("another_ad_often_shows_above", 0.6, "keep", "Keep the ad"),