xdevplatform / twitter-python-ads-sdk

A Twitter supported and maintained Ads API SDK for Python.
https://twitterdev.github.io/twitter-python-ads-sdk/
MIT License
189 stars 106 forks source link

How to access Reach endpoint? #305

Closed brunopini closed 7 months ago

brunopini commented 7 months ago

I saw #156 was closed with what I believe is not a valid solution to the question years ago, and I'm still facing the same challenge.

The original answer comments on Reach Estimates, associated with specific audiences, which is not the same as accessing reach stats on specific campaigns and funding instruments: https://developer.twitter.com/en/docs/twitter-ads-api/analytics/api-reference/reach

I couldn't find any class here that would mimic the behavior of the endpoints above.

Does it exist?

brunopini commented 7 months ago

I realized after opening the issue that this repo is no longer being maintained - bummer, seems like we are on our own when developing Ads API connectors for now... I used a workaround with a custom class, calling the Request class, and passing the reach endpoint URL as the resource param, which look like:

Before passing to the class, you will need to include the API version in the beginning, which I'm referencing from twitter_ads import API_VERSION.

from twitter_ads import API_VERSION
from twitter_ads.client import Client
from twitter_ads.http import Request

creds = {}  # Your auth credentials
account_id = ...  # Your account id
params = {}  # Request params

resource_base = 'stats/accounts/{account_id}/reach/campaigns'  # or 'stats/accounts/{account_id}/reach/funding_instruments'

resource = '/' + '/'.join([
            API_VERSION,
            resource_base.format(account_id=account.id)])

request = Request(Client(**creds), method='get', resource=resource, params=params)
try:
  response = request.perform()
except RateLimit:
  ...

One last note is the more frequent RateLimit errors, which you will need to deal with, since calls to this endpoint will only run on one entity_id per request, and limits are reached earlier than with the batch async analytics endpoints.