Hello my name is Vlad and I am a marketer with coding experience, I am proficient in Google Ads Scripts but I am having a hard time developing any scripts in Facebook Ads Manager (Scripts that use Facebook API). I am either using outdated libraries of python or I don't understand how to use the Facebook API. If any of you can help me, I would like to select 2 ads by ID and store the number of clicks that each ad has (using Today's data) and how to pause/enable an ad by ID. Thank you for much for your assistance and I am looking forward to your response.
These are 2 examples of code that I have written that do not work: Code 1:
import facebook
fb = facebook.GraphAPI(access_token='')
clicks_A = fb.get_object(id="", fields="clicks")['clicks']['value']
clicks_B = fb.get_object(id="", fields="clicks")['clicks']['value']
print(clicks_A)
print(clicks_B)
fb.put_object(id="", status="PAUSED")
fb.put_object(id="", status="ACTIVE")
Code 2:
import time
import facebook
from facebook_business.api import FacebookAdsApi
from facebook_business.adobjects.adaccount import AdAccount
from facebook_business.adobjects.adsinsights import AdsInsights
from facebook_business.adobjects.ad import Ad
FacebookAdsApi.init(access_token=TOKEN_DE_ACCES)
account = AdAccount(ACCOUNT_ID)
ads_insights_a = AdsInsights.get(
fields=[
AdsInsights.Field.ad_id,
AdsInsights.Field.clicks,
],
params={
'level': 'ad',
'time_range': {'since': 'today', 'until': 'today'},
'filtering': [{'field': 'ad_id', 'operator': 'EQUAL', 'value': AD_ID_A}]
}
)
ads_insights_b = AdsInsights.get(
fields=[
AdsInsights.Field.ad_id,
AdsInsights.Field.clicks,
],
params={
'level': 'ad',
'time_range': {'since': 'today', 'until': 'today'},
'filtering': [{'field': 'ad_id', 'operator': 'EQUAL', 'value': AD_ID_B}]
}
)
clicks_a = ads_insights_a[0]['clicks']
clicks_b = ads_insights_b[0]['clicks']
ad_a = Ad(AD_ID_A)
ad_a.update(params={'status': 'PAUSED'})
ad_b = Ad(AD_ID_B)
ad_b.update(params={'status': 'ACTIVE'})