Search code examples
c#jsonfacebookfacebook-graph-apifacebook-custom-audience

How to filter Facebook custom audience GET request by data_source type and subtype via filtering parameter


I am trying to filter Facebook custom audience that we get via this endpoint:

var url = $"{advertiserId}/customaudiences?limit={limit}&fields=name" 
                  + "&filtering=[{'field':'subtype','operator':'EQUAL', 'value':'CUSTOM'}]";

The above call works and returns the users custom audience. However, I need to additionally filter custom audiences by data_source.type and data-source.subtype, i.e.

filtering=[...{'field':'data_source.type','operator':'EQUAL', 'value':'FILE_IMPORTED'}]";

I have read through the documentation and cannot see where I am going wrong.

I get an

Invalid parameter

error when trying to filter by data_source.type

Here is the documentation I've looked at:

https://developers.facebook.com/docs/marketing-api/reference/ad-account/customaudiences/

https://developers.facebook.com/docs/marketing-api/reference/custom-audience/#parameters-2


Solution

  • I managed to fix this by adding data_souce to my request URL and the filtering on data_source using a list I created and stored in the config:

    public FacebookEntitiesResult<FacebookAudience> GetFacebookAudiences(string advertiserId, string token, int? limit, PagingCursors cursors)
        {
            if (!limit.HasValue)
                limit = ConfigValues.FacebookGraphResultLimit;
    
            var url = $"{advertiserId}/customaudiences?limit={limit}&fields=name,data_source"
                      + "&filtering=[{'field':'subtype','operator':'EQUAL', 'value':'CUSTOM'}]";
    
            var customAudienceTypes = Instance.Of<IConfigHelper>().GetStringArrayOfAppSettings(ConfigKeys.Extensions.FacebookAudienceUnsupportedTypeArray, ",");
    
            var customAudiences = GetResult<FacebookEntitiesResult<FacebookAudience>>(url, token, cursors);
            customAudiences.Data = customAudiences.Data.Where(a => !customAudienceTypes.Contains(a.Data_Source.Type)).ToArray();
    
            return customAudiences;
        }
    

    Config:

    <key name="FacebookAudienceUnsupportedTypeArray" defaultValue="THIRD_PARTY_IMPORTED"></key>