ตั้งค่าการกําหนดเป้าหมาย

ระบบจะใช้บริการตัวเลือกการกําหนดเป้าหมาย, ตัวเลือกการกําหนดเป้าหมายที่กําหนด และรายการโฆษณาร่วมกันเพื่อกําหนดการกําหนดเป้าหมายรายการโฆษณาใน Display & Video 360 API หน้านี้จะอธิบายและแสดงตัวอย่างวิธีค้นหาตัวเลือกการกำหนดเป้าหมายที่ใช้ได้ กําหนดตัวเลือกการกำหนดเป้าหมายให้กับรายการโฆษณา และดําเนินการแบบเป็นกลุ่มกับรายการโฆษณาเพื่อแสดงรายการและแก้ไขตัวเลือกการกำหนดเป้าหมายที่กําหนด

ค้นหาตัวเลือกการกำหนดเป้าหมายที่ใช้ได้

ตัวเลือกการกำหนดเป้าหมายจะใช้ตัวแปรที่ผู้ใช้ระบุ เอนทิตีที่กำหนดเป้าหมายได้ที่มีอยู่ หรือตัวเลือกที่มีอยู่เพื่อกําหนดกลุ่มเป้าหมายที่ต้องการ ระบบจะระบุตัวเลือกที่มีอยู่ล่วงหน้าโดยใช้ค่า enum หรือรหัสตัวเลือกการกําหนดเป้าหมาย ทั้งนี้ขึ้นอยู่กับประเภทการกําหนดเป้าหมาย ระบบจะระบุเอนทิตีที่กำหนดเป้าหมายได้โดยใช้รหัสเอนทิตี คุณสามารถดูรหัสตัวเลือกการกำหนดเป้าหมายและรหัสเอนทิตีได้โดยใช้ Display & Video 360 API

ใช้ค่า enum ที่กำหนด

ตัวเลือกการกำหนดเป้าหมายสําหรับประเภทการกําหนดเป้าหมายต่อไปนี้จะกําหนดโดยใช้ประเภท enum ที่เฉพาะเจาะจง

TargetingType ค่าแจกแจง
TARGETING_TYPE_AGE_RANGE AgeRange
TARGETING_TYPE_CONTENT_INSTREAM_POSITION ContentInstreamPosition
TARGETING_TYPE_CONTENT_OUTSTREAM_POSITION ContentOutstreamPosition
TARGETING_TYPE_DEVICE_TYPE DeviceType
TARGETING_TYPE_DIGITAL_CONTENT_LABEL_EXCLUSION ContentRatingTier
TARGETING_TYPE_ENVIRONMENT Environment
TARGETING_TYPE_EXCHANGE Exchange
TARGETING_TYPE_GENDER Gender
TARGETING_TYPE_HOUSEHOLD_INCOME HouseholdIncome
TARGETING_TYPE_NATIVE_CONTENT_POSITION NativeContentPosition
TARGETING_TYPE_OMID Omid
TARGETING_TYPE_PARENTAL_STATUS ParentalStatus
TARGETING_TYPE_SENSITIVE_CATEGORY_EXCLUSION SensitiveCategory
TARGETING_TYPE_VIDEO_PLAYER_SIZE VideoPlayerSize
TARGETING_TYPE_VIEWABILITY Viewability

คุณสามารถใช้ค่าสตริงของ Enum ที่เกี่ยวข้องเพื่อระบุแหล่งข้อมูล AssignedTargetingOption ที่มีอยู่ของประเภทการกําหนดเป้าหมายเหล่านี้ และดูข้อมูลดังกล่าวได้ในช่อง assignedTargetingOptionIdAlias คุณสามารถใช้ค่าแทนนี้แทน assignedTargetingOptionId เมื่อดึงข้อมูลหรือลบตัวเลือกการกำหนดเป้าหมายที่กำหนด

เรียกข้อมูลรหัสตัวเลือกการกำหนดเป้าหมาย

ประเภทการกําหนดเป้าหมายที่ใช้ตัวเลือกที่มีอยู่จะกําหนดโดยใช้รหัสตัวเลือกการกําหนดเป้าหมายที่เกี่ยวข้อง

เช่น มีตําแหน่งบนหน้าจอจํากัดจำนวนที่สามารถกําหนดเป้าหมายได้โดยใช้ประเภทการกําหนดเป้าหมาย TARGETING_TYPE_ON_SCREEN_POSITION ตำแหน่งแต่ละตำแหน่งเหล่านี้จะมีรหัสตัวเลือกการกำหนดเป้าหมายที่สอดคล้องกัน

