← 返回目录
来源:Google Ads API 官方文档 · 中文版 · 本地归档

创建“活动”广告组

要为您的“推荐活动广告系列”投放广告,您必须创建一个 AdGroup,且该广告组中至少包含一个广告。如下所示,广告系列仅支持 TRAVEL_ADS 类型的广告组,您可以在 type 字段中对此进行设置。

Java

private String addAdGroup(
    GoogleAdsClient googleAdsClient, long customerId, String campaignResourceName) {
  // Creates an ad group.
  AdGroup adGroup =
      AdGroup.newBuilder()
          .setName("Earth to Mars Cruises #" + getPrintableDateTime())
          .setCampaign(campaignResourceName)
          // Sets the ad group type to TRAVEL_ADS. This cannot be set to other types.
          .setType(AdGroupType.TRAVEL_ADS)
          .setStatus(AdGroupStatus.ENABLED)
          .build();

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

  // Issues a mutate request to add an ad group.
  try (AdGroupServiceClient adGroupServiceClient =
      googleAdsClient.getLatestVersion().createAdGroupServiceClient()) {
    MutateAdGroupResult mutateAdGroupResult =
        adGroupServiceClient
            .mutateAdGroups(Long.toString(customerId), Collections.singletonList(operation))
            .getResults(0);
    System.out.printf(
        "Added an ad group with resource name: '%s'%n", mutateAdGroupResult.getResourceName());
    return mutateAdGroupResult.getResourceName();
  }
}
      

C#

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

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

    MutateAdGroupsResponse response = adGroupService.MutateAdGroups(
        customerId.ToString(), new AdGroupOperation[] { new AdGroupOperation() {
            Create = adGroup
        }}
    );

    string adGroupResourceName = response.Results[0].ResourceName;
    Console.WriteLine("Ad group with resource name = '{0}' was added.", adGroupResourceName);

    return adGroupResourceName;
}
      

PHP

private static function addAdGroup(
    GoogleAdsClient $googleAdsClient,
    int $customerId,
    string $campaignResourceName
) {
    // Creates an ad group.
    $adGroup = new AdGroup([
        'name' => 'Earth to Mars Cruise #' . Helper::getPrintableDatetime(),
        // Sets the campaign.
        'campaign' => $campaignResourceName,
        // Sets the ad group type to TRAVEL_ADS. This cannot be set to other types.
        'type' => AdGroupType::TRAVEL_ADS,
        'status' => AdGroupStatus::ENABLED,
    ]);

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

    // Issues a mutate request to add an ad group.
    $adGroupServiceClient = $googleAdsClient->getAdGroupServiceClient();
    $response = $adGroupServiceClient->mutateAdGroups(
        MutateAdGroupsRequest::build($customerId, [$adGroupOperation])
    );

    /** @var AdGroup $addedAdGroup */
    $addedAdGroup = $response->getResults()[0];
    printf(
        "Added an ad group with resource name '%s'.%s",
        $addedAdGroup->getResourceName(),
        PHP_EOL
    );

    return $addedAdGroup->getResourceName();
}
在接这个接口时卡住了?
Stuck wiring this API into production? We build ad-delivery tooling on these platforms — campaign automation, reporting pipelines, error-handling and migration between API versions.
联系我们 · Talk to us 每日平台更新简报 →