Chỉ định mẫu quảng cáo

Sau khi bạn tạo một mẫu quảng cáo, Display & Video 360 sẽ xem xét để đảm bảo mẫu quảng cáo đó tuân thủ các chính sách của Display & Video 360. Sau khi được phê duyệt, mẫu quảng cáo có thể được phân phát dưới dạng quảng cáo. Gán quảng cáo cho một mục hàng. Sau khi mục hàng hoạt động, hệ thống sẽ sử dụng mục hàng đó trong quá trình phân phát.

Sau đây là cách chỉ định mẫu quảng cáo cho một mục hàng hiện có:

Java

// Provide the parent advertiser ID of the line item and creative.
long advertiserId = advertiser-id;

// Provide the ID of the line item to assign the creative to.
long lineItemId = line-item-id;

// Provide the ID of the creative to assign.
long creativeId = creative-id;

// Build updated list of assigned creative and add ID for new creative.
List<Long> updatedCreativeIds = new ArrayList<>();
updatedCreativeIds.add(creativeId);

// Retrieve the existing line item.
LineItem existingLineItem =
    service.advertisers().lineItems().get(advertiserId, lineItemId).execute();

// Add existing assigned creatives to updated list.
if (existingLineItem.getCreativeIds() != null) {
  for (Long id : existingLineItem.getCreativeIds()) {
    updatedCreativeIds.add(id);
  }
}

// Create the line item structure.
LineItem updatedLineItem = new LineItem().setCreativeIds(updatedCreativeIds);

// Configure the patch request and set update mask to only update entity status.
LineItems.Patch request =
    service
        .advertisers()
        .lineItems()
        .patch(advertiserId, lineItemId, updatedLineItem)
        .setUpdateMask("creativeIds");

// Update the line item.
LineItem response = request.execute();

// Print the updated list of creatives assigned to the line item.
System.out.printf(
    "Line item %s now has the following creative IDs assigned: %s",
    response.getName(), response.getCreativeIds());

Python

# Provide the parent advertiser ID of the line item and creative.
advertiser_id = advertiser-id

# Provide the ID of the line item to assign the creative to.
line_item_id = line-item-id

# Provide the ID of the creative to assign.
creative_id = creative-id

# Retrieve the existing line item.
existing_line_item = (
    service.advertisers()
    .lineItems()
    .get(advertiserId=advertiser_id, lineItemId=line_item_id)
    .execute()
)

# Pull out the existing list of assigned creatives and add new ID.
assigned_creatives = [creative_id]
if "creativeIds" in existing_line_item:
    assigned_creatives.extend(existing_line_item["creativeIds"])

# Build the line item object including only the updated field.
line_item_obj = {"creativeIds": assigned_creatives}

# Update the line item.
line_item_resp = (
    service.advertisers()
    .lineItems()
    .patch(
        advertiserId=advertiser_id,
        lineItemId=line_item_id,
        updateMask="creativeIds",
        body=line_item_obj,
    )
    .execute()
)

# Print the updated list of creatives assigned to the line item.
print(
    f'Line item {line_item_resp["name"]} now has the following creative IDs '
    f'assigned: {line_item_resp["creativeIds"]}.'
)

PHP

// Provide the parent advertiser ID of the line item and creative.
$advertiserId = advertiser-id;

// Provide the ID of the line item to assign the creative to.
$lineItemId = line-item-id;

// Provide the ID of the creative to assign.
$creativeId = creative-id;

// Call the API, retrieving the existing line item.
try {
    $retrievedLineItem = $this->service->advertisers_lineItems->get(
        $advertiserId,
        $lineItemId
    );
} catch (\Exception $e) {
    $this->renderError($e);
    return;
}

// Build updated list of assigned creatives with existing creatives and the
// new creative ID.
$creativeIdsToAssign = array();
if (!empty($retrievedLineItem->getCreativeIds())) {
    $creativeIdsToAssign = $retrievedLineItem->getCreativeIds();
}
$creativeIdsToAssign[] = $creativeId;

// Build the line item object including only the updated field.
$lineItem = new Google_Service_DisplayVideo_LineItem();
$lineItem->setCreativeIds($creativeIdsToAssign);
$optParams = array('updateMask' => 'creativeIds');

// Call the API, updating the creative IDs for the identified line
// item.
try {
    $result = $this->service->advertisers_lineItems->patch(
        $advertiserId,
        $lineItemId,
        $lineItem,
        $optParams
    );
} catch (\Exception $e) {
    $this->renderError($e);
    return;
}

// Print the updated list of creatives assigned to the line item.
printf(
    '<p>Line Item %s now has the following creative IDs assigned: %s.</p>',
    $result['name'],
    $result['creativeIds']
);