คุณเรียกดูรหัสตัวเลือกการกำหนดเป้าหมายเหล่านี้ได้ผ่านบริการตัวเลือกการกำหนดเป้าหมาย การดึงข้อมูลจะดําเนินการด้วยวิธีใดวิธีหนึ่งต่อไปนี้ ทั้งนี้ขึ้นอยู่กับประเภทการกําหนดเป้าหมาย

  • การดึงข้อมูลแต่ละรายการหรือรายการที่ครอบคลุม: การดึงข้อมูลตัวเลือกสําหรับการกําหนดเป้าหมายประเภทส่วนใหญ่ทําได้โดยใช้เมธอด get และ list ใช้ targetingTypes.targetingOptions.get เพื่อดึงรายละเอียดของตัวเลือกการกำหนดเป้าหมายที่ระบุโดยประเภทการกำหนดเป้าหมายและรหัสตัวเลือกการกำหนดเป้าหมาย ใช้ targetingTypes.targetingOptions.list เพื่อแสดงรายการตัวเลือกการกำหนดเป้าหมายทั้งหมดที่มีของการกำหนดเป้าหมายประเภทหนึ่งๆ
  • Search: ตัวเลือกสําหรับประเภทการกําหนดเป้าหมายตามสถานที่ (TARGETING_TYPE_GEO_REGION, TARGETING_TYPE_POI และ TARGETING_TYPE_BUSINESS_CHAIN) ต้องดึงข้อมูลโดยใช้เมธอด search ใช้ targetingTypes.targetingOptions.search เพื่อดึงข้อมูลตัวเลือกการกำหนดเป้าหมายประเภทหนึ่งๆ ที่ตรงกับสตริงการค้นหาที่ระบุ

ตัวอย่างวิธีเรียกข้อมูลรายการตัวเลือกการกำหนดเป้าหมายที่เป็นไปได้สําหรับประเภทการกําหนดเป้าหมาย TARGETING_TYPE_BROWSER มีดังนี้

Java

// Configure the list request.
TargetingOptions.List request =
   service
       .targetingTypes()
       .targetingOptions()
       .list("TARGETING_TYPE_BROWSER")
       .setAdvertiserId(advertiser-id);

// Create the response and nextPageToken variables.
ListTargetingOptionsResponse response;
String nextPageToken = null;

do {
 // Create and execute the list request.
 response = request.setPageToken(nextPageToken).execute();

 // Check if the response is empty.
 if (response.isEmpty()) {
   System.out.print("List request returned no Targeting Options");
   break;
 }

 // Iterate over retrieved targeting options.
 for (TargetingOption option : response.getTargetingOptions()) {
   System.out.printf(
       "Targeting Option ID: %s, Browser Display Name: '%s'\n",
       option.getTargetingOptionId(), option.getBrowserDetails().getDisplayName());
 }

 // Update the next page token.
 nextPageToken = response.getNextPageToken();
} while (!Strings.isNullOrEmpty(nextPageToken));

Python

# Create the page token variable.
next_page_token = ""

while True:
  # Request the targeting options list.
  response = service.targetingTypes() \
    .targetingOptions().list(
      advertiserId=advertiser-id,
      targetingType="TARGETING_TYPE_BROWSER",
      pageToken=next_page_token
  ).execute()

  # Check if response is empty.
  if not response:
    print("List request returned no Targeting Options")
    break

  # Iterate over retrieved targeting options.
  for option in response['targetingOptions']:
    print("Targeting Option ID: %s, Browser Display Name: %s"
          % (option['targetingOptionId'], option['browserDetails']['displayName']))

  # Break out of loop if there is no next page.
  if 'nextPageToken' not in response:
    break

  # Update the next page token.
  next_page_token = response['nextPageToken']

PHP

// Create the page token variable.
$nextPageToken = null;

do {
    // Build the query parameters object for the request.
    $optParams = array(
        'advertiserId' => advertiser-id,
        'pageToken' => $nextPageToken
    );

    // Call the API, getting the browser targeting options for the
    // identified advertiser.
    $response = $this
        ->service
        ->targetingTypes_targetingOptions
        ->listTargetingTypesTargetingOptions(
            'TARGETING_TYPE_BROWSER',
            $optParams
        );

    // Print the resulting targeting options.
    if (!empty($response->getTargetingOptions())) {
        foreach ($response->getTargetingOptions() as $option) {
            printf(
                'Targeting Option ID: %s, Browser Display Name: %s\n',
                $option['targetingOptionId'],
                $option['browserDetails']['displayName']
            );
        }
    } else {
        print('No targeting options returned\n');
    }

    // Update the next page token.
    $nextPageToken = $response->getNextPageToken();
} while (
    !empty($response->getTargetingOptions())
    && $nextPageToken
);

