Google Ads API is returning to beta status. Please read our blog post for more details.

Configuration

Configuration is handled by the google.ads.google_ads.config module, though there is never a reason to access this module directly as it is called by the main GoogleAdsClient class on initialization. There are multiple different ways to configure the client.

Configuration YAML file

You can specify a google-ads.yaml file to use when initializing the client. This file is accessed when a client is initialized via the load_from_storage method.

If you use no arguments when initializing then the library will look in your $HOME directory for the file:

from google.ads.google_ads.client import GoogleAdsClient
ga_client = GoogleAdsClient.load_from_storage()

Alternatively, you can specify a path:

from google.ads.google_ads.client import GoogleAdsClient
client = GoogleAdsClient.load_from_storage('path/to/google-ads.yaml)

in which case the client will look for the file at that filepath.

The easiest way to generate this file is to copy the google-ads.yaml from the GitHub repo and modify it to include your credentials, including your developer token, refresh token, client ID, and client secret.

Configuration via environment variables

You can store all of your client configuration as environment variables, which will be read when using the client's load_from_env method. Environment variables should have the same name as those defined in the google-ads.yaml file, but should be all uppercase and prefixed with the GOOGLE_ADS_ namespace. For example client_id should be stored as GOOGLE_ADS_CLIENT_ID.

Environment variables are commonly defined in a bash configuration file such as a .bashrc or .bash_profile file located in the $HOME directory. They can also be defined via the command line. Note that these instructions assume you're using bash, if you're using a different shell you may need to consult documentation on how to set environment variables in the shell you're using.

Here are some basic steps to define an environment variable via a.bashrc file using a terminal:

# Append the line "export GOOGLE_ADS_CLIENT_ID=1234567890" to
# the bottom of your .bashrc file.
$ echo "export GOOGLE_ADS_CLIENT_ID=1234567890" >> ~/.bashrc
# Update your bash environment to use the most recently updated
# version of your .bashrc file.
$ src ~/.bashrc

Environment variables can also be set in your terminal instance directly from the command line:

$ export GOOGLE_ADS_CLIENT_ID=1234567890
$ echo $GOOGLE_ADS_CLIENT_ID
>> 1234567890

The load_from_env method loads configuration data from the environ attribute on Python's built-in os module. i.e.: os.environ['GOOGLE_ADS_CLIENT_ID']

Here's an example of how to initialize a client instance with configuration from environment variables:

from google.ads.google_ads.client import GoogleAdsClient
client = GoogleAdsClient.load_from_env()

In order to configure logging through environment variables the configuration value must be a JSON object that matches the structure of the YAML keys in the sample google-ads.yaml configuration file.

Here's an example of how this could be set via a .bashrc file:

export GOOGLE_ADS_LOGGING='{
  "version": 1,
  "disable_existing_loggers": false,
  "formatters": {
    "default_fmt": {
      "format": "[%(asctime)s - %(levelname)s] %(message).5000s",
      "datefmt": "%Y-%m-%d %H:%M:%S"
    }
  },
  "handlers": {
    "default_handler": {
      "class": "logging.StreamHandler",
      "formatter": "default_fmt"
    }
  },
  "loggers": {
    "": {
      "handlers": ["default_handler"],
      "level": "INFO"
    }
  }
}'

Configuration via YAML string

If you have read a YAML file into memory you can provide it directly to the client on initialization. To do this just use the load_from_string method.

from google.ads.google_ads import GoogleAdsClient

with open('/path/to/yaml', 'rb') as handle:
    yaml = handle.read()

client = GoogleAdsClient.load_from_string(yaml)

Configuration Fields

The client library configuration supports the following fields.

General fields:

  • refresh_token: Your OAuth refresh token.
  • client_id: Your OAuth client ID.
  • client_secret: Your OAuth client secret.
  • developer_token: Your developer token for accessing the API.
  • login_customer_id: See the login-customer-id documentation.
  • path_to_private_key_file: A path to a local private key file. This is used for authenticating using a service account. See the OAuth2 Service Account
  • delegate_account: An account email used as a delegate. This is used for authenticating using a service account. See the OAuth2 Service Account
  • logging: Logging configuration. Logging fields are described below.

General fields as environment variables:

  • GOOGLE_ADS_REFRESH_TOKEN
  • GOOGLE_ADS_CLIENT_ID
  • GOOGLE_ADS_CLIENT_SECRET
  • GOOGLE_ADS_DEVELOPER_TOKEN
  • GOOGLE_ADS_LOGIN_CUSTOMER_ID
  • GOOGLE_ADS_PATH_TO_PRIVATE_KEY_FILE
  • GOOGLE_ADS_DELEGATE_ACCOUNT
  • GOOGLE_ADS_LOGGING

Logging fields, which are fields underneath the logging configuration field, are derived directly from the logging.config built-in module because the library will pass all fields under the logging namespace directly to the logging.config.dictConfig method. See the logging guide for complete details.

  • version: An integer value representing a schema version.
  • disable_existing_loggers: Whether loggers configured elsewhere in the application should be disabled.
  • formatters: Dictionaries defining different kinds of formatters.
  • handlers: Dictionaries defining different handlers, which control where logs are written to and which formatters should be used.
  • loggers: Dictionaries defining different types of loggers, which include a handler and a log level.

Send feedback about...

Google Ads API Beta
Google Ads API Beta
Need help? Visit our support page.