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

Create campaign criteria

In the get recommendations section we demonstrated how to retrieve a set of KeywordThemeConstant objects using a word or phrase. In this step we use those same keyword theme constants to create a set of CampaignCriterion objects for the Smart campaign to target.

Key requirements for Smart campaign criteria:

In the below example the keyword theme constants have been converted to KeywordThemeInfo objects by setting their resource name onto the KeywordThemeInfo.keyword_theme_constant field. We set the campaign field using the temporary resource name set on the campaign in the previous step.

Java

/**
 * Creates {@link com.google.ads.googleads.v8.resources.CampaignCriterion} operations for add each
 * {@link KeywordThemeInfo}.
 */
private Collection<? extends MutateOperation> createCampaignCriterionOperations(
    long customerId, List<KeywordThemeInfo> keywordThemeInfos) {
  return keywordThemeInfos.stream()
      .map(
          keywordTheme -> {
            MutateOperation.Builder builder = MutateOperation.newBuilder();
            builder
                .getCampaignCriterionOperationBuilder()
                .getCreateBuilder()
                .setCampaign(ResourceNames.campaign(customerId, SMART_CAMPAIGN_TEMPORARY_ID))
                .setType(CriterionType.KEYWORD_THEME)
                .setKeywordTheme(keywordTheme);
            return builder.build();
          })
      .collect(Collectors.toList());
}
      

C#

/// <summary>
/// Creates a list of MutateOperations that create new campaign criteria.
/// </summary>
/// <param name="customerId">The Google Ads customer ID.</param>
/// <param name="keywordThemeInfos">A list of KeywordThemeInfos.</param>
/// <returns>A list of MutateOperations that create new campaign criteria.</returns>
private IEnumerable<MutateOperation> CreateCampaignCriterionOperations(long customerId,
    IEnumerable<KeywordThemeInfo> keywordThemeInfos)
{
    return keywordThemeInfos.Select(keywordThemeInfo => new MutateOperation
    {
        CampaignCriterionOperation = new CampaignCriterionOperation
        {
            Create = new CampaignCriterion
            {
                // Set the campaign ID to a temporary ID.
                Campaign = ResourceNames.Campaign(customerId, SMART_CAMPAIGN_TEMPORARY_ID),
                // Set the criterion type to KeywordTheme.
                Type = CriterionType.KeywordTheme,
                // Set the keyword theme to each KeywordThemeInfo in turn.
                KeywordTheme = keywordThemeInfo,
            }
        }
    }).ToList();
}
      

PHP

private static function createCampaignCriterionOperations(
    int $customerId,
    array $keywordThemeInfos
): array {
    $operations = [];
    foreach ($keywordThemeInfos as $info) {
        // Creates the campaign criterion object.
        $campaignCriterion = new CampaignCriterion([
            // Sets the campaign ID to a temporary ID.
            'campaign' =>
                ResourceNames::forCampaign($customerId, self::SMART_CAMPAIGN_TEMPORARY_ID),
            // Sets the criterion type to KEYWORD_THEME.
            'type' => CriterionType::KEYWORD_THEME,
            // Sets the keyword theme to the given KeywordThemeInfo.
            'keyword_theme' => $info
        ]);

        // Creates the MutateOperation that creates the campaign criterion and adds it to the
        // list of operations.
        $operations[] = new MutateOperation([
            'campaign_criterion_operation' => new CampaignCriterionOperation([
                'create' => $campaignCriterion
            ])
        ]);
    }

    return $operations;
}
      

Python

def _create_campaign_criterion_operations(
    client, customer_id, keyword_theme_infos
):
    """Creates a list of MutateOperations that create new campaign criteria.

    Args:
        client: an initialized GoogleAdsClient instance.
        customer_id: a client customer ID.
        keyword_theme_infos: a list of KeywordThemeInfos.

    Returns:
        a list of MutateOperations that create new campaign criteria.
    """
    campaign_service = client.get_service("CampaignService")

    operations = []
    for info in keyword_theme_infos:
        mutate_operation = client.get_type("MutateOperation")
        campaign_criterion = (
            mutate_operation.campaign_criterion_operation.create
        )
        # Set the campaign ID to a temporary ID.
        campaign_criterion.campaign = campaign_service.campaign_path(
            customer_id, _SMART_CAMPAIGN_TEMPORARY_ID
        )
        # Set the criterion type to KEYWORD_THEME.
        campaign_criterion.type_ = client.get_type(
            "CriterionTypeEnum"
        ).CriterionType.KEYWORD_THEME
        # Set the keyword theme to the given KeywordThemeInfo.
        campaign_criterion.keyword_theme = info
        # Add the mutate operation to the list of other operations.
        operations.append(mutate_operation)

    return operations
      

Ruby

# Creates a list of mutate_operations that create new campaign criteria.
def create_campaign_criterion_operations(
  client,
  customer_id,
  keyword_theme_infos)
  operations = []

  keyword_theme_infos.each do |info|
    operations << client.operation.mutate do |m|
      m.campaign_criterion_operation =
        client.operation.create_resource.campaign_criterion do |cc|
        # Sets the campaign ID to a temporary ID.
        cc.campaign = client.path.campaign(
          customer_id, SMART_CAMPAIGN_TEMPORARY_ID)
        # Sets the criterion type to KEYWORD_THEME.
        cc.type = :KEYWORD_THEME
        # Sets the keyword theme to the given keyword_theme_info.
        cc.keyword_theme = info
      end
    end
  end

  operations
end
      

Perl

# Creates a list of MutateOperations that create new campaign criteria.
sub _create_campaign_criterion_operations {
  my ($customer_id, $keyword_theme_infos) = @_;

  my $campaign_criterion_operations = [];

  foreach my $keyword_theme_info (@$keyword_theme_infos) {
    push @$campaign_criterion_operations,
      Google::Ads::GoogleAds::V8::Services::GoogleAdsService::MutateOperation->
      new({
        campaignCriterionOperation =>
          Google::Ads::GoogleAds::V8::Services::CampaignCriterionService::CampaignCriterionOperation
          ->new({
            create =>
              Google::Ads::GoogleAds::V8::Resources::CampaignCriterion->new({
                # Set the campaign ID to a temporary ID.
                campaign =>
                  Google::Ads::GoogleAds::V8::Utils::ResourceNames::campaign(
                  $customer_id, SMART_CAMPAIGN_TEMPORARY_ID
                  ),
                # Set the criterion type to KEYWORD_THEME.
                type => KEYWORD_THEME,
                # Set the keyword theme to each KeywordThemeInfo in turn.
                keywordTheme => $keyword_theme_info
              })})});
  }

  return @$campaign_criterion_operations;
}