แสดงรายการเอนทิตีที่กำหนดเป้าหมายได้

หากต้องการกําหนดเป้าหมายรายการโฆษณาโดยใช้เอนทิตีที่กําหนดเป้าหมายได้ที่มีอยู่ คุณจะต้องมีรหัสของเอนทิตีนั้น คุณสามารถเรียกข้อมูลเอนทิตีที่กำหนดเป้าหมายได้ เช่น แชแนล กลุ่มเป้าหมายแบบรวม และกลุ่มแหล่งที่มาของพื้นที่โฆษณาผ่านบริการของเอนทิตีนั้นๆ ใน Display & Video 360 API

แต่ละบริการจะมีเมธอด get และ list เป็นของตัวเอง ใช้เมธอด get เพื่อยืนยันว่าเอนทิตีพร้อมใช้งานภายใต้ผู้ลงโฆษณารายหนึ่งๆ ใช้listวิธีนี้เพื่อค้นหาเอนทิตีทั้งหมดของประเภททรัพยากรนั้นที่ใช้ได้กับผู้ลงโฆษณารายหนึ่งๆ จึงนำไปใช้ในการกําหนดการกําหนดเป้าหมายให้กับรายการโฆษณาภายใต้ผู้ลงโฆษณารายนั้นได้

นอกจากนี้ คุณยังจัดการเอนทิตีย่อยที่กำหนดเป้าหมายได้ผ่าน API ซึ่งทําได้ผ่านเมธอด create และ patch ในบริการที่เกี่ยวข้อง รวมถึงบริการสําหรับค่าแต่ละค่าที่แสดงในเอนทิตี เช่น แหล่งที่มาของพื้นที่โฆษณา คีย์เวิร์ดเชิงลบ และสถานที่

สร้างรหัสตัวเลือกการกำหนดเป้าหมายจุดที่น่าสนใจ

ตัวเลือกการกำหนดเป้าหมายจุดที่น่าสนใจที่มีชื่อในส่วน TARGETING_TYPE_POI จะเรียกดูได้โดยใช้ targetingTypes.targetingOptions.search นอกจากนี้ คุณยังสร้างรหัสตัวเลือกการกำหนดเป้าหมาย TARGETING_TYPE_POI ที่กําหนดเองเพื่อกําหนดเป้าหมายพิกัดละติจูด-ลองจิจูดที่เฉพาะเจาะจงได้ด้วย

ทําตามขั้นตอนต่อไปนี้เพื่อสร้างรหัสตัวเลือกการกําหนดเป้าหมายจุดที่น่าสนใจ

  1. ดึงข้อมูลพิกัดละติจูด-ลองจิจูด (เช่น "40.7414691, -74.003387")
  2. ปัดเศษค่าพิกัดเป็นทศนิยม 6 ตำแหน่ง (เช่น "40.741469, -74.003387")
  3. นำจุดทศนิยมออกจากค่าพิกัด (เช่น "40741469, -74003387")
  4. รวมค่า 2 ค่าเข้าด้วยกันเพื่อสร้างสตริงเดียวโดยคั่นด้วยเซมิโคลอน (เช่น "40741469;-74003387")

สตริงที่ได้สามารถใช้เป็น targetingOptionId เมื่อสร้างตัวเลือกการกำหนดเป้าหมายที่TARGETING_TYPE_POIกำหนด

เมื่อสร้างแล้ว ระบบจะอัปเดตฟิลด์ targetingOptionId และ assignedTargetingOptionId ของแหล่งข้อมูลตัวเลือกการกำหนดเป้าหมายที่กำหนด โดยเพิ่มเซมิโคลอนต่อท้ายและแฮชที่เป็นตัวอักษรและตัวเลข

กําหนดตัวเลือกการกําหนดเป้าหมาย

การกำหนดเป้าหมายที่กำหนดให้กับรายการโฆษณาจะแสดงเป็นตัวเลือกการกำหนดเป้าหมายที่กำหนด คุณจัดการเอนทิตีเหล่านี้ได้โดยใช้บริการตัวเลือกการกำหนดเป้าหมายที่กำหนด การสร้างตัวเลือกการกำหนดเป้าหมายที่กำหนดจะนํารายละเอียดการกําหนดเป้าหมายเหล่านั้นไปใช้กับรายการโฆษณาหลัก การลบตัวเลือกการกำหนดเป้าหมายที่กำหนดไว้ที่มีอยู่จะนำการกำหนดเป้าหมายนั้นออก

ใช้ advertisers.lineItems.targetingTypes.assignedTargetingOptions.create เพื่อสร้างตัวเลือกการกำหนดเป้าหมายที่กำหนด ระบุพารามิเตอร์การกําหนดเป้าหมายในช่อง details ของทรัพยากรตัวเลือกการกําหนดเป้าหมายที่กําหนดซึ่งสอดคล้องกับประเภทการกําหนดเป้าหมายที่ต้องการ

