Extension setting services use feeds
to control ad extensions. ExtensionFeedItem
is a
wrapper over a FeedItem
that handles much of the feed
mechanics for you. Still, there are some cleanup that are required when removing
an extension setting.
A functional extension setting is made up of both some set of extension feed
items and either one
CustomerExtensionSetting
,
CampaignExtensionSetting
, or
AdGroupExtensionSetting
. You can delete the
extension setting, which will remove the association between the extension feed
items and that resource, but the extension feed items will still exist and can
be reused with other extension settings in the future. You can also update an
extension setting to use a different set of extension feed items, but again
unused feed items will continue to exist.
To remove extension feed items that are no longer associated with any extension
settings, make a followup call to the
ExtensionFeedItemService
.
Remove the entire extension setting
The example below assumes we want to remove both the extension setting and
extension feed item that were created earlier. Removing the extension setting
doesn't automatically remove the extension feed items, so that must be done in a
separate step. In our example, all removal operations are sent using a
multiresource mutate request,
GoogleAdsService.Mutate()
, to ensure
they're executed as one set of operations. The set of operations will all succeed
if no operation fails or all fail if any single operation fails. To
execute the operations for extension settings and extension feed items
independently, set the
partialFailure
attribute
to true. See Mutating Resources
for more detailed information on mutate operation semantics.
Java
private void runExample(GoogleAdsClient googleAdsClient, long customerId, long campaignId) { // Retrieves all sitelink extension feed items for a customer and campaign. List<String> extensionFeedItemResourceNames = getSitelinkExtensionFeedItems(googleAdsClient, customerId, campaignId); // Constructs operations to remove the extension feed items. List<MutateOperation> feedItemMutateOperations = extensionFeedItemResourceNames.stream() .map( feedItem -> MutateOperation.newBuilder() .setExtensionFeedItemOperation( ExtensionFeedItemOperation.newBuilder().setRemove(feedItem)) .build()) .collect(Collectors.toList()); // Creates a list of operations that contains both the campaign extension setting and the feed // item operations. List<MutateOperation> allOperations = new ArrayList(); // Adds an operation to remove the campaign extension setting. allOperations.add( MutateOperation.newBuilder() .setCampaignExtensionSettingOperation( CampaignExtensionSettingOperation.newBuilder() .setRemove( ResourceNames.campaignExtensionSetting( customerId, campaignId, ExtensionType.SITELINK)) .build()) .build()); // Adds the operations to remove the feed items. allOperations.addAll(feedItemMutateOperations); // Connects to the API. try (GoogleAdsServiceClient client = googleAdsClient.getLatestVersion().createGoogleAdsServiceClient()) { // Issues the request. MutateGoogleAdsResponse response = client.mutate(String.valueOf(customerId), allOperations); // Prints the result of removing the campaign extension setting. // Each mutate operation response is returned in the same order as we passed its // corresponding operation. Therefore, the first belongs to the campaign setting operation, // and the rest belong to the extension feed item operations. System.out.printf( "Removed a campaign extension setting with resource name: '%s'.%n", response .getMutateOperationResponses(0) .getCampaignExtensionSettingResult() .getResourceName()); // Prints the result of removing the extension feed items. for (MutateOperationResponse mutateResponse : response .getMutateOperationResponsesList() .subList(1, response.getMutateOperationResponsesList().size())) { System.out.printf( "Removed an extension feed item with resource name: '%s'.%n", mutateResponse.getExtensionFeedItemResult().getResourceName()); } } }
C#
This example is not yet available in C#; you can take a look at the other languages.
PHP
public static function runExample( GoogleAdsClient $googleAdsClient, int $customerId, int $campaignId ) { $mutateOperations = []; // Creates a mutate operation that contains the campaign extension setting operation // to remove the specified sitelink campaign extension setting. $mutateOperations[] = self::createSitelinkCampaignExtensionSettingMutateOperation($customerId, $campaignId); // Gets all sitelink extension feed items of the specified campaign. $googleAdsServiceClient = $googleAdsClient->getGoogleAdsServiceClient(); $extensionFeedItemResourceNames = self::getAllSitelinkExtensionFeedItems( $googleAdsServiceClient, $customerId, $campaignId ); // Creates mutate operations, each of which contains an extension feed item operation // to remove the specified extension feed items. $mutateOperations = array_merge( $mutateOperations, self::createExtensionFeedItemMutateOperations($extensionFeedItemResourceNames) ); // Issues a mutate request to remove the campaign extension setting and its extension // feed items. $response = $googleAdsServiceClient->mutate($customerId, $mutateOperations); $mutateOperationResponses = $response->getMutateOperationResponses(); // Prints the information on the removed campaign extension setting and its extension feed // items. // Each mutate operation response is returned in the same order as we passed its // corresponding operation. Therefore, the first belongs to the campaign setting operation, // and the rest belong to the extension feed item operations. printf( "Removed a campaign extension setting with resource name: '%s'.%s", $mutateOperationResponses[0]->getCampaignExtensionSettingResult()->getResourceName(), PHP_EOL ); for ($i = 1; $i < count($mutateOperationResponses); $i++) { printf( "Removed an extension feed item with resource name: '%s'.%s", $mutateOperationResponses[$i]->getExtensionFeedItemResult()->getResourceName(), PHP_EOL ); } }
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 remove_entire_sitelink_campaign_extension_setting { my ($api_client, $customer_id, $campaign_id) = @_; my $mutate_operations = []; # Create a mutate operation that contains the campaign extension setting operation # to remove the specified sitelink campaign extension setting. push( @$mutate_operations, create_sitelink_campaign_extension_setting_mutate_operation( $customer_id, $campaign_id )); # Get all sitelink extension feed items of the specified campaign. my $extension_feed_item_resource_names = get_all_sitelink_extension_feed_items($api_client, $customer_id, $campaign_id); # Create mutate operations, each of which contains an extension feed item operation # to remove the specified extension feed items. push( @$mutate_operations, create_extension_feed_item_mutate_operations( $extension_feed_item_resource_names)); # Issue a mutate request to remove the campaign extension setting and its # extension feed items. my $mutate_google_ads_response = $api_client->GoogleAdsService()->mutate({ customerId => $customer_id, mutateOperations => $mutate_operations }); my $mutate_operation_responses = $mutate_google_ads_response->{mutateOperationResponses}; # Print the information on the removed campaign extension setting and its # extension feed items. # Each mutate operation response is returned in the same order as we passed # its corresponding operation. Therefore, the first belongs to the campaign # setting operation, and the rest belong to the extension feed item operations. printf "Removed a campaign extension setting with resource name '%s'.\n", @$mutate_operation_responses[0] ->{campaignExtensionSettingResult}{resourceName}; shift(@$mutate_operation_responses); foreach my $response (@$mutate_operation_responses) { printf "Removed an extension feed item with resource name '%s'.\n", $response->{extensionFeedItemResult}{resourceName}; } return 1; }
To fetch the extension feed items of the specified campaign extension setting,
send a request for campaign_extension_setting
:
Java
private List<String> getSitelinkExtensionFeedItems( GoogleAdsClient googleAdsClient, long customerId, long campaignId) { // Defines a query to retrieve the sitelink extension feed items. String query = String.format( "SELECT campaign_extension_setting.campaign, " + " campaign_extension_setting.extension_type, " + " campaign_extension_setting.extension_feed_items " + "FROM " + " campaign_extension_setting " + "WHERE " + " campaign_extension_setting.campaign = '%s' " + " AND campaign_extension_setting.extension_type = SITELINK", ResourceNames.campaign(customerId, campaignId)); // Connects to the API. try (GoogleAdsServiceClient client = googleAdsClient.getLatestVersion().createGoogleAdsServiceClient()) { // Issues the search request. ServerStream<SearchGoogleAdsStreamResponse> response = client .searchStreamCallable() .call( SearchGoogleAdsStreamRequest.newBuilder() .setCustomerId(String.valueOf(customerId)) .setQuery(query) .build()); // Constructs the result (a list of resource names matching the query). List<String> result = new ArrayList(); for (SearchGoogleAdsStreamResponse page : response) { for (GoogleAdsRow row : page.getResultsList()) { result.addAll(row.getCampaignExtensionSetting().getExtensionFeedItemsList()); } } if (result.isEmpty()) { System.out.println( "The specified campaign does not contain a sitelink campaign extension setting."); } return result; } }
C#
This example is not yet available in C#; you can take a look at the other languages.
PHP
private static function getAllSitelinkExtensionFeedItems( GoogleAdsServiceClient $googleAdsServiceClient, int $customerId, int $campaignId ): array { // Creates a query that retrieves all campaigns. $query = sprintf( "SELECT campaign_extension_setting.campaign, " . "campaign_extension_setting.extension_type, " . "campaign_extension_setting.extension_feed_items " . "FROM campaign_extension_setting " . "WHERE campaign_extension_setting.campaign = '%s' " . "AND campaign_extension_setting.extension_type = %s", ResourceNames::forCampaign($customerId, $campaignId), ExtensionType::name(ExtensionType::SITELINK) ); // Issues a search stream request. /** @var GoogleAdsServerStreamDecorator $stream */ $stream = $googleAdsServiceClient->searchStream($customerId, $query); $extensionFeedItemResourceNames = []; // Iterates over all rows in all messages and prints the requested field values for // the campaign extension setting in each row. foreach ($stream->iterateAllElements() as $googleAdsRow) { /** @var GoogleAdsRow $googleAdsRow */ $extensionFeedItems = $googleAdsRow->getCampaignExtensionSetting()->getExtensionFeedItems(); foreach ($extensionFeedItems as $extensionFeedItem) { /** @var string $extensionFeedItem */ $extensionFeedItemResourceNames[] = $extensionFeedItem; printf( "Extension feed item with resource name '%s' was found.%s", $extensionFeedItem, PHP_EOL ); } } if (empty($extensionFeedItemResourceNames)) { throw new \InvalidArgumentException( 'The specified campaign does not contain a sitelink campaign extension setting.' ); } return $extensionFeedItemResourceNames; }
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 get_all_sitelink_extension_feed_items { my ($api_client, $customer_id, $campaign_id) = @_; my $extension_feed_item_resource_names = []; # Issue a search stream request, then iterate over all responses. my $search_stream_request = Google::Ads::GoogleAds::V6::Services::GoogleAdsService::SearchGoogleAdsStreamRequest ->new({ customerId => $customer_id, query => sprintf( "SELECT campaign_extension_setting.campaign, " . "campaign_extension_setting.extension_type, " . "campaign_extension_setting.extension_feed_items " . "FROM campaign_extension_setting " . "WHERE campaign_extension_setting.campaign = '%s' " . "AND campaign_extension_setting.extension_type = 'SITELINK'", Google::Ads::GoogleAds::V6::Utils::ResourceNames::campaign( $customer_id, $campaign_id ))}); my $search_stream_handler = Google::Ads::GoogleAds::Utils::SearchStreamHandler->new({ service => $api_client->GoogleAdsService(), request => $search_stream_request }); # Print out and store in a list each extension feed item's resource name. $search_stream_handler->process_contents( sub { # Display the results and add the resource names to the list. my $google_ads_row = shift; foreach my $extension_feed_item_resource_name ( @{$google_ads_row->{campaignExtensionSetting}{extensionFeedItems}}) { push(@$extension_feed_item_resource_names, $extension_feed_item_resource_name); printf "Extension feed item with resource name '%s' was found.\n", $extension_feed_item_resource_name; } }); if (!@$extension_feed_item_resource_names) { die("The specified campaign does not contain a sitelink campaign " . "extension setting.\n"); } return $extension_feed_item_resource_names; }
Remove an extension feed item from an existing setting
Alternatively, if you want to keep the extension setting but change which extension feed items it uses, you can update the extension setting and set the full set of extension settings to use. Remember that this doesn't completely remove the extension feed item; it just disassociates it from this extension setting.
Java
private void runExample( GoogleAdsClient googleAdsClient, long customerId, long campaignId, List<Long> feedItemIds) { // Converts the feed item IDs into resource names. List<String> feedItemResourceNames = feedItemIds.stream() .map(id -> ResourceNames.extensionFeedItem(customerId, id)) .collect(Collectors.toList()); // Creates a CampaignExtensionSetting object to update. CampaignExtensionSetting campaignExtensionSetting = CampaignExtensionSetting.newBuilder() .setResourceName( ResourceNames.campaignExtensionSetting( customerId, campaignId, ExtensionType.SITELINK)) .addAllExtensionFeedItems(feedItemResourceNames) .build(); // Creates an operation to update CampaignExtensionSetting. CampaignExtensionSettingOperation operation = CampaignExtensionSettingOperation.newBuilder() .setUpdate(campaignExtensionSetting) .setUpdateMask(FieldMasks.allSetFieldsOf(campaignExtensionSetting)) .build(); // Connects to the API. try (CampaignExtensionSettingServiceClient client = googleAdsClient.getLatestVersion().createCampaignExtensionSettingServiceClient()) { // Issues the mutate request. MutateCampaignExtensionSettingsResponse response = client.mutateCampaignExtensionSettings( String.valueOf(customerId), ImmutableList.of(operation)); // Prints some debugging information. String resourceName = response.getResults(0).getResourceName(); System.out.printf( "Updated a campaign extension setting with resource name: '%s'.%n", resourceName); } }
C#
public void Run(GoogleAdsClient client, long customerId, long campaignId, long[] feedItemIds) { // Get the CampaignExtensionSettingService. CampaignExtensionSettingServiceClient campaignExtensionSettingService = client.GetService(Services.V6.CampaignExtensionSettingService); // Transform the specified extension feed item IDs to an array of resource names. IEnumerable<string> extensionFeedItems = feedItemIds.Select(feedItemId => ResourceNames.ExtensionFeedItem(customerId, feedItemId)); // Create a campaign extension setting using the specified campaign ID and extension // feed item resource names. CampaignExtensionSetting campaignExtensionSetting = new CampaignExtensionSetting { ResourceName = ResourceNames.CampaignExtensionSetting(customerId, campaignId, ExtensionType.Sitelink) }; campaignExtensionSetting.ExtensionFeedItems.Add(extensionFeedItems); // Construct an operation that will update the extension feed item using the FieldMasks // utilities to derive the update mask. This mask tells the Google Ads API which // attributes of the extension feed item you want to change. CampaignExtensionSettingOperation campaignExtensionSettingOperation = new CampaignExtensionSettingOperation { Update = campaignExtensionSetting, UpdateMask = FieldMasks.AllSetFieldsOf(campaignExtensionSetting) }; try { // Issue a mutate request to update the campaign extension setting. MutateCampaignExtensionSettingsResponse response = campaignExtensionSettingService.MutateCampaignExtensionSettings (customerId.ToString(), new[] {campaignExtensionSettingOperation}); // Print the resource name of the updated campaign extension setting. Console.WriteLine("Updated a campaign extension setting with resource name " + $"'{response.Results.First().ResourceName}'."); } 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 $campaignId, array $feedItemIds ) { // Transforms the specified feed item IDs to resource names as required by the API. $extensionFeedItems = array_map(function ($feedItemId) use ($customerId) { return ResourceNames::forExtensionFeedItem($customerId, $feedItemId); }, $feedItemIds); // Creates a campaign extension setting using the specified campaign ID and extension feed // item resource names. $campaignExtensionSetting = new CampaignExtensionSetting([ 'resource_name' => ResourceNames::forCampaignExtensionSetting( $customerId, $campaignId, ExtensionType::SITELINK ), 'extension_feed_items' => $extensionFeedItems ]); // Constructs an operation that will update the campaign extension setting, using the // FieldMasks utility to derive the update mask. This mask tells the Google Ads API which // attributes of the campaign extension setting you want to change. $campaignExtensionSettingOperation = new CampaignExtensionSettingOperation(); $campaignExtensionSettingOperation->setUpdate($campaignExtensionSetting); $campaignExtensionSettingOperation->setUpdateMask( FieldMasks::allSetFieldsOf($campaignExtensionSetting) ); // Issues a mutate request to update the campaign extension setting. $campaignExtensionSettingServiceClient = $googleAdsClient->getCampaignExtensionSettingServiceClient(); $response = $campaignExtensionSettingServiceClient->mutateCampaignExtensionSettings( $customerId, [$campaignExtensionSettingOperation] ); // Prints the resource name of the updated campaign extension setting. /** @var CampaignExtensionSetting $updatedCampaignExtensionSetting */ $updatedCampaignExtensionSetting = $response->getResults()[0]; printf( "Updated a campaign extension setting with resource name: '%s'.%s", $updatedCampaignExtensionSetting->getResourceName(), PHP_EOL ); }
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 update_sitelink_campaign_extension_setting { my ($api_client, $customer_id, $campaign_id, $feed_item_ids) = @_; # Transform the specified extension feed item IDs to the array of resource names. my $extension_feed_items = [ map { Google::Ads::GoogleAds::V6::Utils::ResourceNames::extension_feed_item( $customer_id, $_) } @$feed_item_ids ]; # Create a campaign extension setting using the specified campaign ID and # extension feed item resource names. my $campaign_extension_setting = Google::Ads::GoogleAds::V6::Resources::CampaignExtensionSetting->new({ resourceName => Google::Ads::GoogleAds::V6::Utils::ResourceNames::campaign_extension_setting( $customer_id, $campaign_id, SITELINK ), extensionFeedItems => $extension_feed_items }); # Construct an operation that will update the campaign extension setting, using # the FieldMasks utility to derive the update mask. This mask tells the Google # Ads API which attributes of the campaign extension setting you want to change. my $campaign_extension_setting_operation = Google::Ads::GoogleAds::V6::Services::CampaignExtensionSettingService::CampaignExtensionSettingOperation ->new({ update => $campaign_extension_setting, updateMask => all_set_fields_of($campaign_extension_setting)}); # Issue a mutate request to update the campaign extension setting. my $campaign_extension_settings_response = $api_client->CampaignExtensionSettingService()->mutate({ customerId => $customer_id, operations => [$campaign_extension_setting_operation]}); # Print the resource name of the updated campaign extension setting. printf "Updated a campaign extension setting with resource name: '%s'.\n", $campaign_extension_settings_response->{results}[0]{resourceName}; return 1; }
If you want to remove the now unused extension feed item, send a remove
operation to
MutateExtensionFeedItems
.