Dessert Data
A love for ingesting data

Google Ads

Criteria Metrics

Psst... Dessert Data connects all your marketing data into a neatly organized database. You can use the same query to pull spend for Bing, TikTok, or Google.

Don't worry about APIs changes or database uptime. We handle all that for you.



Google Ads provides well documented APIs for tracking your ad performance. They are medium difficulty to get started, but very robust once you are running. Google recommends using the client libraries, but this blog post will cover the REST API.

Note - the HTTP requests below are compatible with the VSCode Rest Client. I find this extension easier than Postman because you can check-in your requests with your code. Parameters like {{ $dotenv SOME_ID }} are referencing environment variables from a .env file.

Alsø Alsø note - this post goes into details for setting up your Google Ads accounts. If you just want the code, then scroll to the bottom.


Google requires you sto start with sandbox accounts. This holds for your OAuth app as well.
The steps are:
  1. Get a developer token
  2. Setup an OAuth refresh token
  3. Identify your customer ID and manager ID
  4. Query the Google Ads Search API

Get a developer token

To obtain a developer token, you will first need a manager account linked to your Google Ads account. Once you have them linked, you will see an API Center option appear in your Tools and Settings options.CJ Business ID

Setup an OAuth refresh token

Log into Google Cloud, setup an OAuth Consent Screen, and then create OAuth credentials. The setup is application specific, below are examples for
OAuth Consent
  • Test users: [your google cloud email]
  • Authorized Domain:
  • OAuth type: OAuth 2.0 Client IDs
  • Application type: Web application
  • Authorized Javascript Origins: [, http://localhost]
  • Authorized Redirect URLs: [, http://localhost]

Note - write down your OAuth Client ID and Client Secret. You will need them in the next step.

Request your initial OAuth code

The first step in the OAuth flow is to get an authorization code from the client. You do this by crafting a URL and opening it in a browser.

// Remove newlines
    &client_id={{ $dotenv GA_CLIENT_ID }}

Once you authorize your client, you will be redirected to a page that seems broken, but the URL will contain a code parameter. Copy that code and use it to get a refresh token.

http://localhost/?state=anything&code=[YOUR CODE]&scope=...

Get your refresh token

Using the [YOUR CODE] value from the above URL, you can craft an API call to get an access token, and more importantly refresh token.

Content-Type: application/x-www-form-urlencoded

code=[YOUR CODE]
&client_id={{ $dotenv GA_CLIENT_ID }}
&client_secret={{ $dotenv GA_CLIENT_SECRET }}

Your access token

You will need to an access token for future requests, you use your refresh token to retrieve one.

Content-Type: application/x-www-form-urlencoded

&client_id={{ $dotenv GA_CLIENT_ID }}
&client_secret={{ $dotenv GA_CLIENT_SECRET }}
&refresh_token={{ $dotenv GA_REFRESH_TOKEN }}

Identify your customer ID and manager ID

The final thing you will need is the customer ID and manager ID from your test accounts. These are available in the Google Ads UI.Google Ads Manager and Customer ID

Query the Google Ads Search API

Now you have everything you need to make your first call to the Google Ads API. This API lets you make SQL like queries over a REST like API. You will need to learn the schema to make the most of it, but it's straightforward if you've made it this far.
Query Performance and Spend metrics

Note - You need to remove dashes from your customer ID and manager ID for the query. So 123-456-7890 becomes 1234567890.

POST[YOUR CUSTOMER ID]/googleAds:searchStream HTTP/1.1
Content-Type: application/json
Accept: application/json
Authorization: Bearer {{ $dotenv GA_ACCESS_TOKEN }}
linked-customer-id: [YOUR MANAGER ID]
developer-token: {{ $dotenv GA_DEVELOPER_TOKEN }}

  "query" : "SELECT
            FROM keyword_view
            WHERE DURING LAST_30_DAYS"