Google Ads API is returning to beta status. Please read our blog post for more details.

Creating a Shopping Ad Group Ad

Standard Shopping campaigns

Creating a Shopping product ad involves two steps:

  1. Create a ShoppingProductAdInfo object and set it as the shopping_product_ad field of a new Ad object.

  2. Create an AdGroupAd and set the ad field to the previously created Ad object.

This code example demonstrates how to create an SHOPPING_PRODUCT_AD ad group ad for a standard Shopping campaign.

Java

private String addShoppingProductAdGroupAd(
    GoogleAdsClient googleAdsClient, long customerId, String adGroupResourceName) {
  // Creates a new shopping product ad.
  Ad ad =
      Ad.newBuilder().setShoppingProductAd(ShoppingProductAdInfo.newBuilder().build()).build();
  // Creates a new ad group ad and sets the shopping product ad to it.
  AdGroupAd adGroupAd =
      AdGroupAd.newBuilder()
          // Sets the ad to the ad created above.
          .setAd(ad)
          .setStatus(AdGroupAdStatus.PAUSED)
          // Sets the ad group.
          .setAdGroup(StringValue.of(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 a product shopping ad group ad with resource name: '%s'%n",
        mutateAdGroupAdResult.getResourceName());
    return mutateAdGroupAdResult.getResourceName();
  }
}

Smart Shopping campaigns

Creating a Smart Shopping ad involves two steps:

  1. Create a ShoppingSmartAdInfo object and set it as the shopping_smart_ad field of a new Ad object.

  2. Create an AdGroupAd and set the ad field to the previously created Ad object.

Java

private String addSmartShoppingAdGroupAd(
    GoogleAdsClient googleAdsClient, long customerId, String adGroupResourceName) {
  // Creates a new Smart Shopping ad.
  Ad ad = Ad.newBuilder().setShoppingSmartAd(ShoppingSmartAdInfo.newBuilder().build()).build();
  // Creates a new ad group ad and sets the Smart Shopping ad to it.
  AdGroupAd adGroupAd =
      AdGroupAd.newBuilder()
          // Sets the ad to the ad created above.
          .setAd(ad)
          // Sets the ad group.
          .setAdGroup(StringValue.of(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 a Smart Shopping ad group ad with resource name: '%s'%n",
        mutateAdGroupAdResult.getResourceName());
    return mutateAdGroupAdResult.getResourceName();
  }
}

Skicka feedback om ...

Google Ads API Beta
Google Ads API Beta
Behöver du hjälp? Besök vår supportsida.