受众群体名单定位

借助受众群体名单定位,您可以定位到一系列受众群体。所有定位到的受众群体都可以是不同类型的。

所有受众群体都使用类型为 TARGETING_TYPE_AUDIENCE_GROUP 的单个已分配的定位选项进行定位。

受众群体定位逻辑

已分配的受众群体定位选项包含一个受众群体定位详细信息对象。受众群体定位是详细信息对象的各个字段的汇总结果。此结果基于以下逻辑:

  • 每个受众群体组对象都是通过并集组合的受众群体列表。
  • includedFirstPartyAndPartnerAudienceGroups 字段包含受众群体组列表。它会按交集组合受众群体组。
  • 所有以“included”为前缀的字段都表示要定位的用户名单。这些结果通过 UNION 组合在一起。这会生成一个要纳入定位范围的用户列表。
  • 带有“excluded”前缀的所有字段都表示要从定位中排除的用户列表。这些内容通过 UNION 组合在一起。这样会生成一个要从定位条件中排除的用户列表。
  • 系统会定位所有包含的用户,但排除的用户除外。如果仅排除用户名单,则除被排除的用户之外的所有用户都将成为定位对象。

在实践中,这意味着两件事。首先,如果用户位于“排除”字段中的受众群体中,则不会被定位。其次,如果满足以下任一条件,系统就会定位到相应用户:

更新受众群体定位

受众群体名单定位使用分配的定位选项,其 ID 值为“audienceGroup”。如需设置受众群体定位,您需要:

  1. 检索分配了定位选项的所有现有受众群体名单。
  2. 构建新的已分配定位选项对象。如果存在现有定位条件,则此对象应基于现有定位条件,并包含所有所需的更改。
  3. 根据需要,删除任何现有的已分配定位选项。
  4. 创建新的分配的定位条件选项。

您可以使用 listbulkEditAssignedTargetingOptions 方法实现这一目的。

以下是更新订单项的受众群体定位的方法:

Java

// Provide the ID of the parent advertiser.
long advertiserId = advertiser-id;

// Provide the ID of the line item whose targeting will be updated.
long lineItemId = line-item-id;

// Provide a list of Google Audience IDs to add to line item targeting.
List<Long> addedGoogleAudienceIds = google-audience-ids-to-add;

// Build Google Audience targeting settings objects to add to audience
// targeting.
ArrayList<GoogleAudienceTargetingSetting> newGoogleAudienceSettings =
    new ArrayList<GoogleAudienceTargetingSetting>();

// Convert list of Google Audience IDs into list of settings.
for (Long googleAudienceId : addedGoogleAudienceIds) {
  newGoogleAudienceSettings.add(
      new GoogleAudienceTargetingSetting().setGoogleAudienceId(googleAudienceId));
}

// Create relevant bulk edit request objects.
BulkEditAssignedTargetingOptionsRequest requestContent =
    new BulkEditAssignedTargetingOptionsRequest();
requestContent.setLineItemIds(Arrays.asList(lineItemId));

AudienceGroupAssignedTargetingOptionDetails updatedAudienceGroupDetails;
ArrayList<DeleteAssignedTargetingOptionsRequest> audienceGroupDeleteRequests =
    new ArrayList<DeleteAssignedTargetingOptionsRequest>();

try {
  // Retrieve existing audience group targeting.
  AssignedTargetingOption existingAudienceGroupTargetingOption =
      service
          .advertisers()
          .lineItems()
          .targetingTypes()
          .assignedTargetingOptions()
          .get(advertiserId, lineItemId, "TARGETING_TYPE_AUDIENCE_GROUP", "audienceGroup")
          .execute();

  // Extract existing audience group targeting details.
  updatedAudienceGroupDetails = existingAudienceGroupTargetingOption.getAudienceGroupDetails();

  // Build and add delete request for existing audience group targeting.
  ArrayList<String> deleteAudienceGroupAssignedTargetingIds = new ArrayList<String>();
  deleteAudienceGroupAssignedTargetingIds.add("audienceGroup");

  audienceGroupDeleteRequests.add(
      new DeleteAssignedTargetingOptionsRequest()
          .setTargetingType("TARGETING_TYPE_AUDIENCE_GROUP")
          .setAssignedTargetingOptionIds(deleteAudienceGroupAssignedTargetingIds));
} catch (Exception e) {
  updatedAudienceGroupDetails = new AudienceGroupAssignedTargetingOptionDetails();
}

