תוויות

תוויות מאפשרות לכם לחלק את הקמפיינים, קבוצות המודעות, המודעות ומילות המפתח, ולהשתמש בקטגוריות האלה כדי לפשט את תהליך העבודה במגוון דרכים.

במדריך הזה מפורטות הפעולות שצריך לבצע כדי:

  • כדי ליצור תוויות באופן פרוגרמטי באמצעות LabelService.
  • מקצים תוויות לקמפיינים באמצעות בקשות CampaignLabelService.
  • אחזור וסינון של תוצאות הדוח לפי תווית באמצעות שאילתות GoogleAdsService.

המדריך הזה מתמקד בקמפיינים, אבל אפשר להשתמש באותה גישה לקבוצות של מודעות, למודעות ולמילות מפתח. הערה: ה-API מספק גם CustomerLabelService, שמאפשר לחשבונות ניהול להקצות תוויות לחשבונות צאצא.

תרחישים לדוגמה

תרחישים אופייניים לשימוש בתוויות:

  • יש בחשבון שלכם קמפיינים שאתם מפעילים רק בתקופות מסוימות בשנה, ואתם רוצים בקלות לכלול את הקמפיינים האלה בדוחות או להחריג אותם מהם.
  • הוספתם קבוצה חדשה של מילות מפתח לקבוצת המודעות שלכם, ואתם רוצים להשוות את הנתונים הסטטיסטיים שלהן למילות המפתח האחרות בקבוצת המודעות.
  • המשתמשים בחשבון 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 אל 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.V17.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::V17::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::V17::Resources::CampaignLabel->new({
        campaign => Google::Ads::GoogleAds::V17::Utils::ResourceNames::campaign(
          $customer_id, $campaign_id
        ),
        label => $label_resource_name
      });

    # Create a campaign label operation.
    my $campaign_label_operation =
      Google::Ads::GoogleAds::V17::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. בשדה הזה מאחזרים רק את התוויות שבבעלות הלקוח שביצע את הקריאה ל-API.

שימוש בתוויות בדוחות

דיווח על תוויות

בדוח Label מוצג פרטים על התוויות שהוגדרו בחשבון. הפרטים כוללים את השם, המזהה, שם המשאב, הסטטוס, צבע הרקע והתיאור, וגם את המשאב לקוח שמייצג את בעלי התווית.

דוחות עם מדדים

תצוגות הדוח קבוצת מודעות וקמפיין מכילות את השדה 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