Update an extension setting

Updating an extension setting is as simple as modifying the ExtensionFeedItem. Any associated customers, campaigns, and feeds will be automatically updated to use the new values.

In the example below, we demonstrate how to update a sitelink's link text. The extension feed item resource name is carried over from the previous example.

Java

public static void main(String[] args) {
  UpdateSitelinkParams params = new UpdateSitelinkParams();
  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_ITEM_ID_HERE");
    params.sitelinkText = "INSERT_SITELINK_TEXT_HERE";
  }

  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 UpdateSitelink()
        .runExample(googleAdsClient, params.customerId, params.feedItemId, params.sitelinkText);
  } 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,
    string sitelinkText)
{
    // Get the ExtensionFeedItemService.
    ExtensionFeedItemServiceClient extensionFeedItemService =
        client.GetService(Services.V6.ExtensionFeedItemService);

    // Create an extension feed item using the specified feed item ID and sitelink text.
    ExtensionFeedItem extensionFeedItem = new ExtensionFeedItem
    {
        ResourceName = ResourceNames.ExtensionFeedItem(customerId, feedItemId),
        SitelinkFeedItem = new SitelinkFeedItem
        {
            LinkText = sitelinkText
        }
    };

    // 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.
    ExtensionFeedItemOperation extensionFeedItemOperation = new ExtensionFeedItemOperation
    {
        Update = extensionFeedItem,
        UpdateMask = FieldMasks.AllSetFieldsOf(extensionFeedItem)
    };

    try
    {
        // Issue a mutate request to update the extension feed item.
        MutateExtensionFeedItemsResponse response =
            extensionFeedItemService.MutateExtensionFeedItems(
                customerId.ToString(), new[] {extensionFeedItemOperation});

        // Print 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,
    string $sitelinkText
) {
    // Creates an extension feed item using the specified feed item ID and sitelink text.
    $extensionFeedItem = new ExtensionFeedItem([
        'resource_name' => ResourceNames::forExtensionFeedItem($customerId, $feedItemId),
        'sitelink_feed_item' => new SitelinkFeedItem(['link_text' => $sitelinkText])
    ]);

    // 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
    );
}

Ruby

client = Google::Ads::GoogleAds::GoogleAdsClient.new

operation = client.operation.update_resource.extension_feed_item(efi_resource_name) do |efi|
  efi.sitelink_feed_item = client.resource.sitelink_feed_item
  efi.sitelink_feed_item.link_text = "Hours of Operation"
end

client.service.extension_feed_item.mutate_extension_feed_items(customer_id, [operation])
    

Perl

sub update_sitelink {
  my ($api_client, $customer_id, $feed_item_id, $sitelink_text) = @_;

  # Create an extension feed item using the specified feed item ID and sitelink text.
  my $extension_feed_item =
    Google::Ads::GoogleAds::V6::Resources::ExtensionFeedItem->new({
      resourceName =>
        Google::Ads::GoogleAds::V6::Utils::ResourceNames::extension_feed_item(
        $customer_id, $feed_item_id
        ),
      sitelinkFeedItem =>
        Google::Ads::GoogleAds::V6::Common::SitelinkFeedItem->new({
          linkText => $sitelink_text
        })});

  # 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::V6::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.
  printf
    "Updated extension feed item with resource name: '%s'.\n",
    $extension_feed_items_response->{results}[0]{resourceName};

  return 1;
}