ต่อไปนี้เป็นตัวอย่างวิธีสร้างตัวเลือกการกำหนดเป้าหมายที่กําหนดของประเภทการกําหนดเป้าหมาย TARGETING_TYPE_BROWSER

Java

// Create an AssignedTargetingOption object of the
// browser targeting type.
AssignedTargetingOption assignedTargetingOption =
   new AssignedTargetingOption()
       .setBrowserDetails(
           new BrowserAssignedTargetingOptionDetails()
               .setTargetingOptionId(targeting-option-id));

// Configure the create request.
AssignedTargetingOptions.Create request =
   service
       .advertisers()
       .lineItems()
       .targetingTypes()
       .assignedTargetingOptions()
       .create(
           advertiser-id,
           line-item-id,
           "TARGETING_TYPE_BROWSER",
           assignedTargetingOption);

// Send the request.
AssignedTargetingOption response = request.execute();

// Display the new assigned targeting option.
System.out.printf("AssignedTargetingOption %s was created.",
   response.getName());

Python

# Create a assigned targeting option object.
assigned_targeting_option_obj = {
    'browserDetails': {
        'targetingOptionId': targeting-option-id
    }
}

# Create the assigned targeting option.
assigned_targeting_option = service.advertisers().lineItems()\
  .targetingTypes().assignedTargetingOptions().create(
    advertiserId=advertiser-id,
    lineItemId=line-item-id,
    targetingType="TARGETING_TYPE_BROWSER",
    body=assigned_targeting_option_obj
).execute()

# Display the new assigned targeting option.
print("Assigned Targeting Option %s was created."
      % assigned_targeting_option["name"])

PHP

// Create a assigned targeting option object.
$assignedTargetingOption =
    new Google_Service_DisplayVideo_AssignedTargetingOption();

// Create and set browser details.
$details =
    new Google_Service_DisplayVideo_BrowserAssignedTargetingOptionDetails();
$details->setTargetingOptionId(targeting-option-id);
$assignedTargetingOption->setBrowserDetails($details);

// Call the API, creating the browser assigned targeting option for the
// given line item.
$result = $this
    ->service
    ->advertisers_lineItems_targetingTypes_assignedTargetingOptions
    ->create(
        advertiser-id,
        line-item-id,
        'TARGETING_TYPE_BROWSER',
        $assignedTargetingOption
    );

printf(
    'Assigned Targeting Option %s was created.\n',
    $result['name']
);

ข้อผิดพลาด

ข้อผิดพลาดในการกําหนดค่าการกําหนดเป้าหมาย

กฎเกี่ยวกับการการกำหนดเป้าหมายใน Display & Video 360 มีรายละเอียดซับซ้อน ซึ่งจะบังคับใช้ใน Display & Video 360 API ผ่านข้อผิดพลาดที่แสดงเมื่อสร้างตัวเลือกการกำหนดเป้าหมายที่กำหนด ข้อผิดพลาดที่ API แสดงจะระบุการละเมิด

ข้อผิดพลาดส่วนใหญ่เกิดจากการกำหนดเป้าหมายที่มีอยู่ให้กับรายการโฆษณา ใช้ advertisers.lineItems.targetingTypes.assignedTargetingOptions.list เพื่อดึงข้อมูลตัวเลือกการกำหนดเป้าหมายทั้งหมดของประเภทการกำหนดเป้าหมายที่กำหนดให้กับรายการโฆษณา ประเมินว่าการกำหนดเป้าหมายที่ต้องการทำได้หรือไม่เมื่อพิจารณาจากข้อจํากัด และ ใช้ advertisers.lineItems.targetingTypes.assignedTargetingOptions.delete เพื่อนําการกำหนดเป้าหมายที่ไม่ต้องการออกก่อนที่จะพยายามสร้างตัวเลือกการกำหนดเป้าหมายที่ต้องการซึ่งกําหนดไว้อีกครั้ง

ข้อผิดพลาดในการกําหนดเป้าหมาย YouTube และพาร์ทเนอร์

คุณจะอัปเดตการกำหนดเป้าหมายสำหรับแคมเปญ YouTube และพาร์ทเนอร์โดยเฉพาะโดยใช้ Display & Video 360 API ไม่ได้ และหากพยายามดำเนินการดังกล่าว ระบบจะแสดงข้อผิดพลาด

