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#
public void Run(GoogleAdsClient client, long customerId, long campaignId) { // Get the GoogleAdsService client. GoogleAdsServiceClient googleAdsServiceClient = client.GetService(Services.V13.GoogleAdsService); List<MutateOperation> mutateOperations = new List<MutateOperation>(); try { // Create a mutate operation the contains the campaign extension setting operation // to remove the specified sitelink campaign extension setting. mutateOperations.Add( CreateSitelinkCampaignExtensionSettingMutateOperation(customerId, campaignId)); // Get all sitelink extension feed items of the specified campaign. List<string> extensionFeedItemResourceNames = GetAllSitelinkExtensionFeedItems(googleAdsServiceClient, customerId, campaignId); // Create mutate operations, each of which contains an extension feed item // operation to remove the specified extension feed items. mutateOperations.AddRange(CreateExtensionFeedItemMutateOperations( extensionFeedItemResourceNames)); // Issue a mutate request to remove the campaign extension setting and its // extension feed items. MutateGoogleAdsResponse mutateGoogleAdsResponse = googleAdsServiceClient.Mutate( customerId .ToString(), mutateOperations); RepeatedField<MutateOperationResponse> mutateOpResponses = mutateGoogleAdsResponse.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. Console.WriteLine("Removed a campaign extension setting with resource name: " + $"'{mutateOpResponses.First().CampaignExtensionSettingResult.ResourceName}'."); foreach (MutateOperationResponse response in mutateOpResponses.Skip(1)) { Console.WriteLine("Removed an extension feed item with resource name: " + $"'{response.ExtensionFeedItemResult.ResourceName}'."); } } catch (GoogleAdsException e) { Console.WriteLine("Failure:"); Console.WriteLine($"Message: {e.Message}"); Console.WriteLine($"Failure: {e.Failure}"); Console.WriteLine($"Request ID: {e.RequestId}"); throw; } catch (ArgumentException e) { Console.WriteLine("Argument Exception:"); Console.WriteLine($"Message: {e.Message}"); Console.WriteLine(e.StackTrace); throw; } }
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
def main(client, customer_id, campaign_id): """The main method that creates all necessary entities for the example. Args: client: an initialized GoogleAdsClient instance. customer_id: a client customer ID. campaign_id: the campaign ID. """ # Initialize an array of MutateOperations mutate_operations = [] sitelink_campaign_extension_setting_mutate_operation = create_sitelink_campaign_extension_setting_mutate_operation( client, customer_id, campaign_id ) mutate_operations.append( sitelink_campaign_extension_setting_mutate_operation ) ga_service = client.get_service("GoogleAdsService") extension_feed_item_resource_names = get_all_sitelink_extension_feed_items( client, ga_service, customer_id, campaign_id ) extension_feed_item_mutate_operations = create_extension_feed_item_mutate_operations( client, extension_feed_item_resource_names ) mutate_operations.extend(extension_feed_item_mutate_operations) # Issue a mutate request to remove the campaign extension setting and # its extension feed items. response = ga_service.mutate( customer_id=customer_id, mutate_operations=mutate_operations ) mutate_operation_responses = response.mutate_operation_responses # The order of response messages corresponds to the order of operations # passed into the mutate method. Since the first operation sent in the # request was to remove a campaign extension setting, we can read the # resource name of that object in the first message in the response list. print( "Removed a campaign extension setting with resource name " f'"{mutate_operation_responses[0].campaign_extension_setting_result.resource_name}".' ) # Since we read the result of the first remove operation above, next we # read the results for the remaining remove operations by iterating over all # but the first message in the response list. for mutate_operation_response in mutate_operation_responses[1:]: print( "Removed an extension feed item with resource name " f'"{mutate_operation_response.extension_feed_item_result.resource_name}".' )
Ruby
def remove_entire_sitelink_campaign_extension_setting(customer_id, campaign_id) # GoogleAdsClient will read a config file from # ENV['HOME']/google_ads_config.rb when called without parameters client = Google::Ads::GoogleAds::GoogleAdsClient.new mutate_operations = [] # Creates a mutate operation that contains the campaign extension setting # operation to remove the specified sitelink campaign extension setting. mutate_operations << create_sitelink_campaign_extension_setting_mutate_operation( client, customer_id, campaign_id, ) # Gets all sitelink extension feed items of the specified campaign. google_ads_service = client.service.google_ads extension_feed_item_resource_names = get_all_sitelink_extension_feed_items( client, google_ads_service, customer_id, campaign_id, ) # Creates mutate operations, each of which contains an extension feed item # operation to remove the specified extension feed items. mutate_operations += create_extension_feed_item_mutate_operations( client, extension_feed_item_resource_names, ) # Issues a mutate request to remove the campaign extension setting and its # extension feed items. response = google_ads_service.mutate( customer_id: customer_id, mutate_operations: mutate_operations, ) mutate_operation_responses = response.mutate_operation_responses # 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. puts "Removed a campaign extension setting with resource name: " \ "#{mutate_operation_responses[0].campaign_extension_setting_result.resource_name}" for i in 1..mutate_operation_responses.size - 1 puts "Removed an extension feed item with resource name: " \ "#{mutate_operation_responses[i].extension_feed_item_result.resource_name}" end end
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#
private List<string> GetAllSitelinkExtensionFeedItems( GoogleAdsServiceClient googleAdsServiceClient, in long customerId, in long campaignId) { // Create a query that retrieves all campaigns. string query = $@" 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 = '{ResourceNames.Campaign(customerId, campaignId)}' AND campaign_extension_setting.extension_type = 'SITELINK'"; // Issue a search stream request, then iterate over all responses. // Print out and store in a list each extension feed item's resource name. List<string> extensionFeedItemResourceNames = new List<string>(); googleAdsServiceClient.SearchStream(customerId.ToString(), query, delegate (SearchGoogleAdsStreamResponse resp) { foreach (GoogleAdsRow googleAdsRow in resp.Results) { foreach (string extensionFeedItemResourceName in googleAdsRow .CampaignExtensionSetting.ExtensionFeedItems) { extensionFeedItemResourceNames.Add(extensionFeedItemResourceName); Console.WriteLine("Extension feed item with resource name " + $"'{extensionFeedItemResourceName}' was found."); } } } ); if (!extensionFeedItemResourceNames.Any()) { throw new ArgumentException("The specified campaign does not contain a sitelink " + "campaign extension setting."); } return extensionFeedItemResourceNames; }
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
def get_all_sitelink_extension_feed_items( client, ga_service, customer_id, campaign_id ): """Gets all sitelink extension feed items associated to the specified campaign extension setting. Args: client: an initialized GoogleAdsClient instance. ga_service: the Google Ads API service client. customer_id: the client customer ID. campaign_id: the ID of the campaign to get the sitelink extension feed items from. Returns: An array of str resource names of extension feed items. """ campaign_resource_name = client.get_service( "CampaignService" ).campaign_path(customer_id, campaign_id) extension_type_enum = client.enums.ExtensionTypeEnum extension_type_name = extension_type_enum.SITELINK.name # Construct the query. query = f""" 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 = '{campaign_resource_name}' AND campaign_extension_setting.extension_type = '{extension_type_name}'""" # Issue a search request using streaming. stream = ga_service.search_stream(customer_id=customer_id, query=query) extension_feed_item_resource_names = [] # Iterate through each row and append the extension feed item resource # names to the return array. for batch in stream: for row in batch.results: extension_feed_item_resource_names.extend( row.campaign_extension_setting.extension_feed_items ) if len(extension_feed_item_resource_names) == 0: print( "The specified campaign does not contain a sitelink campaign " "extension setting." ) sys.exit(1) return extension_feed_item_resource_names
Ruby
def get_all_sitelink_extension_feed_items( client, google_ads_service, customer_id, campaign_id ) # Creates a query that retrieves all campaigns. query = <<~QUERY 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 = '#{client.path.campaign(customer_id, campaign_id)}' AND campaign_extension_setting.extension_type = 'SITELINK' QUERY # Issues a search stream request stream = google_ads_service.search_stream( customer_id: customer_id, query: query, ) extension_feed_item_resource_names = [] # Iterates over all rows in all messages and prints the requested field values # for the campaign extension setting in each row. stream.each do |response| response.results.each do |row| extension_feed_items = row.campaign_extension_setting.extension_feed_items extension_feed_items.each do |item| extension_feed_item_resource_names << item puts "Extension feed item with resource name #{item} was found." end end end extension_feed_item_resource_names end
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::V13::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::V13::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.V13.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::name(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
def main(client, customer_id, campaign_id, feed_item_ids): """The main method that creates all necessary entities for the example. Args: client: an initialized GoogleAdsClient instance. customer_id: a client customer ID. campaign_id: the campaign ID. feed_item_ids: the IDs the feed items to replace. """ campaign_extension_setting_service = client.get_service( "CampaignExtensionSettingService" ) campaign_extension_setting_operation = client.get_type( "CampaignExtensionSettingOperation" ) extension_feed_item_service = client.get_service("ExtensionFeedItemService") campaign_extension_setting = campaign_extension_setting_operation.update # Replace the current extension feed items with the given list campaign_extension_setting.extension_feed_items.extend( [ extension_feed_item_service.extension_feed_item_path( customer_id, feed_item_id ) for feed_item_id in feed_item_ids ] ) extension_type_enum = client.enums.ExtensionTypeEnum resource_name = campaign_extension_setting_service.campaign_extension_setting_path( customer_id, campaign_id, extension_type_enum.SITELINK.name ) campaign_extension_setting.resource_name = resource_name # Produce a field mask enumerating the changed fields client.copy_from( campaign_extension_setting_operation.update_mask, protobuf_helpers.field_mask(None, campaign_extension_setting._pb), ) # Update the campaign extension settings response = campaign_extension_setting_service.mutate_campaign_extension_settings( customer_id=customer_id, operations=[campaign_extension_setting_operation], ) print( "Updated campaign extension setting with resource name: " f'"{response.results[0].resource_name}".' )
Ruby
def update_sitelink_campaign_extension_setting(customer_id, campaign_id, feed_item_ids) # GoogleAdsClient will read a config file from # ENV['HOME']/google_ads_config.rb when called without parameters client = Google::Ads::GoogleAds::GoogleAdsClient.new # Replace the current extension feed items with the given list operation = client.operation.update_resource.campaign_extension_setting( client.path.campaign_extension_setting( customer_id: customer_id, campaign_id: campaign_id, extension_type: :SITELINK ) ) do |ces| feed_item_ids.each do |feed_item_id| # Transforms the specified feed item IDs to resource names as required by the API. ces.extension_feed_items << client.path.extension_feed_item(customer_id, feed_item_id) end end # Update the campaign extension settings response = client.service.campaign_extension_setting.mutate_campaign_extension_settings( customer_id: customer_id, operations: [operation], ) puts "Updated campaign extension setting with resource name: " \ "'#{response.results.first.resource_name}'." end
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::V13::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::V13::Resources::CampaignExtensionSetting->new({ resourceName => Google::Ads::GoogleAds::V13::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::V13::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
.