adloop 0.41.3__tar.gz → 0.42.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 (104) hide show
  1. {adloop-0.41.3 → adloop-0.42.0}/PKG-INFO +3 -3
  2. {adloop-0.41.3 → adloop-0.42.0}/README.md +2 -2
  3. {adloop-0.41.3 → adloop-0.42.0}/pyproject.toml +1 -1
  4. {adloop-0.41.3 → adloop-0.42.0}/pyproject.toml.orig +1 -1
  5. {adloop-0.41.3 → adloop-0.42.0}/src/adloop/ads/assets.py +9 -5
  6. {adloop-0.41.3 → adloop-0.42.0}/src/adloop/ads/conversion_actions.py +113 -14
  7. {adloop-0.41.3 → adloop-0.42.0}/src/adloop/ads/currency.py +17 -10
  8. {adloop-0.41.3 → adloop-0.42.0}/src/adloop/ads/image_fetch.py +3 -3
  9. {adloop-0.41.3 → adloop-0.42.0}/src/adloop/ads/write.py +322 -14
  10. {adloop-0.41.3 → adloop-0.42.0}/src/adloop/ga4/suggest.py +5 -1
  11. {adloop-0.41.3 → adloop-0.42.0}/src/adloop/reddit/read.py +8 -3
  12. {adloop-0.41.3 → adloop-0.42.0}/src/adloop/rules/adloop.md +5 -4
  13. {adloop-0.41.3 → adloop-0.42.0}/src/adloop/server.py +54 -13
  14. {adloop-0.41.3 → adloop-0.42.0}/src/adloop/__init__.py +0 -0
  15. {adloop-0.41.3 → adloop-0.42.0}/src/adloop/__main__.py +0 -0
  16. {adloop-0.41.3 → adloop-0.42.0}/src/adloop/_mcp_patches.py +0 -0
  17. {adloop-0.41.3 → adloop-0.42.0}/src/adloop/ads/__init__.py +0 -0
  18. {adloop-0.41.3 → adloop-0.42.0}/src/adloop/ads/ai_max.py +0 -0
  19. {adloop-0.41.3 → adloop-0.42.0}/src/adloop/ads/audiences.py +0 -0
  20. {adloop-0.41.3 → adloop-0.42.0}/src/adloop/ads/brands.py +0 -0
  21. {adloop-0.41.3 → adloop-0.42.0}/src/adloop/ads/campaign_settings.py +0 -0
  22. {adloop-0.41.3 → adloop-0.42.0}/src/adloop/ads/client.py +0 -0
  23. {adloop-0.41.3 → adloop-0.42.0}/src/adloop/ads/custom_conversion_goals.py +0 -0
  24. {adloop-0.41.3 → adloop-0.42.0}/src/adloop/ads/enums.py +0 -0
  25. {adloop-0.41.3 → adloop-0.42.0}/src/adloop/ads/forecast.py +0 -0
  26. {adloop-0.41.3 → adloop-0.42.0}/src/adloop/ads/gaql.py +0 -0
  27. {adloop-0.41.3 → adloop-0.42.0}/src/adloop/ads/pmax.py +0 -0
  28. {adloop-0.41.3 → adloop-0.42.0}/src/adloop/ads/read.py +0 -0
  29. {adloop-0.41.3 → adloop-0.42.0}/src/adloop/ads/recommendation_subscriptions.py +0 -0
  30. {adloop-0.41.3 → adloop-0.42.0}/src/adloop/ads/recommendations.py +0 -0
  31. {adloop-0.41.3 → adloop-0.42.0}/src/adloop/ads/shopping.py +0 -0
  32. {adloop-0.41.3 → adloop-0.42.0}/src/adloop/ads/snippet_headers.py +0 -0
  33. {adloop-0.41.3 → adloop-0.42.0}/src/adloop/ads/validate_only.py +0 -0
  34. {adloop-0.41.3 → adloop-0.42.0}/src/adloop/auth.py +0 -0
  35. {adloop-0.41.3 → adloop-0.42.0}/src/adloop/cli.py +0 -0
  36. {adloop-0.41.3 → adloop-0.42.0}/src/adloop/config.py +0 -0
  37. {adloop-0.41.3 → adloop-0.42.0}/src/adloop/conversion_setup.py +0 -0
  38. {adloop-0.41.3 → adloop-0.42.0}/src/adloop/crossref.py +0 -0
  39. {adloop-0.41.3 → adloop-0.42.0}/src/adloop/diagnostics.py +0 -0
  40. {adloop-0.41.3 → adloop-0.42.0}/src/adloop/ga4/__init__.py +0 -0
  41. {adloop-0.41.3 → adloop-0.42.0}/src/adloop/ga4/admin.py +0 -0
  42. {adloop-0.41.3 → adloop-0.42.0}/src/adloop/ga4/annotations.py +0 -0
  43. {adloop-0.41.3 → adloop-0.42.0}/src/adloop/ga4/attribution.py +0 -0
  44. {adloop-0.41.3 → adloop-0.42.0}/src/adloop/ga4/audit.py +0 -0
  45. {adloop-0.41.3 → adloop-0.42.0}/src/adloop/ga4/client.py +0 -0
  46. {adloop-0.41.3 → adloop-0.42.0}/src/adloop/ga4/definitions.py +0 -0
  47. {adloop-0.41.3 → adloop-0.42.0}/src/adloop/ga4/funnels.py +0 -0
  48. {adloop-0.41.3 → adloop-0.42.0}/src/adloop/ga4/reports.py +0 -0
  49. {adloop-0.41.3 → adloop-0.42.0}/src/adloop/ga4/retention.py +0 -0
  50. {adloop-0.41.3 → adloop-0.42.0}/src/adloop/ga4/tracking.py +0 -0
  51. {adloop-0.41.3 → adloop-0.42.0}/src/adloop/ga4/write.py +0 -0
  52. {adloop-0.41.3 → adloop-0.42.0}/src/adloop/gsc/__init__.py +0 -0
  53. {adloop-0.41.3 → adloop-0.42.0}/src/adloop/gsc/client.py +0 -0
  54. {adloop-0.41.3 → adloop-0.42.0}/src/adloop/gsc/reports.py +0 -0
  55. {adloop-0.41.3 → adloop-0.42.0}/src/adloop/gtm/__init__.py +0 -0
  56. {adloop-0.41.3 → adloop-0.42.0}/src/adloop/gtm/client.py +0 -0
  57. {adloop-0.41.3 → adloop-0.42.0}/src/adloop/gtm/entities.py +0 -0
  58. {adloop-0.41.3 → adloop-0.42.0}/src/adloop/gtm/read.py +0 -0
  59. {adloop-0.41.3 → adloop-0.42.0}/src/adloop/gtm/write.py +0 -0
  60. {adloop-0.41.3 → adloop-0.42.0}/src/adloop/merchant/__init__.py +0 -0
  61. {adloop-0.41.3 → adloop-0.42.0}/src/adloop/merchant/client.py +0 -0
  62. {adloop-0.41.3 → adloop-0.42.0}/src/adloop/merchant/read.py +0 -0
  63. {adloop-0.41.3 → adloop-0.42.0}/src/adloop/microsoft_ads/__init__.py +0 -0
  64. {adloop-0.41.3 → adloop-0.42.0}/src/adloop/microsoft_ads/auth.py +0 -0
  65. {adloop-0.41.3 → adloop-0.42.0}/src/adloop/microsoft_ads/client.py +0 -0
  66. {adloop-0.41.3 → adloop-0.42.0}/src/adloop/microsoft_ads/manage/__init__.py +0 -0
  67. {adloop-0.41.3 → adloop-0.42.0}/src/adloop/microsoft_ads/manage/_common.py +0 -0
  68. {adloop-0.41.3 → adloop-0.42.0}/src/adloop/microsoft_ads/manage/ad_groups.py +0 -0
  69. {adloop-0.41.3 → adloop-0.42.0}/src/adloop/microsoft_ads/manage/ads.py +0 -0
  70. {adloop-0.41.3 → adloop-0.42.0}/src/adloop/microsoft_ads/manage/campaigns.py +0 -0
  71. {adloop-0.41.3 → adloop-0.42.0}/src/adloop/microsoft_ads/manage/extensions.py +0 -0
  72. {adloop-0.41.3 → adloop-0.42.0}/src/adloop/microsoft_ads/manage/negatives.py +0 -0
  73. {adloop-0.41.3 → adloop-0.42.0}/src/adloop/microsoft_ads/manage/removal.py +0 -0
  74. {adloop-0.41.3 → adloop-0.42.0}/src/adloop/microsoft_ads/manage/targeting.py +0 -0
  75. {adloop-0.41.3 → adloop-0.42.0}/src/adloop/microsoft_ads/read.py +0 -0
  76. {adloop-0.41.3 → adloop-0.42.0}/src/adloop/microsoft_ads/write.py +0 -0
  77. {adloop-0.41.3 → adloop-0.42.0}/src/adloop/oauth_redirect.py +0 -0
  78. {adloop-0.41.3 → adloop-0.42.0}/src/adloop/pagespeed.py +0 -0
  79. {adloop-0.41.3 → adloop-0.42.0}/src/adloop/reddit/__init__.py +0 -0
  80. {adloop-0.41.3 → adloop-0.42.0}/src/adloop/reddit/auth.py +0 -0
  81. {adloop-0.41.3 → adloop-0.42.0}/src/adloop/reddit/client.py +0 -0
  82. {adloop-0.41.3 → adloop-0.42.0}/src/adloop/reddit/schedule.py +0 -0
  83. {adloop-0.41.3 → adloop-0.42.0}/src/adloop/reddit/write.py +0 -0
  84. {adloop-0.41.3 → adloop-0.42.0}/src/adloop/rules/__init__.py +0 -0
  85. {adloop-0.41.3 → adloop-0.42.0}/src/adloop/rules/commands/analyze-performance.md +0 -0
  86. {adloop-0.41.3 → adloop-0.42.0}/src/adloop/rules/commands/budget-plan.md +0 -0
  87. {adloop-0.41.3 → adloop-0.42.0}/src/adloop/rules/commands/create-ad.md +0 -0
  88. {adloop-0.41.3 → adloop-0.42.0}/src/adloop/rules/commands/create-campaign.md +0 -0
  89. {adloop-0.41.3 → adloop-0.42.0}/src/adloop/rules/commands/diagnose-tracking.md +0 -0
  90. {adloop-0.41.3 → adloop-0.42.0}/src/adloop/rules/commands/optimize-campaign.md +0 -0
  91. {adloop-0.41.3 → adloop-0.42.0}/src/adloop/rules_install.py +0 -0
  92. {adloop-0.41.3 → adloop-0.42.0}/src/adloop/runtime.py +0 -0
  93. {adloop-0.41.3 → adloop-0.42.0}/src/adloop/safety/__init__.py +0 -0
  94. {adloop-0.41.3 → adloop-0.42.0}/src/adloop/safety/approval.py +0 -0
  95. {adloop-0.41.3 → adloop-0.42.0}/src/adloop/safety/approval_card.html +0 -0
  96. {adloop-0.41.3 → adloop-0.42.0}/src/adloop/safety/audit.py +0 -0
  97. {adloop-0.41.3 → adloop-0.42.0}/src/adloop/safety/guards.py +0 -0
  98. {adloop-0.41.3 → adloop-0.42.0}/src/adloop/safety/outcomes.py +0 -0
  99. {adloop-0.41.3 → adloop-0.42.0}/src/adloop/safety/preview.py +0 -0
  100. {adloop-0.41.3 → adloop-0.42.0}/src/adloop/tracking.py +0 -0
  101. {adloop-0.41.3 → adloop-0.42.0}/src/adloop/upload_card.html +0 -0
  102. {adloop-0.41.3 → adloop-0.42.0}/src/adloop/uploads.py +0 -0
  103. {adloop-0.41.3 → adloop-0.42.0}/src/adloop/views/__init__.py +0 -0
  104. {adloop-0.41.3 → adloop-0.42.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.41.3
3
+ Version: 0.42.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
@@ -330,12 +330,12 @@ All write operations follow a **draft → preview → confirm** workflow. Nothin
330
330
 
331
331
  | Tool | What It Does |
332
332
  |------|-------------|
333
- | `draft_campaign` | Create a full campaign structure — budget + campaign (PAUSED) + ad group + optional keywords. Supports Search partners, display expansion, the location option (presence vs presence or interest), campaign URL options (tracking template, final URL suffix, custom parameters), and `max_cpc` for either MANUAL_CPC initial ad-group bids or TARGET_SPEND (Maximize Clicks) CPC caps. |
333
+ | `draft_campaign` | Create a full campaign structure — budget + campaign (PAUSED) + ad group + optional keywords. Supports Search partners, display expansion, the location option (presence vs presence or interest), campaign URL options (tracking template, final URL suffix, custom parameters), geo exclusions, an ad schedule, and `max_cpc` for either MANUAL_CPC initial ad-group bids or TARGET_SPEND (Maximize Clicks) CPC caps. |
334
334
  | `draft_shopping_campaign` | Create a Standard Shopping campaign for a linked Merchant Center feed — budget, campaign (PAUSED), product ad group, Shopping ad and an "All products" group in one atomic request. Manual CPC, Maximize clicks or target ROAS. |
335
335
  | `draft_asset_group_assets` | Add and remove Performance Max assets (text, images incl. uploads, YouTube videos) in one change, so replacing an asset never drops a group below Google's minimums |
336
336
  | `draft_asset_group_signals` | Add search themes or set the audience signal of a PMax asset group |
337
337
  | `draft_pmax_product_exclusions` | Exclude products from a retail PMax asset group by item id, brand or custom label |
338
- | `update_campaign` | Modify existing campaign settings — bidding, budget, geo/language targeting, the location option, campaign URL options, campaign-level broad match (Smart Bidding only), Search partners, display expansion, and TARGET_SPEND (Maximize Clicks) `max_cpc` caps. |
338
+ | `update_campaign` | Modify existing campaign settings — bidding, budget, geo/language targeting, geo exclusions and the ad schedule (both replace the current list), the location option, campaign URL options, campaign-level broad match (Smart Bidding only), Search partners, display expansion, and TARGET_SPEND (Maximize Clicks) `max_cpc` caps. |
339
339
  | `draft_ad_group` | Create a paused SEARCH_STANDARD ad group inside an existing campaign, with optional MANUAL_CPC `max_cpc`. |
340
340
  | `update_ad_group` | Update an ad group name and/or MANUAL_CPC `max_cpc`. Use `pause_entity` / `enable_entity` for ad-group status changes. |
341
341
  | `draft_responsive_search_ad` | Create RSA preview (3-15 headlines ≤30 chars, 2-4 descriptions ≤90 chars). Warns if headline/description count is below best practice. |
@@ -298,12 +298,12 @@ All write operations follow a **draft → preview → confirm** workflow. Nothin
298
298
 
299
299
  | Tool | What It Does |
300
300
  |------|-------------|
301
- | `draft_campaign` | Create a full campaign structure — budget + campaign (PAUSED) + ad group + optional keywords. Supports Search partners, display expansion, the location option (presence vs presence or interest), campaign URL options (tracking template, final URL suffix, custom parameters), and `max_cpc` for either MANUAL_CPC initial ad-group bids or TARGET_SPEND (Maximize Clicks) CPC caps. |
301
+ | `draft_campaign` | Create a full campaign structure — budget + campaign (PAUSED) + ad group + optional keywords. Supports Search partners, display expansion, the location option (presence vs presence or interest), campaign URL options (tracking template, final URL suffix, custom parameters), geo exclusions, an ad schedule, and `max_cpc` for either MANUAL_CPC initial ad-group bids or TARGET_SPEND (Maximize Clicks) CPC caps. |
302
302
  | `draft_shopping_campaign` | Create a Standard Shopping campaign for a linked Merchant Center feed — budget, campaign (PAUSED), product ad group, Shopping ad and an "All products" group in one atomic request. Manual CPC, Maximize clicks or target ROAS. |
303
303
  | `draft_asset_group_assets` | Add and remove Performance Max assets (text, images incl. uploads, YouTube videos) in one change, so replacing an asset never drops a group below Google's minimums |
304
304
  | `draft_asset_group_signals` | Add search themes or set the audience signal of a PMax asset group |
305
305
  | `draft_pmax_product_exclusions` | Exclude products from a retail PMax asset group by item id, brand or custom label |
306
- | `update_campaign` | Modify existing campaign settings — bidding, budget, geo/language targeting, the location option, campaign URL options, campaign-level broad match (Smart Bidding only), Search partners, display expansion, and TARGET_SPEND (Maximize Clicks) `max_cpc` caps. |
306
+ | `update_campaign` | Modify existing campaign settings — bidding, budget, geo/language targeting, geo exclusions and the ad schedule (both replace the current list), the location option, campaign URL options, campaign-level broad match (Smart Bidding only), Search partners, display expansion, and TARGET_SPEND (Maximize Clicks) `max_cpc` caps. |
307
307
  | `draft_ad_group` | Create a paused SEARCH_STANDARD ad group inside an existing campaign, with optional MANUAL_CPC `max_cpc`. |
308
308
  | `update_ad_group` | Update an ad group name and/or MANUAL_CPC `max_cpc`. Use `pause_entity` / `enable_entity` for ad-group status changes. |
309
309
  | `draft_responsive_search_ad` | Create RSA preview (3-15 headlines ≤30 chars, 2-4 descriptions ≤90 chars). Warns if headline/description count is below best practice. |
@@ -1,6 +1,6 @@
1
1
  [project]
2
2
  name = "adloop"
3
- version = "0.41.3"
3
+ version = "0.42.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.41.3"
3
+ version = "0.42.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 = [
@@ -1132,6 +1132,14 @@ def update_call_asset(
1132
1132
  return preview
1133
1133
 
1134
1134
 
1135
+ def _first_schedule_warning(campaign_name: str) -> str:
1136
+ return (
1137
+ f"Campaign '{campaign_name}' has no ad schedule, so it serves at all hours. "
1138
+ "With these windows it serves only inside them: every other hour, and every "
1139
+ "day without a window, stops serving."
1140
+ )
1141
+
1142
+
1135
1143
  def _campaign_schedule(config: AdLoopConfig, customer_id: str, campaign_id: str) -> tuple[dict | None, list[dict]]:
1136
1144
  """The campaign (name, status, account time zone) and its current windows."""
1137
1145
  from adloop.ads.gaql import execute_query
@@ -1193,11 +1201,7 @@ def add_ad_schedule(
1193
1201
  warnings = [_time_zone_note(campaign["time_zone"])]
1194
1202
  first = not existing
1195
1203
  if first:
1196
- warnings.append(
1197
- f"Campaign '{campaign['name']}' has no ad schedule, so it serves at all hours. "
1198
- "With these windows it serves only inside them: every other hour, and every "
1199
- "day without a window, stops serving."
1200
- )
1204
+ warnings.append(_first_schedule_warning(campaign["name"]))
1201
1205
  if campaign["status"] != "ENABLED":
1202
1206
  warnings.append(f"The campaign is {campaign['status']}; the schedule applies once it is enabled.")
1203
1207
  plan = ChangePlan(
@@ -204,8 +204,9 @@ def draft_create_conversion_action(
204
204
  False = Secondary (records but doesn't affect bidding).
205
205
  include_in_conversions_metric: True (default) = appears in the
206
206
  "Conversions" column; False = "All conversions" only. NOTE: this is
207
- IMMUTABLE on create — Google derives it from the category and rejects
208
- any value set in the create mutate. To change it, use
207
+ IMMUTABLE — Google derives it from primary_for_goal and the category's
208
+ goal and rejects any value set directly. To change whether an action
209
+ counts, use primary_for_goal or category in
209
210
  draft_update_conversion_action after the create succeeds.
210
211
  click_through_window_days / view_through_window_days: attribution
211
212
  windows. 30/1 is the typical lead-gen pair.
@@ -305,13 +306,19 @@ def draft_update_conversion_action(
305
306
  click_through_window_days: int = 0,
306
307
  view_through_window_days: int = 0,
307
308
  attribution_model: str = "",
309
+ category: str = "",
308
310
  ) -> dict:
309
311
  """Draft a partial UPDATE of an existing ConversionAction — returns PREVIEW.
310
312
 
311
313
  Only the parameters you pass non-empty/non-default will be sent to the
312
314
  API. Use this to rename, demote a Primary to Secondary, change value,
313
- adjust the call-duration threshold, or change attribution settings.
314
- include_in_conversions_metric IS mutable here (unlike on create).
315
+ adjust the call-duration threshold, change attribution settings, or move
316
+ the action to another goal (category; the preview shows the current one,
317
+ and the apply sends nothing if it changed in Google Ads since).
318
+
319
+ include_in_conversions_metric is refused: Google answers IMMUTABLE_FIELD
320
+ at apply, and validate_only does not catch it. It derives the value from
321
+ primary_for_goal and whether the goal of the action's category is biddable.
315
322
 
316
323
  conversion_action_id: numeric ID. Find via:
317
324
  SELECT conversion_action.id, conversion_action.name FROM conversion_action
@@ -341,9 +348,44 @@ def draft_update_conversion_action(
341
348
  view_through_window_days=view_through_window_days,
342
349
  attribution_model=attribution_model,
343
350
  )
351
+ if include_in_conversions_metric is not None:
352
+ errors.append(
353
+ "include_in_conversions_metric cannot be changed: Google answers "
354
+ "IMMUTABLE_FIELD at apply, and a dry run does not catch it. Whether an "
355
+ "action counts in Conversions follows primary_for_goal and whether the "
356
+ "goal of its category is biddable: set primary_for_goal, or move it to "
357
+ "a biddable goal with category."
358
+ )
359
+ category = category.strip().upper()
360
+ if category and category not in _VALID_CATEGORIES:
361
+ errors.append(
362
+ f"category '{category}' invalid; valid: {sorted(_VALID_CATEGORIES)}"
363
+ )
364
+ if category and not str(conversion_action_id).strip().isdigit():
365
+ errors.append(
366
+ f"conversion_action_id must be numeric, got '{conversion_action_id}'"
367
+ )
344
368
  if errors:
345
369
  return {"error": "Validation failed", "details": errors}
346
370
 
371
+ warnings: list[str] = []
372
+ current_category = ""
373
+ if category:
374
+ current = _current_conversion_action(config, customer_id, conversion_action_id)
375
+ if current is None:
376
+ return {"error": f"Conversion action {conversion_action_id} was not found in this account"}
377
+ if current["status"] == "REMOVED":
378
+ return {"error": f"Conversion action {conversion_action_id} is removed"}
379
+ if current["category"] == category:
380
+ return {"error": f"Conversion action {conversion_action_id} is already in category {category}"}
381
+ current_category = current["category"]
382
+ warnings.append(
383
+ f"Moves '{current['name']}' from the {current_category} goal to the {category} "
384
+ "goal. It counts in Conversions only if it is primary and the new goal is "
385
+ "biddable, so campaigns bidding on these goals can start or stop optimizing "
386
+ "toward it, which restarts Smart Bidding's learning period."
387
+ )
388
+
347
389
  # Track which fields the caller actually wants to update so we build
348
390
  # the right field_mask at apply time.
349
391
  changes: dict = {"conversion_action_id": str(conversion_action_id)}
@@ -361,10 +403,9 @@ def draft_update_conversion_action(
361
403
  changes["counting_type"] = counting_type
362
404
  if phone_call_duration_seconds:
363
405
  changes["phone_call_duration_seconds"] = int(phone_call_duration_seconds)
364
- if include_in_conversions_metric is not None:
365
- changes["include_in_conversions_metric"] = bool(
366
- include_in_conversions_metric
367
- )
406
+ if category:
407
+ changes["current_category"] = current_category
408
+ changes["category"] = category
368
409
  if click_through_window_days:
369
410
  changes["click_through_window_days"] = int(click_through_window_days)
370
411
  if view_through_window_days:
@@ -375,7 +416,6 @@ def draft_update_conversion_action(
375
416
  if len(changes) == 1: # only conversion_action_id
376
417
  return {"error": "No fields to update"}
377
418
 
378
- warnings: list[str] = []
379
419
  # Same fallback-vs-override nuance as create: on update, a positive
380
420
  # default_value with always_use_default_value explicitly set to False is
381
421
  # legal (fallback). Warn rather than silently overriding intent.
@@ -404,6 +444,24 @@ def draft_update_conversion_action(
404
444
  return preview
405
445
 
406
446
 
447
+ def _current_conversion_action(
448
+ config: AdLoopConfig, customer_id: str, conversion_action_id: str
449
+ ) -> dict | None:
450
+ """Name, category and status of a conversion action, or None if not found."""
451
+ from adloop.ads.gaql import execute_query
452
+
453
+ rows = execute_query(config, customer_id, (
454
+ "SELECT conversion_action.name, conversion_action.category, conversion_action.status "
455
+ f"FROM conversion_action WHERE conversion_action.id = {str(conversion_action_id).strip()} LIMIT 1"))
456
+ if not rows:
457
+ return None
458
+ return {
459
+ "name": rows[0].get("conversion_action.name") or "",
460
+ "category": str(rows[0].get("conversion_action.category") or ""),
461
+ "status": str(rows[0].get("conversion_action.status") or ""),
462
+ }
463
+
464
+
407
465
  def draft_remove_conversion_action(
408
466
  config: AdLoopConfig,
409
467
  *,
@@ -472,8 +530,8 @@ def _apply_create_conversion_action(client: object, cid: str, changes: dict) ->
472
530
  ca.primary_for_goal = changes["primary_for_goal"]
473
531
  # NOTE: include_in_conversions_metric is IMMUTABLE on create — Google
474
532
  # derives it from the conversion category and rejects any value set in
475
- # the create mutate (IMMUTABLE_FIELD). To change it, use
476
- # draft_update_conversion_action after the create succeeds.
533
+ # the create mutate (IMMUTABLE_FIELD). draft_update_conversion_action
534
+ # changes what it derives from: primary_for_goal and category.
477
535
  if changes.get("phone_call_duration_seconds"):
478
536
  ca.phone_call_duration_seconds = changes["phone_call_duration_seconds"]
479
537
  if changes.get("click_through_window_days"):
@@ -491,10 +549,40 @@ def _apply_create_conversion_action(client: object, cid: str, changes: dict) ->
491
549
  return {"resource_name": response.results[0].resource_name}
492
550
 
493
551
 
552
+ def _check_category_unchanged(client: object, cid: str, changes: dict) -> None:
553
+ """Refuse a category move when the category changed in Google Ads after the preview.
554
+
555
+ Moving an action to another goal can start or stop campaigns bidding on
556
+ it, so the move is pinned to ``current_category``, the category the
557
+ preview showed. A read, so it also runs in the dry run: the validate-only
558
+ client passes searches through. The value is converted as the draft's GAQL
559
+ read converts it (the enum name), so the two compare like for like.
560
+ """
561
+ from adloop.ads.gaql import _extract_field
562
+
563
+ action_id = str(changes["conversion_action_id"]).strip()
564
+ service = client.get_service("GoogleAdsService")
565
+ rows = list(service.search(customer_id=cid, query=(
566
+ "SELECT conversion_action.category FROM conversion_action "
567
+ f"WHERE conversion_action.id = {action_id}")))
568
+ if not rows:
569
+ raise RuntimeError(f"Conversion action {action_id} no longer exists; nothing was sent.")
570
+ now = str(_extract_field(rows[0], "conversion_action.category") or "")
571
+ pinned = changes.get("current_category", "")
572
+ if now != pinned:
573
+ raise RuntimeError(
574
+ f"Conversion action {action_id} was changed in Google Ads after this preview "
575
+ f"(its category is now {now}, the preview showed {pinned}); nothing was sent. "
576
+ "Draft the edit again so the preview shows the current category."
577
+ )
578
+
579
+
494
580
  def _apply_update_conversion_action(client: object, cid: str, changes: dict) -> dict:
495
581
  """Partial update of an existing ConversionAction.
496
582
 
497
583
  Builds a FieldMask listing only the fields the caller wanted to update.
584
+ A category move first re-reads the action and sends nothing if its
585
+ category changed in Google Ads since the preview (dry run included).
498
586
  """
499
587
  from google.protobuf import field_mask_pb2
500
588
 
@@ -505,6 +593,15 @@ def _apply_update_conversion_action(client: object, cid: str, changes: dict) ->
505
593
  cid, changes["conversion_action_id"]
506
594
  )
507
595
 
596
+ if "include_in_conversions_metric" in changes:
597
+ # A plan drafted before the draft refused it; Google would reject it.
598
+ raise ValueError(
599
+ "include_in_conversions_metric cannot be changed; nothing was sent. "
600
+ "Draft again with primary_for_goal or category."
601
+ )
602
+ if "category" in changes:
603
+ _check_category_unchanged(client, cid, changes)
604
+
508
605
  paths: list[str] = []
509
606
 
510
607
  if "name" in changes:
@@ -530,9 +627,11 @@ def _apply_update_conversion_action(client: object, cid: str, changes: dict) ->
530
627
  if "phone_call_duration_seconds" in changes:
531
628
  ca.phone_call_duration_seconds = changes["phone_call_duration_seconds"]
532
629
  paths.append("phone_call_duration_seconds")
533
- if "include_in_conversions_metric" in changes:
534
- ca.include_in_conversions_metric = changes["include_in_conversions_metric"]
535
- paths.append("include_in_conversions_metric")
630
+ if "category" in changes:
631
+ ca.category = getattr(
632
+ client.enums.ConversionActionCategoryEnum, changes["category"]
633
+ )
634
+ paths.append("category")
536
635
  if "click_through_window_days" in changes:
537
636
  ca.click_through_lookback_window_days = changes["click_through_window_days"]
538
637
  paths.append("click_through_lookback_window_days")
@@ -37,21 +37,26 @@ _CURRENCY_SYMBOLS: dict[str, str] = {
37
37
  "UAH": "\u20b4",
38
38
  }
39
39
 
40
- # Module-level cache: customer_id -> currency_code (one API call per session)
41
- _cache: dict[str, str] = {}
40
+ # Module-level cache: (tenant, customer_id) -> currency_code (one API call
41
+ # per session). The tenant is part of the key because a hosted server runs
42
+ # many tenants in one process: what one of them reads, or fails to read,
43
+ # about an account must never be served to another.
44
+ _cache: dict[tuple[str, str], str] = {}
42
45
 
43
46
 
44
47
  def get_currency_code(config: AdLoopConfig, customer_id: str) -> str:
45
48
  """Detect the account's currency via ``customer.currency_code``.
46
49
 
47
- Result is cached per *customer_id* for the lifetime of the server process.
48
- Falls back to ``"EUR"`` on any error.
50
+ A successful lookup is cached per tenant and *customer_id* for the
51
+ lifetime of the server process. Falls back to ``"EUR"`` on any error;
52
+ the fallback is not cached, so a passing failure does not stick.
49
53
  """
50
54
  from adloop.ads.client import normalize_customer_id
55
+ from adloop.runtime import current_tenant
51
56
 
52
- cid = normalize_customer_id(customer_id)
53
- if cid in _cache:
54
- return _cache[cid]
57
+ key = (current_tenant(), normalize_customer_id(customer_id))
58
+ if key in _cache:
59
+ return _cache[key]
55
60
 
56
61
  try:
57
62
  from adloop.ads.gaql import execute_query
@@ -59,11 +64,13 @@ def get_currency_code(config: AdLoopConfig, customer_id: str) -> str:
59
64
  rows = execute_query(
60
65
  config, customer_id, "SELECT customer.currency_code FROM customer LIMIT 1"
61
66
  )
62
- code = (rows[0].get("customer.currency_code", "EUR") if rows else "EUR") or "EUR"
67
+ code = rows[0].get("customer.currency_code") if rows else None
63
68
  except Exception:
64
- code = "EUR"
69
+ code = None
70
+ if not code:
71
+ return "EUR"
65
72
 
66
- _cache[cid] = code
73
+ _cache[key] = code
67
74
  return code
68
75
 
69
76
 
@@ -3,9 +3,9 @@
3
3
  The fetched bytes are uploaded into the user's Google Ads account, where
4
4
  they become visible. A fetch that could be steered at an internal address
5
5
  (cloud metadata, a database admin page, a loopback service) would therefore
6
- be a way to exfiltrate whatever lives there. So, unlike the up/down
7
- reachability check for landing pages, this fetcher closes the DNS-rebinding
8
- gap:
6
+ be a way to exfiltrate whatever lives there. So this fetcher closes the
7
+ DNS-rebinding gap (the reachability check for landing pages reuses its
8
+ pinned connections):
9
9
 
10
10
  * the hostname is resolved exactly once per request, and every resolved
11
11
  address must be public;