การกําหนดเป้าหมาย YouTube และพาร์ทเนอร์ประกอบด้วยการกําหนดเป้าหมายทั้งหมดที่กำหนดให้กับรายการโฆษณา YouTube และพาร์ทเนอร์และกลุ่มโฆษณาโดยตรง รวมถึงการกำหนดเป้าหมายประเภทต่อไปนี้

  • TARGETING_TYPE_SESSION_POSITION
  • TARGETING_TYPE_YOUTUBE_CHANNEL
  • TARGETING_TYPE_YOUTUBE_VIDEO

ข้อผิดพลาดเกี่ยวกับการทำงานพร้อมกัน

การพยายามอัปเดตการตั้งค่าหรือการกําหนดเป้าหมายของรายการโฆษณารายการเดียวผ่านคําขอหลายรายการพร้อมกันจะทําให้เกิดความผิดพลาด

หากต้องการเพิ่มหรือนําตัวเลือกการกําหนดเป้าหมายที่กําหนดไว้หลายรายการสําหรับรายการโฆษณารายการเดียวออกพร้อมกัน คุณควรใช้คําขอแก้ไขแบบเป็นกลุ่ม หากต้องการอัปเดตการตั้งค่าและการกําหนดเป้าหมายของรายการโฆษณา ให้ส่งคําขอ advertisers.lineItems.patch และคำขอการกําหนดเป้าหมายที่เกี่ยวข้องตามลําดับเพื่อให้ระบบไม่ส่งคําขอที่ 2 จนกว่าคําขอแรกจะได้รับการตอบกลับ

การดำเนินการกำหนดเป้าหมายแบบเป็นกลุ่มและทั่วทั้งทรัพยากร

คุณสามารถใช้วิธีการกําหนดเป้าหมายแบบเป็นกลุ่มและทั้งทรัพยากรเพื่อจัดการตัวเลือกการกําหนดเป้าหมายที่กําหนดให้กับประเภทการกําหนดเป้าหมายต่างๆ ดังนี้

  • ใช้วิธีการกําหนดเป้าหมายแบบเป็นกลุ่มเพื่อดึงข้อมูลหรือแก้ไขตัวเลือกการกําหนดเป้าหมายในประเภทการกําหนดเป้าหมายหลายประเภทและในหลายแหล่งข้อมูล คุณสามารถเรียกข้อมูลการกำหนดค่าการกําหนดเป้าหมายทั้งหมดของรายการโฆษณาหลายรายการได้โดยใช้ advertisers.lineItems.bulkListAssignedTargetingOptions หรืออัปเดตการกําหนดเป้าหมายในรายการโฆษณาหลายรายการให้เหมือนกันได้โดยใช้ advertisers.lineItems.bulkEditAssignedTargetingOptions ฟีเจอร์เหล่านี้ใช้ได้เฉพาะในบริการ advertisers.lineItems เท่านั้น
  • ใช้วิธีการกําหนดเป้าหมายทั้งทรัพยากรเพื่อดึงข้อมูลหรือแก้ไขตัวเลือกการกําหนดเป้าหมายในประเภทการกําหนดเป้าหมายหลายประเภทภายใต้ทรัพยากรเดียว ซึ่งพร้อมให้บริการในบริการ partners และ advertisers และตั้งชื่อว่า listAssignedTargetingOptions หรือ editAssignedTargetingOptions

หากต้องการดูภาพรวมที่สมบูรณ์ของการกำหนดเป้าหมายปัจจุบันของรายการโฆษณา ต้องการใช้การกำหนดค่าการกำหนดเป้าหมายที่กำหนดไว้ล่วงหน้ากับรายการโฆษณา หรือต้องการเปลี่ยนแปลงการกำหนดเป้าหมายของรายการโฆษณาหลายรายการพร้อมกัน ให้ลองใช้วิธีการกําหนดเป้าหมายเหล่านี้

กำหนดเป้าหมายรายการหลายรายการพร้อมกัน

advertisers.lineItems.bulkListAssignedTargetingOptions มีวิธีดูการกำหนดเป้าหมายทั้งหมดที่กำหนดให้กับรายการโฆษณาอย่างน้อย 1 รายการในประเภทการกำหนดเป้าหมายที่หลากหลาย ซึ่งจะทำงานคล้ายกับlistวิธีอื่นๆ คุณสามารถใช้พารามิเตอร์การค้นหา filter เพื่อกรองผลลัพธ์ตาม TargetingType หรือ Inheritance

ต่อไปนี้คือตัวอย่างวิธีแสดงตัวเลือกการกำหนดเป้าหมายทั้งหมดที่กำหนดให้กับรายการโฆษณาซึ่งพาร์ทเนอร์หลักหรือผู้ลงโฆษณารับค่ามา

Java

