Building Top Google Ads Scripts from Scratch

Building  Top Google Ads Scripts from scratch
Building Top Google Ads Scripts from scratch

Top Google ads scripts can help you ease your Google ads campaign management process to a great extent by enabling you to automate routine campaign management and campaign data reporting activities. As a campaign manager you can then focus on the broader strategy rather than toiling through the mundane repetitive tasks.

If you are just getting started with Google Ads Scripts or AdWords Scripts and not in the habit of coding, I would recommend you to check out my other blog where I have explained in details how you can use top Google Ads scripts without coding. There are ready to use scripts available for various account level as well as campaign level task automation such as budget allocation, bid management etc. On the other hand , if you are planning to get your hands dirty by coding your first Google ads Script. Read on.

How to code Top Google Ads Scripts

The Google Ads Scripts basically help us communicate with the Google Ads API. In this case, since the scripts are placed within the Ad account – we don’t need to worry about sending authentication details at a code level. To become an Google Ads script pro, it’s important to understand how the API works.

These API calls are based on calling objects & methods. If objects & methods seem confusing for you don’t worry – I got you covered! Check out my blog on object oriented programming in JavaScripts.

I would also recommend you to familiarize yourself with Google’s documentation on AdWords API.

Understanding the AdsApp Object

AdsApp is the root object that you need to call for any kind of Google Ads Scripting. Calling AdsApp object is like logging in to your Google Ads account.

When you log into your account – what do you see ? You could see your Campaigns, AdGroups or keywords. Same happens with AdsApp as well! After calling the AdsApp object you could call the Campaign objects or the Ad Group objects or the Keyword objects.

Google Ads Scripts Example : Last 7 day’s performance of all Campaigns

Let’s get started with a simple example. Let’s write a Script that would let us retrieve the campaign performance for the past 7 days. We could measure performance based on the following metrics : Clicks, Conversions, Average CPC & Total cost.

Calling the campaign iterator

Before proceeding further let’s understand a few things –

  • Why use an iterator ? We have multiple campaigns. To get the report that we need we could have to iterate over the list of campaigns & for each campaign capture the details.
  • A Campaign iterator can be imagined to be some sort of a list of Campaigns. Don’t confuse “Campaign” with “Campaign name”. “Campaign name” is a property of a campaign. Just like you are more than just “your name” !

So how do we call the Campaign Iterator? Remember it is housed within AdsApp. So we first call AdsApp & then the campaign iterator. Here’s how the code looks like:

function main() {
  var campaigns = AdsApp.campaigns().get()
}

We just called the campaign iterator here and assigned it to a variable ( campaigns). When we iterate over campaigns we would be able to check the details of each campaign. But before we get there , let’s fine-tune things a bit. Do we really need to see all campaigns? Let’s say we want to see only campaigns which have had at least 1 conversion in the past 7 days. We can filter accordingly:

function main() {
   var campaigns = AdsApp.campaigns().forDateRange("LAST_7_DAYS").withCondition("Conversions >0").orderBy("Conversions DESC").get()
 }

We made use of 3 methods here :

  • forDateRange : To specify the date range
  • withCondition: To specify that we are only interested to see campaigns with atleast 1 conversion
  • orderBy: To sort campaigns by number of conversions

To understand what arguments are valid for these methods I would recommend you to check the API documentation.

Fetching the campaign details

The iterator that we created above stores the entire campaign data. However, to view the details we need to interate through the campaigns and log the details of the individual campaigns.

How do we do that ? We make use of 2 important functions : hasnext() & next().

hasnext() checks whether we are at the end of the list. It returns false when we hit the end. next() fetches the details of an individual campaign. Here’s how it looks it . Note the comments in the code as well:

function main() {
//Defining the Campaign iterator
   var campaigns = AdsApp.campaigns().forDateRange("LAST_7_DAYS").withCondition("Conversions >0").orderBy("Conversions DESC").get()

// Iterating over the campaigns. Checking whether we are at the end of the list. 
// If not fetch the details of the next campaign
// Individual campaign details are stored in the variable "campaign"
while (campaigns.hasNext()){
    var campaign = campaigns.next();
 }
}

Now if you log the variable “campaign” – it won’t make much sense. That’s because it’s an object & not a simple string or number. To get the details that we need – we would now have to call the individual methods:

  • campaign.getName() – for getting the campaign name
  • campaign.getStatsFor(time) -for getting the other performance details

Under getStatsFor we have multiple methods to get the different KPI values:

  • getClicks() – to get the number of clicks
  • getConversions() – to get the numer of conversions
  • getCost() – to get the total cost
  • getAverageCPC() – to get the average CPC

The below code , will retrieve these details & log them as & when retrieved:

function main() {
   var campaigns = AdsApp.campaigns().forDateRange("LAST_7_DAYS").withCondition("Conversions >0").orderBy("Conversions DESC").get()

while (campaigns.hasNext()){
    var campaign = campaigns.next();
    var CampaignName=campaign.getName()
    var clicks = campaign.getStatsFor("LAST_7_DAYS").getClicks()
    var conversions = campaign.getStatsFor("LAST_7_DAYS").getConversions()
    var cpc = campaign.getStatsFor("LAST_7_DAYS").getAverageCPC()
    var cost = campaign.getStatsFor("LAST_7_D").getCost()
    Logger.log("Name: "+CampaignName+" "+"Clicks: "+clicks+" "+"Conversions: "+conversions+" "+"Cost: "+cost+" "+"CPC: "+cpc)
 }
}

That’s it! Your first AdWords Script is ready. Once you get a hang of the above code , you may want to check ways to make it better. Here are some ideas:

  • Instead of Logging one at a time , store the details in a string variable & log the string at the end. Make use of “\t” (for tab separation) & “\n” for new lines.
  • Convert it to a table. Send the table automatically via email.

FAQs: Building Top Google Ads Scripts

What are some of the things that you can automate with Google Ads Scripts ?

Using Google Ads scripts you can automate budget allocation across campaigns, keyword level bid management, campaign reporting etc.

What is AdsApp object ?

AdsApp is the root object of Google Ads scripts API. It exposes methods for querying various Google Ads entities and creating Google Ads reports.

Summary
 Top Google Ads Scripts |   AdWords Scripts Basics | Build from Scratch
Article Name
Top Google Ads Scripts | AdWords Scripts Basics | Build from Scratch
Description
Learn how to build top google ads scripts from scratch with code examples. Read on to understand the logic behind top adwords scripts.
Author
Publisher Name
Digital Marketing Chef
Publisher Logo