现在,我们已经了解了如何为创建新的智能广告系列所需的各个实体创建 mutate 操作,接下来可以将这些操作合并为一个请求,以便一次性创建所有实体。
使用单个 mutate 请求的好处包括:
- 由于这些实体不共享且高度依赖彼此,因此降低了复杂性。
- 防止因任何原因导致其中一项操作失败时出现孤立实体。
使用临时资源名称在单个请求中创建实体(如上例所示)时,请确保按顺序执行操作,以便先创建具有依赖关系的实体。
Java
/** * Sends a mutate request with a group of mutate operations. * * <p>The {@link GoogleAdsServiceClient} allows batching together a list of operations. These are * executed sequentially, and later operations my refer to previous operations via temporary IDs. * For more detail on this, please refer to * https://developers.google.com/google-ads/api/docs/batch-processing/temporary-ids. */ private void sendMutateRequest( GoogleAdsClient googleAdsClient, long customerId, List<MutateOperation> operations) { try (GoogleAdsServiceClient client = googleAdsClient.getLatestVersion().createGoogleAdsServiceClient()) { MutateGoogleAdsResponse outerResponse = client.mutate(String.valueOf(customerId), operations); for (MutateOperationResponse innerResponse : outerResponse.getMutateOperationResponsesList()) { OneofDescriptor oneofDescriptor = innerResponse.getDescriptorForType().getOneofs().stream() .filter(o -> o.getName().equals("response")) .findFirst() .get(); Message createdEntity = (Message) innerResponse.getField(innerResponse.getOneofFieldDescriptor(oneofDescriptor)); String resourceName = (String) createdEntity.getField( createdEntity.getDescriptorForType().findFieldByName("resource_name")); System.out.printf( "Created a(n) %s with resource name: '%s'.%n", createdEntity.getClass().getSimpleName(), resourceName); } } }
C#
// The below methods create and return MutateOperations that we later provide to // the GoogleAdsService.Mutate method in order to create the entities in a single // request. Since the entities for a Smart campaign are closely tied to one-another // it's considered a best practice to create them in a single Mutate request; the // entities will either all complete successfully or fail entirely, leaving no // orphaned entities. See: // https://developers.google.com/google-ads/api/docs/mutating/overview MutateOperation campaignBudgetOperation = CreateCampaignBudgetOperation(customerId, suggestedBudgetAmount); MutateOperation smartCampaignOperation = CreateSmartCampaignOperation(customerId); MutateOperation smartCampaignSettingOperation = CreateSmartCampaignSettingOperation(customerId, businessProfileLocation, businessName); IEnumerable<MutateOperation> campaignCriterionOperations = CreateCampaignCriterionOperations(customerId, keywordThemeInfos, suggestionInfo); MutateOperation adGroupOperation = CreateAdGroupOperation(customerId); MutateOperation adGroupAdOperation = CreateAdGroupAdOperation(customerId, adSuggestions); // Send the operations in a single mutate request. MutateGoogleAdsRequest mutateGoogleAdsRequest = new MutateGoogleAdsRequest { CustomerId = customerId.ToString() }; // It's important to create these entities in this order because they depend on // each other, for example the SmartCampaignSetting and ad group depend on the // campaign, and the ad group ad depends on the ad group. mutateGoogleAdsRequest.MutateOperations.Add(campaignBudgetOperation); mutateGoogleAdsRequest.MutateOperations.Add(smartCampaignOperation); mutateGoogleAdsRequest.MutateOperations.Add(smartCampaignSettingOperation); mutateGoogleAdsRequest.MutateOperations.Add(campaignCriterionOperations); mutateGoogleAdsRequest.MutateOperations.Add(adGroupOperation); mutateGoogleAdsRequest.MutateOperations.Add(adGroupAdOperation); MutateGoogleAdsResponse response = googleAdsServiceClient.Mutate(mutateGoogleAdsRequest); PrintResponseDetails(response);
PHP
// The below methods create and return MutateOperations that we later provide to the // GoogleAdsService.Mutate method in order to create the entities in a single request. // Since the entities for a Smart campaign are closely tied to one-another it's considered // a best practice to create them in a single Mutate request so they all complete // successfully or fail entirely, leaving no orphaned entities. // See: https://developers.google.com/google-ads/api/docs/mutating/overview. $campaignBudgetOperation = self::createCampaignBudgetOperation( $customerId, $suggestedBudgetAmount ); $smartCampaignOperation = self::createSmartCampaignOperation($customerId); $smartCampaignSettingOperation = self::createSmartCampaignSettingOperation( $customerId, $businessProfileLocationResourceName, $businessName ); $campaignCriterionOperations = self::createCampaignCriterionOperations( $customerId, $keywordThemeInfos, $suggestionInfo ); $adGroupOperation = self::createAdGroupOperation($customerId); $adGroupAdOperation = self::createAdGroupAdOperation($customerId, $adSuggestions); // Issues a single mutate request to add the entities. $googleAdsServiceClient = $googleAdsClient->getGoogleAdsServiceClient(); $response = $googleAdsServiceClient->mutate(MutateGoogleAdsRequest::build( $customerId, // It's important to create these entities in this order because they depend on // each other, for example the SmartCampaignSetting and ad group depend on the // campaign, and the ad group ad depends on the ad group. array_merge( [ $campaignBudgetOperation, $smartCampaignOperation, $smartCampaignSettingOperation, ], $campaignCriterionOperations, [ $adGroupOperation, $adGroupAdOperation ] ) )); self::printResponseDetails($response); }