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 a 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(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();
  }
}

C#

private string AddProductShoppingAdGroupAd(GoogleAdsClient client, long customerId,
    string adGroupResourceName)
{
    // Get the AdGroupAdService.
    AdGroupAdServiceClient adGroupAdService = client.GetService(
        Services.V5.AdGroupAdService);

    // Creates a new shopping product ad.
    Ad ad = new Ad()
    {
        ShoppingProductAd = new ShoppingProductAdInfo()
        {
        }
    };

    // Creates a new ad group ad and sets the shopping product ad to it.
    AdGroupAd adGroupAd = new AdGroupAd()
    {
        // Sets the ad to the ad created above.
        Ad = ad,

        Status = AdGroupAdStatus.Paused,

        // Sets the ad group.
        AdGroup = adGroupResourceName
    };

    // Creates an ad group ad operation.
    AdGroupAdOperation operation = new AdGroupAdOperation()
    {
        Create = adGroupAd
    };

    // Issues a mutate request to add an ad group ad.
    MutateAdGroupAdResult mutateAdGroupAdResult = adGroupAdService.MutateAdGroupAds(
        customerId.ToString(), new AdGroupAdOperation[] { operation }).Results[0];
    Console.WriteLine("Added a product shopping ad group ad with resource name: '{0}'.",
        mutateAdGroupAdResult.ResourceName);
    return mutateAdGroupAdResult.ResourceName;
}


PHP

