Feed Your Inventory Count into AdWords Ads

Posted on January 29, 2015 in

If you run an eCommerce store it can be cumbersome to keep your ads up to date; especially when a product is out of stock. What if you have hundreds of products and are running ads to individual product landing pages and a handful of them are out of stock? It would be really nice to have something turn ads on and off depending on their status within your store. WooCommerce is an extremely popular online store, and since we use it so often, we created this simple script that checks inventory status for you and will enable/disable ads based on the response from the WooCommerce REST API.

Enable API

This is the simple part. If your WooCommerce API isn’t already enabled, just login as an admin and click “Enable the REST API” under WooCommerce –> Settings.



Simple Test Script

Not knowing everyone’s specific scenario, here’s how I setup our test.

  • Created a label for every SKU we’re testing
  • Applied that label to every ad we want controlled by whether a specific product is in stock or not

So knowing that, here’s the extremely simple example of enabling and disabling ads when a product goes out of stock (or resupplied).

Please note: The only reason I added the key & secret keys as URL query parameters is because there are some issues with HTTPS and basic authorization in this way.

var CAMPAIGN_NAME = "Your Campaign Name";
var userKey = "Your API Key"; 
var userSecret = "Your API Secret";
function main() {  
  var campaign = getCampaign();
  var url = 'https://www.yourstore.com/wc-api/v2/products?consumer_key=' + userKey + '&consumer_secret=' + userSecret;
  var response = UrlFetchApp.fetch(url);
  response = response.getContentText();
  var data = JSON.parse(response);
  for(var i=0; i < data.products.length; i++) {
    var product = data.products[i];
    if (product.in_stock == true) {
      Logger.log('enable ad: ' + product.sku);
      toggleAd(campaign, product.sku, true);
    } else {
      Logger.log('disable ad: ' + product.sku);
      toggleAd(campaign, product.sku, false);

function toggleAd(campaign, sku, status) {
  var adsIterator = campaign.ads().withCondition('LabelNames CONTAINS_ANY ["' + sku + '"]').get();
  if (adsIterator.hasNext()) {
    //Find all ad groups and store them in an array 
    while (adsIterator.hasNext()) {
      var ad = adsIterator.next();
      if (status) {
        if (!ad.isEnabled()) {
     } else if (!status) {
       //disable ad
       if (ad.isEnabled()) {

 * Get a specified campaign based on a global var we've set
function getCampaign() {
 return AdWordsApp.campaigns().withCondition("Name = '" + CAMPAIGN_NAME + "'").get().next();

By Nathan Byloff

Nathan is the CTO for RankHammer. His area of expertise is technical SEO and everything to do with data - collection, analysis, etc. He is driven by automating any reporting task that has to be done more than once.