I'm using the Graph API 17.0 to try and create a custom audience, but I can't even get a basic test JSON working without getting "Invalid rule JSON format" back. I've tried all kinds of different permutations, adding quotes, adding "optional" fields, removing optional fields, changing operators between "or" and "and", converting the pixelIds to strings...nothing works and there is no explanation in the error for what the issue actually is. The only thing I have established is that not stringifying the rule creates an error "this field must be a string", so that isn't the problem.
This is the documentation, but judging by other issues people have with it, it isn't very reliable.
Here's a copy of the code. I have both the rule that I actually want to add and a simplified test rule, but neither work.
// Build rule: include users triggering either "SilaBlock" or "SilaBlock-<pixelId>".
const rule = {
'inclusions': {
'operator': 'or',
'rules': [
{
'event_sources': [{ 'id': pixelId, 'type': 'pixel' }],
'retention_seconds': 15552000, // 180 days in seconds
'filter': {
'operator': 'or',
'filters': [
{ 'field': 'event', 'operator': 'eq', 'value': 'SilaBlock' }
]
},
'aggregation': { 'operator': 'count', 'value': 1 }
},
{
'event_sources': [{ 'id': pixelId, 'type': 'pixel' }],
'retention_seconds': 15552000,
'filter': {
'operator': 'or',
'filters': [
{ 'field': 'event', 'operator': 'eq', 'value': `SilaBlock-${pixelId}` }
]
},
'aggregation': { 'operator': 'count', 'value': 1 }
}
]
},
'exclusions': {
'operator': 'or',
'rules': []
}
};
const testRule = {
"inclusions": {
"operator": "or",
"rules": [
{
"event_sources": [
{
"type": "pixel",
"id": pixelId
}
],
"retention_seconds": 600
}
]
}
}
const params = {
name: audienceName,
rule: JSON.stringify(testRule),
retention_days: 180,
access_token: accessToken,
pixel_id: pixelId,
};
try {
const createResponse = await axios.post(
`.0/${adAccount}/customaudiences`,
qs.stringify(params),
{
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
},
}
);
existingAudienceId = createResponse.data.id;
console.log(`Created audience "${audienceName}" with id: ${existingAudienceId}`);
} catch (err) {
console.error(
`Error creating audience for pixel ${pixelId}:`,
err.response ? err.response.data : err.message,
);
return { pixelId, error: err.message };
}