private static function addShoppingProductAdGroupAd(
    GoogleAdsClient $googleAdsClient,
    int $customerId,
    string $adGroupResourceName
) {
    // Creates a new shopping product ad.
    $ad = new Ad(['shopping_product_ad' => new ShoppingProductAdInfo()]);

    // Creates a new ad group ad and sets the shopping product ad to it.
    $adGroupAd = new AdGroupAd([
        'ad' => $ad,
        'status' => AdGroupAdStatus::PAUSED,
        // 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($customerId, [$adGroupAdOperation]);

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

Python

def add_shopping_product_ad_group_ad(
    client, customer_id, ad_group_resource_name
):
    """Creates a new shopping product ad group ad in the specified ad group."""
    ad_group_ad_service = client.get_service("AdGroupAdService", version="v5")

    # Creates a new ad group ad and sets the product ad to it.
    ad_group_ad_operation = client.get_type("AdGroupAdOperation", version="v5")
    ad_group_ad = ad_group_ad_operation.create
    ad_group_ad.ad_group = ad_group_resource_name
    ad_group_ad.status = client.get_type(
        "AdGroupAdStatusEnum", version="v5"
    ).PAUSED
    ad_group_ad.ad.shopping_product_ad.CopyFrom(
        client.get_type("ShoppingProductAdInfo")
    )

    # Add the ad group ad.
    try:
        ad_group_ad_response = ad_group_ad_service.mutate_ad_group_ads(
            customer_id, [ad_group_ad_operation]
        )
    except google.ads.google_ads.errors.GoogleAdsException as ex:
        print(
            'Request with ID "%s" failed with status "%s" and includes the '
            "following errors:" % (ex.request_id, ex.error.code().name)
        )
        for error in ex.failure.errors:
            print('\tError with message "%s".' % error.message)
            if error.location:
                for field_path_element in error.location.field_path_elements:
                    print("\t\tOn field: %s" % field_path_element.field_name)
        sys.exit(1)

    ad_group_ad_resource_name = ad_group_ad_response.results[0].resource_name

    print(
        "Created shopping product ad group ad %s." % ad_group_ad_resource_name
    )

    return ad_group_resource_name



Ruby

def add_shopping_product_ad_group_ad(client, customer_id, ad_group_name)

  operation = client.operation.create_resource.ad_group_ad do |ad_group_ad|
    ad_group_ad.ad_group = ad_group_name
    ad_group_ad.status = :PAUSED
    ad_group_ad.ad = client.resource.ad do |ad|
      ad.shopping_product_ad = client.resource.shopping_product_ad_info
    end
  end

  service = client.service.ad_group_ad
  response = service.mutate_ad_group_ads(
    customer_id: customer_id,
    operations: [operation],
  )

  puts "Created shopping product ad group ad " \
       "#{response.results.first.resource_name}"
end

Perl

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

  # Create an ad group ad and set a shopping product ad to it.
  my $ad_group_ad = Google::Ads::GoogleAds::V5::Resources::AdGroupAd->new({
      # Set the ad group.
      adGroup => $ad_group_resource_name,
      # Set the ad to a new shopping product ad.
      ad => Google::Ads::GoogleAds::V5::Resources::Ad->new({
          shoppingProductAd =>
            Google::Ads::GoogleAds::V5::Common::ShoppingProductAdInfo->new()}
      ),
      status => Google::Ads::GoogleAds::V5::Enums::AdGroupAdStatusEnum::PAUSED
    });

  # Create an ad group ad operation.
  my $ad_group_ad_operation =
    Google::Ads::GoogleAds::V5::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 a product shopping ad group ad with resource name: '%s'.\n",
    $ad_group_ad_resource_name;

  return $ad_group_ad_resource_name;
}

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(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();
  }
}

C#

private string AddSmartShoppingAdGroupAd(GoogleAdsClient client, long customerId,
    string adGroupResourceName)
{
    // Get the AdGroupAdService.
    AdGroupAdServiceClient adGroupAdService = client.GetService(
        Services.V5.AdGroupAdService);

    // Creates a new shopping product ad.
    Ad ad = new Ad()
    {
        ShoppingSmartAd = new ShoppingSmartAdInfo()
        {
        }
    };

    // Creates a new ad group ad and sets the shopping product ad to it.
    AdGroupAd adGroupAd = new AdGroupAd()
    {
        // Sets the ad to the ad created above.
        Ad = ad,

        Status = AdGroupAdStatus.Paused,

        // Sets the ad group.
        AdGroup = adGroupResourceName
    };

    // Creates an ad group ad operation.
    AdGroupAdOperation operation = new AdGroupAdOperation()
    {
        Create = adGroupAd
    };

    // Issues a mutate request to add an ad group ad.
    MutateAdGroupAdResult mutateAdGroupAdResult = adGroupAdService.MutateAdGroupAds(
        customerId.ToString(), new AdGroupAdOperation[] { operation }).Results[0];
    Console.WriteLine("Added a Smart Shopping ad group ad with resource name: '{0}'.",
        mutateAdGroupAdResult.ResourceName);
    return mutateAdGroupAdResult.ResourceName;
}


PHP

private static function addSmartShoppingAdGroupAd(
    GoogleAdsClient $googleAdsClient,
    int $customerId,
    string $adGroupResourceName
) {
    // Creates a new ad group ad.
    $adGroupAd = new AdGroupAd([
        // Sets a new Smart Shopping ad.
        'ad' => new Ad(['shopping_smart_ad' => new ShoppingSmartAdInfo()]),
        // 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 the ad group ad.
    $adGroupAdServiceClient = $googleAdsClient->getAdGroupAdServiceClient();
    $response = $adGroupAdServiceClient->mutateAdGroupAds($customerId, [$adGroupAdOperation]);

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

Ruby

def add_smart_shopping_ad_group_ad(client, customer_id, ad_group_name)

  operation = client.operation.create_resource.ad_group_ad do |ad_group_ad|
    ad_group_ad.ad_group = ad_group_name
    ad_group_ad.ad = client.resource.ad do |ad|
      ad.shopping_smart_ad = client.resource.shopping_smart_ad_info
    end
  end

  service = client.service.ad_group_ad
  response = service.mutate_ad_group_ads(
    customer_id: customer_id,
    operations: [operation],
  )

  puts "Created a Smart Shopping ad group ad " \
       "#{response.results.first.resource_name}"
end

Perl

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

  # Create an ad group ad and set a shopping smart ad to it.
  my $ad_group_ad = Google::Ads::GoogleAds::V5::Resources::AdGroupAd->new({
      # Set the ad group.
      adGroup => $ad_group_resource_name,
      # Set a new smart shopping ad.
      ad => Google::Ads::GoogleAds::V5::Resources::Ad->new({
          shoppingSmartAd =>
            Google::Ads::GoogleAds::V5::Common::ShoppingSmartAdInfo->new()}
      ),
      status => Google::Ads::GoogleAds::V5::Enums::AdGroupAdStatusEnum::PAUSED
    });

  # Create an ad group ad operation.
  my $ad_group_ad_operation =
    Google::Ads::GoogleAds::V5::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 a smart shopping ad group ad with resource name: '%s'.\n",
    $ad_group_ad_resource_name;

  return $ad_group_ad_resource_name;
}