制作推荐活动广告组广告

制作推荐活动广告涉及两个步骤:

  1. 创建 Ad 并将其 travel_ad 设置为 TravelAdInfo 的实例。

  2. 创建 AdGroupAd 并将 之前创建的 Ad

Java

private String addAddGroupAd(
    GoogleAdsClient googleAdsClient, long customerId, String adGroupResourceName) {
  // Creates a new travel ad.
  Ad ad = Ad.newBuilder().setTravelAd(TravelAdInfo.newBuilder()).build();
  // Creates a new ad group ad and sets its ad to the travel ad.
  AdGroupAd adGroupAd =
      AdGroupAd.newBuilder()
          // Sets the ad to the ad created above.
          .setAd(ad)
          // Set the ad group ad to enabled. Setting this to paused will cause an error for
          // Things to do campaigns. Pausing should happen at either the ad group or campaign
          // level.
          .setStatus(AdGroupAdStatus.ENABLED)
          // Sets the ad group.
          .setAdGroup(adGroupResourceName)
          .build();

  // Creates an ad group ad operation.
  AdGroupAdOperation operation = AdGroupAdOperation.newBuilder().setCreate(adGroupAd).build();

  // Issues a mutate request to add an ad group ad.
  try (AdGroupAdServiceClient adGroupAdServiceClient =
      googleAdsClient.getLatestVersion().createAdGroupAdServiceClient()) {
    MutateAdGroupAdResult mutateAdGroupAdResult =
        adGroupAdServiceClient
            .mutateAdGroupAds(Long.toString(customerId), Collections.singletonList(operation))
            .getResults(0);
    System.out.printf(
        "Added an ad group ad with resource name: '%s'%n",
        mutateAdGroupAdResult.getResourceName());
    return mutateAdGroupAdResult.getResourceName();
  }
}
      

C#

private static void CreateAdGroupAd(GoogleAdsClient client, long customerId,
    string adGroup)
{

    // Get the AdGroupAdService.
    AdGroupAdServiceClient adGroupAdService =
        client.GetService(Services.V17.AdGroupAdService);

    // Creates a new ad group ad and sets a travel ad info.
    AdGroupAd adGroupAd = new AdGroupAd()
    {
        Ad = new Ad()
        {
            TravelAd = new TravelAdInfo()
        },
        // Set the ad group ad to enabled. Setting this to paused will cause an error for
        // Things to do campaigns. Pausing should happen at either the ad group or campaign
        // level.
        Status = AdGroupAdStatus.Enabled,
        AdGroup = adGroup
    };

    MutateAdGroupAdsResponse response = adGroupAdService.MutateAdGroupAds(
        customerId.ToString(), new AdGroupAdOperation[] { new AdGroupAdOperation() {
            Create = adGroupAd
        }}
    );

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

PHP

private static function addAdGroupAd(
    GoogleAdsClient $googleAdsClient,
    int $customerId,
    string $adGroupResourceName
) {
    // Creates a new ad group ad and sets a travel ad info.
    $adGroupAd = new AdGroupAd([
        'ad' => new Ad(['travel_ad' => new TravelAdInfo()]),
        // Set the ad group ad to enabled. Setting this to paused will cause an error for Things
        // to do campaigns. Pausing should happen at either the ad group or campaign level.
        'status' => AdGroupAdStatus::ENABLED,
        // Sets the ad group.
        'ad_group' => $adGroupResourceName
    ]);

    // Creates an ad group ad operation.
    $adGroupAdOperation = new AdGroupAdOperation();
    $adGroupAdOperation->setCreate($adGroupAd);

    // Issues a mutate request to add an ad group ad.
    $adGroupAdServiceClient = $googleAdsClient->getAdGroupAdServiceClient();
    $response = $adGroupAdServiceClient->mutateAdGroupAds(
        MutateAdGroupAdsRequest::build($customerId, [$adGroupAdOperation])
    );

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

Python

def add_ad_group_ad(client, customer_id, ad_group_resource_name):
    """Creates a new ad group ad in the specified ad group.

    Args:
        client: an initialized GoogleAdsClient instance.
        customer_id: a client customer ID.
        ad_group_resource_name: the resource name of ad group that a new ad
            group ad will belong to.
    """
    # Creates an ad group ad operation.
    operation = client.get_type("AdGroupAdOperation")
    # Creates a new ad group ad and sets a travel ad info.
    ad_group_ad = operation.create
    # Sets the ad group ad to enabled. Setting this to paused will cause an error
    # for Things to do campaigns. Pausing should happen at either the ad group
    # or campaign level.
    ad_group_ad.status = client.enums.AdGroupAdStatusEnum.ENABLED
    ad_group_ad.ad.travel_ad = client.get_type("TravelAdInfo")
    # Sets the ad group.
    ad_group_ad.ad_group = ad_group_resource_name

    # Issues a mutate request to add an ad group ad.
    ad_group_ad_service = client.get_service("AdGroupAdService")
    response = ad_group_ad_service.mutate_ad_group_ads(
        customer_id=customer_id, operations=[operation]
    )

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

Ruby

def add_ad_group_ad(client, customer_id, ad_group_resource)
  # Creates a new ad group ad and sets a travel ad info.
  ad_group_ad_operation = client.operation.create_resource.ad_group_ad do |aga|
    aga.ad = client.resource.ad do |ad|
      ad.travel_ad = client.resource.travel_ad_info
    end
    # Set the ad group ad to enabled. Setting this to paused will cause an error
    # for Things to Do campaigns. Pausing should happen at either the ad group
    # or campaign level.
    aga.status = :ENABLED

    # Set the ad group.
    aga.ad_group = ad_group_resource
  end

  # Issue a mutate request to add the ad group ad.
  ad_group_ad_service = client.service.ad_group_ad
  response = ad_group_ad_service.mutate_ad_group_ads(
    customer_id: customer_id,
    operations: [ad_group_ad_operation],
  )

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

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

Perl

sub add_ad_group_ad {
  my ($api_client, $customer_id, $ad_group_resource_name) = @_;

  # Create an ad group ad and set a travel ad info.
  my $ad_group_ad = Google::Ads::GoogleAds::V17::Resources::AdGroupAd->new({
      # Set the ad group.
      adGroup => $ad_group_resource_name,
      ad      => Google::Ads::GoogleAds::V17::Resources::Ad->new({
          travelAd => Google::Ads::GoogleAds::V17::Common::TravelAdInfo->new()}
      ),
      # Set the ad group to enabled. Setting this to paused will cause an error
      # for Things to do campaigns. Pausing should happen at either the ad group
      # or campaign level.
      status => Google::Ads::GoogleAds::V17::Enums::AdGroupAdStatusEnum::ENABLED
    });

  # Create an ad group ad operation.
  my $ad_group_ad_operation =
    Google::Ads::GoogleAds::V17::Services::AdGroupAdService::AdGroupAdOperation
    ->new({create => $ad_group_ad});

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

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

  return $ad_group_ad_resource_name;
}