Search code examples
phpfacebookfacebook-ads-apifacebook-marketing-api

Create Facebook Custom Audience for website Traffic


I'm trying to create custom audience for web-traffic using the marketing API (Facebook Ads SDK 2.5).

This is what I tried.

$audience = new CustomAudience(null, 'act_'.$account_id);
$aud_data = array(
  CustomAudienceFields::NAME => $name,
  CustomAudienceFields::SUBTYPE => CustomAudienceSubtypes::CUSTOM,
  CustomAudienceFields::RULE => array('event' => array('i_contains' => 'ViewContent','i_contains' => $name ) ),
  CustomAudienceFields::PIXEL_ID => $pixelId,
  CustomAUdienceFields::DESCRIPTION => '',
  CustomAudienceFields::RETENTION_DAYS => 180,
  CustomAudienceFields::PREFILL => True
);

I'm getting a success message & I can see the audience on Ads Manager too. But, the problem is, when I try edit those audience, I'm getting this error.

Can't Edit Audience
This audience can't be edited because it was created using settings that are no longer available. You can still use this audience for your ads. To make changes or updates, create a new audience.

What am I doing wrong?


Solution

  • If anyone has the same question, here's the answer.

    The CustomAudienceFields::RULE should be a JSON string not an array (at least, that is what worked for me.)

    So, putting it together..

    $audience = new CustomAudience(null, 'act_'.$account_id);
    $aud_data = array(
      CustomAudienceFields::NAME => $name,
      CustomAudienceFields::SUBTYPE => CustomAudienceSubtypes::CUSTOM,
      CustomAudienceFields::RULE => '{"and": [{"event": {"i_contains": "ViewContent"}},{"content_name": {"i_contains": "'.$name.'"}}]}',
      CustomAudienceFields::PIXEL_ID => $pixelId,
      CustomAUdienceFields::DESCRIPTION => '',
      CustomAudienceFields::RETENTION_DAYS => 180,
      CustomAudienceFields::PREFILL => True
    );
    

    This should work.