This guide covers some best practices you can implement to optimize the
efficiency and performance of your apps.

## Maintain your app

To ensure that your app runs uninterrupted, do the following:

- Make sure that the list of owners and editors of your Google Cloud
  project is up to date. We will contact these users in case of any
  emergencies or topics related to compliance with the API Terms and
  Conditions. If we're unable to contact you regarding compliance with the API
  Terms and Conditions, your API access may be lowered or revoked.

- To be informed of issues such as product changes, maintenance downtime, and
  deprecation dates, subscribe to our [API blog](https://ads-developers.googleblog.com/search/label/google_ads_api) and
  [Product blog](https://blog.google/products/ads-commerce/).

- Keep your app compliant with the Google Ads API [Terms and
  Conditions](https://developers.google.com/google-ads/api/docs/api-policy/terms) (T\&C). If required, the API compliance
  team will reach out to the owners and editors of your Google Cloud
  project with API access. If you have questions or concerns about the T\&C,
  you can reach out to the compliance team by responding to the email they
  sent you when reviewing your API access application.

## Optimization

You can optimize your app by running batch operations and, if appropriate,
sending sparse objects.

### Batch operations

Making a request to the API entails a number of fixed costs, such as round-trip
network latency, serialization and deserialization processing, and calls to
backend systems. To lessen the impact of these fixed costs and increase overall
performance, most mutate methods in the API are designed to accept an array of
operations. By batching multiple operations into each request, you can reduce
the number of requests you make and the associated fixed costs. If you can,
avoid making requests with only one operation.

For example, suppose you're adding 50,000 keywords to a campaign across multiple
ad groups. Instead of making 50,000 requests with 1 keyword each, make 100
requests with 500 keywords each, or even 10 requests with 5,000 keywords each.
There are limits on the number of operations allowed in a request, so you may
need to adjust your batch size to achieve optimal performance.

### Send sparse objects

When objects are sent to the API, fields must be deserialized, validated, and
stored in the database. Passing in full objects when you only want to update a
few fields can result in extra processing time and decreased performance. To
mitigate this, the Google Ads API supports sparse updates, allowing you to populate
only the fields in an object that you need to change or that are required.
Sparse updates process faster and are less likely to produce errors. Fields that
aren't in the `update_mask` (also known as `FieldMask`) are left unchanged.

For example, an app that updates keyword-level bids can benefit from using
sparse updates, as only the ad group ID, criterion ID, and bids fields would
need to be populated.

## Error handling and management

During development, you're likely to encounter errors. This section describes
considerations and strategies for building error management into your app. In
addition to this section, visit the [Troubleshooting
guide](https://developers.google.com/google-ads/api/docs/best-practices/troubleshooting) for more information on managing errors.

### Distinguish request sources

Some apps are primarily interactive, issuing API calls directly in response to
user-initiated actions in a UI. Others work primarily offline, issuing API calls
as part of a periodic backend process. Many apps combine the two. When thinking
about error management, it can be useful to distinguish these different types of
requests.

For user-initiated requests, your primary concern should be providing a good
experience for your users. Use the specific error that occurred to provide the
user with as much context as you can in the UI. Offer clear steps they can take
to resolve the error (see the following suggestions).

For requests initiated on the backend, implement handlers for the different
types of errors your app may encounter. Always include a default handler to
address rare or previously unencountered errors. A good approach for a default
handler is to add the failed operation and error to a queue for a human operator
to review and determine an appropriate resolution.

### Distinguish error types

Knowing the differences between error types in Google Ads API is crucial when building
robust error handling. Some of the most common error types are:

- [Authentication errors](https://developers.google.com/google-ads/api/docs/best-practices/error-types#authentication_errors)
- [Errors that can be retried](https://developers.google.com/google-ads/api/docs/best-practices/error-types#retryable_errors)
- [Validation errors](https://developers.google.com/google-ads/api/docs/best-practices/error-types#validation_errors)
- [Sync-related errors](https://developers.google.com/google-ads/api/docs/best-practices/error-types#sync-related_errors)

Refer to [Error types](https://developers.google.com/google-ads/api/docs/best-practices/error-types) and [Common errors](https://developers.google.com/google-ads/api/docs/common-errors) for more
details.

### Sync backends

If your app's users have manual access to Google Ads accounts, they may make
changes that your app is not aware of, causing your app's local database to go
out of sync. As noted in our [Error types](https://developers.google.com/google-ads/api/docs/best-practices/error-types) guide, you can address
sync-related errors reactively when they occur, but you can also try to prevent
them proactively. One proactive strategy is to run a periodic sync job to
reconcile your local database with the Google Ads objects in your accounts. For
large account hierarchies, avoid pulling all objects across every account
nightly to prevent exhausting daily quotas; instead, query
[`ChangeStatus`](https://developers.google.com/google-ads/api/docs/change-status) or filter for recently modified entities to sync
changes incrementally.

### Log errors

All errors should be logged to facilitate debugging and monitoring. At a
minimum, log the request ID, the operations that caused the error, and the error
itself. Other information to log includes customer ID, API service, round-trip
request latency, number of retries, and the sanitized raw request and response
(making sure to redact sensitive credentials such as OAuth tokens and any PII).

### Monitor trends

Be sure to monitor trends in API errors so that you can detect and address
problems with your app. Consider building your own solution or employing one of
many available commercial tools that can use your logs to produce interactive
dashboards and send automated alerts.

## Development

Use test accounts during development.

### Use test accounts

[Test accounts](https://developers.google.com/google-ads/api/docs/best-practices/test-accounts) are Google Ads accounts that don't actually serve
ads. You can use a test account to experiment with the Google Ads API and test that
your app's connectivity, campaign management logic, or other processing are
working as expected. Your Google Cloud project needs only Test access to be used
on a test account, so you can start developing with the Google Ads API immediately,
while waiting for Google to review your application for higher API access
levels.