: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): say where to start when reviewing a costly campaign (#127860)
Javier Bahamondesed7d4eae5d08
diff --git a/frontend/src/queries/schema.json b/frontend/src/queries/schema.jsonindex b8e4fe2a35c7..c1a17c68861f 100644--- a/frontend/src/queries/schema.json+++ b/frontend/src/queries/schema.json@@ -36383,7 +36383,7 @@"additionalProperties": false,"properties": {"copyValue": {- "description": "Text to paste into Google Ads at this step, such as a campaign name or a keyword in match type syntax.",+ "description": "Text to paste into Google Ads at this step, such as a campaign name or keywords in match type syntax, one per line.","type": ["string", "null"]},"linkText": {diff --git a/frontend/src/queries/schema/schema-general.ts b/frontend/src/queries/schema/schema-general.tsindex b43c6dd50019..de5fbefffaba 100644--- a/frontend/src/queries/schema/schema-general.ts+++ b/frontend/src/queries/schema/schema-general.ts@@ -8606,7 +8606,7 @@ export type MarketingAnalyticsRecommendationImpactType = 'measured' | 'estimatedexport interface MarketingAnalyticsRecommendationStep {text: string- /** Text to paste into Google Ads at this step, such as a campaign name or a keyword in match type syntax. */+ /** Text to paste into Google Ads at this step, such as a campaign name or keywords in match type syntax, one per line. */copyValue?: string | null/** Google Ads page for this step. It opens the section for the whole account, because links cannot filter to one campaign. */url?: string | nulldiff --git a/posthog/schema.py b/posthog/schema.pyindex bbc5dd9b81c0..262a71ee479b 100644--- a/posthog/schema.py+++ b/posthog/schema.py@@ -2187,7 +2187,8 @@ class MarketingAnalyticsRecommendationStep(BaseModel):copyValue: str | None = Field(default=None,description=(- "Text to paste into Google Ads at this step, such as a campaign name or a keyword in match type syntax."+ "Text to paste into Google Ads at this step, such as a campaign name or"+ " keywords in match type syntax, one per line."),)linkText: str | None = Field(diff --git a/products/marketing_analytics/backend/hogql_queries/marketing_recommendations.py b/products/marketing_analytics/backend/hogql_queries/marketing_recommendations.pyindex d99ba386d76f..b67b0507c849 100644--- a/products/marketing_analytics/backend/hogql_queries/marketing_recommendations.py+++ b/products/marketing_analytics/backend/hogql_queries/marketing_recommendations.py@@ -70,6 +70,9 @@ ADS_CAMPAIGNS_URL = "https://ads.google.com/aw/campaigns"ADS_KEYWORDS_URL = "https://ads.google.com/aw/keywords"ADS_NEGATIVE_KEYWORDS_URL = "https://ads.google.com/aw/keywords/negative"ADS_SEARCH_TERMS_URL = "https://ads.google.com/aw/keywords/searchterms"+REVIEW_TERMS_SHOWN = 3+# A non-brand campaign is up for review when its cost per conversion reaches this multiple of the non-brand average.+REVIEW_CPA_MULTIPLE = 3@frozen@@ -286,7 +289,7 @@ class _Recommender:nonbrand_cpaand search_costand campaign.cost >= 0.05 * search_cost- and (cpa is None or cpa >= 3 * nonbrand_cpa)+ and (cpa is None or cpa >= REVIEW_CPA_MULTIPLE * nonbrand_cpa)):recommendations.append(self._review(campaign, cpa, nonbrand_cpa))return recommendations@@ -423,8 +426,75 @@ class _Recommender:campaign.cost * self.per_month,"save","measured",+ steps=self._review_steps(campaign, nonbrand_cpa),)+ def _review_steps(self, campaign: CampaignStats, nonbrand_cpa: float) -> tuple[Step, ...]:+ spend: dict[str, list[float]] = defaultdict(lambda: [0.0, 0.0])+ # ADDED means the search is already a keyword and EXCLUDED means it is already a negative.+ targeted_or_excluded: set[str] = set()+ excluded: set[str] = set()+ for row in self.inputs.search_terms:+ if row.campaign_id != campaign.id or row.term in self.brand_terms:+ continue+ spend[row.term][0] += row.cost+ spend[row.term][1] += row.conversions+ if row.status not in ("NONE", ""):+ targeted_or_excluded.add(row.term)+ if "EXCLUDED" in row.status:+ excluded.add(row.term)+ competitors = self._competitor_names()+ draining = sorted(+ (+ term+ for term, (cost, conversions) in spend.items()+ if cost >= nonbrand_cpa+ and not conversions+ and term not in targeted_or_excluded+ and not self._mentions(term, competitors)+ ),+ key=lambda term: -spend[term][0],+ )[:REVIEW_TERMS_SHOWN]+ converting = sorted(+ (+ term+ for term, (cost, conversions) in spend.items()+ if conversions > 0 and cost / conversions <= nonbrand_cpa and term not in excluded+ ),+ key=lambda term: -spend[term][1],+ )[:REVIEW_TERMS_SHOWN]+ steps = [self._open_campaign(campaign.name)]+ if draining:+ listed = ", ".join(f'"{term}" ({self._money(spend[term][0])})' for term in draining)+ steps.append(+ Step(+ text=f"Start with the searches that spent the most without converting: {listed}. "+ "Open Negative keywords and add them for the campaign in exact match.",+ copyValue="\n".join(f"[{term}]" for term in draining),+ url=ADS_NEGATIVE_KEYWORDS_URL,+ linkText="Negative keywords",+ )+ )+ if converting:+ listed = ", ".join(+ f'"{term}" ({self._money(spend[term][0] / spend[term][1])} per conversion)' for term in converting+ )+ steps.append(+ Step(+ text=f"Keep the searches that convert at or below the non-brand average: {listed}. "+ "Check that their keywords stay active."+ )+ )+ steps.append(+ Step(+ text=f"If its non-brand cost per conversion is still more than {REVIEW_CPA_MULTIPLE}x the non-brand "+ "average after two weeks, pause it in Campaigns.",+ url=ADS_CAMPAIGNS_URL,+ linkText="Campaigns",+ )+ )+ return tuple(steps)+# Search term rulesdef _search_term_rules(self) -> list[MarketingAnalyticsRecommendation]:diff --git a/products/marketing_analytics/backend/hogql_queries/test_marketing_recommendations.py b/products/marketing_analytics/backend/hogql_queries/test_marketing_recommendations.pyindex 7626f3c31618..dfa1a58520b9 100644--- a/products/marketing_analytics/backend/hogql_queries/test_marketing_recommendations.py+++ b/products/marketing_analytics/backend/hogql_queries/test_marketing_recommendations.py@@ -127,6 +127,30 @@ class TestMarketingRecommendations(SimpleTestCase):assert [step.copyValue for step in leak.steps or [] if step.copyValue] == ["Generic", '"example"']assert not any(rec.rule == "scale_campaign" and rec.subject == "Generic" for rec in recs)+ def test_review_campaign_steps_start_with_the_searches_that_waste_spend(self) -> None:+ good = _campaign(id="c1", name="Good", cost=1000, conversions=10)+ costly = _campaign(id="c2", name="Costly", cost=2000, conversions=2)+ terms = (+ _term("product analytics", "c1", cost=1000, conversions=10),+ _term("drain small", "c2", cost=300, conversions=0),+ _term("drain big", "c2", cost=400, conversions=0),+ _term("drain too small", "c2", cost=20, conversions=0),+ replace(_term("already a keyword", "c2", cost=500, conversions=0), status="ADDED"),+ _term("example login", "c2", cost=600, conversions=0),+ _term("cheap keeper", "c2", cost=50, conversions=1),+ replace(_term("cheap but excluded", "c2", cost=40, conversions=1), status="EXCLUDED"),+ _term("costly converter", "c2", cost=300, conversions=1),+ )++ review = next(rec for rec in recommend(_inputs((good, costly), terms)) if rec.rule == "review_campaign")++ steps = review.steps or []+ assert review.subject == "Costly"+ assert [step.copyValue for step in steps if step.copyValue] == ["Costly", "[drain big]\n[drain small]"]+ assert '"cheap keeper"' in steps[2].text+ assert "costly converter" not in steps[2].text and "cheap but excluded" not in steps[2].text+ assert all(step.linkText and step.linkText in step.text for step in steps if step.url)+@parameterized.expand([("another_ad_often_shows_above", 0.6, "keep", "Keep the ad"),diff --git a/products/marketing_analytics/frontend/recommendations/RecommendationSteps.tsx b/products/marketing_analytics/frontend/recommendations/RecommendationSteps.tsxindex 9d6ec683c034..7385e89943af 100644--- a/products/marketing_analytics/frontend/recommendations/RecommendationSteps.tsx+++ b/products/marketing_analytics/frontend/recommendations/RecommendationSteps.tsx@@ -14,11 +14,13 @@ export function RecommendationSteps({ steps }: { steps: MarketingAnalyticsRecomm<div className="mt-1"><CopyToClipboardInlinedescription="value"+ explicitValue={step.copyValue}iconSize="xsmall"className="rounded border bg-surface-primary px-1.5 py-0.5 font-mono text-xs"data-attr="marketing-recommendations-step-copy">- {step.copyValue}+ {/* A list of keywords goes one per line, the way Google Ads takes a pasted list. */}+ <span className="whitespace-pre-line">{step.copyValue}</span></CopyToClipboardInline></div>)}diff --git a/services/mcp/src/api/generated.ts b/services/mcp/src/api/generated.tsindex 863d75e91ed8..89b706d0573a 100644--- a/services/mcp/src/api/generated.ts+++ b/services/mcp/src/api/generated.ts@@ -62080,7 +62080,7 @@ export namespace Schemas {} as const;export interface MarketingAnalyticsRecommendationStep {- /** Text to paste into Google Ads at this step, such as a campaign name or a keyword in match type syntax. */+ /** Text to paste into Google Ads at this step, such as a campaign name or keywords in match type syntax, one per line. */copyValue?: string | null;/** The words in `text` that link to `url`, usually the section name. */linkText?: string | null;