ป้ายกำกับ

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

คู่มือนี้ครอบคลุมขั้นตอนที่จำเป็นในการทำสิ่งต่อไปนี้

  • สร้างป้ายกำกับแบบเป็นโปรแกรมโดยใช้ LabelService
  • กำหนดป้ายกำกับให้กับแคมเปญโดยใช้คำขอ CampaignLabelService
  • ดึงและกรองผลลัพธ์ของรายงานตามป้ายกำกับโดยใช้การค้นหา GoogleAdsService

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

Use Case

สถานการณ์ทั่วไปในการใช้ป้ายกำกับมีดังนี้

  • บัญชีของคุณมีแคมเปญที่เปิดใช้เฉพาะบางช่วงเวลาของปี และคุณต้องการรวมหรือยกเว้นแคมเปญเหล่านั้นในรายงานอย่างง่ายดาย
  • คุณเพิ่มชุดคีย์เวิร์ดใหม่ลงในกลุ่มโฆษณา และต้องการเปรียบเทียบสถิติของคีย์เวิร์ดกับคีย์เวิร์ดอื่นๆ ในกลุ่มโฆษณา
  • ผู้ใช้แต่ละคนของบัญชี Google Ads จะจัดการชุดย่อยของแคมเปญ และคุณต้องการวิธีระบุชุดแคมเปญสำหรับผู้ใช้แต่ละราย
  • แอปของคุณจำเป็นต้องทำเครื่องหมายสถานะของออบเจ็กต์บางอย่าง

สร้างป้ายกำกับ

สร้างป้ายกำกับด้วยออบเจ็กต์ TextLabel ด้วยวิธีดังนี้

  1. สร้างอินสแตนซ์ TextLabel
  2. ตั้งค่าสีพื้นหลังสำหรับ TextLabel นี้
  3. ป้อนข้อความสำหรับ TextLabel นี้โดยใช้ช่องคำอธิบาย
  4. รวม TextLabel ใน LabelOperation แล้วส่งไปยัง LabelService.MutateLabels

จดบันทึกรหัสของป้ายกำกับใหม่สำหรับการค้นหาในภายหลัง ระบบจะฝังรหัสในช่อง resource_name ใน MutateLabelResults ที่แสดงผลใน MutateLabelsResponse

นอกจากนี้ คุณยังใช้คำขอ LabelService.GetLabel หรือคำขอ GoogleAdsService Search หรือ SearchStream เพื่อดึงข้อมูลรหัสได้ด้วย

กำหนดป้ายกำกับ

คุณสามารถกำหนดป้ายกำกับให้กับแคมเปญ ลูกค้า กลุ่มโฆษณา เกณฑ์ หรือโฆษณา ใช้การดำเนินการ Mutate ในบริการที่เหมาะสมเพื่อกำหนดป้ายกำกับ

ตัวอย่างเช่น หากต้องการกำหนดป้ายกำกับให้กับแคมเปญ ให้ส่ง CampaignLabelOperation อย่างน้อย 1 รายการไปยัง CampaignLabelService.MutateCampaignLabels CampaignLabelOperation แต่ละรายการจะมีอินสแตนซ์ CampaignLabel ซึ่งประกอบด้วยช่องต่อไปนี้

  • label: รหัสของป้ายกำกับ
  • campaign: รหัสของแคมเปญ

สร้างอินสแตนซ์ CampaignLabel สำหรับคู่แคมเปญป้ายกำกับแต่ละคู่ รวมไว้ใน CampaignLabelOperation ด้วยการดำเนินการ create แล้วส่งไปยัง CampaignService.MutateCampaignLabels

เพิ่มป้ายกำกับแคมเปญ

ตัวอย่างโค้ดที่แสดงวิธีเพิ่มป้ายกำกับแคมเปญลงในรายการแคมเปญมีดังนี้

Java

private void runExample(
    GoogleAdsClient googleAdsClient, long customerId, List<Long> campaignIds, Long labelId) {
  // Gets the resource name of the label to be added across all given campaigns.
  String labelResourceName = ResourceNames.label(customerId, labelId);

  List<CampaignLabelOperation> operations = new ArrayList<>(campaignIds.size());
  // Creates a campaign label operation for each campaign.
  for (Long campaignId : campaignIds) {
    // Gets the resource name of the given campaign.
    String campaignResourceName = ResourceNames.campaign(customerId, campaignId);
    // Creates the campaign label.
    CampaignLabel campaignLabel =
        CampaignLabel.newBuilder()
            .setCampaign(campaignResourceName)
            .setLabel(labelResourceName)
            .build();

    operations.add(CampaignLabelOperation.newBuilder().setCreate(campaignLabel).build());
  }

  try (CampaignLabelServiceClient campaignLabelServiceClient =
      googleAdsClient.getLatestVersion().createCampaignLabelServiceClient()) {
    MutateCampaignLabelsResponse response =
        campaignLabelServiceClient.mutateCampaignLabels(Long.toString(customerId), operations);
    System.out.printf("Added %d campaign labels:%n", response.getResultsCount());
    for (MutateCampaignLabelResult result : response.getResultsList()) {
      System.out.println(result.getResourceName());
    }
  }
}
      