// Configure the bulk list request.
LineItems.BulkListAssignedTargetingOptions request =
    service.advertisers().lineItems()
        .bulkListAssignedTargetingOptions(advertiser-id);

// Set Line Items to retrieve targeting for.
request.setLineItemIds(line-item-ids);

// Set filter to only return inherited assigned targeting options.
request.setFilter(
    "inheritance=\"INHERITED_FROM_ADVERTISER\" OR inheritance=\"INHERITED_FROM_PARTNER\"");

// Create the response and nextPageToken variables.
BulkListAssignedTargetingOptionsResponse response;
String nextPageToken = null;

do {
  // Set page token and execute the list request.
  response = request.setPageToken(nextPageToken).execute();

  // Check if the response is empty.
  if (response.isEmpty()) {
    System.out.print("Bulk list request returned no Assigned Targeting Options");
    break;
  }

  // Iterate over retrieved line item assigned targeting option wrapper objects.
  for (LineItemAssignedTargetingOption lineItemAssignedTargetingOption
      : response.getLineItemAssignedTargetingOptions()) {
    System.out.printf(
        "Assigned Targeting Option %s found\n",
        lineItemAssignedTargetingOption.getAssignedTargetingOption().getName());
  }

  // Update the next page token.
  nextPageToken = response.getNextPageToken();
} while (!Strings.isNullOrEmpty(nextPageToken));

Python

# Create the page token variable.
next_page_token = ""

while True:
  # Execute the list request.
  response = service.advertisers().lineItems() \
    .bulkListAssignedTargetingOptions(
      advertiserId=advertiser-id,
      lineItemIds=line-item-ids,
      filter="inheritance=\"INHERITED_FROM_ADVERTISER\" OR "
             "inheritance=\"INHERITED_FROM_PARTNER\"",
      pageToken=next_page_token
  ).execute()

  # Check if response is empty.
  if not response:
    print("Bulk list request returned no Assigned Targeting Options")
    break

  # Iterate over retrieved assigned targeting options.
  for lineItemAssignedTargetingOption in response['lineItemAssignedTargetingOptions']:
    print("Assigned Targeting Option %s found"
          % (lineItemAssignedTargetingOption['assignedTargetingOption']['name']))

  # Break out of loop if there is no next page.
  if 'nextPageToken' not in response:
    break

  # Update the next page token.
  next_page_token = response['nextPageToken']

PHP

// Create the page token variable.
$nextPageToken = null;

do {
    // Build the query parameters object for the request.
    $optParams = array(
        'lineItemIds' => line-item-ids,
        'filter' => "inheritance=\"INHERITED_FROM_ADVERTISER\" OR "
            . "inheritance=\"INHERITED_FROM_PARTNER\"",
        'pageToken' => $nextPageToken
    );

    // Call the API, getting all the assigned targeting options for the
    // identified line item.
    $response = $service
        ->advertisers_lineItems
        ->bulkListAssignedTargetingOptions(
            advertiser-id,
            $optParams
    );

    // Print the returned assigned targeting options.
    if (!empty($response->getLineItemAssignedTargetingOptions())) {
        foreach ($response->getLineItemAssignedTargetingOptions() as $option) {
            printf('Assigned Targeting Option %s found\n', $option->getAssignedTargetingOption()['name']);
        }
    } else {
        print('No targeting options returned\n');
    }

    // Update the next page token.
    $nextPageToken = $response->getNextPageToken();
} while (
    !empty($response->getLineItemAssignedTargetingOptions())
    && $nextPageToken);

แก้ไขการกำหนดเป้าหมายหลายรายการพร้อมกัน

advertisers.lineItems.bulkEditAssignedTargetingOptions มีวิธีเพิ่มและนําตัวเลือกการกําหนดเป้าหมายหลายรายการของประเภทการกําหนดเป้าหมายต่างๆ ออกจากรายการโฆษณาอย่างน้อย 1 รายการพร้อมกัน

โดยเมธอดนี้จะรับรายการ DeleteAssignedTargetingOptionsRequests และรายการ CreateAssignedTargetingOptionsRequests ออบเจ็กต์คําขอรายการเดียวอาจแสดงการลบหรือสร้างตัวเลือกการกําหนดเป้าหมายหลายรายการของประเภทการกําหนดเป้าหมายเดียวกัน

หากการพยายามลบหรือสร้างตัวเลือกการกำหนดเป้าหมายที่กําหนดทําให้เกิดข้อผิดพลาดสําหรับรายการโฆษณา ระบบจะยกเลิกการดำเนินการแบบเป็นกลุ่มสําหรับรายการโฆษณานั้น คำขอจะแสดงรายการโฆษณาที่อัปเดตสําเร็จ รวมถึงรายการรายการโฆษณาที่อัปเดตไม่สําเร็จและข้อผิดพลาดที่เกี่ยวข้อง

