Google Ads Script to Pause Ads When your Prices aren’t Competitive

E-commerce websites are now vulnerable to price wars from competitors who want to gain more market share. When a competitor makes an aggressive price move,  one of the first steps you should take is to stop the marketing activity of the affected products/services, until you think about how your participation should look like. So, you avoid any wasted spend on price-driven users, who are the majority these days.

To help you in handling price wars, we’ve written a Google Ads script to pause or enable campaigns based on the price difference between you and your competitors. The script is linked to a spreadsheet that contains the competitor prices and your prices. Both price data are fetched and updated automatically from the website. You don’t have to worry about updating them manually.

The script then pauses the campaign if the price difference between your product and competitors’ is greater than a specific threshold you determine. And vice versa, it enables paused campaigns if your prices return to be competitive.

Finally, the script sends an email with all the actions that have been taken, which campaigns have been paused or enabled.

How to use the script?

1. Add your campaigns name to a Google sheet

The first step is you add a column in your spreadsheet and name it “Campaign Name” and then include your AdWords campaign names of the relevant products.
Let’s say, you have two campaigns, one is promoting the iPhone 7 and another is advertising Galaxy S8. You want to compare your prices to the market prices, and if your prices are 15% higher than the lowest price, you want to pause these product campaigns.

2. Fetch your Competitors Prices Automatically

Google sheets have a function that allows you to import any content from any website and extract specific information to your sheet. The formula is IMPORTXML(); You can read more about it here.
In our case, we want to import the price of specific products from our competitor’s website (or price comparison websites) to Google sheets.
Visit your competitor or the price comparison website and inspect (right click, then select Inspect) the price elements on the product page.


This brings up the developer inspection window where we can inspect the HTML element for the price. Then copy the class or id name of the price.


Navigate to your spreadsheet and add a new column next to your campaign name and call it “Competitor price”.  We are going to use IMPORTXML function with a second argument to access the HTML element on any pages. The syntax of the formula is as below:

=IMPORTXML("The Page URL","The Xpath Query")

The XPath query is to look for an HTML element on the page.  In our example, we copied the class name which is “lowPrice”.

=IMPORTXML("http://example.com/en/product/apple-iphone-7-plus-price","//span[@class='lowPrice']")

The output of the formula should return the price on the page. Do the exact step with other products.

3. Fetch your Prices Automatically

Add a new column to the sheet next to the "Competitor Price" Column and name it "My Price".  Use the exact formula above IMPORTXML to retrieve the prices from your product pages. Make sure to add the relevant page URL and XPath query.

=IMPORTXML("http://mywebiste.com/en/product/apple-iphone-7-plus-price","//span[@class='MyPrice']")

4. Add and Schedule your AdWords Script

Navigate to your AdWords account and copy the script source below. Make sure to add your spreadsheet URL that contains the prices.Also,  change the price threshold based on your needs. Price threshold is the maximum difference between your product prices and competitor prices. In the example below, it’s set to 0.15, this means when my prices are 15% greater than my competitors’ prices, the campaigns will be paused.

Finally, add your email address, so you can receive a summary of all modifications happened.

/***************************************************
* Price War Script
* Version 1.0
* Created By: Ahmed Ali
*https://optimizationup.com/
****************************************************/
function main() {

 var SPREADSHEET_URL = "Add Your Spreadsheet URL Here";
 var priceThreshold = 0.15; //Change it based on your needs
 var Notify_Me ="Add Your Email here"

 var spreadsheet = SpreadsheetApp.openByUrl(SPREADSHEET_URL);
 var sheet = spreadsheet.getActiveSheet();
 var data = sheet.getRange("A:C").getValues();
 var campaignPaused = [];
 var campaignEnabled= [];


 for (i in data) {

 // SKIP HEADER ROW
 if (i == 0) {
 continue;
 }
 
 var [CampaignName, CompetitorPrice, MyPrice] = data[i];

 // BREAK IF CAMPAIGN IS EMPTY
 if (CampaignName == "") {
 break;
 } 

 else {
 
 var campaignIterator = AdWordsApp.campaigns().withCondition('Name = ' + CampaignName).get();
 
 var difference = (MyPrice - CompetitorPrice) / MyPrice;

 while (campaignIterator.hasNext()) {
 var campaign = campaignIterator.next();
 
 if (difference >= priceThreshold) {

 if (campaign.isEnabled() == true) {
 campaignPaused.push(campaign.getName());
 }
 campaign.pause();
 }

 else if (difference < priceThreshold) {
 if (campaign.isPaused() == true) {
 campaignEnabled.push(campaign.getName());
 }
 campaign.enable();
}
}
}
} 

var pricePercent = priceThreshold * 100;

 if ((campaignPaused.length > 0) && (campaignEnabled.length > 0)) {
MailApp.sendEmail(Notify_Me,
 'Price War Script',
 'The below campaigns have been paused, since your prices are '+pricePercent+'% greater than your competitors: \n\n'+campaignPaused+ ' \n\nAlso, the below campaigns have been enabled, since your prices are '+pricePercent+'% less than your competitors: \n\n'+campaignEnabled+'\n\nPlease visit this spreadsheet for more info: '
+SPREADSHEET_URL+'Thanks'); 
 }

 else if ((campaignPaused.length > 0) && (campaignEnabled.length == 0)) {
MailApp.sendEmail(Notify_Me,
 'Price War Script',
 'The below campaigns have been paused, since your prices are '+pricePercent+'% greater than your competitors: \n\n'+campaignPaused+ '\n\n\Please visit this spreadsheet for more info: '
+SPREADSHEET_URL+'Thanks'); 
 }

 else if ((campaignPaused.length == 0) && (campaignEnabled.length > 0)) {
MailApp.sendEmail(Notify_Me,
 'Price War Script',
 'The below campaigns have been enabled, since your prices are '+pricePercent+'% greater than your competitors: \n\n'+campaignEnabled+ '\n\n\Please visit this spreadsheet for more info: '
+SPREADSHEET_URL+'Thanks'); 
 }
 }

12 Comments

  1. Hi Ahmed,

    Thanks a lot for writing out this killer script.

    Wanted to check how you would go about tracking for discounts as well? For example, if my competitor uses a different class/id for discount prices, would I have to go in and check manually and update the XML path?

    Hope to hear from you soon.

    Cheers,

    Jerry

    1. Hi Jerry,

      Thanks for your comment.

      You can pull the discount class as well and multiply it to the price class. The result should be the price after discount.

      Let me know how it goes. Also, feel free to share the website URL through the “Contact Me” page 🙂

      Best,
      Ahmed

  2. Hi Ahmed,

    Thank you for your script 🙂 How can I do if the website use the same class for several price. For the full price, or lowprice is the same class.
    So google spreadsheet returns me the first class it reads.

    Many thanks

  3. Hi Ahmed,

    A big thank for this amazing post ! Really useful and smart !

    I just have a question, how can we do if the page has several same span class. The excel file display the first one, but if I want to display the second, how can I do ?

    Many thanks

  4. Hi there,

    Could you kindly shed some light on what I’m doing wrong for the first bit?

    My sheet says N/A

    =IMPORTXML(“https://www.appliancesdelivered.ie/montpellier-5kg-1000rpm-white-freestanding-washing-machine-mw5101p/3856″,”//span[@class=’Price’]”)

    Any feedback would be hugely appreciated.

    Thank you,

    Stephen.