Menu
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.

GET STARTED FREE

Overview

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.

Prerequisites

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 dessertdata.com
OAuth Consent
  • Test users: [your google cloud email]
  • Authorized Domain: dessertdata.com
Credentials
  • OAuth type: OAuth 2.0 Client IDs
  • Application type: Web application
  • Authorized Javascript Origins: [https://dessertdata.com, http://localhost]
  • Authorized Redirect URLs: [https://dessertdata.com, 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
GET https://accounts.google.com/o/oauth2/v2/auth
    ?scope=https://www.googleapis.com/auth/adwords
    &access_type=offline
    &include_granted_scopes=true
    &response_type=code
    &state=anything
    &redirect_uri=http://localhost
    &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.


POST https://oauth2.googleapis.com/token HTTP/1.1
Content-Type: application/x-www-form-urlencoded

code=[YOUR CODE]
&client_id={{ $dotenv GA_CLIENT_ID }}
&client_secret={{ $dotenv GA_CLIENT_SECRET }}
&redirect_uri=http://localhost
&grant_type=authorization_code

Your access token

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

POST https://www.googleapis.com/oauth2/v3/token HTTP/1.1
Content-Type: application/x-www-form-urlencoded

grant_type=refresh_token
&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 https://googleads.googleapis.com/v16/customers/[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
              segments.date,
              metrics.clicks,
              metrics.conversions,
              metrics.impressions,
              metrics.cost_per_all_conversions,
              metrics.cost_per_conversion,
              metrics.cost_per_all_conversions
            FROM keyword_view
            WHERE segments.date DURING LAST_30_DAYS"
}