制作推荐活动广告组

如需为推荐活动广告系列投放广告,您必须创建一个 AdGroup,且该广告组中至少包含一个广告。如下所示,广告系列仅支持 TRAVEL_ADS 类型的广告组,您可以在 type 字段中设置该类型。

Java

private String addAdGroup(
    GoogleAdsClient googleAdsClient, long customerId, String campaignResourceName) {
  // Creates an ad group.
  AdGroup adGroup =
      AdGroup.newBuilder()
          .setName("Earth to Mars Cruises #" + getPrintableDateTime())
          .setCampaign(campaignResourceName)
          // Sets the ad group type to TRAVEL_ADS. This cannot be set to other types.
          .setType(AdGroupType.TRAVEL_ADS)
          .setStatus(AdGroupStatus.ENABLED)
          .build();

  // Creates an ad group operation.
  AdGroupOperation operation = AdGroupOperation.newBuilder().setCreate(adGroup).build();

  // Issues a mutate request to add an ad group.
  try (AdGroupServiceClient adGroupServiceClient =
      googleAdsClient.getLatestVersion().createAdGroupServiceClient()) {
    MutateAdGroupResult mutateAdGroupResult =
        adGroupServiceClient
            .mutateAdGroups(Long.toString(customerId), Collections.singletonList(operation))
            .getResults(0);
    System.out.printf(
        "Added an ad group with resource name: '%s'%n", mutateAdGroupResult.getResourceName());
    return mutateAdGroupResult.getResourceName();
  }
}
      

C#

private static string CreateAdGroup(GoogleAdsClient client, long customerId,
    string campaign)
{
    // Get the AdGroupService.
    AdGroupServiceClient adGroupService = client.GetService(Services.V16.AdGroupService);

    // Create the ad group.
    AdGroup adGroup = new AdGroup()
    {
        Name = $"Earth to Mars Cruises #{ExampleUtilities.GetRandomString()}",
        Status = AdGroupStatus.Enabled,
        Campaign = campaign,
        Type = AdGroupType.TravelAds
    };

    MutateAdGroupsResponse response = adGroupService.MutateAdGroups(
        customerId.ToString(), new AdGroupOperation[] { new AdGroupOperation() {
            Create = adGroup
        }}
    );

    string adGroupResourceName = response.Results[0].ResourceName;
    Console.WriteLine("Ad group with resource name = '{0}' was added.", adGroupResourceName);

    return adGroupResourceName;
}
      

PHP

private static function addAdGroup(
    GoogleAdsClient $googleAdsClient,
    int $customerId,
    string $campaignResourceName
) {
    // Creates an ad group.
    $adGroup = new AdGroup([
        'name' => 'Earth to Mars Cruise #' . Helper::getPrintableDatetime(),
        // Sets the campaign.
        'campaign' => $campaignResourceName,
        // Sets the ad group type to TRAVEL_ADS. This cannot be set to other types.
        'type' => AdGroupType::TRAVEL_ADS,
        'status' => AdGroupStatus::ENABLED,
    ]);

    // Creates an ad group operation.
    $adGroupOperation = new AdGroupOperation();
    $adGroupOperation->setCreate($adGroup);

    // Issues a mutate request to add an ad group.
    $adGroupServiceClient = $googleAdsClient->getAdGroupServiceClient();
    $response = $adGroupServiceClient->mutateAdGroups(
        MutateAdGroupsRequest::build($customerId, [$adGroupOperation])
    );

    /** @var AdGroup $addedAdGroup */
    $addedAdGroup = $response->getResults()[0];
    printf(
        "Added an ad group with resource name '%s'.%s",
        $addedAdGroup->getResourceName(),
        PHP_EOL
    );

    return $addedAdGroup->getResourceName();
}
      

Python

def add_ad_group(client, customer_id, campaign_resource_name):
    """Creates a new ad group in the specified Things to do campaign.

    Args:
        client: an initialized GoogleAdsClient instance.
        customer_id: a client customer ID.
        campaign_resource_name: the resource name of campaign that a new ad
            group will belong to.

    Returns:
        The resource name of the newly created ad group.
    """
    # Creates an ad group operation.
    operation = client.get_type("AdGroupOperation")
    # Creates an ad group.
    ad_group = operation.create
    ad_group.name = f"Earth to Mars cruise #{get_printable_datetime()}"
    # Sets the campaign.
    ad_group.campaign = campaign_resource_name
    # Sets the ad group type to TRAVEL_ADS. This is the only value allowed
    # for this field on an ad group for a Things to do campaign.
    ad_group.type_ = client.enums.AdGroupTypeEnum.TRAVEL_ADS
    ad_group.status = client.enums.AdGroupStatusEnum.ENABLED

    # Issues a mutate request to add an ad group.
    ad_group_service = client.get_service("AdGroupService")
    ad_group_response = ad_group_service.mutate_ad_groups(
        customer_id=customer_id, operations=[operation]
    )

    resource_name = ad_group_response.results[0].resource_name
    print(f"Added an ad group with resource name: '{resource_name}'.")
    return resource_name
      

Ruby

def add_ad_group(client, customer_id, campaign_resource)
  # Create an ad group.
  ad_group_operation = client.operation.create_resource.ad_group do |ag|
    ag.name = generate_random_name_field("Earth to Mars Cruise")

    # Set the campaign.
    ag.campaign = campaign_resource

    # Set the ad group type to TRAVEL_ADS.
    # This cannot be set to other types.
    ag.type = :TRAVEL_ADS
    ag.status = :ENABLED
  end

  # Issue a mutate request to add the ad group.
  ad_group_service = client.service.ad_group
  response = ad_group_service.mutate_ad_groups(
    customer_id: customer_id,
    operations: [ad_group_operation]
  )

  # Fetch the new ad group's resource name.
  ad_group_resource = response.results.first.resource_name

  puts "Added an ad group with resource name '#{ad_group_resource}'."

  ad_group_resource
end
      

Perl

sub add_ad_group {
  my ($api_client, $customer_id, $campaign_resource_name) = @_;

  # Create an ad group.
  my $ad_group = Google::Ads::GoogleAds::V16::Resources::AdGroup->new({
    name => "Earth to Mars Cruise #" . uniqid(),
    # Set the campaign.
    campaign => $campaign_resource_name,
    # Set the ad group type to TRAVEL_ADS.
    # This cannot be set to other types.
    type   => TRAVEL_ADS,
    status => Google::Ads::GoogleAds::V16::Enums::AdGroupStatusEnum::ENABLED
  });

  # Create an ad group operation.
  my $ad_group_operation =
    Google::Ads::GoogleAds::V16::Services::AdGroupService::AdGroupOperation->
    new({create => $ad_group});

  # Add the ad group.
  my $ad_group_resource_name = $api_client->AdGroupService()->mutate({
      customerId => $customer_id,
      operations => [$ad_group_operation]})->{results}[0]{resourceName};

  printf "Added an ad group with resource name: '%s'.\n",
    $ad_group_resource_name;

  return $ad_group_resource_name;
}