Create an Ad Group

Stay organized with collections Save and categorize content based on your preferences.

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.V11.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

def create_ad_group(client, customer_id, campaign_resource_name):
    """Adds an ad group to the given client account under the given campaign.

    Args:
        client: an initialized GoogleAdsClient instance.
        customer_id: a client customer ID str.
        campaign_resource_name: the resource name str for a campaign.

    Returns:
        A str of the resource name for the newly created ad group.
    """
    ad_group_operation = client.get_type("AdGroupOperation")
    ad_group = ad_group_operation.create
    # Note that the ad group type must not be set.
    # Since the advertising_channel_subType is LOCAL_CAMPAIGN:
    #   1. you cannot override bid settings at the ad group level.
    #   2. you cannot add ad group criteria.
    ad_group.name = f"Earth to Mars Cruises #{uuid4()}"
    ad_group.status = client.enums.AdGroupStatusEnum.ENABLED
    ad_group.campaign = campaign_resource_name

    ad_group_service = client.get_service("AdGroupService")
    response = ad_group_service.mutate_ad_groups(
        customer_id=customer_id, operations=[ad_group_operation]
    )
    resource_name = response.results[0].resource_name
    print(f"Created AdGroup with resource name: '{resource_name}'")
    return resource_name
      

Ruby

def create_ad_group(client, customer_id, campaign_resource_name)
  # Creates an ad group operation.
  # Note that the ad group type must not be set.
  # Since the advertising_channel_sub_type is LOCAL_CAMPAIGN:
  #   1. you cannot override bid settings at the ad group level.
  #   2. you cannot add ad group criteria.
  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_resource_name
    ag.status = :ENABLED
  end

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

  ad_group_resource_name = response.results.first.resource_name
  puts "Created ad group with resource name: '#{ad_group_resource_name}'."

  ad_group_resource_name
end
      

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::V11::Resources::AdGroup->new({
    name   => "Earth to Mars Cruises #" . uniqid(),
    status => Google::Ads::GoogleAds::V11::Enums::AdGroupStatusEnum::ENABLED,
    campaign => $campaign_resource_name
  });

  # Create an ad group operation.
  my $ad_group_operation =
    Google::Ads::GoogleAds::V11::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;
}