How to add user to facebook custom audience

2020-04-16 19:13发布

问题:

I am trying to add user to custom audience using following code

$audience = new CustomAudience($custom_audience_id);
$audience->addUsers(array(trim($mailAddress)), CustomAudienceTypes::EMAIL);

and

$users = array(
 array('fname', 'lname', 'someone@example.com'),
 array('fnamenew', 'lnamenew', 'someone_new@example.com'),
);

$schema = array(
  CustomAudienceMultikeySchemaFields::FIRST_NAME,
  CustomAudienceMultikeySchemaFields::LAST_NAME,
  CustomAudienceMultikeySchemaFields::EMAIL,
);

$audience = new CustomAudienceMultiKey(<CUSTOM_AUDIENCE_ID>);

$audience->addUsers($users, $schema);

But I am getting error in both codes

(#2650) Failed to update the custom audience: This audience was created from data source EVENT_BASED.WEB_PIXEL_HITS, which does not support data source FILE_IMPORTED.HASHES_OR_USER_IDS

Following code I am using to create custom audience.

$audience = new CustomAudience(null, $accountId);
        $audience->setData(array(
          CustomAudienceFields::NAME            => $associative_arr['name'],
          CustomAudienceFields::PIXEL_ID        => $associative_arr['pixelId'],
          CustomAudienceFields::DESCRIPTION     => $associative_arr['description'],
          CustomAudienceFields::RETENTION_DAYS  => $associative_arr['retensionDays'],  
          CustomAudienceFields::SUBTYPE         => CustomAudienceSubtypes::WEBSITE,
          CustomAudienceFields::RULE            => array('url' => array('i_contains' => $associative_arr['websiteUrl'])),
          CustomAudienceFields::PREFILL         => $associative_arr['prefill'],
          CustomAudienceFields::DATA_SOURCE     => array('EVENT_BASED' => 'WEB_PIXEL_HITS')
        ));         
        $audience->create();

Any suggestion how I can solve this?

回答1:

You can only add user to the custom audience that is created through emails, While the code you shown in creating audience is based on user activities on your website. You can follow this doc to create your custom audience through email: https://developers.facebook.com/docs/marketing-api/audiences-api#build

And then in your ads, you can combine the two audience (From emails and/ from