The Google Ads API can be confusing and hard to understand. This article will guide you through all the steps to make your first API call and store the data into Snowflake.
Step 1: Set Up Google Accounts
Create a Google Manager Account
Firstly, create a Google Ads Manager account using this link if you don’t have one. Note that a Google Ads Manager account is different from a regular Google account.
Request a Developer Token
More information can be found here.
With your account created, go to the Admin and API Center and request a Developer token for test account access level.
Create a Test Manager Account
More information can be found here.
You need to create a test manager account to use this token. Use this link to create the test manager account. With your test manager account, create a new account to be used in the API request and set up a campaign for this account.
Step 2: Set Up a Google API Console Project
Create a Project
Create a project using this URL.
Configure OAuth Consent Screen
Follow the steps on this page to configure the OAuth consent screen. After creating the client ID and client secret, save these credentials for the next steps.
Step 3: Generate OAuth Refresh Tokens
Here are two options to generate the refresh token, depending on the app type you choose:
OAuth Desktop and Web Application Flows
This option can be used for both Desktop and Web App types. Follow this guide.
OAuth Playground
If you choose Web App, you can also use OAuth Playground to generate refresh and access tokens.
Step 4: Configure Using YAML File
Modify the google-ads.yaml Example
Copy the google-ads.yaml example from the GitHub repository and modify it to include your credentials:
Developer token: Generated in the API section of your Google Ads Manager account.
Client ID and client secret: Generated in the GCP credentials section.
Refresh token: Generated in the previous step.
Login customer ID: The number of your Google Ads Manager test account without dashes (e.g., 9662175825).
The google-ads.yaml is located by default in your home directory, but you can change the file location using this code:
from google.ads.googleads.client import GoogleAdsClient
client = GoogleAdsClient.load_from_storage("path/to/google-ads.yaml")
More information can be found here.
Step 5: Running the Code
Download Python script
Access this Github Page to download the handler.py script, this file will handle the API calls and store the data in snowflake.
Install Prerequisite Libraries
Install the necessary libraries to run the script:
pip install snowflake-connector-python google-ads
Create a JSON Configuration File
Create a JSON file called config.json in the same folder to include the Snowflake credentials using this format:
{
"SNOWFLAKE_ACCOUNT": "example.us-east-1",
"SNOWFLAKE_USER": "EXAMPLE",
"SNOWFLAKE_PASSWORD": "EXAMPLE123",
"SNOWFLAKE_WAREHOUSE": "DEMO_WH",
"SNOWFLAKE_DATABASE": "GOOGLE_ADS",
"SNOWFLAKE_SCHEMA": "RAW"
}
Run the script
This script receive two parameters to run the code the first paramenter -l is the google ads manager test account number and the second parameter -c is a list of google ads accounts included in this manager account.
python ./handler.py -l 9662175825 -c 9929372154
Step 6: Check the Data
After this, you can run the script, and the Google Ads Python client will pull the data and store it into Snowflake in:
{SNOWFLAKE_DATABASE}.{SNOWFLAKE_SCHEMA}.campaign_data.
Conclusion
By following these steps, you will be able to successfully make your first API call to the Google Ads API and store the retrieved data in Snowflake using Python. Feel free to change the script based on your requirements or reach out to us if you need professional assistance. BRF Consulting can help your business continue to grow and be more efficient. Reach us at contact@brfconsulting.com
About the author: Felipe Novais is a Data Engineer at BRF Consulting, a Snowflake partner specialized on Data Engineering and Business Intelligence.
Comments