// Set delete requests in edit request.
requestContent.setDeleteRequests(audienceGroupDeleteRequests);

// Construct new group of Google Audiences to include in targeting.
GoogleAudienceGroup updatedIncludedGoogleAudienceGroup =
    updatedAudienceGroupDetails.getIncludedGoogleAudienceGroup();
if (updatedIncludedGoogleAudienceGroup != null) {
  List<GoogleAudienceTargetingSetting> updatedGoogleAudienceSettings =
      updatedIncludedGoogleAudienceGroup.getSettings();
  updatedGoogleAudienceSettings.addAll(newGoogleAudienceSettings);
  updatedIncludedGoogleAudienceGroup.setSettings(updatedGoogleAudienceSettings);
} else {
  updatedIncludedGoogleAudienceGroup = new GoogleAudienceGroup();
  updatedIncludedGoogleAudienceGroup.setSettings(newGoogleAudienceSettings);
}

// Add new Google Audience group to audience group targeting details.
updatedAudienceGroupDetails.setIncludedGoogleAudienceGroup(updatedIncludedGoogleAudienceGroup);

// Create new targeting option to assign.
AssignedTargetingOption newAudienceGroupTargeting = new AssignedTargetingOption();
newAudienceGroupTargeting.setAudienceGroupDetails(updatedAudienceGroupDetails);

// Build audience group targeting create request and add to list of create
// requests.
ArrayList<AssignedTargetingOption> createAudienceGroupAssignedTargetingOptions =
    new ArrayList<AssignedTargetingOption>();
createAudienceGroupAssignedTargetingOptions.add(newAudienceGroupTargeting);
ArrayList<CreateAssignedTargetingOptionsRequest> targetingCreateRequests =
    new ArrayList<CreateAssignedTargetingOptionsRequest>();
targetingCreateRequests.add(
    new CreateAssignedTargetingOptionsRequest()
        .setTargetingType("TARGETING_TYPE_AUDIENCE_GROUP")
        .setAssignedTargetingOptions(createAudienceGroupAssignedTargetingOptions));

// Set create requests in edit request.
requestContent.setCreateRequests(targetingCreateRequests);

// Configure and execute the bulk edit request.
BulkEditAssignedTargetingOptionsResponse response =
    service
        .advertisers()
        .lineItems()
        .bulkEditAssignedTargetingOptions(advertiserId, requestContent)
        .execute();

// Display API response information.
if (response.getUpdatedLineItemIds() != null && !response.getUpdatedLineItemIds().isEmpty()) {
  System.out.printf(
      "Targeting configurations for %s were successfully updated.%n",
      response.getUpdatedLineItemIds().get(0));
}
if (response.getFailedLineItemIds() != null && !response.getFailedLineItemIds().isEmpty()) {
  System.out.printf(
      "Targeting configurations for %s failed to update.%n",
      response.getFailedLineItemIds().get(0));
}
if (response.getErrors() != null && !response.getErrors().isEmpty()) {
  System.out.println("The failed updates were caused by the following errors:");
  for (Status error : response.getErrors()) {
    System.out.printf("Code %s: %s%n", error.getCode(), error.getMessage());
  }
} else {
  System.out.println("No successful or failed updates were reported.");
}

Python

# Provide the ID of the parent advertiser.
advertiser_id = advertiser-id

# Provide the ID of the line item whose targeting will be updated.
line_item_id = line-item-id

# Provide a list of Google Audience IDs to add to line item targeting.
added_google_audiences = google-audience-ids-to-add

# Build Google Audience targeting settings objects to create.
new_google_audience_targeting_settings = []
for google_audience_id in added_google_audiences:
  new_google_audience_targeting_settings.append(
      {'googleAudienceId': google_audience_id}
  )

