adloop 0.45.0__tar.gz → 0.46.0__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (105) hide show
  1. {adloop-0.45.0 → adloop-0.46.0}/PKG-INFO +2 -2
  2. {adloop-0.45.0 → adloop-0.46.0}/README.md +1 -1
  3. {adloop-0.45.0 → adloop-0.46.0}/pyproject.toml +1 -1
  4. {adloop-0.45.0 → adloop-0.46.0}/pyproject.toml.orig +1 -1
  5. {adloop-0.45.0 → adloop-0.46.0}/src/adloop/merchant/read.py +226 -42
  6. {adloop-0.45.0 → adloop-0.46.0}/src/adloop/server.py +6 -4
  7. {adloop-0.45.0 → adloop-0.46.0}/src/adloop/__init__.py +0 -0
  8. {adloop-0.45.0 → adloop-0.46.0}/src/adloop/__main__.py +0 -0
  9. {adloop-0.45.0 → adloop-0.46.0}/src/adloop/_mcp_patches.py +0 -0
  10. {adloop-0.45.0 → adloop-0.46.0}/src/adloop/ads/__init__.py +0 -0
  11. {adloop-0.45.0 → adloop-0.46.0}/src/adloop/ads/ai_max.py +0 -0
  12. {adloop-0.45.0 → adloop-0.46.0}/src/adloop/ads/assets.py +0 -0
  13. {adloop-0.45.0 → adloop-0.46.0}/src/adloop/ads/audiences.py +0 -0
  14. {adloop-0.45.0 → adloop-0.46.0}/src/adloop/ads/brands.py +0 -0
  15. {adloop-0.45.0 → adloop-0.46.0}/src/adloop/ads/campaign_settings.py +0 -0
  16. {adloop-0.45.0 → adloop-0.46.0}/src/adloop/ads/client.py +0 -0
  17. {adloop-0.45.0 → adloop-0.46.0}/src/adloop/ads/conversion_actions.py +0 -0
  18. {adloop-0.45.0 → adloop-0.46.0}/src/adloop/ads/currency.py +0 -0
  19. {adloop-0.45.0 → adloop-0.46.0}/src/adloop/ads/custom_conversion_goals.py +0 -0
  20. {adloop-0.45.0 → adloop-0.46.0}/src/adloop/ads/enums.py +0 -0
  21. {adloop-0.45.0 → adloop-0.46.0}/src/adloop/ads/forecast.py +0 -0
  22. {adloop-0.45.0 → adloop-0.46.0}/src/adloop/ads/gaql.py +0 -0
  23. {adloop-0.45.0 → adloop-0.46.0}/src/adloop/ads/image_fetch.py +0 -0
  24. {adloop-0.45.0 → adloop-0.46.0}/src/adloop/ads/pmax.py +0 -0
  25. {adloop-0.45.0 → adloop-0.46.0}/src/adloop/ads/read.py +0 -0
  26. {adloop-0.45.0 → adloop-0.46.0}/src/adloop/ads/recommendation_subscriptions.py +0 -0
  27. {adloop-0.45.0 → adloop-0.46.0}/src/adloop/ads/recommendations.py +0 -0
  28. {adloop-0.45.0 → adloop-0.46.0}/src/adloop/ads/shopping.py +0 -0
  29. {adloop-0.45.0 → adloop-0.46.0}/src/adloop/ads/snippet_headers.py +0 -0
  30. {adloop-0.45.0 → adloop-0.46.0}/src/adloop/ads/validate_only.py +0 -0
  31. {adloop-0.45.0 → adloop-0.46.0}/src/adloop/ads/write.py +0 -0
  32. {adloop-0.45.0 → adloop-0.46.0}/src/adloop/auth.py +0 -0
  33. {adloop-0.45.0 → adloop-0.46.0}/src/adloop/cli.py +0 -0
  34. {adloop-0.45.0 → adloop-0.46.0}/src/adloop/config.py +0 -0
  35. {adloop-0.45.0 → adloop-0.46.0}/src/adloop/conversion_setup.py +0 -0
  36. {adloop-0.45.0 → adloop-0.46.0}/src/adloop/crossref.py +0 -0
  37. {adloop-0.45.0 → adloop-0.46.0}/src/adloop/diagnostics.py +0 -0
  38. {adloop-0.45.0 → adloop-0.46.0}/src/adloop/ga4/__init__.py +0 -0
  39. {adloop-0.45.0 → adloop-0.46.0}/src/adloop/ga4/admin.py +0 -0
  40. {adloop-0.45.0 → adloop-0.46.0}/src/adloop/ga4/annotations.py +0 -0
  41. {adloop-0.45.0 → adloop-0.46.0}/src/adloop/ga4/attribution.py +0 -0
  42. {adloop-0.45.0 → adloop-0.46.0}/src/adloop/ga4/audiences.py +0 -0
  43. {adloop-0.45.0 → adloop-0.46.0}/src/adloop/ga4/audit.py +0 -0
  44. {adloop-0.45.0 → adloop-0.46.0}/src/adloop/ga4/client.py +0 -0
  45. {adloop-0.45.0 → adloop-0.46.0}/src/adloop/ga4/definitions.py +0 -0
  46. {adloop-0.45.0 → adloop-0.46.0}/src/adloop/ga4/funnels.py +0 -0
  47. {adloop-0.45.0 → adloop-0.46.0}/src/adloop/ga4/reports.py +0 -0
  48. {adloop-0.45.0 → adloop-0.46.0}/src/adloop/ga4/retention.py +0 -0
  49. {adloop-0.45.0 → adloop-0.46.0}/src/adloop/ga4/suggest.py +0 -0
  50. {adloop-0.45.0 → adloop-0.46.0}/src/adloop/ga4/tracking.py +0 -0
  51. {adloop-0.45.0 → adloop-0.46.0}/src/adloop/ga4/write.py +0 -0
  52. {adloop-0.45.0 → adloop-0.46.0}/src/adloop/gsc/__init__.py +0 -0
  53. {adloop-0.45.0 → adloop-0.46.0}/src/adloop/gsc/client.py +0 -0
  54. {adloop-0.45.0 → adloop-0.46.0}/src/adloop/gsc/reports.py +0 -0
  55. {adloop-0.45.0 → adloop-0.46.0}/src/adloop/gtm/__init__.py +0 -0
  56. {adloop-0.45.0 → adloop-0.46.0}/src/adloop/gtm/client.py +0 -0
  57. {adloop-0.45.0 → adloop-0.46.0}/src/adloop/gtm/entities.py +0 -0
  58. {adloop-0.45.0 → adloop-0.46.0}/src/adloop/gtm/read.py +0 -0
  59. {adloop-0.45.0 → adloop-0.46.0}/src/adloop/gtm/write.py +0 -0
  60. {adloop-0.45.0 → adloop-0.46.0}/src/adloop/merchant/__init__.py +0 -0
  61. {adloop-0.45.0 → adloop-0.46.0}/src/adloop/merchant/client.py +0 -0
  62. {adloop-0.45.0 → adloop-0.46.0}/src/adloop/microsoft_ads/__init__.py +0 -0
  63. {adloop-0.45.0 → adloop-0.46.0}/src/adloop/microsoft_ads/auth.py +0 -0
  64. {adloop-0.45.0 → adloop-0.46.0}/src/adloop/microsoft_ads/client.py +0 -0
  65. {adloop-0.45.0 → adloop-0.46.0}/src/adloop/microsoft_ads/manage/__init__.py +0 -0
  66. {adloop-0.45.0 → adloop-0.46.0}/src/adloop/microsoft_ads/manage/_common.py +0 -0
  67. {adloop-0.45.0 → adloop-0.46.0}/src/adloop/microsoft_ads/manage/ad_groups.py +0 -0
  68. {adloop-0.45.0 → adloop-0.46.0}/src/adloop/microsoft_ads/manage/ads.py +0 -0
  69. {adloop-0.45.0 → adloop-0.46.0}/src/adloop/microsoft_ads/manage/campaigns.py +0 -0
  70. {adloop-0.45.0 → adloop-0.46.0}/src/adloop/microsoft_ads/manage/extensions.py +0 -0
  71. {adloop-0.45.0 → adloop-0.46.0}/src/adloop/microsoft_ads/manage/negatives.py +0 -0
  72. {adloop-0.45.0 → adloop-0.46.0}/src/adloop/microsoft_ads/manage/removal.py +0 -0
  73. {adloop-0.45.0 → adloop-0.46.0}/src/adloop/microsoft_ads/manage/targeting.py +0 -0
  74. {adloop-0.45.0 → adloop-0.46.0}/src/adloop/microsoft_ads/read.py +0 -0
  75. {adloop-0.45.0 → adloop-0.46.0}/src/adloop/microsoft_ads/write.py +0 -0
  76. {adloop-0.45.0 → adloop-0.46.0}/src/adloop/oauth_redirect.py +0 -0
  77. {adloop-0.45.0 → adloop-0.46.0}/src/adloop/pagespeed.py +0 -0
  78. {adloop-0.45.0 → adloop-0.46.0}/src/adloop/reddit/__init__.py +0 -0
  79. {adloop-0.45.0 → adloop-0.46.0}/src/adloop/reddit/auth.py +0 -0
  80. {adloop-0.45.0 → adloop-0.46.0}/src/adloop/reddit/client.py +0 -0
  81. {adloop-0.45.0 → adloop-0.46.0}/src/adloop/reddit/read.py +0 -0
  82. {adloop-0.45.0 → adloop-0.46.0}/src/adloop/reddit/schedule.py +0 -0
  83. {adloop-0.45.0 → adloop-0.46.0}/src/adloop/reddit/write.py +0 -0
  84. {adloop-0.45.0 → adloop-0.46.0}/src/adloop/rules/__init__.py +0 -0
  85. {adloop-0.45.0 → adloop-0.46.0}/src/adloop/rules/adloop.md +0 -0
  86. {adloop-0.45.0 → adloop-0.46.0}/src/adloop/rules/commands/analyze-performance.md +0 -0
  87. {adloop-0.45.0 → adloop-0.46.0}/src/adloop/rules/commands/budget-plan.md +0 -0
  88. {adloop-0.45.0 → adloop-0.46.0}/src/adloop/rules/commands/create-ad.md +0 -0
  89. {adloop-0.45.0 → adloop-0.46.0}/src/adloop/rules/commands/create-campaign.md +0 -0
  90. {adloop-0.45.0 → adloop-0.46.0}/src/adloop/rules/commands/diagnose-tracking.md +0 -0
  91. {adloop-0.45.0 → adloop-0.46.0}/src/adloop/rules/commands/optimize-campaign.md +0 -0
  92. {adloop-0.45.0 → adloop-0.46.0}/src/adloop/rules_install.py +0 -0
  93. {adloop-0.45.0 → adloop-0.46.0}/src/adloop/runtime.py +0 -0
  94. {adloop-0.45.0 → adloop-0.46.0}/src/adloop/safety/__init__.py +0 -0
  95. {adloop-0.45.0 → adloop-0.46.0}/src/adloop/safety/approval.py +0 -0
  96. {adloop-0.45.0 → adloop-0.46.0}/src/adloop/safety/approval_card.html +0 -0
  97. {adloop-0.45.0 → adloop-0.46.0}/src/adloop/safety/audit.py +0 -0
  98. {adloop-0.45.0 → adloop-0.46.0}/src/adloop/safety/guards.py +0 -0
  99. {adloop-0.45.0 → adloop-0.46.0}/src/adloop/safety/outcomes.py +0 -0
  100. {adloop-0.45.0 → adloop-0.46.0}/src/adloop/safety/preview.py +0 -0
  101. {adloop-0.45.0 → adloop-0.46.0}/src/adloop/tracking.py +0 -0
  102. {adloop-0.45.0 → adloop-0.46.0}/src/adloop/upload_card.html +0 -0
  103. {adloop-0.45.0 → adloop-0.46.0}/src/adloop/uploads.py +0 -0
  104. {adloop-0.45.0 → adloop-0.46.0}/src/adloop/views/__init__.py +0 -0
  105. {adloop-0.45.0 → adloop-0.46.0}/src/adloop/views/performance.py +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.3
