|
1 | 1 | """Insights and Reporting functionality for Meta Ads API.""" |
2 | 2 |
|
3 | 3 | import json |
4 | | -from typing import Optional, Union, Dict |
| 4 | +from typing import Optional, Union, Dict, List |
5 | 5 | from .api import meta_api_tool, make_api_request |
6 | 6 | from .utils import download_image, try_multiple_download_methods, ad_creative_images, create_resource_from_image |
7 | 7 | from .server import mcp_server |
|
11 | 11 |
|
12 | 12 | @mcp_server.tool() |
13 | 13 | @meta_api_tool |
14 | | -async def get_insights(object_id: str, access_token: Optional[str] = None, |
15 | | - time_range: Union[str, Dict[str, str]] = "maximum", breakdown: str = "", |
16 | | - level: str = "ad", limit: int = 25, after: str = "") -> str: |
| 14 | +async def get_insights(object_id: str, access_token: Optional[str] = None, |
| 15 | + time_range: Union[str, Dict[str, str]] = "maximum", breakdown: str = "", |
| 16 | + level: str = "ad", limit: int = 25, after: str = "", |
| 17 | + action_attribution_windows: Optional[List[str]] = None) -> str: |
17 | 18 | """ |
18 | 19 | Get performance insights for a campaign, ad set, ad or account. |
19 | 20 | |
@@ -51,6 +52,8 @@ async def get_insights(object_id: str, access_token: Optional[str] = None, |
51 | 52 | level: Level of aggregation (ad, adset, campaign, account) |
52 | 53 | limit: Maximum number of results to return per page (default: 25, Meta API allows much higher values) |
53 | 54 | after: Pagination cursor to get the next set of results. Use the 'after' cursor from previous response's paging.next field. |
| 55 | + action_attribution_windows: Optional list of attribution windows (e.g., ["1d_click", "7d_click", "1d_view"]). |
| 56 | + When specified, actions include additional fields for each window. The 'value' field always shows 7d_click. |
54 | 57 | """ |
55 | 58 | if not object_id: |
56 | 59 | return json.dumps({"error": "No object ID provided"}, indent=2) |
@@ -78,7 +81,11 @@ async def get_insights(object_id: str, access_token: Optional[str] = None, |
78 | 81 |
|
79 | 82 | if after: |
80 | 83 | params["after"] = after |
81 | | - |
| 84 | + |
| 85 | + if action_attribution_windows: |
| 86 | + # Meta API expects single-quote format: ['1d_click','7d_click'] |
| 87 | + params["action_attribution_windows"] = "[" + ",".join(f"'{w}'" for w in action_attribution_windows) + "]" |
| 88 | + |
82 | 89 | data = await make_api_request(endpoint, access_token, params) |
83 | 90 |
|
84 | 91 | return json.dumps(data, indent=2) |
|
0 commit comments