Stock Check Script

This script will let you know if products you are selling have gone out of stock and let you pause or enable campaigns related to them allowing you to save budget for other products. When using Google Merchant Centre, and one of your products go out of stock, it changes the Merchant Feed and your Shopping Ads will not serve until it is back in stock. Other Ad formats such as Text and Display will continue to be served and will cost you money to send users to a page where they cannot buy the product they were searching for.

 

With the script we are about to go through is used and written in Google Ads, it will check daily that all of your products are in stock. If one goes out of stock or comes back into stock, the script will email you to let you know to pause or enable the products campaigns. To write scripts in Google Ads you go to TOOLs and it is under Bulk Actions

Tools->Bulk Actions->Scripts

Firstly, you will need a Google Sheet to store the products name, page, and whether or not it is currently in stock. There is a template you can use here.

 

https://docs.google.com/spreadsheets/d/17nkX8476SHvL4hdNvkiSd02XCDYKIYiC5UG8DVkLsgU/edit?usp=sharing

 

You will be able to get a list of your products from an export from your websites admin section, or if you only need to get a few that you are running campaigns for you could get them manually from the site.

 

If you choose to use  a sheet of your own be sure to make the headers name, stock, and url named by selecting Named ranges under Data.

Data->Named Ranges...

Using the named range will let the script know where on the sheet to look for the information you put in about the products name, whether or not it’s in stock and its page on your site.

 

The script must first get the Google Sheet by using the function

 

var spreadsheet = SpreadsheetApp.openByUrl(‘SPREADSHEET_SHARE_LINK’);

 

The next step is to import data from it. Initialise 3 sets which will contain the product data, products gone out of stock and those back in stock.

 

var products = [];

var inStock= [];

var outStock= [];

 

Now we need the script to go through the sheet we just made collecting all the information on the products

 

var headers = spreadsheet.getRangeByName(“Headers”);

var headerRow = headers.getRow();

var headerCol = headers.getColumn();

var total = headers.getLastRow() – headerRow;

for(var i=headerRow+1; i<total;i++){

 products.push([spreadsheet.getRange(‘A’+i),

        spreadsheet.getRange(‘B’+i),

             spreadsheet.getRange(‘C’+i)

           ])

 }

 

This will populate the set with the information in the sheet one row at a time. Now that we have the data, we need to go through it and check if the stock has changed. First, use the forEach() function on the products set to make sure this is done for each item in the main set and get the source code for the sites page.

 

products.forEach(function (p){

 var name = p[0].getValue();

 var stock = p[1].getValue();

 var url = p[2].getValue();

 var sourceCode = UrlFetchApp.fetch(url).getContentText();

 

Once we have the source code for the site, we use regex to find the section of the site that contains notification of the product being out of stock. This could be a section of text saying “Out Of Stock” or maybe an image that gets overlayed over it will depend on how your site displays that information.

 

 var regex = [your regex];  

 var match = sourceCode.match(regex);

 

Be sure to copy and paste the entire section that contains the information as long as it doesn’t contain the products name so that we are sure that this is the only instance of it found on the site. Then if match contains a result then we know the product is out of stock.

 

 if(match != null){

  var currentStock = “No”;

 }

 else{

  var currentStock = “Yes”;

 }

 

Now it is just a matter of checking if the value has changed from what we already have and if so then we put it in the set for products that have gone out of stock or come back in stock, and close the forEach() function.

 

 if(stock == “Yes” && currentStock == “No”){

   p[1].setValue(“No”);

   outStock.push(“\n”+name+“\n”);

 }else if(stock == “No” && currentStock == “Yes”){

   p[1].setValue(“Yes”);

   inStock.push(“\n”+name+“\n”);

 }

});

 

The “\n” is to indicate a new line.

 

Finally, if inStock or outStock have a length greater than 0 then we know something has changed in the stock and we should email those changes to ourselves so we know that some campaigns or ads need to be enabled or paused depending on whether or not the product has come back into stock. This is done with the code below

 

if(outStock.length > 0 || inStock.length > 0){

 MailApp.sendEmail({

      to: “[your email]”,

     subject: “Priority Stock Change Alert”,

     body: “The following products have gone out of stock. \n”+

           outStock+“\n”+

           “The following products have come back into stock. \n”+

           inStock,

     name: “Stock alert”,

     noReply: true,

     priority: “high”

    });

}else{Logger.log(“No stock changes”);}

 

All that’s left is to save and set the script to run every day or however often you feel it needs to run, and you will get regular emails on how your stock is changing.

Are you looking for:

PPC Agency
Seo Marketing
Adwords Agency
Google Analytics Agency

Our Qualifications

Our Services

Can we help?

Your Name:*
When is the best time to contact you:*
Phone number:*

* Indicates required fields

How to find us

Stay as up to date as a Blue Thirst Account Engineer

The latest changes from Google Ads, Facebook Ads, SEO, Instagram ads & anywhere else important in a less than 10 min Video every 14 days straight to your inbox!

Subscribe Here:








})(jQuery);