Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -364,6 +364,7 @@ For advanced users who need to self-host, the package can be installed from sour
- `time_range`: Time range for insights (default: maximum)
- `breakdown`: Optional breakdown dimension (e.g., age, gender, country)
- `level`: Level of aggregation (ad, adset, campaign, account)
- `action_attribution_windows` (optional): List of attribution windows for conversion data (e.g., ["1d_click", "1d_view", "7d_click", "7d_view"]). When specified, actions and cost_per_action_type include additional fields for each window. The 'value' field always shows 7d_click attribution.
- Returns: Performance metrics for the specified object

21. `mcp_meta_ads_get_login_link`
Expand Down
17 changes: 12 additions & 5 deletions meta_ads_mcp/core/insights.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
"""Insights and Reporting functionality for Meta Ads API."""

import json
from typing import Optional, Union, Dict
from typing import Optional, Union, Dict, List
from .api import meta_api_tool, make_api_request
from .utils import download_image, try_multiple_download_methods, ad_creative_images, create_resource_from_image
from .server import mcp_server
Expand All @@ -11,9 +11,10 @@

@mcp_server.tool()
@meta_api_tool
async def get_insights(object_id: str, access_token: Optional[str] = None,
time_range: Union[str, Dict[str, str]] = "maximum", breakdown: str = "",
level: str = "ad", limit: int = 25, after: str = "") -> str:
async def get_insights(object_id: str, access_token: Optional[str] = None,
time_range: Union[str, Dict[str, str]] = "maximum", breakdown: str = "",
level: str = "ad", limit: int = 25, after: str = "",
action_attribution_windows: Optional[List[str]] = None) -> str:
"""
Get performance insights for a campaign, ad set, ad or account.

Expand Down Expand Up @@ -51,6 +52,8 @@ async def get_insights(object_id: str, access_token: Optional[str] = None,
level: Level of aggregation (ad, adset, campaign, account)
limit: Maximum number of results to return per page (default: 25, Meta API allows much higher values)
after: Pagination cursor to get the next set of results. Use the 'after' cursor from previous response's paging.next field.
action_attribution_windows: Optional list of attribution windows (e.g., ["1d_click", "7d_click", "1d_view"]).
When specified, actions include additional fields for each window. The 'value' field always shows 7d_click.
"""
if not object_id:
return json.dumps({"error": "No object ID provided"}, indent=2)
Expand Down Expand Up @@ -78,7 +81,11 @@ async def get_insights(object_id: str, access_token: Optional[str] = None,

if after:
params["after"] = after


if action_attribution_windows:
# Meta API expects single-quote format: ['1d_click','7d_click']
params["action_attribution_windows"] = "[" + ",".join(f"'{w}'" for w in action_attribution_windows) + "]"

data = await make_api_request(endpoint, access_token, params)

return json.dumps(data, indent=2)
Expand Down