How to connect to Google Ads API with R (Advanced AdWords)

In this tutorial, I will help you to analyze Google Adwords data with R using the Google Ads API. This will be a useful tool in automation or programmatic strategies that can help you save a massive amount of time and supercharge your revenues.

Let’s learn how to pull data in with Google Apps Script from R.

How to connect to the Google Ads API

If this is your first time using an API, don’t worry, I was once a beginner not so long ago. I will make it as easy as possible to set up an API call in Google Ads from R.


Subscribe to my Newsletter



Requirements

To be able to use Google Ads API you need to meet the following requirements:

  • You need a Google Ads Manager Account
  • You need to request access to the API in the AdWords API Center
  • You need your API Developer Token
  • You should have at least a “Basic Access” in the AdWords API. The “Test account” will not work with the Radwords package.

What is an API?

The Google Ads API, or Application Programming Interface, was built for advertisers and developers to interact directly with the Google AdWords server to help them manage large Google Ads Accounts.

Step 1: Create a Project in Google AdWords API

The first step to create your AdWords API is to set up your first project.

1. Go to Google’s developers console, and sign in.

Here, I will call my project Adwords-API

Create a new project in Google Ads API
Create a new project in Google Ads API

2. Go to Dashboard and click “Enable APIs and Services”

Click Enable APIs and Services
Click Enable APIs and Services

3.  Search for the terms “Google Ads API” and click on the API to create

Select Google Ads API
Select Google Ads API

4. Click on “Enable”

Enable google ads api
Enable Google AdWords api

Step 2: Create your credentials

Now, that you have enabled your Google Ads API. It is time that you create your credentials. The goal of this part of the tutorial is to generate your client.id and your client.secret.

Go in the “credential” tab on the left, click on “Credentials in APIs & Services”.

Select Credentials in APIs & Services

Then click on “create credential” and select “OAuth Client ID”.

auth client id
Create AdWords API

Now it is time to name your project. So click on “configure consent screen”.

configure consent screen

3. You will now have to set your product name and Save your product. Here I will call it Google Ads API.

Once you have save your project, a new screen appears. Choose the application type: Other. Name it whatever you want. I will call it Google Ads API. Click on “Create”.

5. Bravo, you now have access to your client.id and client.secret .

OAuth Client Id and Client Secret
OAuth Client Id and Client Secret

This is it, you can now connect to the Google Ads API using R.

Soon, I will update this post to show you how to use the adwordsR package, to get batch keywords search volume for thousands of keywords.

Connect to AdWords API using Radwords package

If you have followed every step you should now have access to: a Google Ads Manager Account, a valid client id, a client secret and a developer token with “Basic Access” level. Here you can learn how to apply for access to the adwords api.

If you have all of this, you are now ready to connect to the Google Ads API using Radwords package.

Step 1: Install the required packages

The Radwords package can be either installed from CRAN or from github. Here I will use CRAN.

Add this bit of code to your R script to install and load the Radwords library.

#Install the Radwords package
install.packages("RAdwords")
library(RAdwords)

Step 2: Authenticate to the API

It is now time to load your credentials in the authentication process. This step is like adding your password to the API. This is where you will need your client id, client secret and developer token.

We will start the authentication using doAuth() and store the credential in a R object called google_oauth.

#Authenticate
google_oauth <-doAuth()

By adding this bit of code, it will prompt you to import your keys within R. Follow the instructions in the R console:

1. Add the client.ID that we created earlier from the Developer console and press Enter:

Paste the Client secret here :=>
xxxxxxxxxxxxxxxxxxxx.apps.googleusercontent.com # Copy the API key from Google developer console

2. Paste your client secret and press Enter:

Paste the Client secret here :=>
xxxxxxxxxxxxxxxxxxxx.apps.googleusercontent.com # Copy the API key from Google developer console

3. Add the AdWords developer token that you were given when requesting access to the API center

Paste the Developer Token here :=>
xxxxxxxxxxxxx  #This was given to you when you requested for API access. A popup window will open, copy the client token.
Paste the client token here :=>
4/8xxhSxxxxxxxbQxxxxxxxxxxxxxxxxxxx 

4. A window should open automatically in your browser asking you to authorize the access. 

5. Connect with your email that you used to create your API project and to access the Google Ads Manager Account.

6.  Click on Authorize

7. Copy the client token and paste it in the console

Paste the client token here :=> 4/8xxhSxxxxxxxbQxxxxxxxxxxxxxxxxxxx  

Awesome! You are now connected to the Google AdWords API!

In your project you will now find an object google_oauth that contains all your credentials and an adwords api refresh token that will automatically refresh your token when they expire. Next, we will learn how to load Google Adwords data into R.

How to set-up a Google Ads API Call with R

It is important to know that this R package is not officially supported in the AdWords API. This is only a workaround to help you access the Google Ads API from R. See the supported client libraries.

Explore the Radwords Package

Before we make our first AdWords API call, let’s explore what kind of reports we can do:

#See possible reports
reports()

#See metrics for a single report (e.g. campaign performance
metrics(report='CAMPAIGN_PERFORMANCE_REPORT')

Build a Query

To build your first query, you need to use the statement() function. You can save each of those query in a R object to mannage each query.

#Authenticate
library(RAdwords)
google_oauth <-doAuth()

#Query a single campaign performance
campaign_performance <-statement(select=c('Date',
'AccountDescriptiveName',
'CampaignName',
'Impressions',
'Clicks',
'Cost',
'Conversions'),
report="CAMPAIGN_PERFORMANCE_REPORT",
start="2019-01-01",
end="2019-02-01")

#See your adwords data in r using getData()
data <- getData(clientCustomerId='XXX-XXX-XXXX', #Put your AdWords Account ID and not your Google Ads Manager Account.
google_auth=google_oauth,
statement=body)

Other APIs You Might Be Interested In

Looking for the next steps to improve your skills using APIs?How to Connect to a Google API

Congratulations, you’ve built your first Query in Google Ads reporting API using the Radwords package. You are now ready to manage all your Adwords data in R.

5/5 - (2 votes)