定位描述

获取一组定位参数的直观易懂描述。如要读取特定 ads 的定位描述,请向 https://graph.facebook.com/{AD_ID}/targetingsentencelines 执行 HTTP GET 请求。

现有广告的定位描述

如要获取与现有广告相关的 targetingsentencelines,请执行以下请求:

use FacebookAds\Object\Ad;

$ad = new Ad(<AD_ID>);
$targeting_description = $ad->getTargetingDescription();

// Output targeting description
foreach ($targeting_description->targetingsentencelines as $description) {
  echo $description['content'].PHP_EOL;
  foreach ($description['children'] as $child) {
    echo "\t".$child.PHP_EOL;
  }
}
from facebookads.adobjects.ad import Ad

ad = Ad(<AD_ID>)
targeting_description = ad.get_targeting_sentence_lines().get_one()

# Output the targeting description
for description in targeting_description['targetingsentencelines']:
    print(description['content'])
    for child in description['children']:
        print("\t" + child)
curl -G \
  -d 'access_token=<ACCESS_TOKEN>' \
  https://graph.facebook.com/v2.5/<AD_ID>/targetingsentencelines

响应:

{
    "id": "<AD_ID>/targetingsentencelines",
    "targetingsentencelines": [
    {
        "content": "Location - Living In:",
        "children": [
            "Japan",
            "United States"
        ]
    },
    {
        "content": "Age:",
        "children": [
            "20 - 24"
        ]
    },
    {
        "content": "Gender:",
        "children": [
            "Male"
        ]
    }]
}

响应中包含以下字段:

名称 描述

id

类型:字符串

targetingsentencelines 的编号。

targetingsentencelines

类型:由 JSON 对象组成的数组

定位参数的直观易懂描述。每个对象都包含 content(即定位类型)以及 children(即定位参数)。此字段仅考虑有效版位

广告账户的定位描述

您还可以向 https://graph.facebook.com/{AD_ACCOUNT_ID}/targetingsentencelines 执行 HTTP GET 请求,从而获取广告账户某个定位参数的定位描述。

例如,如要获取居住在美国或日本且年龄在 20 至 24 岁之间的男性用户的定位描述,应执行以下请求:

use FacebookAds\Object\AdAccount;
use FacebookAds\Object\Fields\TargetingFields;
use FacebookAds\Object\Targeting;

$account = new AdAccount('act_<AD_ACCOUNT_ID>');
$targeting = new Targeting();
$targeting->setData(array(
  TargetingFields::GEO_LOCATIONS => array(
    'countries' => array('US', 'JP')
  ),
  TargetingFields::GENDERS => array(1),
  TargetingFields::AGE_MIN => 20,
  TargetingFields::AGE_MAX => 24,
));

$params = array(
  'targeting_spec' => $targeting->exportData(),
);

foreach ($account->getTargetingSentenceLines(array(), $params) as $description) {
  echo $description->{'content'}.PHP_EOL;
  foreach ($description->{'children'} as $child) {
    echo " - ".$child.PHP_EOL;
  }
}
from facebookads.adobjects.adaccount import AdAccount
from facebookads.adobjects.targeting import Targeting

account = AdAccount('act_<AD_ACCOUNT_ID>')
params = {
    'targeting_spec': {
        Targeting.Field.geo_locations: {
            Targeting.Field.countries: ['US', 'JP'],
        },
        Targeting.Field.genders: [1],
        Targeting.Field.age_min: 20,
        Targeting.Field.age_max: 24,
    },
}

targeting_description = account.get_targeting_sentence_lines(params=params) \
    .get_one()

# Output the targeting description
for description in targeting_description['targetingsentencelines']:
    print(description['content'])
    for child in description['children']:
        print("\t" + child)
curl -G \
  --data-urlencode 'targeting_spec={ 
    "age_max": 24, 
    "age_min": 20, 
    "genders": [1], 
    "geo_locations": {"countries":["US","JP"]} 
  }' \
  -d 'access_token=<ACCESS_TOKEN>' \
  https://graph.facebook.com/v2.11/act_<AD_ACCOUNT_ID>/targetingsentencelines

响应:

{
    "params": {
        "genders": [1],
        "age_min": 20,
        "age_max": 24,
        "geo_locations": {
            "countries": [
                "US",
                "JP"
            ]
        }
    },
    "targetingsentencelines": [{
        "content": "Location - Living In:",
        "children": [
            "Japan",
            "United States"
        ]
    }, {
        "content": "Age:",
        "children": [
            "20 - 24"
        ]
    }, {
        "content": "Gender:",
        "children": [
            "Male"
        ]
    }]
}

其他参数包括:

名称 描述

targeting_spec

类型:JSON 对象

必要。

获取此定位参数的定位描述。

hide_targeting_spec_from_return

类型:布尔值

可选。

响应中是否包含所请求的 targeting_spec。默认值为 false

响应中包含以下字段:

名称 描述

targetingsentencelines

类型:由 JSON 对象组成的数组

定位参数的直观易懂描述。每个对象都包含 content(即定位类型)以及 children(即定位参数)。

params

类型:JSON 对象

您提供的定位参数。

如果 Facebook 自动更正了您的参数,params 可能会与您提供的参数不同。例如,如果您请求的 params 值为 {'age_min':10},响应中的 params 将为 {'age_min':13},这是支持的最小值。