التصنيفات

تتيح لك التصنيفات تصنيف حملاتك ومجموعاتك الإعلانية وإعلاناتك وكلماتك الرئيسية، واستخدام هذه التصنيفات لتبسيط سير عملك بطرق متنوّعة.

يتناول هذا الدليل الخطوات المطلوبة لتنفيذ ما يلي:

  • إنشاء تصنيفات آليًا باستخدام LabelService
  • يمكنك تعيين تصنيفات لحملاتك باستخدام طلبات CampaignLabelService.
  • يمكنك استرداد نتائج التقارير وفلترتها حسب التصنيف باستخدام طلبات GoogleAdsService.

يركّز هذا الدليل على الحملات، ولكن يمكنك استخدام الأسلوب نفسه للمجموعات الإعلانية والإعلانات والكلمات الرئيسية. توفّر واجهة برمجة التطبيقات هذه أيضًا CustomerLabelService، ما يتيح للحسابات الإدارية تعيين تصنيفات للحسابات الفرعية.

حالات الاستخدام

تشمل السيناريوهات النموذجية لاستخدام التصنيفات ما يلي:

  • يحتوي حسابك على حملات لا تفعّلها إلا خلال أوقات معيّنة من السنة، وتريد تضمين هذه الحملات أو استبعادها من التقارير.
  • أضفت مجموعة جديدة من الكلمات الرئيسية إلى مجموعتك الإعلانية وتريد مقارنة إحصاءاتها بإحصاءات الكلمات الرئيسية الأخرى في مجموعتك الإعلانية.
  • يدير مستخدمو حسابك على "إعلانات Google" مجموعة فرعية من الحملات، وتريد طريقة لتحديد مجموعة الحملات لكل مستخدم.
  • يجب أن يحدّد تطبيقك حالة عناصر معيّنة.

إنشاء التصنيفات

إنشاء تصنيفات باستخدام عنصر TextLabel:

  1. أنشئ مثيلاً من TextLabel.
  2. اضبط لون الخلفية لهذا TextLabel.
  3. أدخِل نصًا لهذا TextLabel باستخدام حقل الوصف.
  4. ضَع TextLabel بين علامتي LabelOperation وأرسِله إلى LabelService.MutateLabels.

دوِّن أرقام تعريف التصنيفات الجديدة لاستخدامها في الاستعلامات اللاحقة. تكون أرقام التعريف مضمّنة في الحقل resource_name ضمن MutateLabelResults المعروض في MutateLabelsResponse.

يمكنك أيضًا استخدام طلب LabelService.GetLabel أو طلب GoogleAdsService Search أو SearchStream لاسترداد أرقام التعريف.

تعيين التصنيفات

يمكنك تعيين تصنيفات لحملاتك أو عملائك أو مجموعاتك الإعلانية أو معاييرك أو إعلاناتك. استخدِم عملية Mutate في الخدمة المناسبة لتعيين التصنيفات.

على سبيل المثال، لتعيين تصنيفات لحملة، مرِّر قيمة واحدة أو أكثر من CampaignLabelOperation إلى 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.V20.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::V20::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::V20::Resources::CampaignLabel->new({
        campaign => Google::Ads::GoogleAds::V20::Utils::ResourceNames::campaign(
          $customer_id, $campaign_id
        ),
        label => $label_resource_name
      });

    # Create a campaign label operation.
    my $campaign_label_operation =
      Google::Ads::GoogleAds::V20::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 على سبيل المثال، يعرض طلب البحث التالي رقم التعريف والاسم والتصنيفات لكل حملة مرتبطة بأي من أرقام تعريف التصنيفات الثلاثة:

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. يستردّ هذا الحقل التصنيفات التي يملكها العميل الذي يرسل طلب البيانات من واجهة برمجة التطبيقات فقط.

استخدام التصنيفات في التقارير

استخدِم التصنيفات لتحسين فائدة تقاريرك.

إعداد التقارير عن التصنيفات

تعرض أداة تقرير التصنيفات تفاصيل حول التصنيفات المحدّدة في حساب. وتشمل التفاصيل الاسم والمعرّف واسم المورد والحالة ولون الخلفية والوصف، بالإضافة إلى مورد العميل الذي يمثّل مالك التصنيف.

التقارير التي تتضمّن مقاييس

تحتوي طرق عرض تقرير المجموعة الإعلانية والحملة على الحقل 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