Learn more about migrating to the Google Ads API by watching our latest webinar

Create ad group and ad group ad

Create an ad group

Similar to the Campaign created in previous steps, this ad group requires minimal configuration because it is optimized automatically.

Key requirements for Smart campaign ad groups:

Below we set the campaign field to the temporary resource name used to create a campaign in earlier steps.

Java

private MutateOperation createAdGroupOperation(long customerId) {
  MutateOperation.Builder builder = MutateOperation.newBuilder();
  builder
      .getAdGroupOperationBuilder()
      .getCreateBuilder()
      .setResourceName(ResourceNames.adGroup(customerId, AD_GROUP_TEMPORARY_ID))
      .setName("Smart campaign ad group " + CodeSampleHelper.getShortPrintableDateTime())
      .setCampaign(ResourceNames.campaign(customerId, SMART_CAMPAIGN_TEMPORARY_ID))
      .setType(AdGroupType.SMART_CAMPAIGN_ADS);
  return builder.build();
}
      

C#

/// <summary>
/// Creates a MutateOperation that creates a new ad group.
/// A temporary ID will be used in the campaign resource name for this ad group to associate
/// it with the Smart campaign created in earlier steps. A temporary ID will also be used
/// for its own resource name so that we can associate an ad group ad with it later in the
/// process.
/// Only one ad group can be created for a given Smart campaign.
/// </summary>
/// <param name="customerId">The Google Ads customer ID.</param>
/// <returns>A MutateOperation that creates a new ad group.</returns>
private MutateOperation CreateAdGroupOperation(long customerId)
{
    return new MutateOperation
    {
        AdGroupOperation = new AdGroupOperation
        {
            Create = new AdGroup
            {
                // Set the ad group ID to a temporary ID.
                ResourceName = ResourceNames.AdGroup(customerId, AD_GROUP_TEMPORARY_ID),
                Name = $"Smart campaign ad group #{ExampleUtilities.GetRandomString()}",
                // Set the campaign ID to a temporary ID.
                Campaign = ResourceNames.Campaign(customerId, SMART_CAMPAIGN_TEMPORARY_ID),
                // The ad group type must be SmartCampaignAds.
                Type = AdGroupType.SmartCampaignAds
            }
        }
    };
}
      

PHP

private static function createAdGroupOperation(int $customerId): MutateOperation
{
    // Creates the ad group object.
    $adGroup = new AdGroup([
        // Sets the ad group ID to a temporary ID.
        'resource_name' => ResourceNames::forAdGroup($customerId, self::AD_GROUP_TEMPORARY_ID),
        'name' => "Smart campaign ad group #" . Helper::getPrintableDatetime(),
        // Sets the campaign ID to a temporary ID.
        'campaign' =>
            ResourceNames::forCampaign($customerId, self::SMART_CAMPAIGN_TEMPORARY_ID),
        // The ad group type must be set to SMART_CAMPAIGN_ADS.
        'type' => AdGroupType::SMART_CAMPAIGN_ADS
    ]);

    // Creates the MutateOperation that creates the ad group.
    return new MutateOperation([
        'ad_group_operation' => new AdGroupOperation(['create' => $adGroup])
    ]);
}
      

Python

def _create_ad_group_operation(client, customer_id):
    """Creates a MutateOperation that creates a new ad group.

    A temporary ID will be used in the campaign resource name for this
    ad group to associate it with the Smart campaign created in earlier steps.
    A temporary ID will also be used for its own resource name so that we can
    associate an ad group ad with it later in the process.

    Only one ad group can be created for a given Smart campaign.

    Args:
        client: an initialized GoogleAdsClient instance.
        customer_id: a client customer ID.

    Returns:
        a MutateOperation that creates a new ad group.
    """
    mutate_operation = client.get_type("MutateOperation")
    ad_group = mutate_operation.ad_group_operation.create
    # Set the ad group ID to a temporary ID.
    ad_group.resource_name = client.get_service("AdGroupService").ad_group_path(
        customer_id, _AD_GROUP_TEMPORARY_ID
    )
    ad_group.name = f"Smart campaign ad group #{uuid4()}"
    # Set the campaign ID to a temporary ID.
    ad_group.campaign = client.get_service("CampaignService").campaign_path(
        customer_id, _SMART_CAMPAIGN_TEMPORARY_ID
    )
    # The ad group type must be set to SMART_CAMPAIGN_ADS.
    ad_group.type_ = client.get_type(
        "AdGroupTypeEnum"
    ).AdGroupType.SMART_CAMPAIGN_ADS

    return mutate_operation
      

Ruby

# Creates a mutate_operation that creates a new ad group.
# A temporary ID will be used in the campaign resource name for this
# ad group to associate it with the Smart campaign created in earlier steps.
# A temporary ID will also be used for its own resource name so that we can
# associate an ad group ad with it later in the process.
# Only one ad group can be created for a given Smart campaign.
def create_ad_group_operation(client, customer_id)
  mutate_operation = client.operation.mutate do |m|
    m.ad_group_operation = client.operation.create_resource.ad_group do |ag|
      # Set the ad group ID to a temporary ID.
      ag.resource_name = client.path.ad_group(customer_id, AD_GROUP_TEMPORARY_ID)
      ag.name = "Smart campaign ad group ##{(Time.new.to_f * 1000).to_i}"
      # Set the campaign ID to a temporary ID.
      ag.campaign = client.path.campaign(customer_id, SMART_CAMPAIGN_TEMPORARY_ID)
      # The ad group type must be set to SMART_CAMPAIGN_ADS.
      ag.type = :SMART_CAMPAIGN_ADS
    end
  end

  mutate_operation
end
      

Perl

# Creates a MutateOperation that creates a new ad group. A temporary ID will be
# used in the campaign resource name for this ad group to associate it with the
# Smart campaign created in earlier steps. A temporary ID will also be used for
# its own resource name so that we can associate an ad group ad with it later in
# the process. Only one ad group can be created for a given Smart campaign.
sub _create_ad_group_operation {
  my ($customer_id) = @_;

  return
    Google::Ads::GoogleAds::V8::Services::GoogleAdsService::MutateOperation->
    new({
      adGroupOperation =>
        Google::Ads::GoogleAds::V8::Services::AdGroupService::AdGroupOperation
        ->new({
          create => Google::Ads::GoogleAds::V8::Resources::AdGroup->new({
              # Set the ad group ID to a temporary ID.
              resourceName =>
                Google::Ads::GoogleAds::V8::Utils::ResourceNames::ad_group(
                $customer_id, AD_GROUP_TEMPORARY_ID
                ),
              name => "Smart campaign ad group #" . uniqid(),
              # Set the campaign ID to a temporary ID.
              campaign =>
                Google::Ads::GoogleAds::V8::Utils::ResourceNames::campaign(
                $customer_id, SMART_CAMPAIGN_TEMPORARY_ID
                ),
              # The ad group type must be SMART_CAMPAIGN_ADS.
              type => SMART_CAMPAIGN_ADS
            })})});
}
      

Create an ad group ad

Even though a Smart campaign can contain only a single ad group, that ad group can contain multiple ads. Since the creatives for Smart campaigns are created dynamically, it's possible that the headlines and descriptions from multiple different ads could be combined.

Key requirements for Smart campaign ads:

Below we set the ad_group field to the temporary resource name used to create the ad group above.

Java

private MutateOperation createAdGroupAdOperation(long customerId) {
  MutateOperation.Builder opBuilder = MutateOperation.newBuilder();

  // Constructs an Ad instance containing a SmartCampaignAd.
  Ad.Builder adBuilder = Ad.newBuilder();
  adBuilder
      .setType(AdType.SMART_CAMPAIGN_AD)
      .getSmartCampaignAdBuilder()
      .addHeadlines(AdTextAsset.newBuilder().setText("Headline number one"))
      .addHeadlines(AdTextAsset.newBuilder().setText("Headline number two"))
      .addHeadlines(AdTextAsset.newBuilder().setText("Headline number three"))
      .addDescriptions(AdTextAsset.newBuilder().setText("Description number one"))
      .addDescriptions(AdTextAsset.newBuilder().setText("Description number two"));

  opBuilder
      .getAdGroupAdOperationBuilder()
      .getCreateBuilder()
      .setAdGroup(ResourceNames.adGroup(customerId, AD_GROUP_TEMPORARY_ID))
      .setAd(adBuilder);
  return opBuilder.build();
}
      

C#

/// <summary>
/// Creates a MutateOperation that creates a new ad group ad.
/// A temporary ID will be used in the ad group resource name for this ad group ad to
/// associate it with the ad group created in earlier steps.
/// </summary>
/// <param name="customerId">The Google Ads customer ID.</param>
/// <returns>A MutateOperation that creates a new ad group ad.</returns>
private MutateOperation CreateAdGroupAdOperation(long customerId)
{
    AdGroupAd adGroupAd = new AdGroupAd
    {
        AdGroup = ResourceNames.AdGroup(customerId, AD_GROUP_TEMPORARY_ID),
        Ad = new Ad
        {
            SmartCampaignAd = new SmartCampaignAdInfo()
        }
    };

    // At most, three headlines can be specified for a Smart campaign ad.
    adGroupAd.Ad.SmartCampaignAd.Headlines.Add(new[]
        {
            new AdTextAsset
            {
                Text = "Headline number one"
            },
            new AdTextAsset
            {
                Text = "Headline number two"
            },
            new AdTextAsset
            {
                Text = "Headline number three"
            }
        }
    );

    // At most, two descriptions can be specified for a Smart campaign ad.
    adGroupAd.Ad.SmartCampaignAd.Descriptions.Add(new[]
        {
            new AdTextAsset
            {
                Text = "Description number one"
            },
            new AdTextAsset
            {
                Text = "Description number two"
            },
        }
    );

    return new MutateOperation
    {
        AdGroupAdOperation = new AdGroupAdOperation
        {
            Create = adGroupAd
        }
    };
}
      

PHP

private static function createAdGroupAdOperation(int $customerId): MutateOperation
{
    // Creates the ad group ad object.
    $adGroupAd = new AdGroupAd([
        // Sets the ad group ID to a temporary ID.
        'ad_group' => ResourceNames::forAdGroup($customerId, self::AD_GROUP_TEMPORARY_ID),
        'ad' => new Ad([
            // Sets the type to SMART_CAMPAIGN_AD.
            'type' => AdType::SMART_CAMPAIGN_AD,
            'smart_campaign_ad' => new SmartCampaignAdInfo([
                // At most, three headlines can be specified for a Smart campaign ad.
                'headlines' => [
                    new AdTextAsset(['text' => 'Headline number one']),
                    new AdTextAsset(['text' => 'Headline number two']),
                    new AdTextAsset(['text' => 'Headline number three'])
                ],
                // At most, two descriptions can be specified for a Smart campaign ad.
                'descriptions' => [
                    new AdTextAsset(['text' => 'Description number one']),
                    new AdTextAsset(['text' => 'Description number two'])
                ]
            ])
        ])
    ]);

    // Creates the MutateOperation that creates the ad group ad.
    return new MutateOperation([
        'ad_group_ad_operation' => new AdGroupAdOperation(['create' => $adGroupAd])
    ]);
}
      

Python

def _create_ad_group_ad_operation(client, customer_id):
    """Creates a MutateOperation that creates a new ad group ad.

    A temporary ID will be used in the ad group resource name for this
    ad group ad to associate it with the ad group created in earlier steps.

    Args:
        client: an initialized GoogleAdsClient instance.
        customer_id: a client customer ID.

    Returns:
        a MutateOperation that creates a new ad group ad.
    """
    mutate_operation = client.get_type("MutateOperation")
    ad_group_ad = mutate_operation.ad_group_ad_operation.create
    # Set the ad group ID to a temporary ID.
    ad_group_ad.ad_group = client.get_service("AdGroupService").ad_group_path(
        customer_id, _AD_GROUP_TEMPORARY_ID
    )
    # Set the type to SMART_CAMPAIGN_AD.
    ad_group_ad.ad.type_ = client.get_type(
        "AdTypeEnum"
    ).AdType.SMART_CAMPAIGN_AD
    ad = ad_group_ad.ad.smart_campaign_ad
    # At most, three headlines can be specified for a Smart campaign ad.
    headline_1 = client.get_type("AdTextAsset")
    headline_1.text = "Headline number one"
    headline_2 = client.get_type("AdTextAsset")
    headline_2.text = "Headline number two"
    headline_3 = client.get_type("AdTextAsset")
    headline_3.text = "Headline number three"
    ad.headlines.extend([headline_1, headline_2, headline_3])
    # At most, two descriptions can be specified for a Smart campaign ad.
    description_1 = client.get_type("AdTextAsset")
    description_1.text = "Description number one"
    description_2 = client.get_type("AdTextAsset")
    description_2.text = "Description number two"
    ad.descriptions.extend([description_1, description_2])

    return mutate_operation
      

Ruby

# Creates a mutate_operation that creates a new ad group ad.
# A temporary ID will be used in the ad group resource name for this
# ad group ad to associate it with the ad group created in earlier steps.
def create_ad_group_ad_operation(client, customer_id)
  mutate_operation = client.operation.mutate do |m|
    m.ad_group_ad_operation = client.operation.create_resource.ad_group_ad do |aga|
      # Set the ad group ID to a temporary ID.
      aga.ad_group = client.path.ad_group(customer_id, AD_GROUP_TEMPORARY_ID)
      aga.ad = client.resource.ad do |ad|
        # Set the type to SMART_CAMPAIGN_AD.
        ad.type = :SMART_CAMPAIGN_AD
        ad.smart_campaign_ad = client.resource.smart_campaign_ad_info do |sca|
          # At most, three headlines can be specified for a Smart campaign ad.
          sca.headlines << client.resource.ad_text_asset do |ata|
            ata.text = "Headline number one"
          end
          sca.headlines << client.resource.ad_text_asset do |ata|
            ata.text = "Headline number two"
          end
          sca.headlines << client.resource.ad_text_asset do |ata|
            ata.text = "Headline number three"
          end
          # At most, two descriptions can be specified for a Smart campaign ad.
          sca.descriptions << client.resource.ad_text_asset do |ata|
            ata.text = "Descriptions number one"
          end
          sca.descriptions << client.resource.ad_text_asset do |ata|
            ata.text = "Descriptions number two"
          end
        end
      end
    end
  end

  mutate_operation
end
      

Perl

# Creates a MutateOperation that creates a new ad group ad.
# A temporary ID will be used in the ad group resource name for this ad group
# ad to associate it with the ad group created in earlier steps.
sub _create_ad_group_ad_operation {
  my ($customer_id) = @_;

  return
    Google::Ads::GoogleAds::V8::Services::GoogleAdsService::MutateOperation->
    new({
      adGroupAdOperation =>
        Google::Ads::GoogleAds::V8::Services::AdGroupAdService::AdGroupAdOperation
        ->new({
          create => Google::Ads::GoogleAds::V8::Resources::AdGroupAd->new({
              adGroup =>
                Google::Ads::GoogleAds::V8::Utils::ResourceNames::ad_group(
                $customer_id, AD_GROUP_TEMPORARY_ID
                ),
              ad => Google::Ads::GoogleAds::V8::Resources::Ad->new({
                  # Set the type to SMART_CAMPAIGN_AD.
                  type            => SMART_CAMPAIGN_AD,
                  smartCampaignAd =>
                    Google::Ads::GoogleAds::V8::Common::SmartCampaignAdInfo->
                    new({
                      # At most, three headlines can be specified for a Smart
                      # campaign ad.
                      headlines => [
                        Google::Ads::GoogleAds::V8::Common::AdTextAsset->new({
                            text => "Headline number one"
                          }
                        ),
                        Google::Ads::GoogleAds::V8::Common::AdTextAsset->new({
                            text => "Headline number two"
                          }
                        ),
                        Google::Ads::GoogleAds::V8::Common::AdTextAsset->new({
                            text => "Headline number three"
                          })
                      ],
                      # At most, two descriptions can be specified for a Smart
                      # campaign ad.
                      descriptions => [
                        Google::Ads::GoogleAds::V8::Common::AdTextAsset->new(
                          {text => "Description number one"}
                        ),
                        Google::Ads::GoogleAds::V8::Common::AdTextAsset->new(
                          {text => "Description number two"})]})})})})});
}