Python in a Marketing World: Reporting with the Google Ads API

Python in a Marketing World Episode 2: Reporting With The Google Ads API

Posted in Analytics & Reporting, MarTech & Data Integration

Posted on May 19th, 2020

We are excited to announce part 2 of our new video series, “Python in a Marketing World,” hosted by Ads Operations Analyst Tanner Riebel. The series provides a jumpstart for marketers interested in learning Python.

Episode 1 was a primer on connecting directly to the Google Analytics API and pulling site data into Google BigQuery for advanced data analysis. In Episode 1: “Reporting With The Google Analytics API” Tanner provided:

Episode 2: “Reporting With The Google Ads API” continues the series and includes:

Watch the video, or follow along with the transcript posted below. Be sure to subscribe to our YouTube channel to catch the next episode, where Tanner will demonstrate how to use Python with the Google AdWords API.

Read Episode 2: “Reporting with the Google Ads API”

Tanner: Hey everyone. My name’s Tanner Riebel. I’m an AdOps analyst at AIMCLEAR, and today I’m going to be walking you through episode two of our Python video series: Reporting with the Google Ads API. As far as the video outline, it’s going to be very similar to what we did in episode one, which was reporting with the Google Analytics API, where the skill level is definitely geared more towards beginners.

The whole goal of the video is going to be providing you with a script that you can easily transfer to your own systems and then walking you through what you have to do to transfer it to your own systems and really how the script is built so you have an idea of how you can build off the script and make it better as you get more used to the Google Ads API.

So, with that, let’s just jump right in.

Before walking through the script, I just wanted to provide the final output of when the script actually executes. So, we’re inside Google BigQuery, and you can see on the left-hand side, the three tables here is what gets uploaded once our script actually runs.

So, we’ll have an ads, campaigns and keywords table. And really the only difference between these three is the metrics and dimensions that they’re pulling. And we’ll get more into that as we go through the script. Starting at the top of the script we have here, lines one through 15 nothing has to be changed. [We are] just calling out the version and modules we’re using throughout the scripts.

Lines 17 and 18 is where we start to authenticate with our own systems. So, if you haven’t done so already, you’ll have to create your own Google Ads YAML file. And a good resource for creating that is here. The final result being getting that Google Ads YAML file that you can use through the script or inside the script.

Line 18 is basically hooking you up to Google BigQuery so you’re able to upload data to BigQuery. So, in episode one of Python in a Marketing World, we actually already walked through this, and if you’ve done that, then you can just exchange this with the file or file path that is pointing to that JSON file.

If you haven’t already done so, then a good resource to create the service account is right here. And one thing to note about this in general too, is if you don’t plan on using BigQuery, then this credential is not needed and anything with the BigQuery outputs can just be deleted as well. So, you only need to create this if you plan on uploading it to BigQuery.

Line 26 is where we get to our first function called googleads_report. And nothing needs to be changed on your side for this function to work. And really all it’s doing is formatting so Google Ads API can read it with whatever you’re specifying. That’s what date range, the dimensions and metrics that you want, and the report type you want. Again, nothing needs to be changed on your side. A good resource if you want to understand more of what this is doing is this reporting basics.

And line 54 brings us to our main function. So, this is really where you can do a lot of the customizing of the different date ranges you want to pull, different metrics, and dimensions you want to pull. So, starting out at the top, we have three different report types that we’re going to specify to grab the data that we want, the campaign, keywords, and ads.

And if you want to pull data different from the general metrics data of those three, then a good resource is this report types where there’s about 50 or so different reports. And you can pull different things like audience performance where you can’t pull that in the three that we’re using now. That’s the main difference between each of these. If you have specific data that you want, then you should find the report that would best pull that data for the dimensions and metrics that you want.

So, we’re grabbing those three reports. And once we hit this for loop, we are going through and specifying for each report what we want pulled.

For the campaigns table and campaigns performance report, we’re specifying under here the campaign columns variable, the different dimensions and metrics that we want. Same goes for the keywords and ads. We’re also specifying here the different dimensions, metrics that we want as well. Once we get through these, then we specify the start date, which is start of 2019 in this example.

And we are ready now to call on that googleads_report function. We’re able to call on that because we have everything that we need. So, we’ll have the Google Ads customer ID that you would wish to pull the data from, the reports that we want, the columns that we want (that’s the different dimensions and metrics), and then the start date and end date. End date would be yesterday for this example. So, once we call that function, then we’re ready to start printing or giving our output.

First output, just once we’re running the script, just posting or printing the data frame head in wherever we’re running the script.

The second one is a CSV output. Main difference between this and the Google Analytics example of the CSV output is since we’re pulling three tables at once, we have to dynamically create this file name that we want for each of the tables.

So, you can see, you also have to change the file path. I have it going to my desktop. And then the file name for campaigns, as an example would be example-export-googleads-campaigns. Same would go for example-export-googleads-keywords. And so on. So that’s what you’d have to change to get into CSV output.

And then the last output is BigQuery’s. Kind of the same logic here for the table suffix, for the table name that you’re putting into BigQuery, it will just depend on whatever the table suffix is for each report, which we have outlined up here.

Also, you need to add your project ID for BigQuery. And where that’s located is if you highlight over the project name, that black box is your project ID.

And the only other thing that you potentially would want to change is the if_exists. So, this is saying if the table exists in BigQuery we’re setting it to this new data, replace that table. You can also change it to append, which just instead of overwriting the existing table, it’ll just append the new data to the bottom of that existing table.

So that outlines everything in this google ads python script. The last part you would have to do is call this main function and add your Google Ads customer ID. So, once you have that all set to go, then the script will be ready to run.

That concludes episode two of our Python video series. As always, we’ll be providing the script so you can easily transfer it to your own system and the links to the different resources that we highlighted throughout the video.

Episode three and on, we’ll be focusing more on how we can use the existing data that we now have from Google Ads and Google Analytics to make optimizations and decisions off of. So, definitely excited to get started with that. And episode three in particular, we’ll be focusing on creating your own performance-based alert system.

Let us know if you have any questions and thanks for watching.

Subscribe Today

We'll keep you updated on the latest Aimclear musings & appearances