AndVla AndVla - 1 year ago 79
PHP Question

Facebook Marketing API ProductAudience doesn't save some values

I'm facing the following:

  1. I create a pixel (use it in project: ViewProduct, AddToBasket & Purchase)

  2. Create a Catalog + create products feed + create product sets

  3. Implement ProductAudience to create Audience on Facebook.

Everything until then works just great, but when I go to Facebook, and navigate to the product audience, It doesn't have any data that I've set while creating the Audience. (eg. PIXEL_ID, PRODUCT_SET_ID, INCLUSIONS & EXCLUSIONS are not set)

That's how my code look like:

use FacebookAds\Object\ProductAudience;
use FacebookAds\Object\Fields\ProductAudienceFields;

$productAudience = new ProductAudience($this->id, <myFacebookAdAccountId>);
$audienceData = [
ProductAudienceFields::PIXEL_ID => <myPixelId>,
ProductAudienceFields::PRODUCT_SET_ID => <myProductSetId>,
ProductAudienceFields::NAME => 'Name for the audience',
if (count($this->inclusions)) $audienceData[ProductAudienceFields::INCLUSIONS] = $this->inclusions;
if (count($this->exclusions)) $audienceData[ProductAudienceFields::EXCLUSIONS] = $this->exclusions;
return $productAudience->create();

So I'm wondering, what am I doing wrong? Hope to get some guidelines... Have lost already one working day on trying to fix it without any result.

I also tried to retrieve the data trough API:

$productAudience = new ProductAudience($this->id);
return $productAudience->read([

The following object is been returned:

#data: array:7 [
"id" => "6047126484024"
"name" => "Name for the audience"
"description" => null
"product_set_id" => null
"pixel_id" => null
"inclusions" => null
"exclusions" => null

The values are set to null as well... Version of API I'm using is 2.5.0

Answer Source

found the solution for my problem

$productAudience = new ProductAudience($this->id);

It doesn't set the values, so the product_set_id and few other fields ares still set to null, but we get all the fields in 'creation_params' object.

I also created a helper function that sets the needed values:

private function fieldsFromDataSourceToObject(ProductAudience &$productAudience)
    $creationParams = json_decode($productAudience->data_source['creation_params']);
        ProductAudienceFields::PRODUCT_SET_ID => $creationParams->product_set_id,
        ProductAudienceFields::INCLUSIONS => json_decode($creationParams->inclusions, true),
        ProductAudienceFields::EXCLUSIONS => json_decode($creationParams->exclusions, true),

So after reading existing product audience, I just call the helper function with response as param


Hope this solution saves someones time.