Nhãn

Nhãn giúp bạn phân loại chiến dịch, nhóm quảng cáo, quảng cáo và từ khoá, đồng thời sử dụng các danh mục đó để đơn giản hoá quy trình làm việc theo nhiều cách.

Hướng dẫn này trình bày các bước cần thiết để thực hiện những việc sau:

  • Tạo nhãn theo phương thức lập trình bằng cách sử dụng LabelService.
  • Chỉ định nhãn cho chiến dịch bằng cách sử dụng các yêu cầu CampaignLabelService.
  • Truy xuất và lọc kết quả báo cáo theo nhãn bằng cách sử dụng các truy vấn GoogleAdsService.

Hướng dẫn này tập trung vào chiến dịch, nhưng bạn có thể sử dụng cùng một phương pháp cho nhóm quảng cáo, quảng cáo và từ khoá. API này cũng cung cấp CustomerLabelService, cho phép tài khoản người quản lý chỉ định nhãn cho tài khoản con.

Trường hợp sử dụng

Sau đây là các trường hợp điển hình để sử dụng nhãn:

  • Tài khoản của bạn có những chiến dịch mà bạn chỉ bật vào một số thời điểm nhất định trong năm và bạn muốn đưa hoặc loại trừ những chiến dịch đó khỏi báo cáo.
  • Bạn đã thêm một bộ từ khoá mới vào nhóm quảng cáo và muốn so sánh số liệu thống kê của bộ từ khoá đó với các từ khoá khác trong nhóm quảng cáo.
  • Mỗi người dùng trong tài khoản Google Ads của bạn quản lý một nhóm nhỏ chiến dịch và bạn muốn có cách xác định nhóm chiến dịch cho từng người dùng.
  • Ứng dụng của bạn cần đánh dấu trạng thái của một số đối tượng.

Tạo nhãn

Tạo nhãn bằng đối tượng TextLabel:

  1. Tạo một thực thể TextLabel.
  2. Đặt màu nền cho TextLabel này.
  3. Nhập văn bản cho TextLabel này bằng trường nội dung mô tả.
  4. Bọc TextLabel trong một LabelOperation rồi gửi đến LabelService.MutateLabels.

Ghi lại mã nhận dạng của nhãn mới để dùng cho các truy vấn sau này. Các mã nhận dạng được nhúng trong trường resource_name trong MutateLabelResults được trả về trong MutateLabelsResponse.

Bạn có thể sử dụng yêu cầu GoogleAdsService Search hoặc SearchStream để truy xuất các mã nhận dạng.

Chỉ định nhãn

Bạn có thể chỉ định nhãn cho chiến dịch, khách hàng, nhóm quảng cáo, tiêu chí hoặc quảng cáo. Sử dụng thao tác Mutate trong dịch vụ thích hợp để chỉ định nhãn.

Ví dụ: để chỉ định nhãn cho một chiến dịch, hãy truyền một hoặc nhiều CampaignLabelOperation đến CampaignLabelService.MutateCampaignLabels. Mỗi CampaignLabelOperation đều có một thực thể CampaignLabel chứa các trường sau:

  • label: Mã nhận dạng của nhãn
  • campaign: Mã nhận dạng của một chiến dịch

Tạo một phiên bản CampaignLabel cho mỗi cặp nhãn-chiến dịch. Gói yêu cầu này trong một CampaignLabelOperation bằng thao tác create rồi gửi đến CampaignService.MutateCampaignLabels.

Thêm nhãn chiến dịch

Sau đây là ví dụ về mã cho thấy cách thêm nhãn chiến dịch vào danh sách chiến dịch:

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.V22.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
        );
    }
}