ต่อไปนี้คือตัวอย่างวิธีแก้ไขตัวเลือกการกำหนดเป้าหมายที่กำหนดให้กับบรรทัดรายการอย่างน้อย 1 รายการพร้อมกัน โดยให้รายการตัวเลือกการกำหนดเป้าหมายที่กำหนดไว้เพื่อลบและตัวเลือกการกำหนดเป้าหมายที่จะสร้าง

Java

// Create a bulk edit request.
BulkEditAssignedTargetingOptionsRequest requestContent =
    new BulkEditAssignedTargetingOptionsRequest();

// Set line item IDs in edit request.
requestContent.setLineItemIds(line-item-ids);

// Build delete request list.
ArrayList<DeleteAssignedTargetingOptionsRequest> deleteRequests =
    new ArrayList<DeleteAssignedTargetingOptionsRequest>();

// Add browser assigned targeting option IDs to delete request list.
deleteRequests.add(new DeleteAssignedTargetingOptionsRequest()
    .setTargetingType("TARGETING_TYPE_BROWSER")
    .setAssignedTargetingOptionIds(delete-browser-assigned-targeting-ids));

// Add device make or model assigned targeting option IDs to delete request list.
deleteRequests.add(new DeleteAssignedTargetingOptionsRequest()
    .setTargetingType("TARGETING_TYPE_DEVICE_MAKE_MODEL")
    .setAssignedTargetingOptionIds(
        delete-device-make-model-assigned-targeting-ids));

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

// Build create request list.
ArrayList<CreateAssignedTargetingOptionsRequest> createRequests =
    new ArrayList<CreateAssignedTargetingOptionsRequest>();

// Create browser assigned targeting option create request.
CreateAssignedTargetingOptionsRequest createBrowserTargetingRequest =
    new CreateAssignedTargetingOptionsRequest();
createBrowserTargetingRequest.setTargetingType("TARGETING_TYPE_BROWSER");

// Create and set list of browser assigned targeting options.
ArrayList<AssignedTargetingOption> createBrowserAssignedTargetingOptions =
    new ArrayList<AssignedTargetingOption>();
for (String targetingOptionId : create-browser-assigned-targeting-ids) {
  createBrowserAssignedTargetingOptions.add(new AssignedTargetingOption()
      .setBrowserDetails(
          new BrowserAssignedTargetingOptionDetails()
              .setTargetingOptionId(targetingOptionId)));
}
createBrowserTargetingRequest
    .setAssignedTargetingOptions(createBrowserAssignedTargetingOptions);

// Add browser assigned targeting options to list of create requests.
createRequests.add(createBrowserTargetingRequest);

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

// Configure the bulk edit request.
LineItems.BulkEditAssignedTargetingOptions request =
    service.advertisers().lineItems()
        .bulkEditAssignedTargetingOptions(
            advertiser-id,
            requestContent);

// Execute bulk edit request.
BulkEditAssignedTargetingOptionsResponse response = request.execute();

// Check if any line items updated successfully.
if (response.getUpdatedLineItemIds() == null || response.getUpdatedLineItemIds().isEmpty()) {
  System.out.println("No line items were updated successfully.");
} else {
  System.out.printf(
      "Targeting configurations for the following line item IDs were updated: %s.\n",
      Arrays.toString(response.getUpdatedLineItemIds().toArray()));
}

// Check if any line items failed to update.
if (response.getFailedLineItemIds() == null || response.getFailedLineItemIds().isEmpty()) {
  System.out.println("No line items failed to update.");
} else {
  // Print the line items that failed to update.
  System.out.printf(
      "Targeting configurations for the following line item IDs failed to update: %s.\n",
      Arrays.toString(response.getFailedLineItemIds().toArray()));

  // Print errors thrown for failed updates.
  System.out.println("The failed updates were caused by the following errors:");
  for (Status error : response.getErrors()) {
    System.out.printf("Error Code: %s, Message: %s\n", error.getCode(), error.getMessage());
  }
}

Python

# Build assigned targeting option objects to create.
createBrowserAssignedTargetingOptions = []
for targeting_id in create-browser-assigned-targeting-ids:
  createBrowserAssignedTargetingOptions.append(
      {'browserDetails': {'targetingOptionId': targeting_id}}
  )