C#

public void Run(GoogleAdsClient client, long customerId, long[] campaignIds, long labelId)
{
    // Get the CampaignLabelServiceClient.
    CampaignLabelServiceClient campaignLabelService =
        client.GetService(Services.V16.CampaignLabelService);

    // Gets the resource name of the label to be added across all given campaigns.
    string labelResourceName = ResourceNames.Label(customerId, labelId);

    List<CampaignLabelOperation> operations = new List<CampaignLabelOperation>();
    // Creates a campaign label operation for each campaign.
    foreach (long campaignId in campaignIds)
    {
        // Gets the resource name of the given campaign.
        string campaignResourceName = ResourceNames.Campaign(customerId, campaignId);
        // Creates the campaign label.
        CampaignLabel campaignLabel = new CampaignLabel()
        {
            Campaign = campaignResourceName,
            Label = labelResourceName
        };

        operations.Add(new CampaignLabelOperation()
        {
            Create = campaignLabel
        });
    }

    // Send the operation in a mutate request.
    try
    {
        MutateCampaignLabelsResponse response =
            campaignLabelService.MutateCampaignLabels(customerId.ToString(), operations);
        Console.WriteLine($"Added {response.Results} campaign labels:");

        foreach (MutateCampaignLabelResult result in response.Results)
        {
            Console.WriteLine(result.ResourceName);
        }
    }
    catch (GoogleAdsException e)
    {
        Console.WriteLine("Failure:");
        Console.WriteLine($"Message: {e.Message}");
        Console.WriteLine($"Failure: {e.Failure}");
        Console.WriteLine($"Request ID: {e.RequestId}");
        throw;
    }
}
      

PHP

public static function runExample(
    GoogleAdsClient $googleAdsClient,
    int $customerId,
    array $campaignIds,
    int $labelId
) {
    // Gets the resource name of the label to be added across all given campaigns.
    $labelResourceName = ResourceNames::forLabel($customerId, $labelId);

    // Creates a campaign label operation for each campaign.
    $operations = [];
    foreach ($campaignIds as $campaignId) {
        // Creates the campaign label.
        $campaignLabel = new CampaignLabel([
            'campaign' => ResourceNames::forCampaign($customerId, $campaignId),
            'label' => $labelResourceName
        ]);
        $campaignLabelOperation = new CampaignLabelOperation();
        $campaignLabelOperation->setCreate($campaignLabel);
        $operations[] = $campaignLabelOperation;
    }

    // Issues a mutate request to add the labels to the campaigns.
    $campaignLabelServiceClient = $googleAdsClient->getCampaignLabelServiceClient();
    $response = $campaignLabelServiceClient->mutateCampaignLabels(
        MutateCampaignLabelsRequest::build($customerId, $operations)
    );

    printf("Added %d campaign labels:%s", $response->getResults()->count(), PHP_EOL);

    foreach ($response->getResults() as $addedCampaignLabel) {
        /** @var CampaignLabel $addedCampaignLabel */
        printf(
            "New campaign label added with resource name: '%s'.%s",
            $addedCampaignLabel->getResourceName(),
            PHP_EOL
        );
    }
}
      

Python

def main(client, customer_id, label_id, campaign_ids):
    """This code example adds a campaign label to a list of campaigns.

    Args:
        client: An initialized GoogleAdsClient instance.
        customer_id: A client customer ID str.
        label_id: The ID of the label to attach to campaigns.
        campaign_ids: A list of campaign IDs to which the label will be added.
    """

    # Get an instance of CampaignLabelService client.
    campaign_label_service = client.get_service("CampaignLabelService")
    campaign_service = client.get_service("CampaignService")
    label_service = client.get_service("LabelService")

    # Build the resource name of the label to be added across the campaigns.
    label_resource_name = label_service.label_path(customer_id, label_id)

    operations = []

    for campaign_id in campaign_ids:
        campaign_resource_name = campaign_service.campaign_path(
            customer_id, campaign_id
        )
        campaign_label_operation = client.get_type("CampaignLabelOperation")

        campaign_label = campaign_label_operation.create
        campaign_label.campaign = campaign_resource_name
        campaign_label.label = label_resource_name
        operations.append(campaign_label_operation)

    response = campaign_label_service.mutate_campaign_labels(
        customer_id=customer_id, operations=operations
    )
    print(f"Added {len(response.results)} campaign labels:")
    for result in response.results:
        print(result.resource_name)
      

Ruby

def add_campaign_label(customer_id, label_id, campaign_ids)
  # GoogleAdsClient will read a config file from
  # ENV['HOME']/google_ads_config.rb when called without parameters
  client = Google::Ads::GoogleAds::GoogleAdsClient.new

  label_resource_name = client.path.label(customer_id, label_id)

  labels = campaign_ids.map { |campaign_id|
    client.resource.campaign_label do |label|
      campaign_resource_name = client.path.campaign(customer_id, campaign_id)
      label.campaign = campaign_resource_name
      label.label = label_resource_name
    end
  }

  ops = labels.map { |label|
    client.operation.create_resource.campaign_label(label)
  }

  response = client.service.campaign_label.mutate_campaign_labels(
    customer_id: customer_id,
    operations: ops,
  )
  response.results.each do |result|
    puts("Created campaign label with id: #{result.resource_name}")
  end
