Targeting for extension settings is done by using various fields on the
ExtensionFeedItem
. You can specify a start
time, end time, ad schedule, device, location target, or keyword. This will
restrict this extension feed item to only serving when the indicated criteria
are met.
Below is a demonstration of adding a geo target to an existing extension feed item.
Java
public static void main(String[] args) { AddGeoTargetParams params = new AddGeoTargetParams(); if (!params.parseArguments(args)) { // Either pass the required parameters for this example on the command line, or insert them // into the code here. See the parameter class definition above for descriptions. params.customerId = Long.parseLong("INSERT_CUSTOMER_ID_HERE"); params.feedItemId = Long.parseLong("INSERT_FEED_ID_HERE"); // Optional: Specify a geoTargetConstantId. params.geoTargetConstantId = GEO_TARGET_CONSTANT_ID; } GoogleAdsClient googleAdsClient = null; try { googleAdsClient = GoogleAdsClient.newBuilder().fromPropertiesFile().build(); } catch (FileNotFoundException fnfe) { System.err.printf( "Failed to load GoogleAdsClient configuration from file. Exception: %s%n", fnfe); System.exit(1); } catch (IOException ioe) { System.err.printf("Failed to create GoogleAdsClient. Exception: %s%n", ioe); System.exit(1); } try { new AddGeoTarget() .runExample( googleAdsClient, params.customerId, params.feedItemId, params.geoTargetConstantId); } catch (GoogleAdsException gae) { // GoogleAdsException is the base class for most exceptions thrown by an API request. // Instances of this exception have a message and a GoogleAdsFailure that contains a // collection of GoogleAdsErrors that indicate the underlying causes of the // GoogleAdsException. System.err.printf( "Request ID %s failed due to GoogleAdsException. Underlying errors:%n", gae.getRequestId()); int i = 0; for (GoogleAdsError googleAdsError : gae.getGoogleAdsFailure().getErrorsList()) { System.err.printf(" Error %d: %s%n", i++, googleAdsError); } System.exit(1); } }
C#
public void Run(GoogleAdsClient client, long customerId, long feedItemId, long? geoTargetConstantId) { // Get the ExtensionFeedItemServiceClient. ExtensionFeedItemServiceClient extensionFeedItemServiceClient = client.GetService(Services.V13.ExtensionFeedItemService); // Apply the default geo target constant ID (USA) if none was passed to the function. if (!geoTargetConstantId.HasValue) { geoTargetConstantId = 2840L; } // Creates an extension feed item using the specified feed item ID and geo target // constant ID for targeting. ExtensionFeedItem extensionFeedItem = new ExtensionFeedItem() { ResourceName = ResourceNames.ExtensionFeedItem(customerId, feedItemId), TargetedGeoTargetConstant = ResourceNames.GeoTargetConstant(geoTargetConstantId.Value) }; // Constructs an operation that will update the extension feed item, using the // FieldMasks utility to derive the update mask. This mask tells the Google Ads API // which attributes of the extension feed item you want to change. ExtensionFeedItemOperation operation = new ExtensionFeedItemOperation() { Update = extensionFeedItem, UpdateMask = FieldMasks.AllSetFieldsOf(extensionFeedItem) }; try { // Issues a mutate request to update the extension feed item. MutateExtensionFeedItemsResponse response = extensionFeedItemServiceClient.MutateExtensionFeedItems(customerId.ToString(), new[] { operation }); // Prints the resource name of the updated extension feed item. Console.WriteLine("Updated extension feed item 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 $feedItemId, int $geoTargetConstantId ) { // Creates an extension feed item using the specified feed item ID and geo target constant // ID for targeting. $extensionFeedItem = new ExtensionFeedItem([ 'resource_name' => ResourceNames::forExtensionFeedItem($customerId, $feedItemId), 'targeted_geo_target_constant' => ResourceNames::forGeoTargetConstant($geoTargetConstantId) ]); // Constructs an operation that will update the extension feed item, using the FieldMasks // utility to derive the update mask. This mask tells the Google Ads API which attributes of // the extension feed item you want to change. $extensionFeedItemOperation = new ExtensionFeedItemOperation(); $extensionFeedItemOperation->setUpdate($extensionFeedItem); $extensionFeedItemOperation->setUpdateMask(FieldMasks::allSetFieldsOf($extensionFeedItem)); // Issues a mutate request to update the extension feed item. $extensionFeedItemServiceClient = $googleAdsClient->getExtensionFeedItemServiceClient(); $response = $extensionFeedItemServiceClient->mutateExtensionFeedItems( $customerId, [$extensionFeedItemOperation] ); // Prints the resource name of the updated extension feed item. /** @var ExtensionFeedItem $updatedExtensionFeedItem */ $updatedExtensionFeedItem = $response->getResults()[0]; printf( "Updated extension feed item with resource name: '%s'.%s", $updatedExtensionFeedItem->getResourceName(), PHP_EOL ); }
Python
def main(client, customer_id, feed_item_id, geo_target_constant_id): """The main method that creates all necessary entities for the example. Args: client: an initialized GoogleAdsClient instance. customer_id: a client customer ID. feed_item_id: the ID of an extension feed item. geo_target_constant_id: the geo target constant ID to add to the extension feed item. """ extension_feed_item_service = client.get_service("ExtensionFeedItemService") extension_feed_item_operation = client.get_type( "ExtensionFeedItemOperation" ) extension_feed_item = extension_feed_item_operation.update # Creates an extension feed item using the specified feed item ID and # geo target constant ID for targeting. extension_feed_item.resource_name = extension_feed_item_service.extension_feed_item_path( customer_id, feed_item_id ) extension_feed_item.targeted_geo_target_constant = client.get_service( "GeoTargetConstantService" ).geo_target_constant_path(geo_target_constant_id) client.copy_from( extension_feed_item_operation.update_mask, protobuf_helpers.field_mask(None, extension_feed_item._pb), ) response = extension_feed_item_service.mutate_extension_feed_items( customer_id=customer_id, operations=[extension_feed_item_operation] ) print( "Updated extension feed item with resource name: " f"'{response.results[0].resource_name}'." )
Ruby
def add_geo_target(customer_id, feed_item_id, geo_target_constant_id) # GoogleAdsClient will read a config file from # ENV['HOME']/google_ads_config.rb when called without parameters client = Google::Ads::GoogleAds::GoogleAdsClient.new if geo_target_constant_id.nil? geo_target_constant_id = GEO_TARGET_CONSTANT_ID end resource_name = client.path.extension_feed_item(customer_id, feed_item_id) # Creates the update operation for extension feed item using the # specified feed item ID and geo target constant ID for targeting. operation = client.operation.update_resource.extension_feed_item(resource_name) do |efi| efi.targeted_geo_target_constant = client.path.geo_target_constant(geo_target_constant_id) end # Issues a mutate request to update the extension feed item. response = client.service.extension_feed_item.mutate_extension_feed_items( customer_id: customer_id, operations: [operation] ) # Prints the resource name of the updated extension feed item. puts "Updated extension feed item with resource name: " \ "'#{response.results.first.resource_name}'" end
Perl
sub add_geo_target { my ($api_client, $customer_id, $feed_item_id, $geo_target_constant_id) = @_; # Create an extension feed item using the specified feed item ID and geo target # constant ID for targeting. my $extension_feed_item = Google::Ads::GoogleAds::V13::Resources::ExtensionFeedItem->new({ resourceName => Google::Ads::GoogleAds::V13::Utils::ResourceNames::extension_feed_item( $customer_id, $feed_item_id ), targetedGeoTargetConstant => Google::Ads::GoogleAds::V13::Utils::ResourceNames::geo_target_constant( $geo_target_constant_id)}); # Construct an operation that will update the extension feed item, using the # FieldMasks utility to derive the update mask. This mask tells the Google Ads # API which attributes of the extension feed item you want to change. my $extension_feed_item_operation = Google::Ads::GoogleAds::V13::Services::ExtensionFeedItemService::ExtensionFeedItemOperation ->new({ update => $extension_feed_item, updateMask => all_set_fields_of($extension_feed_item)}); # Issue a mutate request to update the extension feed item. my $extension_feed_items_response = $api_client->ExtensionFeedItemService()->mutate({ customerId => $customer_id, operations => [$extension_feed_item_operation]}); # Print the resource name of the updated extension feed item. my $updated_extension_feed_item = $extension_feed_items_response->{results}[0]; printf "Updated extension feed item with resource name: '%s'.\n", $updated_extension_feed_item->{resourceName}; return 1; }