Create an Ad Group

In order to serve ads for your Local campaign, you must create at least one AdGroup using AdGroupService. Don't specify a type for the ad group.

Java

private String createAdGroup(
    GoogleAdsClient googleAdsClient, long customerId, String campaignResourceName) {
  // Creates an ad group. Note that the ad group type must not be set.
  // Also, since the advertisingChannelSubType is LOCAL_CAMPAIGN:
  //   1. you cannot override bid settings at the ad group level.
  //   2. you cannot add ad group criteria.
  AdGroup adGroup =
      AdGroup.newBuilder()
          .setName("Earth to Mars Cruises #" + getPrintableDateTime())
          .setStatus(AdGroupStatus.ENABLED)
          .setCampaign(campaignResourceName)
          .build();

  // Creates an operation to add the ad group.
  AdGroupOperation operation = AdGroupOperation.newBuilder().setCreate(adGroup).build();

  // Connects to the API.
  try (AdGroupServiceClient client =
      googleAdsClient.getLatestVersion().createAdGroupServiceClient()) {
    // Issues the mutate request.
    MutateAdGroupsResponse response =
        client.mutateAdGroups(String.valueOf(customerId), ImmutableList.of(operation));

    // Prints some debugging information.
    String resourceName = response.getResults(0).getResourceName();
    System.out.printf("Created ad group with resource name: '%s'.%n", resourceName);
    return resourceName;
  }
}
      

C#

private string CreateAdGroup(GoogleAdsClient client, long customerId,
    string campaignResourceName)
{
    // Create the ad group service client.
    AdGroupServiceClient adGroupServiceClient =
        client.GetService(Services.V6.AdGroupService);

    // Create the ad group.
    // Note that the ad group type must not be set.
    // Since the advertisingChannelSubType is LOCAL_CAMPAIGN:
    //   1. you cannot override bid settings at the ad group level.
    //   2. you cannot add ad group criteria.
    AdGroup adGroup = new AdGroup()
    {
        Name = $"Earth to Mars Cruises #{ExampleUtilities.GetRandomString()}",
        Campaign = campaignResourceName,
        Status = AdGroupStatus.Enabled
    };

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

    // Issue a mutate request to add the ad group, then print and return the resulting ad
    // group's resource name.
    MutateAdGroupsResponse adGroupResponse = adGroupServiceClient.MutateAdGroups(
        customerId.ToString(), new[] {adGroupOperation});

    string adGroupResourceName = adGroupResponse.Results.First().ResourceName;
    Console.WriteLine($"Created ad group with resource name '{adGroupResourceName}'.");
    return adGroupResourceName;
}
      

PHP

private static function createAdGroup(
    GoogleAdsClient $googleAdsClient,
    int $customerId,
    string $campaignResourceName
) {
    // Creates an ad group.
    // Note that the ad group type must not be set.
    // Since the advertisingChannelSubType is LOCAL_CAMPAIGN:
    //   1. you cannot override bid settings at the ad group level.
    //   2. you cannot add ad group criteria.
    $adGroup = new AdGroup([
        'name' => 'Earth to Mars Cruises #' . Helper::getPrintableDatetime(),
        'campaign' => $campaignResourceName,
        'status' => AdGroupStatus::ENABLED
    ]);

    // 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]);

    $adGroupResourceName = $adGroupResponse->getResults()[0]->getResourceName();
    printf("Created ad group with resource name: '%s'.%s", $adGroupResourceName, PHP_EOL);

    return $adGroupResourceName;
}
      

Python

This example is not yet available in Python; you can take a look at the other languages.
    

Ruby

This example is not yet available in Ruby; you can take a look at the other languages.
    

Perl

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

  # Create an ad group.
  # Note that the ad group type must not be set.
  # Since the advertisingChannelSubType is LOCAL_CAMPAIGN:
  #   1. you cannot override bid settings at the ad group level.
  #   2. you cannot add ad group criteria.
  my $ad_group = Google::Ads::GoogleAds::V6::Resources::AdGroup->new({
    name     => "Earth to Mars Cruises #" . uniqid(),
    status   => Google::Ads::GoogleAds::V6::Enums::AdGroupStatusEnum::ENABLED,
    campaign => $campaign_resource_name
  });

  # 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]});

  my $ad_group_resource_name =
    $ad_groups_response->{results}[0]{resourceName};
  printf "Created ad group with resource name: '%s'.\n",
    $ad_group_resource_name;

  return $ad_group_resource_name;
}