2
2
  Name: adloop
3
- Version: 0.45.0
3
+ Version: 0.46.0
4
4
  Summary: Google Ads MCP server for Claude, ChatGPT and Cursor, with Reddit Ads, GA4 and Tag Manager. Every change is previewed first.
5
5
  Keywords: mcp,mcp-server,google-ads,google-ads-mcp,reddit-ads,google-analytics,ga4,claude,chatgpt,cursor,marketing
6
6
  Author: Daniel Klose
@@ -248,7 +248,7 @@ The Google account also needs **Edit** permission on the container (and **Publis
248
248
  | Tool | What It Does |
249
249
  |------|-------------|
250
250
  | `list_merchant_accounts` | Discover accessible Merchant Center accounts |
251
- | `get_merchant_feed_health` | Feed health — approved/pending/disapproved counts per reporting context, top product issues with docs, account-level issues. Disapprovals silently starve Shopping/PMax. |
251
+ | `get_merchant_feed_health` | Feed health — approved/pending/disapproved counts per reporting context, paid surfaces kept apart from free listings, top product issues with docs, account-level issues. Disapprovals silently starve Shopping/PMax. |
252
252
  | `get_merchant_product_issues` | The products behind the feed-health counts: which are disapproved, limited or pending, and each issue with its attribute and affected countries |
253
253
 
254
254
  > **Setup for Merchant Center tools** — Enable the **Merchant API** in your GCP project (the Content API for Shopping is deprecated). The Merchant API has no read-only scope; AdLoop uses it strictly read-only. Upgrading OAuth users re-authorize once.
@@ -216,7 +216,7 @@ The Google account also needs **Edit** permission on the container (and **Publis
216
216
  | Tool | What It Does |
217
217
  |------|-------------|
218
218
  | `list_merchant_accounts` | Discover accessible Merchant Center accounts |
219
- | `get_merchant_feed_health` | Feed health — approved/pending/disapproved counts per reporting context, top product issues with docs, account-level issues. Disapprovals silently starve Shopping/PMax. |
219
+ | `get_merchant_feed_health` | Feed health — approved/pending/disapproved counts per reporting context, paid surfaces kept apart from free listings, top product issues with docs, account-level issues. Disapprovals silently starve Shopping/PMax. |
220
220
  | `get_merchant_product_issues` | The products behind the feed-health counts: which are disapproved, limited or pending, and each issue with its attribute and affected countries |
221
221
 
222
222
  > **Setup for Merchant Center tools** — Enable the **Merchant API** in your GCP project (the Content API for Shopping is deprecated). The Merchant API has no read-only scope; AdLoop uses it strictly read-only. Upgrading OAuth users re-authorize once.
@@ -1,6 +1,6 @@
1
1
  [project]
2
2
  name = "adloop"
3
- version = "0.45.0"
3
+ version = "0.46.0"
4
4
  description = "Google Ads MCP server for Claude, ChatGPT and Cursor, with Reddit Ads, GA4 and Tag Manager. Every change is previewed first."
5
5
  readme = "README.md"
6
6
  requires-python = ">=3.11"
@@ -1,6 +1,6 @@
1
1
  [project]
2
2
  name = "adloop"
3
- version = "0.45.0"
3
+ version = "0.46.0"
4
4
  description = "Google Ads MCP server for Claude, ChatGPT and Cursor, with Reddit Ads, GA4 and Tag Manager. Every change is previewed first."
5
5
  readme = "README.md"
6
6
  authors = [
@@ -106,6 +106,130 @@ def list_merchant_accounts(config: AdLoopConfig) -> dict:
106
106
  return {"accounts": accounts, "total": len(accounts), "insights": insights}
107
107
 
108
108
 
109
+ # ---------------------------------------------------------------------------
110
+ # Feed health (issueresolution/v1 aggregateProductStatuses)
111
+ # ---------------------------------------------------------------------------
112
+
113
+ # Stats counters of an AggregateProductStatus row, as output key -> API
114
+ # field. The API calls serving products "active"; the output keeps
115
+ # "approved". An expiring product is still active, so "expiring" is
116
+ # reported but never added into a total or a percentage.
117
+ _STAT_FIELDS = (
118
+ ("approved", "activeCount"),
119
+ ("pending", "pendingCount"),
120
+ ("disapproved", "disapprovedCount"),
121
+ ("expiring", "expiringCount"),
122
+ )
123
+ _LISTED_FIELDS = ("approved", "pending", "disapproved")
124
+
125
+ # Reporting contexts in which products are advertised for money. A
126
+ # disapproval there costs Shopping / Performance Max reach; one in a free
127
+ # context (FREE_LISTINGS, FREE_LOCAL_LISTINGS, ...) costs organic exposure
128
+ # only. Everything else (YouTube, reviews, cloud retail) is "other".
129
+ _PAID_CONTEXTS = frozenset({
130
+ "SHOPPING_ADS",
131
+ "DISPLAY_ADS",
132
+ "VIDEO_ADS",
133
+ "DEMAND_GEN_ADS",
134
+ "DEMAND_GEN_ADS_DISCOVER_SURFACE",
135
+ "DISCOVERY_ADS",
136
+ "LOCAL_INVENTORY_ADS",
137
+ "VEHICLE_INVENTORY_ADS",
138
+ "RENTAL_ADS",
139
+ })
140
+ _UNSPECIFIED_CONTEXT = "REPORTING_CONTEXT_ENUM_UNSPECIFIED"
141
+ _SURFACE_ORDER = {"paid": 0, "free": 1, "other": 2, "unspecified": 3}
142
+ _SEVERITY_ORDER = {"DISAPPROVED": 0, "DEMOTED": 1, "NOT_IMPACTED": 2}
143
+ _SERVING_IMPACT = ("DISAPPROVED", "DEMOTED")
144
+
145
+ # The API returns 25 rows per page by default and at most 250. An account
146
+ # with a few target countries already spans several reporting contexts per
147
+ # country, so the first page alone undercounts.
148
+ _AGGREGATE_PAGE_SIZE = 250
149
+
150
+ _FEED_HEALTH_NOTE = (
151
+ "A product is counted once per reporting context and target country, "
152
+ "so never add reporting contexts together: read the counts per "
153
+ "context. affected_products on an item issue is its largest single "
154
+ "context, a floor on the products it hits. Product-status data lags "
155
+ "feed changes by about 30 minutes."
156
+ )
157
+
158
+
159
+ def _surface(context: str) -> str:
160
+ """paid / free / other / unspecified for a reporting context."""
161
+ if context in _PAID_CONTEXTS:
162
+ return "paid"
163
+ if context.startswith("FREE_"):
164
+ return "free"
165
+ if context == _UNSPECIFIED_CONTEXT:
166
+ return "unspecified"
167
+ return "other"
168
+
169
+
170
+ def _row_context(row: dict) -> str:
171
+ """Reporting context of an aggregate row.
172
+
173
+ Google leaves reportingContext out on some rows. The resource name of
174
+ such a row still ends in ``{context}~{country}``, so fall back to it;
175
+ a row that names no context either is unspecified.
176
+ """
177
+ context = row.get("reportingContext") or ""
178
+ if context:
179
+ return context
180
+ tail = (row.get("name") or "").rsplit("/", 1)[-1]
181
+ candidate = tail.split("~", 1)[0] if "~" in tail else ""
182
+ if candidate and candidate.replace("_", "").isalpha() and candidate.isupper():
183
+ return candidate
184
+ return _UNSPECIFIED_CONTEXT
185
+
186
+
187
+ def _fetch_aggregate_statuses(config: AdLoopConfig, account_id: str) -> list[dict]:
188
+ """Every aggregateProductStatuses row, across all pages.
189
+
190
+ Google can return the same row on more than one page; a row is one
191
+ (reporting context, country) pair with a unique resource name, so rows
192
+ whose name was already seen are dropped instead of being counted twice.
193
+ """
194
+ from adloop.merchant.client import API_ISSUE_RESOLUTION, merchant_get
195
+
196
+ rows: list[dict] = []
197
+ seen_names: set[str] = set()
198
+ seen_tokens: set[str] = set()
199
+ page_token = ""
200
+ while True:
201
+ params: dict = {"pageSize": _AGGREGATE_PAGE_SIZE}
202
+ if page_token:
203
+ params["pageToken"] = page_token
204
+ # Product-status aggregation is served by the issueresolution
205
+ # sub-API, not accounts — the wrong prefix 404s.
206
+ payload = merchant_get(
207
+ config,
208
+ f"accounts/{account_id}/aggregateProductStatuses",
209
+ params,
210
+ api=API_ISSUE_RESOLUTION,
211
+ )
212
+ for row in payload.get("aggregateProductStatuses") or []:
213
+ name = row.get("name") or ""
214
+ if name:
215
+ if name in seen_names:
216
+ continue
217
+ seen_names.add(name)
218
+ rows.append(row)
219
+ page_token = payload.get("nextPageToken") or ""
220
+ # A token handed out twice would loop forever.
221
+ if not page_token or page_token in seen_tokens:
222
+ break
223
+ seen_tokens.add(page_token)
224
+ return rows
225
+
226
+
227
+ def _disapproval_share(context: str, counts: dict) -> str:
228
+ listed = sum(counts[key] for key in _LISTED_FIELDS)
229
+ pct = round(counts["disapproved"] / listed * 100, 1) if listed else 0.0
230
+ return f"{context} {counts['disapproved']:,} of {listed:,} ({pct}%)"
231
+
232
+
109
233
  def get_merchant_feed_health(
110
234
  config: AdLoopConfig,
111
235
  *,
@@ -113,12 +237,14 @@ def get_merchant_feed_health(
113
237
  ) -> dict:
114
238
  """Feed health for a Merchant Center account — disapprovals + issues.
115
239
 
116
- Combines aggregateProductStatuses (per reporting-context counts of
117
- approved/pending/disapproved products plus the product issues behind
118
- them) with account-level issues that can suspend the whole account.
240
+ Combines aggregateProductStatuses (per reporting context and country:
241
+ approved/pending/disapproved/expiring products plus the item issues
242
+ behind them) with account-level issues that can suspend the whole
243
+ account. Paid reporting contexts (Shopping, display, video, ...) are
244
+ reported apart from free listings: only the former starve Shopping and
245
+ Performance Max campaigns.
119
246
  """
120
247
  from adloop.merchant.client import (
121
- API_ISSUE_RESOLUTION,
122
248
  MerchantNotRegistered,
123
249
  merchant_get,
124
250
  register_gcp,
@@ -128,15 +254,9 @@ def get_merchant_feed_health(
128
254
  if error:
129
255
  return error
130
256
 
131
- def _fetch() -> tuple[dict, dict]:
257
+ def _fetch() -> tuple[list[dict], dict]:
132
258
  return (
133
- # Product-status aggregation is served by the issueresolution
134
- # sub-API, not accounts — the wrong prefix 404s.
135
- merchant_get(
136
- config,
137
- f"accounts/{account_id}/aggregateProductStatuses",
138
- api=API_ISSUE_RESOLUTION,
139
- ),
259
+ _fetch_aggregate_statuses(config, account_id),
140
260
  merchant_get(config, f"accounts/{account_id}/issues"),
141
261
  )
142
262
 
@@ -170,42 +290,103 @@ def get_merchant_feed_health(
170
290
  for issue in issues_payload.get("accountIssues", [])
171
291
  ]
172
292
 
173
- contexts = []
174
- top_item_issues: list[dict] = []
175
- totals = {"approved": 0, "pending": 0, "disapproved": 0}
176
- for status in statuses.get("aggregateProductStatuses", []):
177
- stats = status.get("statistics", {}) or {}
293
+ contexts: list[dict] = []
294
+ totals: dict[str, dict] = {}
295
+ by_code: dict[str, dict] = {}
296
+ for status in statuses:
297
+ context = _row_context(status)
298
+ surface = _surface(context)
299
+ stats = status.get("stats") or {}
178
300
  entry = {
179
- "reporting_context": status.get("reportingContext", ""),
180
- "country": status.get("countryCode", ""),
181
- "approved": int(stats.get("approvedCount", 0) or 0),
182
- "pending": int(stats.get("pendingCount", 0) or 0),
183
- "disapproved": int(stats.get("disapprovedCount", 0) or 0),
301
+ "reporting_context": context,
302
+ "surface": surface,
303
+ "country": status.get("country", ""),
184
304
  }
305
+ for key, field in _STAT_FIELDS:
306
+ entry[key] = int(stats.get(field, 0) or 0)
185
307
  contexts.append(entry)
186
- totals["approved"] += entry["approved"]
187
- totals["pending"] += entry["pending"]
188
- totals["disapproved"] += entry["disapproved"]
189
- for issue in status.get("issues", []):
190
- top_item_issues.append({
191
- "reporting_context": status.get("reportingContext", ""),
192
- "issue": issue.get("issueType", "")
193
- or issue.get("title", ""),
194
- "severity": issue.get("severity", ""),
195
- "affected_products": int(issue.get("numProducts", 0) or 0),
308
+
309
+ total = totals.setdefault(
310
+ context, {"surface": surface, **{key: 0 for key, _ in _STAT_FIELDS}}
311
+ )
312
+ for key, _ in _STAT_FIELDS:
313
+ total[key] += entry[key]
314
+
315
+ # The same issue code repeats for every context and country it
316
+ # touches; one entry per code, sized by its largest single row,
317
+ # because summing rows would count a product once per context.
318
+ for issue in status.get("itemLevelIssues") or []:
319
+ code = issue.get("code", "")
320
+ severity = issue.get("severity", "")
321
+ record = by_code.setdefault(code, {
322
+ "issue": code,
323
+ "description": issue.get("description", ""),
324
+ "severity": severity,
325
+ "resolution": issue.get("resolution", ""),
326
+ "attribute": issue.get("attribute", ""),
327
+ "affected_products": 0,
328
+ "hits_paid_surface": False,
329
+ "reporting_contexts": set(),
196
330
  "documentation": issue.get("documentationUri", ""),
197
331
  })
198
- top_item_issues.sort(key=lambda i: i["affected_products"], reverse=True)
332
+ record["affected_products"] = max(
333
+ record["affected_products"], int(issue.get("productCount", 0) or 0)
334
+ )
335
+ if _SEVERITY_ORDER.get(severity, 9) < _SEVERITY_ORDER.get(record["severity"], 9):
336
+ record["severity"] = severity
337
+ record["reporting_contexts"].add(context)
338
+ if surface == "paid" and severity in _SERVING_IMPACT:
339
+ record["hits_paid_surface"] = True
340
+
341
+ contexts.sort(key=lambda c: (
342
+ _SURFACE_ORDER[c["surface"]], c["reporting_context"], c["country"],
343
+ ))
344
+ totals = dict(sorted(
345
+ totals.items(), key=lambda kv: (_SURFACE_ORDER[kv[1]["surface"]], kv[0])
346
+ ))
347
+ top_item_issues = sorted(
348
+ by_code.values(),
349
+ key=lambda i: (
350
+ _SEVERITY_ORDER.get(i["severity"], 9),
351
+ not i["hits_paid_surface"],
352
+ -i["affected_products"],
353
+ ),
354
+ )
355
+ for issue in top_item_issues:
356
+ issue["reporting_contexts"] = sorted(issue["reporting_contexts"])
199
357
 
200
358
  insights = []
201
- serving_total = sum(totals.values())
202
- if serving_total and totals["disapproved"] > 0:
203
- pct = round(totals["disapproved"] / serving_total * 100, 1)
359
+ paid = {c: t for c, t in totals.items() if t["surface"] == "paid"}
360
+ free = {c: t for c, t in totals.items() if t["surface"] == "free"}
361
+ paid_hit = [c for c, t in paid.items() if t["disapproved"]]
362
+ free_hit = [c for c, t in free.items() if t["disapproved"]]
363
+ if paid_hit:
364
+ insights.append(
365
+ "Paid surfaces: "
366
+ + "; ".join(_disapproval_share(c, paid[c]) for c in paid_hit)
367
+ + " products DISAPPROVED and not serving — this directly starves "
368
+ "Shopping and Performance Max campaigns. Fix the top issues "
369
+ "below before touching bids or budgets."
370
+ )
371
+ elif paid:
372
+ insights.append(
373
+ "Paid surfaces (" + ", ".join(paid) + ") have no disapproved "
374
+ "products — disapprovals are not what holds Shopping or "
375
+ "Performance Max back."
376
+ )
377
+ if free_hit:
378
+ insights.append(
379
+ "Free listings: "
380
+ + "; ".join(_disapproval_share(c, free[c]) for c in free_hit)
381
+ + " products disapproved — this costs organic exposure only, "
382
+ "not Shopping or Performance Max reach."
383
+ )
384
+ unspecified = [c for c in contexts if c["surface"] == "unspecified"]
385
+ if unspecified:
204
386
  insights.append(
205
- f"{totals['disapproved']:,} product(s) ({pct}%) are DISAPPROVED "
206
- f"and not serving — this directly starves Shopping and "
207
- f"Performance Max campaigns. Fix the top issues below before "
208
- f"touching bids or budgets."
387
+ f"{len(unspecified)} row(s) came back without a reporting "
388
+ "context; they are listed in by_reporting_context but left out "
389
+ "of the paid / free split."
209
390
  )
210
391
  critical = [
211
392
  i for i in account_issues if i["severity"] in ("CRITICAL", "ERROR")
@@ -216,7 +397,9 @@ def get_merchant_feed_health(
216
397
  f"severity — CRITICAL means offers stop serving entirely: "
217
398
  + "; ".join(i["title"] for i in critical[:3])
218
399
  )
219
- if not serving_total:
400
+ any_products = any(c[key] for c in contexts for key in _LISTED_FIELDS)
401
+ any_disapproved = any(c["disapproved"] for c in contexts)
402
+ if not any_products:
220
403
  # Zero products is not a healthy feed: Shopping and Performance Max
221
404
  # have nothing to advertise.
222
405
  insights.append(
@@ -226,7 +409,7 @@ def get_merchant_feed_health(
226
409
  "products were added in the last ~30 minutes (status lags), or "
227
410
  "the products live in another Merchant Center account."
228
411
  )
229
- elif not account_issues and not totals["disapproved"]:
412
+ elif not account_issues and not any_disapproved:
230
413
  insights.append("No disapprovals or account issues — feed is healthy.")
231
414
 
232
415
  return {
@@ -236,6 +419,7 @@ def get_merchant_feed_health(
236
419
  "account_issues": account_issues,
237
420
  "top_item_issues": top_item_issues[:15],
238
421
  "insights": insights,
422
+ "note": _FEED_HEALTH_NOTE,
239
423
  }
240
424
 
241
425
 
@@ -1707,10 +1707,12 @@ def get_merchant_feed_health(account_id: str = "") -> dict:
1707
1707
  """Merchant Center feed health — disapproved products + account issues.
1708
1708
 
1709
1709
  Disapproved feed items silently starve Shopping and Performance Max
1710
- campaigns; this reports approved/pending/disapproved counts per
1711
- reporting context (Shopping ads, free listings, ...), the top product
1712
- issues by affected products (with documentation links), and
1713
- account-level issues — CRITICAL ones stop offers serving entirely.
1710
+ campaigns; this reports approved/pending/disapproved/expiring counts
1711
+ per reporting context and country, each context marked paid (Shopping,
1712
+ display, video ads, ...) or free (free listings), the top item issues
1713
+ by affected products (with documentation links), and account-level
1714
+ issues — CRITICAL ones stop offers serving entirely. A product counts
1715
+ once per reporting context, so contexts are never added together.
1714
1716
  Product-status data lags reality by ~30 minutes.
1715
1717
 
1716
1718
  Args:
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes