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.

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

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.

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