end
      

Perl

sub add_campaign_labels {
  my ($api_client, $customer_id, $campaign_ids, $label_id) = @_;

  my $label_resource_name =
    Google::Ads::GoogleAds::V16::Utils::ResourceNames::label($customer_id,
    $label_id);

  my $campaign_label_operations = [];

  # Create a campaign label operation for each campaign.
  foreach my $campaign_id (@$campaign_ids) {
    # Create a campaign label.
    my $campaign_label =
      Google::Ads::GoogleAds::V16::Resources::CampaignLabel->new({
        campaign => Google::Ads::GoogleAds::V16::Utils::ResourceNames::campaign(
          $customer_id, $campaign_id
        ),
        label => $label_resource_name
      });

    # Create a campaign label operation.
    my $campaign_label_operation =
      Google::Ads::GoogleAds::V16::Services::CampaignLabelService::CampaignLabelOperation
      ->new({
        create => $campaign_label
      });

    push @$campaign_label_operations, $campaign_label_operation;
  }

  # Add the campaign labels to the campaigns.
  my $campaign_labels_response = $api_client->CampaignLabelService()->mutate({
    customerId => $customer_id,
    operations => $campaign_label_operations
  });

  my $campaign_label_results = $campaign_labels_response->{results};
  printf "Added %d campaign labels:\n", scalar @$campaign_label_results;

  foreach my $campaign_label_result (@$campaign_label_results) {
    printf "Created campaign label '%s'.\n",
      $campaign_label_result->{resourceName};
  }

  return 1;
}
      

ดึงข้อมูลออบเจ็กต์โดยใช้ป้ายกำกับ

หลังจากกำหนดป้ายกำกับให้กับแคมเปญแล้ว คุณจะใช้ช่องป้ายกำกับเพื่อดึงข้อมูลออบเจ็กต์ตามรหัสได้

ส่งต่อคำค้นหา GAQL ที่เหมาะสมไปยังคำขอ GoogleAdsService Search หรือ SearchStream ตัวอย่างเช่น การค้นหาต่อไปนี้จะแสดงรหัส ชื่อ และป้ายกำกับของแต่ละแคมเปญที่เชื่อมโยงกับรหัสป้ายกำกับหนึ่งใน 3 รหัสต่อไปนี้

SELECT
  campaign.id,
  campaign.name,
  label.id,
  label.name
FROM campaign_label
WHERE label.id IN (123456, 789012, 345678)

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

SELECT
  label.id,
  label.name
FROM label
WHERE label.name = "LABEL_NAME"

ดึงข้อมูลป้ายกำกับที่ใช้กับลูกค้า

เมื่อดูลำดับชั้นของบัญชีภายใต้บัญชีดูแลจัดการ คุณจะเรียกข้อมูลรายการป้ายกำกับที่ใช้กับบัญชีลูกค้าย่อยได้โดยขอช่อง applied_labels จากออบเจ็กต์ CustomerClient ช่องนี้จะดึงเฉพาะป้ายกำกับที่ลูกค้าเป็นเจ้าของในการเรียก API

ใช้ป้ายกำกับในรายงาน

การรายงานป้ายกำกับ

ทรัพยากรรายงานป้ายกำกับจะแสดงรายละเอียดเกี่ยวกับป้ายกำกับที่กำหนดในบัญชี รายละเอียดต่างๆ ได้แก่ ชื่อ รหัส ชื่อทรัพยากร สถานะ สีพื้นหลัง และคำอธิบาย ตลอดจนทรัพยากรลูกค้าที่แสดงถึงเจ้าของป้ายกำกับ

รายงานที่มีเมตริก

มุมมองรายงานกลุ่มโฆษณาและแคมเปญจะมีช่อง labels บริการการรายงานจะแสดงชื่อทรัพยากรของป้ายกำกับในรูปแบบ customers/{customer_id}/labels/{label_id} เช่น ชื่อทรัพยากร customers/123456789/labels/012345 หมายถึงป้ายกำกับที่มีรหัส 012345 ในบัญชีที่มีรหัส 123456789

รายงานที่ไม่มีเมตริก

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

คุณกรองผลลัพธ์ของรายงานด้านบนได้โดยเปรียบเทียบช่อง label.id โดยใช้โอเปอเรเตอร์เปรียบเทียบตัวเลขใดก็ได้ หรือโอเปอเรเตอร์ BETWEEN, IS NULL, IS NOT NULL, IN หรือ NOT IN

ตัวอย่างเช่น คุณสามารถรับแคมเปญทั้งหมดที่มีรหัสป้ายกำกับเฉพาะดังนี้

SELECT
  campaign.id,
  campaign.name,
  label.id,
  label.name
FROM campaign_label
WHERE label.id = LABEL_ID
ORDER BY campaign.id