Automatically pause Google Ads Campaigns when a product is out of stock

When a DTC brand has a handful of products, monitoring associated Google Ads campaigns is easy…  But as more SKUs are added, it gets, well, not so easy.

One solution? Set up a Google Ads script that automatically monitors product availability and pauses a campaign or ad group when a product is out of stock. 

Amazing, right? But how do you write that script?

🛠 The Set-Up 

In the next few hundred words, you'll learn how to use a Google Ads script to discover if a product is in stock, check an associated campaign or ad group, and turn that campaign or ad group on or off.

Even if your business decided to hire someone to write this script or one like it, understanding how Google Ads scripts work could help you identify other automation possibilities. 

This example script is not something you can copy and paste. Remember the old saying, "give someone a fish and you feed them for a day, teach them to fish and you feed them for a lifetime."

We are not handing you a fish 🐟, we're describing the concept of fishing.

🔎 Google Ad Scripts 

To find the Google Ads script environment, open your Google Ads account, click the "Tools & Settings" menu, and navigate to "Bulk Actions" and then "Scripts."

In a sense, Google Ads scripts are like jQuery or React.js. They use Javascript and a library of methods to make script writing easy. 

📊 Get Inventory 

In this example, we are using an inventory feed from Dream Pops. Most Shopify stores have a product feed available at domain/products.json

The UrlFetchApp method allows Google Ads scripts to integrate with third-party data, like a product feed.

let response = UrlFetchApp.fetch('https://dreampops.com/products.json');

For the example script, we will focus on Dream Pops' Vanilla Matcha four-pack, referencing it via its Shopify "handle."

The code for getting the Vanilla Matcha's availability takes a few steps.

const json = response.getContentText();

We take the response from the UrlFetchApp method and use getContentText —not surprisingly— to get the text or string value of that response. 

Next, we have to convert that text into something we can manipulate. In this case, we use Javascript Object Notation or JSON. 

const data = JSON.parse(json);

The JSON.parse method is built into Javascript and returns an "object" from which we can get information about the Vanilla Matcha product.

const product = data.products.filter(product => product.handle === "vanilla-matcha-4-pack");

The line above sorts or "filters" the JSON object looking for the Vanilla Matcha four-pack specifically. It stores all of the details about the item in a constant called "product."

Logger.log(product[0].variants[0].available);

Next, we log the output to learn if the Vanilla Matcha is in stock. A response of true would mean it was available for sale. If it is false, we know the pop is out of stock.

Now that we know we can check availability for the Vanilla Matcha four pack, we will modify the script slightly, storing the value —true or false— in a variable called "availability."

Don't worry about the product[0].variants… part. It has to do with the JSON structure from Shopify.

availability = product[0].variants[0].available;

🚀 The Campaign 

To make our example script easy to understand, we are going to target a Google Ads campaign named identically to the product handle, "vanilla-matcha-4-pack." 

  let campaign = AdsApp.campaigns()

      .withCondition("CampaignName = 'vanilla-matcha-4-pack'")

      .get()

      .next();

First, we declare a variable to represent the campaign. Then we use the AdsApp.compaigns method to gain access to all of the campaigns in the account.

let campaign = AdsApp.campaigns()

We set a specific condition, identifying the campaign we want by name.

.withCondition("CampaignName = 'vanilla-matcha-4-pack'")

The get and next methods tell Google Ads that we want to work with the matching campaign.

💡 Toggle the Campaign 

Now, let's toggle the campaign based on product availability. 

availability ? campaign.enable() : campaign.pause();

This line of code is called ternary operator. It says "if availability is true, enable the campaign, if not pause it." 

The question mark (?) and colon (:) act as separators distinguishing the thing to be tested —availability— from the two actions to be taken.

Summary 👇

The example script checks to learn if the Vanilla Matcha four pack is in stock, then uses what it learns to enable or pause the associated Google Ads campaign.

As mentioned above, copying and pasting this script into your brand's Google Ads account won't work. But if this mini-tutorial helped you learn about Google Ads scripts and has you thinking about processes your business could automate, it was a success.

window.lintrk('track', { conversion_id: 10616324 });