# Create a bulk edit request.
bulk_edit_line_item_request = {
    'lineItemIds': line-item-ids,
    'deleteRequests': [
        {
            'targetingType': 'TARGETING_TYPE_BROWSER',
            'assignedTargetingOptionIds':
              delete-browser-assigned-targeting-ids
        },
        {
            'targetingType': 'TARGETING_TYPE_DEVICE_MAKE_MODEL',
            'assignedTargetingOptionIds':
              delete-device-make-model-assigned-targeting-ids
        }
    ],
    'createRequests': [
        {
            'targetingType': 'TARGETING_TYPE_BROWSER',
            'assignedTargetingOptions':
              createBrowserAssignedTargetingOptions
        }
    ]
}

# Edit the line item targeting.
response = service.advertisers().lineItems()\
  .bulkEditAssignedTargetingOptions(
    advertiserId=advertiser-id,
    body=bulk_edit_line_item_request
).execute()

# Print successfully updated line items.
if 'updatedLineItemIds' not in response:
  print("No line items were updated successfully.")
else:
  print("Targeting configurations for the following line item IDs were updated: %s"
        % response['updatedLineItemIds'])

# Print line items that failed to update.
if 'failedLineItemIds' not in response:
  print("No line items failed to update.")
else:
  print("Targeting configurations for the following line item IDs failed to update: %s"
        % response['failedLineItemIds'])
  if 'errors' in response:
    print("The failed updates were caused by the following errors:")
    for error in response["errors"]:
      print("Error code: %s, Message: %s" % (error["code"], error["message"]))

PHP

// Create delete request list.
$deleteRequests = array();

// Create and add browser assigned targeting option IDs to delete request list.
$deleteBrowserTargetingRequest =
    new Google_Service_DisplayVideo_DeleteAssignedTargetingOptionsRequest();
$deleteBrowserTargetingRequest->setTargetingType(
    "TARGETING_TYPE_BROWSER"
);
$deleteBrowserTargetingRequest->setAssignedTargetingOptionIds(
    delete-browser-assigned-targeting-ids
);
$deleteRequests[] = $deleteBrowserTargetingRequest;

// Create and add device assigned targeting option IDs to delete request list.
$deleteDeviceTargetingRequest =
    new Google_Service_DisplayVideo_DeleteAssignedTargetingOptionsRequest();
$deleteDeviceTargetingRequest->setTargetingType(
    "TARGETING_TYPE_DEVICE_MAKE_MODEL"
);
$deleteDeviceTargetingRequest->setAssignedTargetingOptionIds(
    delete-device-make-model-assigned-targeting-ids
);
$deleteRequests[] = $deleteDeviceTargetingRequest;

// Create create request list.
$createRequests = array();

// Create and populate list of browser assigned targetion options to create.
$createBrowserAssignedTargetingOptions = array();
foreach (create-browser-assigned-targeting-ids as $optionId) {
    $option = new Google_Service_DisplayVideo_AssignedTargetingOption();
    $details =
        new Google_Service_DisplayVideo_BrowserAssignedTargetingOptionDetails();
    $details->setTargetingOptionId($optionId);

    $option->setBrowserDetails($details);
    $createBrowserAssignedTargetingOptions[] = $option;
}

// Create and add browser assigned targeting option create request to create
// request list.
$createBrowserTargetingRequest =
    new Google_Service_DisplayVideo_CreateAssignedTargetingOptionsRequest();
$createBrowserTargetingRequest->setTargetingType(
    "TARGETING_TYPE_BROWSER"
);
$createBrowserTargetingRequest->setAssignedTargetingOptions(
    $createBrowserAssignedTargetingOptions
);
$createRequests[] = $createBrowserTargetingRequest;

// Create a bulk edit request and assign create and delete request lists.
$body =
    new Google_Service_DisplayVideo_BulkEditAssignedTargetingOptionsRequest();
$body->setLineItemIds(line-item-ids);
$body->setCreateRequests($createRequests);
$body->setDeleteRequests($deleteRequests);

// Call the API, editing the assigned targeting options for the identified
// line item.
$response = $service
    ->advertisers_lineItems
    ->bulkEditAssignedTargetingOptions(
        advertiser-id,
        $body
    );

// Print successfully updated line items.
if (!empty($response->getUpdatedLineItemIds())) {
    printf('Targeting configurations for the following line item IDs were updated:\n');
    foreach ($response->getUpdatedLineItemIds() as $id) {
        printf('%s\n', $id);
    }
} else {
    print('No line items were updated successfully.\n');
}

// Print line items that failed to update.
if (!empty($response->getFailedLineItemIds())) {
    print('Targeting configurations for the following line item IDs failed to update:\n');
    foreach ($response->getFailedLineItemIds() as $id) {
        printf('%s\n', $id);
    }
    print('The failed updates were caused by the following errors:\n');
    foreach ($response->getErrors() as $error) {
        printf('Error Code: %s, Message: %s\n', $error->getCode(), $error->getMessage());
    }
} else {
    print('No line items failed to update.\n');
}