Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Facebook Ads API error: must be a valid custom audience id

Unlike targeting a custom audience, it seems impossible to create an ad targeting a saved audience using Facebook Ads API. Can someone confirm that is the case? What would be a workaround? This is the error I'm getting:

Error #100: Param targeting[custom_audiences][id] must be a valid custom audience id

There is a difference between a saved audience and a custom audience. A saved audience is an audience you manually create specifying age range, geographic region and interests. While a custom audience is a list of customers you gathered (uploaded or visitors or your site etc). You can create these audiences manually through the Ads Manager interface:

enter image description here

Using Graph API Explorer, one can retrieve saved audiences as follow:

act_1234567890/saved_audiences

enter image description here

Using Graph API Explorer, one can retrieve custom audiences as follow:

act_123456789/customaudiences

enter image description here

Note that 123456789 is not my real account number. I changed it for security reasons.

So, I can retrieve IDs for both custom audiences and saved audiences and creating an ad targeting a custom audience works fine, unlike targeting a saved audience which gives the above error message.

A cumbersome workaround could be to save the flexible_spec of each saved audience locally and use that spec when creating ads. The problem with that is that some targeting segments become invalid (Facebook decides to discontinue some segments at random times) which causes Facebook Ads API to hickup. Additionally, this means I constantly need to keep saved audiences in sync with my local copy. Unless of course I retrieve the targeting of a saved audience on the fly and re-use it, each time I create an ad resulting in yet another API request.

like image 446
bart Avatar asked Oct 30 '22 05:10

bart


1 Answers

I figured it out myself. It seems impossible to directly target a saved audience unlike a custom audience, which is odd. What I did is retrieve the targeting from the saved audience first and reuse that targeting when creating the ad.

Here you see a screenshot of the official documentation showing only custom audiences and lookalike audiences can be targeted.

enter image description here

Retrieving targeting from saved audience (with Facebook PHP SDK):

$api = Api::instance();

use FacebookAds\Http\Request;

$response = $api->call(
  "/987654321", 'GET',
  array('fields'=>'targeting')
);
$audience = $response->getContent();

With 987654321 as the saved audience ID.

Then copy that targeting to the ad (with Facebook PHP SDK):

use FacebookAds\Object\Targeting;
use FacebookAds\Object\Fields\TargetingFields;

$targeting = new Targeting();
$targeting->setData($audience['targeting']);

The disadvantage of this technique is that the ad won't be updated when the saved audience is updated and that it requires an extra API call which slows down the creation of the ad.

like image 109
bart Avatar answered Jan 02 '23 19:01

bart