您可以更改或暂停广告。
更改自适应搜索广告
如前文所述,只有某些广告类型是可变的。以下代码示例展示了如何修改现有的自适应搜索广告。
Java
private void runExample(GoogleAdsClient googleAdsClient, long customerId, long adId) { // Creates an AdOperation to update an ad. AdOperation.Builder adOperation = AdOperation.newBuilder(); // Creates an Ad in the update field of the operation. Ad.Builder adBuilder = adOperation .getUpdateBuilder() .setResourceName(ResourceNames.ad(customerId, adId)) .addFinalUrls("http://www.example.com/") .addFinalMobileUrls("http://www.example.com/mobile"); // Sets the responsive search ad properties to update on the ad. adBuilder .getResponsiveSearchAdBuilder() .addAllHeadlines( ImmutableList.of( AdTextAsset.newBuilder() .setText("Cruise to Pluto #" + getShortPrintableDateTime()) .setPinnedField(ServedAssetFieldType.HEADLINE_1) .build(), AdTextAsset.newBuilder().setText("Tickets on sale now").build(), AdTextAsset.newBuilder().setText("Buy your ticket now").build())) .addAllDescriptions( ImmutableList.of( AdTextAsset.newBuilder().setText("Best space cruise ever.").build(), AdTextAsset.newBuilder() .setText("The most wonderful space experience you will ever have.") .build())); // Sets the update mask (the fields which will be modified) to be all the fields we set above. adOperation.setUpdateMask(FieldMasks.allSetFieldsOf(adBuilder.build())); // Creates a service client to connect to the API. try (AdServiceClient adServiceClient = googleAdsClient.getLatestVersion().createAdServiceClient()) { // Issues the mutate request. MutateAdsResponse response = adServiceClient.mutateAds( String.valueOf(customerId), ImmutableList.of(adOperation.build())); // Displays the result. for (MutateAdResult result : response.getResultsList()) { System.out.printf("Ad with resource name '%s' was updated.%n", result.getResourceName()); } } }
C#
public void Run(GoogleAdsClient client, long customerId, long adId) { // Get the AdService. AdServiceClient adService = client.GetService(Services.V25.AdService); Ad ad = new Ad() { ResourceName = ResourceNames.Ad(customerId, adId), ResponsiveSearchAd = new ResponsiveSearchAdInfo() { // Update some properties of the responsive search ad. Headlines = { new AdTextAsset() { Text = "Cruise to Pluto #" + ExampleUtilities.GetShortRandomString(), PinnedField = ServedAssetFieldTypeEnum.Types.ServedAssetFieldType.Headline1 }, new AdTextAsset() { Text = "Tickets on sale now" }, new AdTextAsset() { Text = "Buy your ticket now" } }, Descriptions = { new AdTextAsset() { Text = "Best space cruise ever." }, new AdTextAsset() { Text = "The most wonderful space experience you will ever have." }, } }, FinalUrls = { "http://www.example.com/" }, FinalMobileUrls = { "http://www.example.com/mobile" } }; AdOperation operation = new AdOperation() { Update = ad, UpdateMask = FieldMasks.AllSetFieldsOf(ad) }; try { // Issue the update request. MutateAdsResponse response = adService.MutateAds(customerId.ToString(), new[] { operation }); // Display the results. foreach (MutateAdResult updatedAd in response.Results) { Console.WriteLine($"Ad with resource ID = '{updatedAd.ResourceName}' was " + $"updated."); } } catch (GoogleAdsException e) { Console.WriteLine("Failure:"); Console.WriteLine($"Message: {e.Message}"); Console.WriteLine($"Failure: {e.Failure}"); Console.WriteLine($"Request ID: {e.RequestId}"); throw; } }
PHP
public static function runExample( GoogleAdsClient $googleAdsClient, int $customerId, int $adId ) { // Creates an ad with the specified resource name and other changes. $ad = new Ad([ 'resource_name' => ResourceNames::forAd($customerId, $adId), 'responsive_search_ad' => new ResponsiveSearchAdInfo([ // Update some properties of the responsive search ad. 'headlines' => [ new AdTextAsset([ 'text' => 'Cruise to Pluto #' . Helper::getShortPrintableDatetime(), 'pinned_field' => ServedAssetFieldType::HEADLINE_1 ]), new AdTextAsset(['text' => 'Tickets on sale now']), new AdTextAsset(['text' => 'Buy your ticket now']) ], 'descriptions' => [ new AdTextAsset(['text' => 'Best space cruise ever.']), new AdTextAsset([ 'text' => 'The most wonderful space experience you will ever have.']) ] ]), 'final_urls' => ['http://www.example.com'], 'final_mobile_urls' => ['http://www.example.com/mobile'] ]); // Constructs an operation that will update the ad, using the FieldMasks to derive the // update mask. This mask tells the Google Ads API which attributes of the ad you want to // change. $adOperation = new AdOperation(); $adOperation->setUpdate($ad); $adOperation->setUpdateMask(FieldMasks::allSetFieldsOf($ad)); // Issues a mutate request to update the ad. $adServiceClient = $googleAdsClient->getAdServiceClient(); $response = $adServiceClient->mutateAds(MutateAdsRequest::build($customerId, [$adOperation])); // Prints the resource name of the updated ad. /** @var Ad $updatedAd */ $updatedAd = $response->getResults()[0]; printf( "Updated ad with resource name: '%s'.%s", $updatedAd->getResourceName(), PHP_EOL ); }