try:
  # Retrieve any existing line item audience targeting.
  retrieved_audience_targeting = (
      service.advertisers()
      .lineItems()
      .targetingTypes()
      .assignedTargetingOptions()
      .get(
          advertiserId=advertiser_id,
          lineItemId=line_item_id,
          targetingType='TARGETING_TYPE_AUDIENCE_GROUP',
          assignedTargetingOptionId='audienceGroup',
      )
      .execute()
  )
except Exception:
  print(
      'Error retrieving existing audience targeting. Assuming no '
      'existing audience targeting.'
  )
  retrieved_audience_targeting = {}
updated_audience_group_details = {}

# Copy over any existing audience targeting.
if 'audienceGroupDetails' in retrieved_audience_targeting:
  updated_audience_group_details = retrieved_audience_targeting[
      'audienceGroupDetails'
  ]

# Append the new Google Audience IDs to any existing positive Google
# audience targeting.
if 'includedGoogleAudienceGroup' in updated_audience_group_details:
  updated_audience_group_details['includedGoogleAudienceGroup'][
      'settings'
  ].extend(new_google_audience_targeting_settings)
else:
  updated_audience_group_details['includedGoogleAudienceGroup'] = {
      'settings': new_google_audience_targeting_settings
  }

# Build bulk edit request.
bulk_edit_request = {
    'lineItemIds': [line_item_id],
    'deleteRequests': [{
        'targetingType': 'TARGETING_TYPE_AUDIENCE_GROUP',
        'assignedTargetingOptionIds': ['audienceGroup'],
    }],
    'createRequests': [{
        'targetingType': 'TARGETING_TYPE_AUDIENCE_GROUP',
        'assignedTargetingOptions': [
            {'audienceGroupDetails': updated_audience_group_details}
        ],
    }],
}

# Update the audience targeting
response = (
    service.advertisers()
    .lineItems()
    .bulkEditAssignedTargetingOptions(
        advertiserId=advertiser_id, body=bulk_edit_request
    )
    .execute()
)

# Print the line item IDs the successfully updated.
if 'updatedLineItemIds' in response:
  for id in response['updatedLineItemIds']:
    print(
        f'Line Item ID {id} has been updated to target the following '
        f'Google Audiences: {added_google_audiences}.'
    )

# Print the line item IDs that failed to update.
if 'failedLineItemIds' in response:
  for id in response['failedLineItemIds']:
    print(f'Could not update the audience targeting for Line Item ID {id}')
  if 'errors' in response:
    print('The updates failed due to the following errors:')
    for error in response['errors']:
      print(f'Error code: {error["code"]}, Message: {error["message"]}')

PHP

// Provide the ID of the parent advertiser.
$advertiserId = advertiser-id;

// Provide the ID of the line item whose targeting will be updated.
$lineItemId = line-item-id;

// Provide a list of Google Audience IDs to add to line item targeting.
$addedGoogleAudienceIds = array($audienceId);

// Build Google audience targeting setting objects to add.
$newGoogleAudienceTargetingSettings = array();
foreach ($addedGoogleAudienceIds as $googleAudienceId) {
    $googleAudienceSetting =
        new Google_Service_DisplayVideo_GoogleAudienceTargetingSetting();
    $googleAudienceSetting->setGoogleAudienceId($googleAudienceId);
    $newGoogleAudienceTargetingSettings[] = $googleAudienceSetting;
}

// Build bulk edit request.
$bulkEditRequest = new Google_Service_DisplayVideo_BulkEditAssignedTargetingOptionsRequest();
$bulkEditRequest->setLineItemIds(array($lineItemId));

