In this guide, I’m going to show you how to pull data out of Google Ads using the APIs and an R Package called RAdwords.
Generate the client Id and client secret credentials
Open the Google Developers Console, the platform where you can generate the credentials for every Google API (e.g. Google Analytics API, Google Search Console API, Indexing API,… ) and go to the APIs Library.
Find the Google Ads API and Enable the service. In the credentials section, you can generate the client ID and client secret that you need for the OAuth process.
Click on OAuth client ID and select Web application in order to generate the right credentials for RAdwords.
You have to save somewhere the credentials because you’ll need them in a later step.
Get a Developer Token for the Google Ads API
You also need a developer token which you can get in the Google Ads interface.
Open the Tools & settings menu and go to API Centre.
If you want to work with the Google Ads API in R you’ll need a developer token with at least a Basic Access level, the packages don’t work with a Test Access level.
In order to get a Basic Access token, you have to fill out the application form.
Connect the API and RAdwords using the doAuth() function
Open your R Ide and install RAdwords using the install.packages() function. In order to access the Google Ads API you have to use the doAuth() function which guides you through the OAuth2.0 authentication process.
1 2 3 4 5 6 |
# Install and Load the RAdwords package install.packages("RAdwords") library("RAdwords") # Launch the OAuth process using doAuth googleAuth <- doAuth() |
Run the code above and follow the messages you’ll see in your console, they’ll ask you to enter the credentials you generated earlier:
- The client ID from the Google API Console
1 2 3 4 |
Authentication process needs your Client ID from the Adwords API project for native apps. Paste the Client ID here :=> XXXXXXXXXXXXXXXXXXXXXXXXXX.apps.googleusercontent.com |
2. The client secretΒ from the Google API Console
1 2 3 |
Authentication process needs your Client secret from the Adwords API project. Paste the Client secret here :=> XXXXXXXXXXXXXXXXXXXXX |
3. The developer token from Google Ads
1 2 3 |
Authentication process needs your Developer Token from the Adwords MCC. Paste the Developer Token here :=> XXXXXXXXXXXXXXXXXXXXXXXX |
4. The client token will be generated after you login in your Google Account and granted access to your Google Ads account.
1 2 3 4 |
Authentication process needs your Client token in order to receive the access token from the API. Copy the Client token from your webbrowser and paste it here. Paste the client token here :=> XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX |
Write your first API Request
If you want to download the data from the Google Ads interface, you have to use getData(). Don’t forget to specify the arguments:
- clientCustomerId – you have to specify the Id of a Google Ads account. You can’t use the Id of an MCC (manager) account!
- google_auth – it should contain the credentials you enter with doAuth()
- statement – an AWQL statement where you specify dimensions, metrics, date range and report. Here you can find the list of all the available fields
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
# First of all you have to specify the data you want to get from the API CampaignData <-statement(select=c("Week", "CampaignName", "Clicks", "Impressions", "AccountDescriptiveName", "Cost", "Conversions"), report = "CAMPAIGN_PERFORMANCE_REPORT", start = "2020-01-01", end = "2020-07-31" ) # Send a request to the API using getData # ATTENTION - The clientCustomerId must be an account ID and it can't be an MCC (manager) ID myData <- getData(clientCustomerId = "XXX-XXX-XXXX", google_auth = googleAuth, statement = CampaignData, transformation = T) |
Get the same data for multiple accounts
As I said before, you can only use an account ID and not an MCC ID. This could be an issue if you want to get the data from multiple accounts at the same time.
But, you can use a vector and map_dfr() to run the same call over a list of account IDs, here you can find an example.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
# Load the packages library("tidyverse") library("RAdwords") # First of all you have to specify the data you want to get from the API CampaignData <-statement(select=c("Week", "CampaignName", "Clicks", "Impressions", "AccountDescriptiveName", "Cost", "Conversions"), report = "CAMPAIGN_PERFORMANCE_REPORT", start = "2020-01-01", end = "2020-07-31" ) # Vector containing the Account IDs accounts <- c("XXX-XXX-XXXX", "XXX-XXX-XXXX", "XXX-XXX-XXXX") # The getAdsData function will get the data from the API getAdsData <- function (i) { myData <- getData(clientCustomerId = i, google_auth = googleAuth, statement = CampaignData, transformation = T) return(myData) } # map_dfr execute the getAdsData function over the accounts vector, the resulting data frames are merged into one myAccountsData <- map_dfr( .x = accounts, .f = getAdsData) |
Hi there!
Thanks for your article, very useful! ππ»
I was just wondering if this package (or an other that you might know) could use Google Adwords API pour get Search volume for Keywords?
I did it with python (right here https://www.doparank.fr/script-python/python-adwords-recuperer-les-volumes-de-recherche-pour-une-liste-de-mots-cles/) but I would also like to do it with R π
Hi!
Unfortunately, this package works only with the reporting API. But maybe in the future they’ll include specific functions for the Keyword Planner API.
Hi, very nice post. Just wondering if you know how to the the list of the account via API.
Hi Marco,
right now you can’t download the account ids through RAdwords. Then only way you have is to use a character vector (e.g. copy-paste each single account id or upload a .csv file)
Hi, can you please post how to use this library for the new version. this version is going to be deprecated on April 27 2022.
Hi Joel,
I’m currently writing a new post about a package that works with the new Google Ads API. I’ll keep you updated π