Create an Ad Group

To create an ad group for your Smart Display campaign, follow the same process you use for other ad groups. However, since Google Ads manages most aspects of a Smart Display campaign, targeting options at the ad group level are not allowed.

Java

private static String createAdGroup(
    GoogleAdsClient googleAdsClient, long customerId, String campaignResourceName) {
  // Creates the ad group.
  AdGroup adGroup =
      AdGroup.newBuilder()
          .setName("Earth to Mars Cruises #" + System.currentTimeMillis())
          .setCampaign(campaignResourceName)
          .setStatus(AdGroupStatus.PAUSED)
          .build();

  // Creates the operation.
  AdGroupOperation operation = AdGroupOperation.newBuilder().setCreate(adGroup).build();

  // Creates the ad group service client.
  try (AdGroupServiceClient adGroupServiceClient =
      googleAdsClient.getLatestVersion().createAdGroupServiceClient()) {
    MutateAdGroupsResponse response =
        adGroupServiceClient.mutateAdGroups(
            Long.toString(customerId), ImmutableList.of(operation));
    String adGroupResourceName = response.getResults(0).getResourceName();
    // Displays the results.
    System.out.printf("Added ad group with resource name '%s'.%n", adGroupResourceName);
    return adGroupResourceName;
  }
}

C#

private string CreateAdGroup(GoogleAdsClient client, long customerId,
    string campaignResourceName)
{
    // Get the AdGroupService.
    AdGroupServiceClient adGroupService = client.GetService(Services.V6.AdGroupService);

    // Create the ad group.
    AdGroup adGroup = new AdGroup()
    {
        Name = $"Earth to Mars Cruises #{ExampleUtilities.GetRandomString()}",
        Status = AdGroupStatusEnum.Types.AdGroupStatus.Enabled,
        Campaign = campaignResourceName,
    };

    // Create the operation.
    AdGroupOperation operation = new AdGroupOperation()
    {
        Create = adGroup
    };

    // Create the ad groups.
    MutateAdGroupsResponse response = adGroupService.MutateAdGroups(
        customerId.ToString(), new[] { operation });

    string adGroupResourceName = response.Results.First().ResourceName;

    // Print out some information about the added ad group.
    Console.WriteLine($"Added ad group with resource name = '{adGroupResourceName}'.");

    return adGroupResourceName;
}

PHP

private static function createAdGroup(
    GoogleAdsClient $googleAdsClient,
    int $customerId,
    string $campaignResourceName
) {
    // Constructs an ad group and set its type.
    $adGroup = new AdGroup([
        'name' => 'Earth to Mars Cruises #' . uniqid(),
        'campaign' => $campaignResourceName,
        'status' => AdGroupStatus::PAUSED,
    ]);

    // Creates an ad group operation.
    $adGroupOperation = new AdGroupOperation();
    $adGroupOperation->setCreate($adGroup);

    // Issues a mutate request to add the ad group.
    $adGroupServiceClient = $googleAdsClient->getAdGroupServiceClient();
    /** @var MutateAdGroupsResponse $adGroupResponse */
    $adGroupResponse = $adGroupServiceClient->mutateAdGroups($customerId, [$adGroupOperation]);

    // Print out some information about the added ad group.
    $adGroupResourceName = $adGroupResponse->getResults()[0]->getResourceName();
    printf("Added ad group named '%s'.%s", $adGroupResourceName, PHP_EOL);

    return $adGroupResourceName;
}

Python

def _create_ad_group(client, customer_id, campaign_resource_name):
    ad_group_operation = client.get_type("AdGroupOperation", version="v6")
    ad_group = ad_group_operation.create
    ad_group.name = f"Earth to Mars Cruises #{uuid4()}"
    ad_group_status_enum = client.get_type("AdGroupStatusEnum", version="v6")
    ad_group.status = ad_group_status_enum.PAUSED
    ad_group.campaign = campaign_resource_name

    ad_group_service = client.get_service("AdGroupService", version="v6")

    try:
        ad_group_response = ad_group_service.mutate_ad_groups(
            customer_id, [ad_group_operation]
        )
        return ad_group_response.results[0].resource_name
    except GoogleAdsException as ex:
        _handle_google_ads_exception(ex)

Ruby

def create_ad_group(client, customer_id, campaign)
  # Creates an ad group operation.
  operation = client.operation.create_resource.ad_group do |ag|
    ag.name = "Earth to Mars Cruises #{(Time.new.to_f * 1000).to_i}"
    ag.campaign = campaign
    ag.status = :PAUSED
  end

  # Issues a mutate request to add the ad group.
  response = client.service.ad_group.mutate_ad_groups(
    customer_id: customer_id,
    operations: [operation],
  )

  # Print out some information about the added ad group.
  resource_name = response.results.first.resource_name
  puts "Added ad group named #{resource_name}"

  resource_name
end

Perl

sub create_ad_group {
  my ($api_client, $customer_id, $campaign_resource_name) = @_;

  # Construct an ad group and set its type.
  my $ad_group = Google::Ads::GoogleAds::V6::Resources::AdGroup->new({
    name     => "Earth to Mars Cruises #" . uniqid(),
    campaign => $campaign_resource_name,
    status   => Google::Ads::GoogleAds::V6::Enums::AdGroupStatusEnum::PAUSED
  });

  # Create an ad group operation.
  my $ad_group_operation =
    Google::Ads::GoogleAds::V6::Services::AdGroupService::AdGroupOperation->new(
    {
      create => $ad_group
    });

  # Issue a mutate request to add the ad group.
  my $ad_groups_response = $api_client->AdGroupService()->mutate({
      customerId => $customer_id,
      operations => [$ad_group_operation]});

  # Print out some information about the added ad group.
  my $ad_group_resource_name =
    $ad_groups_response->{results}[0]{resourceName};
  printf "Added ad group named '%s'.\n", $ad_group_resource_name;

  return $ad_group_resource_name;
}