Google Ads Api: understand and manage accounts and campaigns programatically with python

Rihab Rahali
3 min readAug 2, 2020

Google ads API enables developers to interact directly with the Google Ads platform, vastly increasing the efficiency of managing large or complex Google Ads accounts and campaigns.

You’ll need the following information:
* Developer token: Your developer token grants access to the API, not to a particular account
* Client customer ID: the account number of the Google Ads client account you want to manage via the API

Understand Google ads Api

The Google Ads API consists of resources and services.

  • Resources: represents a Google Ads entity like campaigns, customers..
  • Services: retrieve and manipulate Google Ads entities. There are three types of services: modify (mutate) objects, get objects and performance stats and retrieve metadata.

Google ads account structure

Each account can manage one or more compaigns.

For the multiple marketing and advertising uses cases, google ads offers you the possibility to manage: Campaigns, ad groups, ads, targeting, remarketing and audience targeting, dynamic targeting…..

We can’t explain all Google ads services but we just explained to you Google ads basics. To have more details there is no better documentation than Google Ads API Beta documenation

How to manage your Google ads account programatically with python using Google ads API.

Using Google ads Api is very easy you need to have first some knowledge about marketing and advertising terms. The manipulation is not complex and alot of exemples are provided in github. But we can explain to you in general how things work.

Step1: prepare googleads.yaml file

OAuth2 configuration
The below configuration parameters are used to authenticate using the recommended OAuth2 flow. For more information on authenticating with OAuth2 see https://developers.google.com/google-ads/api/docs/oauth/overview
developer_token
client_id
client_secret
refresh_token

# Login customer ID configuration
Required for manager accounts only: Specify the login customer ID used to
authenticate API calls. This will be the customer ID of the authenticated
manager account. It should be set without dashes, for example: 1234567890
instead of 123–456–7890. You can also specify this later in code if your
application uses multiple manager account + OAuth pairs.
login_customer_id

Step2: Make an Api call

Now that your environment and config file are all set up, it’s time to make your first API call:

Exemple1: let’s try to display all campaigns related to a given customer ID.

Query structure

Query -> SelectClause FromClause? WhereClause? OrderByClause? LimitClause? ParametersClause?
SelectClause -> SELECT FieldName (, FieldName)*
FromClause -> FROM ResourceName
WhereClause -> WHERE Condition (AND Condition)*
OrderByClause -> ORDER BY Ordering (, Ordering)*
LimitClause -> LIMIT PositiveInteger
ParametersClause -> PARAMETERS Literal = Value (, Literal = Value)*

Condition -> FieldName Operator Value
Operator -> = | != | > | >= | < | <= | IN | NOT IN |
LIKE | NOT LIKE | CONTAINS ANY | CONTAINS ALL |
CONTAINS NONE | IS NULL | IS NOT NULL | DURING |
BETWEEN
Value -> Literal | LiteralList | Number | NumberList | String |
StringList | Function
Ordering -> FieldName (ASC | DESC)?

FieldName -> [a-z] ([a-zA-Z0–9._])*
ResourceName -> [a-z] ([a-zA-Z_])*

StringList -> ( String (, String)* )
LiteralList -> ( Literal (, Literal)* )
NumberList -> ( Number (, Number)* )

PositiveInteger -> [1–9] ([0–9])*
Number -> -? [0–9]+ (. [0–9] [0–9]*)?
String -> (‘ Char* ‘) | (“ Char* “)
Literal -> [a-zA-Z0–9_]*

Function -> LAST_14_DAYS | LAST_30_DAYS | LAST_7_DAYS |
LAST_BUSINESS_WEEK | LAST_MONTH | LAST_WEEK_MON_SUN |
LAST_WEEK_SUN_SAT | THIS_MONTH | THIS_WEEK_MON_TODAY |
THIS_WEEK_SUN_TODAY | TODAY | YESTERDAY

Another exemple: How to add targeting campaign critera

With Google ads API you can make basic operations add, get, pause, remove, update of a ressource, and other advanced.

Conclusion:

If you have a small bussiness and you have a limited number of google ads account you can just use the interface of google ads to manage your account. But if you have many accounts or you have an aggregate account that manage many client accounts it is more clever to manage them programatically.

for more codes you just check the api github repo it includes many exemples:
https://github.com/googleads/google-ads-python/tree/master/examples

--

--