// Call the API, retrieving the existing audience targeting for the
// line item.
try {
    $existingAudienceGroupTargetingOption = $this
        ->service
        ->advertisers_lineItems_targetingTypes_assignedTargetingOptions
        ->get(
            $advertiserId,
            $lineItemId,
            AppendAudienceAssignedTargetingOption::AUDIENCE_TARGETING_TYPE,
            AppendAudienceAssignedTargetingOption::AUDIENCE_TARGETING_OPTION_ID
        );

    $updatedAudienceGroupDetails = $existingAudienceGroupTargetingOption->getAudienceGroupDetails();

    $deleteAudienceGroupAssignedTargetingIds = array(AppendAudienceAssignedTargetingOption::AUDIENCE_TARGETING_OPTION_ID);
    $audienceTargetingDeleteRequest = new Google_Service_DisplayVideo_DeleteAssignedTargetingOptionsRequest();
    $audienceTargetingDeleteRequest->setTargetingType(AppendAudienceAssignedTargetingOption::AUDIENCE_TARGETING_TYPE);
    $audienceTargetingDeleteRequest->setAssignedTargetingOptionIds($deleteAudienceGroupAssignedTargetingIds);
    $bulkEditRequest->setDeleteRequests(array($audienceTargetingDeleteRequest));
} catch (\Exception $e) {
    $updatedAudienceGroupDetails = new Google_Service_DisplayVideo_AudienceGroupAssignedTargetingOptionDetails();
}

// Build new targeting object with updated list of Google Audience IDs.
$updatedIncludedGoogleAudienceGroup = new Google_Service_DisplayVideo_GoogleAudienceGroup();
if (!empty($updatedAudienceGroupDetails->getIncludedGoogleAudienceGroup())) {
    $updatedIncludedGoogleAudienceGroup
        ->setSettings(
            array_merge(
                $updatedAudienceGroupDetails
                    ->getIncludedGoogleAudienceGroup()
                    ->getSettings(),
                $newGoogleAudienceTargetingSettings
            )
        );
} else {
    $updatedIncludedGoogleAudienceGroup->setSettings($newGoogleAudienceTargetingSettings);
}
$updatedAudienceGroupDetails->setIncludedGoogleAudienceGroup($updatedIncludedGoogleAudienceGroup);

$newAudienceAssignedTargetingOption = new Google_Service_DisplayVideo_AssignedTargetingOption();
$newAudienceAssignedTargetingOption->setAudienceGroupDetails($updatedAudienceGroupDetails);

$createAudienceTargetingRequest = new Google_Service_DisplayVideo_CreateAssignedTargetingOptionsRequest();
$createAudienceTargetingRequest->setTargetingType(AppendAudienceAssignedTargetingOption::AUDIENCE_TARGETING_TYPE);
$createAudienceTargetingRequest->setAssignedTargetingOptions(array($newAudienceAssignedTargetingOption));
$bulkEditRequest->setCreateRequests(array($createAudienceTargetingRequest));

// Call the API, replacing the audience assigned targeting option for the
// line item.
try {
    $response = $this
        ->service
        ->advertisers_lineItems
        ->bulkEditAssignedTargetingOptions(
            $advertiserId,
            $bulkEditRequest
        );
} catch (\Exception $e) {
    $this->renderError($e);
    return;
}

// Print information returned by the bulk edit request.
// List updated line item IDs.
if (empty($response->getUpdatedLineItemIds())) {
    print '<p>No line items were successfully updated.</p>';
} else {
    print '<p>The targeting of the following line item IDs were '
        . 'updated:</p><ul>';
    foreach ($response->getUpdatedLineItemIds() as $id) {
        printf('<li>%s</li>',$id);
    }
    print '</ul>';
}

// List line item IDs that failed to update.
if (empty($response->getFailedLineItemIds())) {
    print '<p>No line items failed to update.</p>';
} else {
    print '<p>The targeting of the following line item IDs failed to '
        . 'update:</p><ul>';
    foreach ($response->getFailedLineItemIds() as $id) {
        printf('<li>%s</li>',$id);
    }
    print '</ul>';
}

// List the errors thrown when the targeting was updated.
if (empty($response->getErrors())) {
    print '<p>No errors were thrown.</p>';
} else {
    print '<p>The following errors were thrown when attempting to '
        . 'update the targeting:</p><ul>';
    foreach ($response->getErrors() as $error) {
        printf(
            '<li>%s: %s</li>',
            $error->getCode(),
            $error->getMessage()
        );
    }
    print '</ul>';
}

优化受众群体定位

借助优化型定位功能,Display & Video 360 可以扩大覆盖面,在所选受众群体之外覆盖新的相关用户。

使用订单项中的 targetingExpansion 字段为订单项设置优化型定位。