You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何为Facebook Marketing API广告组查询添加日期过滤?

Filtering Facebook Ad Sets by Date Range via Marketing API

Absolutely! You can add date-based filters to your API request to narrow down the ad sets you’re fetching, which will drastically cut down on response time and reduce the number of pagination pages you need to handle. Here’s how to do it properly:

Key Filter Parameters to Use

The Facebook Marketing API supports several date-related fields to filter ad sets. The most practical ones are:

1. Filter by Creation Date

If you only care about ad sets created within a specific window, use the created_time field with operators like GREATER_THAN, LESS_THAN, or BETWEEN to define your range.

Example using the Python SDK:

from facebook_business.api import FacebookAdsApi
from facebook_business.adobjects.adaccount import AdAccount

FacebookAdsApi.init(access_token='YOUR_ACCESS_TOKEN')

ad_account = AdAccount('act_YOUR_AD_ACCOUNT_ID')
# Fetch ad sets created between Jan 1, 2024 and Feb 1, 2024
filtered_adsets = ad_account.get_adsets(
    fields=['id', 'name', 'created_time'],
    filtering=[
        {'field': 'created_time', 'operator': 'GREATER_THAN', 'value': '2024-01-01'},
        {'field': 'created_time', 'operator': 'LESS_THAN', 'value': '2024-02-01'}
    ]
)

2. Filter by Active/Run Date Range

If you want ad sets that were actually running during a specific period (regardless of creation date), use start_time and end_time. Note that some ad sets run indefinitely (no end date), so include a check for NULL values to avoid missing them:

filtered_adsets = ad_account.get_adsets(
    fields=['id', 'name', 'start_time', 'end_time'],
    filtering=[
        {'field': 'start_time', 'operator': 'LESS_THAN_OR_EQUAL', 'value': '2024-02-29'},
        # Include ad sets that either end after the start of your range or have no end date
        {'field': 'end_time', 'operator': 'GREATER_THAN_OR_EQUAL', 'value': '2024-02-01'},
        {'field': 'end_time', 'operator': 'EQUAL', 'value': None}
    ]
)

3. Combine with Additional Filters

To further trim results, pair date filters with status filters (like effective_status) to exclude ad sets that are deleted, archived, or permanently paused:

filtered_adsets = ad_account.get_adsets(
    fields=['id', 'name'],
    filtering=[
        {'field': 'created_time', 'operator': 'BETWEEN', 'value': ['2024-01-01', '2024-01-31']}
    ],
    params={
        'effective_status': ['ACTIVE', 'PAUSED']
    }
)

Important Notes

  • Date Format: Always use ISO 8601 format (e.g., YYYY-MM-DD or YYYY-MM-DDTHH:MM:SSZ) for date values.
  • API Version: Stick to a stable, up-to-date API version (v19.0 or newer at the time of writing) to avoid deprecated parameters.
  • Permissions: Ensure your access token has the ads_read permission for the target ad account.

If you prefer raw HTTP requests, here’s a curl example:

curl -G \
  -d "access_token=YOUR_ACCESS_TOKEN" \
  -d "fields=id,name,created_time" \
  -d "filtering=[{'field':'created_time','operator':'BETWEEN','value':['2024-01-01','2024-01-31']}]" \
  "https://graph.facebook.com/v19.0/act_YOUR_AD_ACCOUNT_ID/adsets"

This should significantly reduce the number of ad sets returned, making your API calls faster and pagination far more manageable.

内容的提问来源于stack exchange,提问作者James Portelli

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.26 09:12:19