adloop 0.41.1__tar.gz → 0.41.3__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.
- {adloop-0.41.1 → adloop-0.41.3}/PKG-INFO +2 -1
- {adloop-0.41.1 → adloop-0.41.3}/pyproject.toml +2 -1
- {adloop-0.41.1 → adloop-0.41.3}/pyproject.toml.orig +2 -1
- {adloop-0.41.1 → adloop-0.41.3}/src/adloop/microsoft_ads/manage/_common.py +15 -1
- {adloop-0.41.1 → adloop-0.41.3}/src/adloop/microsoft_ads/manage/campaigns.py +6 -1
- {adloop-0.41.1 → adloop-0.41.3}/src/adloop/microsoft_ads/read.py +1 -1
- {adloop-0.41.1 → adloop-0.41.3}/src/adloop/reddit/write.py +108 -22
- {adloop-0.41.1 → adloop-0.41.3}/src/adloop/rules/adloop.md +1 -1
- {adloop-0.41.1 → adloop-0.41.3}/src/adloop/server.py +28 -10
- {adloop-0.41.1 → adloop-0.41.3}/README.md +0 -0
- {adloop-0.41.1 → adloop-0.41.3}/src/adloop/__init__.py +0 -0
- {adloop-0.41.1 → adloop-0.41.3}/src/adloop/__main__.py +0 -0
- {adloop-0.41.1 → adloop-0.41.3}/src/adloop/_mcp_patches.py +0 -0
- {adloop-0.41.1 → adloop-0.41.3}/src/adloop/ads/__init__.py +0 -0
- {adloop-0.41.1 → adloop-0.41.3}/src/adloop/ads/ai_max.py +0 -0
- {adloop-0.41.1 → adloop-0.41.3}/src/adloop/ads/assets.py +0 -0
- {adloop-0.41.1 → adloop-0.41.3}/src/adloop/ads/audiences.py +0 -0
- {adloop-0.41.1 → adloop-0.41.3}/src/adloop/ads/brands.py +0 -0
- {adloop-0.41.1 → adloop-0.41.3}/src/adloop/ads/campaign_settings.py +0 -0
- {adloop-0.41.1 → adloop-0.41.3}/src/adloop/ads/client.py +0 -0
- {adloop-0.41.1 → adloop-0.41.3}/src/adloop/ads/conversion_actions.py +0 -0
- {adloop-0.41.1 → adloop-0.41.3}/src/adloop/ads/currency.py +0 -0
- {adloop-0.41.1 → adloop-0.41.3}/src/adloop/ads/custom_conversion_goals.py +0 -0
- {adloop-0.41.1 → adloop-0.41.3}/src/adloop/ads/enums.py +0 -0
- {adloop-0.41.1 → adloop-0.41.3}/src/adloop/ads/forecast.py +0 -0
- {adloop-0.41.1 → adloop-0.41.3}/src/adloop/ads/gaql.py +0 -0
- {adloop-0.41.1 → adloop-0.41.3}/src/adloop/ads/image_fetch.py +0 -0
- {adloop-0.41.1 → adloop-0.41.3}/src/adloop/ads/pmax.py +0 -0
- {adloop-0.41.1 → adloop-0.41.3}/src/adloop/ads/read.py +0 -0
- {adloop-0.41.1 → adloop-0.41.3}/src/adloop/ads/recommendation_subscriptions.py +0 -0
- {adloop-0.41.1 → adloop-0.41.3}/src/adloop/ads/recommendations.py +0 -0
- {adloop-0.41.1 → adloop-0.41.3}/src/adloop/ads/shopping.py +0 -0
- {adloop-0.41.1 → adloop-0.41.3}/src/adloop/ads/snippet_headers.py +0 -0
- {adloop-0.41.1 → adloop-0.41.3}/src/adloop/ads/validate_only.py +0 -0
- {adloop-0.41.1 → adloop-0.41.3}/src/adloop/ads/write.py +0 -0
- {adloop-0.41.1 → adloop-0.41.3}/src/adloop/auth.py +0 -0
- {adloop-0.41.1 → adloop-0.41.3}/src/adloop/cli.py +0 -0
- {adloop-0.41.1 → adloop-0.41.3}/src/adloop/config.py +0 -0
- {adloop-0.41.1 → adloop-0.41.3}/src/adloop/conversion_setup.py +0 -0
- {adloop-0.41.1 → adloop-0.41.3}/src/adloop/crossref.py +0 -0
- {adloop-0.41.1 → adloop-0.41.3}/src/adloop/diagnostics.py +0 -0
- {adloop-0.41.1 → adloop-0.41.3}/src/adloop/ga4/__init__.py +0 -0
- {adloop-0.41.1 → adloop-0.41.3}/src/adloop/ga4/admin.py +0 -0
- {adloop-0.41.1 → adloop-0.41.3}/src/adloop/ga4/annotations.py +0 -0
- {adloop-0.41.1 → adloop-0.41.3}/src/adloop/ga4/attribution.py +0 -0
- {adloop-0.41.1 → adloop-0.41.3}/src/adloop/ga4/audit.py +0 -0
- {adloop-0.41.1 → adloop-0.41.3}/src/adloop/ga4/client.py +0 -0
- {adloop-0.41.1 → adloop-0.41.3}/src/adloop/ga4/definitions.py +0 -0
- {adloop-0.41.1 → adloop-0.41.3}/src/adloop/ga4/funnels.py +0 -0
- {adloop-0.41.1 → adloop-0.41.3}/src/adloop/ga4/reports.py +0 -0
- {adloop-0.41.1 → adloop-0.41.3}/src/adloop/ga4/retention.py +0 -0
- {adloop-0.41.1 → adloop-0.41.3}/src/adloop/ga4/suggest.py +0 -0
- {adloop-0.41.1 → adloop-0.41.3}/src/adloop/ga4/tracking.py +0 -0
- {adloop-0.41.1 → adloop-0.41.3}/src/adloop/ga4/write.py +0 -0
- {adloop-0.41.1 → adloop-0.41.3}/src/adloop/gsc/__init__.py +0 -0
- {adloop-0.41.1 → adloop-0.41.3}/src/adloop/gsc/client.py +0 -0
- {adloop-0.41.1 → adloop-0.41.3}/src/adloop/gsc/reports.py +0 -0
- {adloop-0.41.1 → adloop-0.41.3}/src/adloop/gtm/__init__.py +0 -0
- {adloop-0.41.1 → adloop-0.41.3}/src/adloop/gtm/client.py +0 -0
- {adloop-0.41.1 → adloop-0.41.3}/src/adloop/gtm/entities.py +0 -0
- {adloop-0.41.1 → adloop-0.41.3}/src/adloop/gtm/read.py +0 -0
- {adloop-0.41.1 → adloop-0.41.3}/src/adloop/gtm/write.py +0 -0
- {adloop-0.41.1 → adloop-0.41.3}/src/adloop/merchant/__init__.py +0 -0
- {adloop-0.41.1 → adloop-0.41.3}/src/adloop/merchant/client.py +0 -0
- {adloop-0.41.1 → adloop-0.41.3}/src/adloop/merchant/read.py +0 -0
- {adloop-0.41.1 → adloop-0.41.3}/src/adloop/microsoft_ads/__init__.py +0 -0
- {adloop-0.41.1 → adloop-0.41.3}/src/adloop/microsoft_ads/auth.py +0 -0
- {adloop-0.41.1 → adloop-0.41.3}/src/adloop/microsoft_ads/client.py +0 -0
- {adloop-0.41.1 → adloop-0.41.3}/src/adloop/microsoft_ads/manage/__init__.py +0 -0
- {adloop-0.41.1 → adloop-0.41.3}/src/adloop/microsoft_ads/manage/ad_groups.py +0 -0
- {adloop-0.41.1 → adloop-0.41.3}/src/adloop/microsoft_ads/manage/ads.py +0 -0
- {adloop-0.41.1 → adloop-0.41.3}/src/adloop/microsoft_ads/manage/extensions.py +0 -0
- {adloop-0.41.1 → adloop-0.41.3}/src/adloop/microsoft_ads/manage/negatives.py +0 -0
- {adloop-0.41.1 → adloop-0.41.3}/src/adloop/microsoft_ads/manage/removal.py +0 -0
- {adloop-0.41.1 → adloop-0.41.3}/src/adloop/microsoft_ads/manage/targeting.py +0 -0
- {adloop-0.41.1 → adloop-0.41.3}/src/adloop/microsoft_ads/write.py +0 -0
- {adloop-0.41.1 → adloop-0.41.3}/src/adloop/oauth_redirect.py +0 -0
- {adloop-0.41.1 → adloop-0.41.3}/src/adloop/pagespeed.py +0 -0
- {adloop-0.41.1 → adloop-0.41.3}/src/adloop/reddit/__init__.py +0 -0
- {adloop-0.41.1 → adloop-0.41.3}/src/adloop/reddit/auth.py +0 -0
- {adloop-0.41.1 → adloop-0.41.3}/src/adloop/reddit/client.py +0 -0
- {adloop-0.41.1 → adloop-0.41.3}/src/adloop/reddit/read.py +0 -0
- {adloop-0.41.1 → adloop-0.41.3}/src/adloop/reddit/schedule.py +0 -0
- {adloop-0.41.1 → adloop-0.41.3}/src/adloop/rules/__init__.py +0 -0
- {adloop-0.41.1 → adloop-0.41.3}/src/adloop/rules/commands/analyze-performance.md +0 -0
- {adloop-0.41.1 → adloop-0.41.3}/src/adloop/rules/commands/budget-plan.md +0 -0
- {adloop-0.41.1 → adloop-0.41.3}/src/adloop/rules/commands/create-ad.md +0 -0
- {adloop-0.41.1 → adloop-0.41.3}/src/adloop/rules/commands/create-campaign.md +0 -0
- {adloop-0.41.1 → adloop-0.41.3}/src/adloop/rules/commands/diagnose-tracking.md +0 -0
- {adloop-0.41.1 → adloop-0.41.3}/src/adloop/rules/commands/optimize-campaign.md +0 -0
- {adloop-0.41.1 → adloop-0.41.3}/src/adloop/rules_install.py +0 -0
- {adloop-0.41.1 → adloop-0.41.3}/src/adloop/runtime.py +0 -0
- {adloop-0.41.1 → adloop-0.41.3}/src/adloop/safety/__init__.py +0 -0
- {adloop-0.41.1 → adloop-0.41.3}/src/adloop/safety/approval.py +0 -0
- {adloop-0.41.1 → adloop-0.41.3}/src/adloop/safety/approval_card.html +0 -0
- {adloop-0.41.1 → adloop-0.41.3}/src/adloop/safety/audit.py +0 -0
- {adloop-0.41.1 → adloop-0.41.3}/src/adloop/safety/guards.py +0 -0
- {adloop-0.41.1 → adloop-0.41.3}/src/adloop/safety/outcomes.py +0 -0
- {adloop-0.41.1 → adloop-0.41.3}/src/adloop/safety/preview.py +0 -0
- {adloop-0.41.1 → adloop-0.41.3}/src/adloop/tracking.py +0 -0
- {adloop-0.41.1 → adloop-0.41.3}/src/adloop/upload_card.html +0 -0
- {adloop-0.41.1 → adloop-0.41.3}/src/adloop/uploads.py +0 -0
- {adloop-0.41.1 → adloop-0.41.3}/src/adloop/views/__init__.py +0 -0
- {adloop-0.41.1 → adloop-0.41.3}/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
|
+
Version: 0.41.3
|
|
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
|
|
@@ -16,6 +16,7 @@ Requires-Dist: google-api-python-client>=2.0.0
|
|
|
16
16
|
Requires-Dist: phonenumbers>=8.13
|
|
17
17
|
Requires-Dist: pyyaml>=6.0
|
|
18
18
|
Requires-Dist: requests>=2.31
|
|
19
|
+
Requires-Dist: tzdata>=2024.1
|
|
19
20
|
Requires-Dist: pytest>=8.0 ; extra == 'dev'
|
|
20
21
|
Requires-Dist: pytest-asyncio>=0.23 ; extra == 'dev'
|
|
21
22
|
Requires-Dist: prefab-ui==0.21.1 ; extra == 'dev'
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
[project]
|
|
2
2
|
name = "adloop"
|
|
3
|
-
version = "0.41.
|
|
3
|
+
version = "0.41.3"
|
|
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"
|
|
@@ -28,6 +28,7 @@ dependencies = [
|
|
|
28
28
|
"phonenumbers>=8.13",
|
|
29
29
|
"pyyaml>=6.0",
|
|
30
30
|
"requests>=2.31",
|
|
31
|
+
"tzdata>=2024.1",
|
|
31
32
|
]
|
|
32
33
|
|
|
33
34
|
[[project.authors]]
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
[project]
|
|
2
2
|
name = "adloop"
|
|
3
|
-
version = "0.41.
|
|
3
|
+
version = "0.41.3"
|
|
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 = [
|
|
@@ -22,6 +22,7 @@ dependencies = [
|
|
|
22
22
|
# Reddit Ads has no SDK; the REST client (adloop.reddit.client) and the
|
|
23
23
|
# PageSpeed helper call requests directly.
|
|
24
24
|
"requests>=2.31",
|
|
25
|
+
"tzdata>=2024.1",
|
|
25
26
|
]
|
|
26
27
|
|
|
27
28
|
[project.urls]
|
|
@@ -34,7 +34,21 @@ _CONVERSION_BASED = CONVERSION_BASED_STRATEGIES
|
|
|
34
34
|
_MANUAL = {"ManualCpc", "EnhancedCpc"}
|
|
35
35
|
# Strategies that take a maximum CPC cap.
|
|
36
36
|
_CAPPED = {"MaxClicks", "MaxConversions"}
|
|
37
|
-
_ALTERNATIVES = "MaxClicks
|
|
37
|
+
_ALTERNATIVES = "MaxClicks or EnhancedCpc"
|
|
38
|
+
# Microsoft retired manual CPC for search campaigns in 2021: it accepts a
|
|
39
|
+
# ManualCpc scheme and stores EnhancedCpc instead. Drafts ask for EnhancedCpc
|
|
40
|
+
# right away, so the preview shows what the account will have.
|
|
41
|
+
_MANUAL_RETIRED = (
|
|
42
|
+
"Microsoft retired manual CPC bidding for search campaigns and stores it as Enhanced CPC: your own "
|
|
43
|
+
"bids, which Microsoft raises or lowers in auctions it expects to convert or not. This uses EnhancedCpc."
|
|
44
|
+
)
|
|
45
|
+
|
|
46
|
+
|
|
47
|
+
def search_strategy(strategy: str) -> tuple[str, str | None]:
|
|
48
|
+
"""The strategy Microsoft will actually store, and a note when it differs."""
|
|
49
|
+
if strategy == "ManualCpc":
|
|
50
|
+
return "EnhancedCpc", _MANUAL_RETIRED
|
|
51
|
+
return strategy, None
|
|
38
52
|
MATCH_TYPES = ("Exact", "Phrase", "Broad")
|
|
39
53
|
NEGATIVE_MATCH_TYPES = ("Exact", "Phrase")
|
|
40
54
|
NETWORKS = ("OwnedAndOperatedOnly", "OwnedAndOperatedAndSyndicatedSearch")
|
|
@@ -27,6 +27,7 @@ from adloop.microsoft_ads.manage._common import (
|
|
|
27
27
|
_require_unchanged,
|
|
28
28
|
_strategy_name,
|
|
29
29
|
bidding_scheme,
|
|
30
|
+
search_strategy,
|
|
30
31
|
)
|
|
31
32
|
from adloop.microsoft_ads.read import (
|
|
32
33
|
_bidding,
|
|
@@ -71,6 +72,7 @@ def update_microsoft_campaign(
|
|
|
71
72
|
strategy = "MaxConversions"
|
|
72
73
|
if strategy and strategy not in STRATEGIES:
|
|
73
74
|
errors.append(f"bidding_strategy must be one of {list(STRATEGIES)}.")
|
|
75
|
+
strategy, strategy_note = search_strategy(strategy)
|
|
74
76
|
if target_cpa is not None and (strategy or "") not in ("", "MaxConversions"):
|
|
75
77
|
errors.append("target_cpa goes with MaxConversions.")
|
|
76
78
|
if max_cpc is not None and strategy and strategy not in _CAPPED:
|
|
@@ -123,6 +125,8 @@ def update_microsoft_campaign(
|
|
|
123
125
|
update["BiddingScheme"] = scheme
|
|
124
126
|
touched.append("BiddingScheme")
|
|
125
127
|
shown["bidding"] = {"from": _bidding(current_scheme), "to": _bidding(scheme), "currency": meta["currency"]}
|
|
128
|
+
if strategy_note:
|
|
129
|
+
warnings.append(strategy_note)
|
|
126
130
|
switching = chosen != _strategy_name(current_scheme)
|
|
127
131
|
if switching:
|
|
128
132
|
warnings.append("Changing the bid strategy restarts Microsoft's learning period; expect a week or two of unsettled results.")
|
|
@@ -198,6 +202,7 @@ def draft_microsoft_campaign(
|
|
|
198
202
|
strategy = "MaxConversions" if bidding_strategy == "TargetCpa" else (bidding_strategy or "MaxConversions")
|
|
199
203
|
if strategy not in STRATEGIES:
|
|
200
204
|
errors.append(f"bidding_strategy must be one of {list(STRATEGIES)}.")
|
|
205
|
+
strategy, strategy_note = search_strategy(strategy)
|
|
201
206
|
if target_cpa is not None and strategy != "MaxConversions":
|
|
202
207
|
errors.append(f"target_cpa goes with MaxConversions, not {strategy}.")
|
|
203
208
|
langs = _clean(languages)
|
|
@@ -224,7 +229,7 @@ def draft_microsoft_campaign(
|
|
|
224
229
|
existing = {str(c.get("Name")).lower() for c in _campaigns(config, account, customer)}
|
|
225
230
|
if name.lower() in existing:
|
|
226
231
|
return _validation_error([f"A campaign named '{name}' already exists."])
|
|
227
|
-
warnings: list[str] = []
|
|
232
|
+
warnings: list[str] = [strategy_note] if strategy_note else []
|
|
228
233
|
if strategy in _CONVERSION_BASED:
|
|
229
234
|
goals = _counted_goals(config, account, customer)
|
|
230
235
|
if goals == 0:
|
|
@@ -73,7 +73,7 @@ _TIME_ZONES = {
|
|
|
73
73
|
"Brisbane": "Australia/Brisbane",
|
|
74
74
|
"BrusselsCopenhagenMadridParis": "Europe/Paris",
|
|
75
75
|
"Bucharest": "Europe/Bucharest",
|
|
76
|
-
"BuenosAiresGeorgetown": "America/Buenos_Aires",
|
|
76
|
+
"BuenosAiresGeorgetown": "America/Argentina/Buenos_Aires",
|
|
77
77
|
"Cairo": "Africa/Cairo",
|
|
78
78
|
"CanberraMelbourneSydney": "Australia/Sydney",
|
|
79
79
|
"CapeVerdeIsland": "Atlantic/Cape_Verde",
|
|
@@ -18,6 +18,7 @@ entity after review, exactly like new Google campaigns and RSAs.
|
|
|
18
18
|
|
|
19
19
|
from __future__ import annotations
|
|
20
20
|
|
|
21
|
+
import re
|
|
21
22
|
from datetime import datetime, timezone
|
|
22
23
|
from typing import TYPE_CHECKING, Any
|
|
23
24
|
|
|
@@ -151,15 +152,46 @@ def _iso_time(value: str, field: str, errors: list[str]) -> str:
|
|
|
151
152
|
return candidate
|
|
152
153
|
|
|
153
154
|
|
|
155
|
+
def _parse_time(value: str) -> datetime:
|
|
156
|
+
parsed = datetime.fromisoformat(value.replace("Z", "+00:00"))
|
|
157
|
+
return parsed if parsed.tzinfo else parsed.replace(tzinfo=timezone.utc)
|
|
158
|
+
|
|
159
|
+
|
|
154
160
|
def _days_between(start: str, end: str) -> int:
|
|
161
|
+
"""Whole days a lifetime budget still has to be spent in.
|
|
162
|
+
|
|
163
|
+
Counted from now when the schedule already started: a budget is spent in
|
|
164
|
+
the days that are left, so days that have passed must not thin out its
|
|
165
|
+
per-day share. An unreadable schedule counts as one day.
|
|
166
|
+
"""
|
|
167
|
+
now = datetime.now(timezone.utc)
|
|
155
168
|
try:
|
|
156
|
-
s =
|
|
157
|
-
e =
|
|
169
|
+
s = max(_parse_time(start), now) if start else now
|
|
170
|
+
e = _parse_time(end)
|
|
158
171
|
except ValueError:
|
|
159
172
|
return 1
|
|
160
173
|
return max(int((e - s).total_seconds() // 86400), 1)
|
|
161
174
|
|
|
162
175
|
|
|
176
|
+
def _rescheduled_equivalent(current: dict, start_time: str, end_time: str) -> float | None:
|
|
177
|
+
"""Per-day share of an existing lifetime budget under a new schedule.
|
|
178
|
+
|
|
179
|
+
Moving the end date closer squeezes the same budget into fewer days, so a
|
|
180
|
+
schedule change on a lifetime budget is a budget change for the cap. What
|
|
181
|
+
was already spent is not known here; the whole budget is counted.
|
|
182
|
+
"""
|
|
183
|
+
if current.get("goal_type") != "LIFETIME_SPEND" or not (start_time or end_time):
|
|
184
|
+
return None
|
|
185
|
+
lifetime = from_micro(current.get("goal_value"))
|
|
186
|
+
if not lifetime:
|
|
187
|
+
return None
|
|
188
|
+
days = _days_between(
|
|
189
|
+
start_time or str(current.get("start_time") or ""),
|
|
190
|
+
end_time or str(current.get("end_time") or ""),
|
|
191
|
+
)
|
|
192
|
+
return round(float(lifetime) / days, 2)
|
|
193
|
+
|
|
194
|
+
|
|
163
195
|
def _daily_equivalent(
|
|
164
196
|
*, daily_budget: float | None, lifetime_budget: float | None, start_time: str, end_time: str,
|
|
165
197
|
errors: list[str],
|
|
@@ -215,17 +247,45 @@ def _store(plan_kwargs: dict, *, warnings: list[str] | None = None) -> dict:
|
|
|
215
247
|
return store_draft(ChangePlan(platform=PLATFORM, **plan_kwargs), warnings=warnings)
|
|
216
248
|
|
|
217
249
|
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
250
|
+
_ENTITY_ID = re.compile(r"[A-Za-z0-9_]+")
|
|
251
|
+
|
|
252
|
+
|
|
253
|
+
def _in_account(config: AdLoopConfig, entity_type: str, entity: dict, account: str) -> bool:
|
|
254
|
+
"""Whether a fetched campaign, ad group or ad belongs to ``account``."""
|
|
255
|
+
owner = str(entity.get("ad_account_id") or "").strip()
|
|
256
|
+
if owner:
|
|
257
|
+
return owner == account
|
|
258
|
+
# No owner on the entity: the account's own listing decides.
|
|
259
|
+
from adloop.reddit.client import reddit_get_all
|
|
260
|
+
|
|
261
|
+
listed = reddit_get_all(config, f"ad_accounts/{account}/{_COLLECTIONS[entity_type]}")
|
|
262
|
+
return any(str(row.get("id")) == str(entity.get("id")) for row in listed)
|
|
263
|
+
|
|
221
264
|
|
|
265
|
+
def _fetch(config: AdLoopConfig, entity_type: str, entity_id: str, account: str) -> dict:
|
|
266
|
+
"""The entity, or ``{}`` when it is not one of ``account``'s.
|
|
222
267
|
|
|
223
|
-
|
|
224
|
-
|
|
268
|
+
Reddit addresses campaigns, ad groups and ads by id alone, without the ad
|
|
269
|
+
account in the path, and one Reddit login often reaches several accounts.
|
|
270
|
+
A change is drafted, previewed, capped and logged for one account, so an
|
|
271
|
+
id from another account is treated as not found. Ids are checked too:
|
|
272
|
+
they go into the request path.
|
|
273
|
+
"""
|
|
274
|
+
entity_id = str(entity_id or "")
|
|
275
|
+
if not account or not _ENTITY_ID.fullmatch(entity_id):
|
|
276
|
+
return {}
|
|
277
|
+
entity = data_of(reddit_get(config, f"{_COLLECTIONS[entity_type]}/{entity_id}"))
|
|
278
|
+
if not entity or not _in_account(config, entity_type, entity, account):
|
|
279
|
+
return {}
|
|
280
|
+
return entity
|
|
281
|
+
|
|
282
|
+
|
|
283
|
+
def _exists(config: AdLoopConfig, entity_type: str, entity_id: str, account: str) -> bool:
|
|
284
|
+
"""Whether the entity exists in ``account``; a 404 is a no, other errors propagate."""
|
|
225
285
|
from adloop.reddit.auth import RedditApiError
|
|
226
286
|
|
|
227
287
|
try:
|
|
228
|
-
return bool(_fetch(config, entity_type, entity_id))
|
|
288
|
+
return bool(_fetch(config, entity_type, entity_id, account))
|
|
229
289
|
except RedditApiError as e:
|
|
230
290
|
if getattr(e, "status", None) == 404:
|
|
231
291
|
return False
|
|
@@ -283,7 +343,7 @@ def _draft_status(
|
|
|
283
343
|
if errors:
|
|
284
344
|
return _validation_error(errors)
|
|
285
345
|
|
|
286
|
-
current = _fetch(config, entity_type, entity_id)
|
|
346
|
+
current = _fetch(config, entity_type, entity_id, account)
|
|
287
347
|
if not current:
|
|
288
348
|
return {"error": f"Reddit {entity_type} '{entity_id}' was not found."}
|
|
289
349
|
warnings: list[str] = []
|
|
@@ -390,7 +450,7 @@ def update_reddit_campaign(
|
|
|
390
450
|
if errors:
|
|
391
451
|
return _validation_error(errors)
|
|
392
452
|
|
|
393
|
-
current = _fetch(config, "campaign", campaign_id)
|
|
453
|
+
current = _fetch(config, "campaign", campaign_id, account)
|
|
394
454
|
if not current:
|
|
395
455
|
return {"error": f"Reddit campaign '{campaign_id}' was not found."}
|
|
396
456
|
meta = account_meta(config, account)
|
|
@@ -415,6 +475,12 @@ def update_reddit_campaign(
|
|
|
415
475
|
errors.append("Bid settings live on the ad groups of a non-CBO campaign.")
|
|
416
476
|
if errors:
|
|
417
477
|
return _validation_error(errors)
|
|
478
|
+
if goal_value is None:
|
|
479
|
+
daily_equivalent = _rescheduled_equivalent(current, start_time, end_time)
|
|
480
|
+
try:
|
|
481
|
+
_budget_cap(daily_equivalent, config, meta["currency"])
|
|
482
|
+
except SafetyViolation as e:
|
|
483
|
+
return {"error": f"{e} The new schedule spreads the lifetime budget over fewer days."}
|
|
418
484
|
|
|
419
485
|
patch: dict[str, Any] = {}
|
|
420
486
|
display: dict[str, Any] = {}
|
|
@@ -792,7 +858,7 @@ def update_reddit_ad_group(
|
|
|
792
858
|
if errors:
|
|
793
859
|
return _validation_error(errors)
|
|
794
860
|
|
|
795
|
-
current = _fetch(config, "ad_group", ad_group_id)
|
|
861
|
+
current = _fetch(config, "ad_group", ad_group_id, account)
|
|
796
862
|
if not current:
|
|
797
863
|
return {"error": f"Reddit ad group '{ad_group_id}' was not found."}
|
|
798
864
|
meta = account_meta(config, account)
|
|
@@ -815,6 +881,12 @@ def update_reddit_ad_group(
|
|
|
815
881
|
)
|
|
816
882
|
if errors:
|
|
817
883
|
return _validation_error(errors)
|
|
884
|
+
if goal_value is None:
|
|
885
|
+
daily_equivalent = _rescheduled_equivalent(current, start_time, end_time)
|
|
886
|
+
try:
|
|
887
|
+
_budget_cap(daily_equivalent, config, meta["currency"])
|
|
888
|
+
except SafetyViolation as e:
|
|
889
|
+
return {"error": f"{e} The new schedule spreads the lifetime budget over fewer days."}
|
|
818
890
|
|
|
819
891
|
patch: dict[str, Any] = {}
|
|
820
892
|
display: dict[str, Any] = {}
|
|
@@ -949,7 +1021,7 @@ def update_reddit_ad(
|
|
|
949
1021
|
if errors:
|
|
950
1022
|
return _validation_error(errors)
|
|
951
1023
|
|
|
952
|
-
current = _fetch(config, "ad", ad_id)
|
|
1024
|
+
current = _fetch(config, "ad", ad_id, account)
|
|
953
1025
|
if not current:
|
|
954
1026
|
return {"error": f"Reddit ad '{ad_id}' was not found."}
|
|
955
1027
|
if click_url and current.get("post_id"):
|
|
@@ -1299,7 +1371,7 @@ def draft_reddit_ad_group(
|
|
|
1299
1371
|
if errors:
|
|
1300
1372
|
return _validation_error(errors)
|
|
1301
1373
|
|
|
1302
|
-
campaign = _fetch(config, "campaign", campaign_id)
|
|
1374
|
+
campaign = _fetch(config, "campaign", campaign_id, account)
|
|
1303
1375
|
if not campaign:
|
|
1304
1376
|
return {"error": f"Reddit campaign '{campaign_id}' was not found."}
|
|
1305
1377
|
is_cbo = bool(campaign.get("is_campaign_budget_optimization"))
|
|
@@ -1555,7 +1627,7 @@ def _draft_ad_from_post(
|
|
|
1555
1627
|
if errors:
|
|
1556
1628
|
return _validation_error(errors)
|
|
1557
1629
|
|
|
1558
|
-
ad_group = _fetch(config, "ad_group", ad_group_id)
|
|
1630
|
+
ad_group = _fetch(config, "ad_group", ad_group_id, account)
|
|
1559
1631
|
if not ad_group:
|
|
1560
1632
|
return {"error": f"Reddit ad group '{ad_group_id}' was not found."}
|
|
1561
1633
|
|
|
@@ -1866,7 +1938,7 @@ def draft_reddit_ad(
|
|
|
1866
1938
|
if errors:
|
|
1867
1939
|
return _validation_error(errors)
|
|
1868
1940
|
|
|
1869
|
-
ad_group = _fetch(config, "ad_group", ad_group_id)
|
|
1941
|
+
ad_group = _fetch(config, "ad_group", ad_group_id, account)
|
|
1870
1942
|
if not ad_group:
|
|
1871
1943
|
return {"error": f"Reddit ad group '{ad_group_id}' was not found."}
|
|
1872
1944
|
|
|
@@ -2060,7 +2132,7 @@ def draft_reddit_custom_audience(
|
|
|
2060
2132
|
f"(known: {', '.join(sorted(pixels)) or 'none'})"
|
|
2061
2133
|
])
|
|
2062
2134
|
else:
|
|
2063
|
-
missing = [c for c in payload["engagement_audience_config"]["campaign_ids"] if not _exists(config, "campaign", c)]
|
|
2135
|
+
missing = [c for c in payload["engagement_audience_config"]["campaign_ids"] if not _exists(config, "campaign", c, account)]
|
|
2064
2136
|
if missing:
|
|
2065
2137
|
return _validation_error([f"campaigns not found in this account: {', '.join(missing)}"])
|
|
2066
2138
|
|
|
@@ -2085,6 +2157,13 @@ def draft_reddit_custom_audience(
|
|
|
2085
2157
|
# ---------------------------------------------------------------------------
|
|
2086
2158
|
|
|
2087
2159
|
|
|
2160
|
+
# Plans that change an existing campaign, ad group or ad by its id.
|
|
2161
|
+
_ENTITY_OPERATIONS = frozenset({
|
|
2162
|
+
"reddit_set_status", "reddit_archive_entity", "reddit_update_ad",
|
|
2163
|
+
"reddit_update_campaign", "reddit_update_ad_group",
|
|
2164
|
+
})
|
|
2165
|
+
|
|
2166
|
+
|
|
2088
2167
|
def is_reddit_plan(plan: ChangePlan) -> bool:
|
|
2089
2168
|
return str(plan.operation or "").startswith(OPERATION_PREFIX)
|
|
2090
2169
|
|
|
@@ -2105,20 +2184,20 @@ def preflight(config: AdLoopConfig, plan: ChangePlan) -> dict:
|
|
|
2105
2184
|
checks: dict[str, Any] = {"ad_account_id": account, "currency": meta["currency"]}
|
|
2106
2185
|
|
|
2107
2186
|
if plan.operation in ("reddit_set_status", "reddit_archive_entity"):
|
|
2108
|
-
current = _fetch(config, plan.entity_type, plan.entity_id)
|
|
2187
|
+
current = _fetch(config, plan.entity_type, plan.entity_id, account)
|
|
2109
2188
|
if not current:
|
|
2110
2189
|
raise ValueError(f"{plan.entity_type} '{plan.entity_id}' no longer exists.")
|
|
2111
2190
|
checks["entity"] = current.get("name")
|
|
2112
2191
|
checks["configured_status_now"] = current.get("configured_status")
|
|
2113
2192
|
checks["already_in_target_status"] = current.get("configured_status") == changes.get("target_status")
|
|
2114
2193
|
elif plan.operation == "reddit_update_ad":
|
|
2115
|
-
current = _fetch(config, "ad", plan.entity_id)
|
|
2194
|
+
current = _fetch(config, "ad", plan.entity_id, account)
|
|
2116
2195
|
if not current:
|
|
2117
2196
|
raise ValueError(f"ad '{plan.entity_id}' no longer exists.")
|
|
2118
2197
|
checks["entity"] = current.get("name")
|
|
2119
2198
|
checks["post_id"] = current.get("post_id")
|
|
2120
2199
|
elif plan.operation in ("reddit_update_campaign", "reddit_update_ad_group"):
|
|
2121
|
-
current = _fetch(config, plan.entity_type, plan.entity_id)
|
|
2200
|
+
current = _fetch(config, plan.entity_type, plan.entity_id, account)
|
|
2122
2201
|
if not current:
|
|
2123
2202
|
raise ValueError(f"{plan.entity_type} '{plan.entity_id}' no longer exists.")
|
|
2124
2203
|
checks["entity"] = current.get("name")
|
|
@@ -2141,7 +2220,7 @@ def preflight(config: AdLoopConfig, plan: ChangePlan) -> dict:
|
|
|
2141
2220
|
checks["funding_instrument_id"] = (changes.get("payload") or {}).get("funding_instrument_id")
|
|
2142
2221
|
elif plan.operation == "reddit_create_ad_group":
|
|
2143
2222
|
campaign_id = (changes.get("payload") or {}).get("campaign_id", "")
|
|
2144
|
-
campaign = _fetch(config, "campaign", campaign_id)
|
|
2223
|
+
campaign = _fetch(config, "campaign", campaign_id, account)
|
|
2145
2224
|
if not campaign:
|
|
2146
2225
|
raise ValueError(f"campaign '{campaign_id}' no longer exists.")
|
|
2147
2226
|
checks["campaign"] = campaign.get("name")
|
|
@@ -2150,7 +2229,7 @@ def preflight(config: AdLoopConfig, plan: ChangePlan) -> dict:
|
|
|
2150
2229
|
checks["budget_cap_ok"] = True
|
|
2151
2230
|
elif plan.operation == "reddit_create_ad":
|
|
2152
2231
|
ad_group_id = (changes.get("ad") or {}).get("ad_group_id", "")
|
|
2153
|
-
group = _fetch(config, "ad_group", ad_group_id)
|
|
2232
|
+
group = _fetch(config, "ad_group", ad_group_id, account)
|
|
2154
2233
|
if not group:
|
|
2155
2234
|
raise ValueError(f"ad group '{ad_group_id}' no longer exists.")
|
|
2156
2235
|
checks["ad_group"] = group.get("name")
|
|
@@ -2169,7 +2248,7 @@ def preflight(config: AdLoopConfig, plan: ChangePlan) -> dict:
|
|
|
2169
2248
|
raise ValueError(f"pixel '{pixel_ids[0]}' is no longer available to this ad account.")
|
|
2170
2249
|
checks["pixel_id"] = pixel_ids[0]
|
|
2171
2250
|
for campaign_id in (payload.get("engagement_audience_config") or {}).get("campaign_ids") or []:
|
|
2172
|
-
if not _exists(config, "campaign", campaign_id):
|
|
2251
|
+
if not _exists(config, "campaign", campaign_id, account):
|
|
2173
2252
|
raise ValueError(f"campaign '{campaign_id}' no longer exists.")
|
|
2174
2253
|
checks["audience_name"] = payload.get("name")
|
|
2175
2254
|
else:
|
|
@@ -2189,6 +2268,13 @@ def apply_plan(config: AdLoopConfig, plan: ChangePlan) -> dict:
|
|
|
2189
2268
|
account = changes.get("ad_account_id") or plan.customer_id
|
|
2190
2269
|
op = plan.operation
|
|
2191
2270
|
|
|
2271
|
+
if op in _ENTITY_OPERATIONS and not _fetch(config, plan.entity_type, plan.entity_id, account):
|
|
2272
|
+
# The dry run checked this too, but it is the apply that changes
|
|
2273
|
+
# something, and Reddit would take the id whatever account it is in.
|
|
2274
|
+
raise ValueError(
|
|
2275
|
+
f"{plan.entity_type} '{plan.entity_id}' is not in Reddit ad account {account}; nothing was changed."
|
|
2276
|
+
)
|
|
2277
|
+
|
|
2192
2278
|
if op in ("reddit_set_status", "reddit_archive_entity"):
|
|
2193
2279
|
collection = _COLLECTIONS[plan.entity_type]
|
|
2194
2280
|
result = reddit_patch(
|
|
@@ -283,7 +283,7 @@ Microsoft Advertising (Bing, Yahoo, AOL, DuckDuckGo search ads) through the REST
|
|
|
283
283
|
| `update_microsoft_campaign_budget` | Draft a new daily budget (own budget only; capped by `max_daily_budget`) | `campaign_id`, `daily_budget` |
|
|
284
284
|
| `pause_microsoft_entity` / `enable_microsoft_entity` | Draft pausing or enabling a campaign, ad group, keyword or ad | `entity_type`, `entity_id`, `parent_id` (optional) |
|
|
285
285
|
| `search_microsoft_locations` | Location ids (country, state, metro, city, postal code) by name; Microsoft's file is cached | `query`, `location_type` |
|
|
286
|
-
| `update_microsoft_campaign` | Name, bid strategy (
|
|
286
|
+
| `update_microsoft_campaign` | Name, bid strategy (EnhancedCpc, MaxClicks, MaxConversions + target CPA; Microsoft retired manual CPC for search and stores it as EnhancedCpc), tracking template, final URL suffix | `campaign_id` + fields |
|
|
287
287
|
| `update_microsoft_campaign_targeting` | Included / excluded locations (full lists), location intent, ad schedule (replaces; account time zone, or the searcher's when the campaign uses AdScheduleUseSearcherTimeZone: the preview's `hours_in` says which) | `campaign_id`, `location_ids`, `excluded_location_ids`, `location_intent`, `ad_schedule` |
|
|
288
288
|
| `draft_microsoft_campaign` | New Search campaign, paused, with budget, bidding, languages, locations, intent | `name`, `daily_budget`, `languages`, `location_ids` |
|
|
289
289
|
| `draft_microsoft_ad_group` / `update_microsoft_ad_group` | New ad group (with keywords) / name, default bid, network, Audience Network adjustment | `campaign_id` or `ad_group_id` + fields |
|
|
@@ -11,7 +11,7 @@ from fastmcp import FastMCP
|
|
|
11
11
|
from fastmcp.server.middleware import Middleware
|
|
12
12
|
from fastmcp.utilities.docstring_parsing import parse_docstring
|
|
13
13
|
from mcp.types import Icon, ToolAnnotations
|
|
14
|
-
from pydantic import BeforeValidator
|
|
14
|
+
from pydantic import BeforeValidator, TypeAdapter
|
|
15
15
|
|
|
16
16
|
from adloop import diagnostics
|
|
17
17
|
from adloop.runtime import current_config
|
|
@@ -308,11 +308,27 @@ class _AppOnlyToolsForCardClients(Middleware):
|
|
|
308
308
|
mcp.add_middleware(_AppOnlyToolsForCardClients())
|
|
309
309
|
|
|
310
310
|
|
|
311
|
+
_DRY_RUN_FLAG = TypeAdapter(bool)
|
|
312
|
+
|
|
313
|
+
|
|
311
314
|
def _wants_real_apply(arguments: dict | None) -> bool:
|
|
312
|
-
|
|
315
|
+
"""Whether a confirm_and_apply call asks for a real apply.
|
|
316
|
+
|
|
317
|
+
Only a clear dry run skips the dialog. The tool reads ``dry_run`` with
|
|
318
|
+
pydantic's lax booleans, which take more spellings of "no" than any
|
|
319
|
+
list kept here ("off", "f", "n", ...), so the value is read the same
|
|
320
|
+
way, and anything that is not a plain yes counts as a real apply.
|
|
321
|
+
"""
|
|
322
|
+
args = arguments or {}
|
|
323
|
+
if "dry_run" not in args:
|
|
324
|
+
return False # the tool's default is a dry run
|
|
325
|
+
value = args["dry_run"]
|
|
313
326
|
if isinstance(value, str):
|
|
314
|
-
|
|
315
|
-
|
|
327
|
+
value = value.strip()
|
|
328
|
+
try:
|
|
329
|
+
return _DRY_RUN_FLAG.validate_python(value) is not True
|
|
330
|
+
except ValueError:
|
|
331
|
+
return True
|
|
316
332
|
|
|
317
333
|
|
|
318
334
|
class _ConfirmationDialog(Middleware):
|
|
@@ -7684,9 +7700,10 @@ def update_microsoft_campaign(
|
|
|
7684
7700
|
) -> dict:
|
|
7685
7701
|
"""Draft Microsoft Advertising campaign setting changes: name, bid strategy and targets, tracking template, final URL suffix.
|
|
7686
7702
|
|
|
7687
|
-
Bid strategies:
|
|
7688
|
-
|
|
7689
|
-
|
|
7703
|
+
Bid strategies: EnhancedCpc, MaxClicks, MaxConversions (with an optional
|
|
7704
|
+
target_cpa; TargetCpa is accepted and becomes MaxConversions, as Microsoft
|
|
7705
|
+
now does). Microsoft retired manual CPC for search campaigns and stores it
|
|
7706
|
+
as EnhancedCpc, so ManualCpc is drafted as EnhancedCpc with a note. Moving to a strategy that is not conversion-based
|
|
7690
7707
|
(manual bids or MaxClicks) is refused while the campaign has active
|
|
7691
7708
|
broad-match keywords, and moving to MaxConversions is refused while the
|
|
7692
7709
|
account has no active conversion goal. The daily budget has its own
|
|
@@ -7698,7 +7715,7 @@ def update_microsoft_campaign(
|
|
|
7698
7715
|
ad_account_id: Microsoft ad account id. Empty uses the config default.
|
|
7699
7716
|
campaign_id: Campaign id.
|
|
7700
7717
|
name: New name (at most 128 characters, unique in the account).
|
|
7701
|
-
bidding_strategy:
|
|
7718
|
+
bidding_strategy: EnhancedCpc, MaxClicks or MaxConversions (ManualCpc becomes EnhancedCpc).
|
|
7702
7719
|
target_cpa: Target cost per conversion for MaxConversions.
|
|
7703
7720
|
max_cpc: Maximum CPC for MaxClicks / MaxConversions (only changes an existing cap).
|
|
7704
7721
|
tracking_url_template: New template ({lpurl}...); "" removes it.
|
|
@@ -7769,7 +7786,8 @@ def draft_microsoft_campaign(
|
|
|
7769
7786
|
included locations (languages=["All"] is refused). Defaults to
|
|
7770
7787
|
MaxConversions and PeopleIn; MaxConversions is refused while no active
|
|
7771
7788
|
conversion goal counts toward Conversions, because Microsoft refuses the
|
|
7772
|
-
campaign then (pass MaxClicks
|
|
7789
|
+
campaign then (pass MaxClicks or EnhancedCpc; Microsoft stores manual CPC as
|
|
7790
|
+
EnhancedCpc, so ManualCpc is drafted as EnhancedCpc). The apply creates the
|
|
7773
7791
|
campaign, then its location criteria, and names what was created if the
|
|
7774
7792
|
second step fails. The returned plan_id is applied with confirm_and_apply.
|
|
7775
7793
|
|
|
@@ -7777,7 +7795,7 @@ def draft_microsoft_campaign(
|
|
|
7777
7795
|
ad_account_id: Microsoft ad account id. Empty uses the config default.
|
|
7778
7796
|
name: Campaign name (unique, at most 128 characters).
|
|
7779
7797
|
daily_budget: Daily budget in the account currency.
|
|
7780
|
-
bidding_strategy: MaxConversions (default), MaxClicks
|
|
7798
|
+
bidding_strategy: MaxConversions (default), MaxClicks or EnhancedCpc (ManualCpc becomes EnhancedCpc).
|
|
7781
7799
|
target_cpa: Optional target cost per conversion.
|
|
7782
7800
|
languages: Ad languages, e.g. ["English"].
|
|
7783
7801
|
location_ids: Included location ids (search_microsoft_locations).
|
|
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
|
|
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
|
|
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
|
|
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
|
|
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
|