Dynamically update price in ad

When  you are advertising a product for sale on your site or a range of products you may want to put the price in the ad. But then if the prices on your site change for any reason, maybe a sale you now have a load of ads telling everyone the wrong price and you’ve now got to go through them all updating the price. There is an easier way to do this saving you lots of time. when you are making the ad instead of typing in the price put in {param1:default} instead of default put what the price is. The script will say what you want param1 to be but if anything goes wrong for some reason it will show the default. This will only show numbers not word although it will show currency symbols and decimals. Now we need to write the script that will update param1 to the price. First we want to select the ad groups that have have ads with prices on. This can be done by using the ad groups id or by putting a label on it and using a selector to get the ad groups with a label saying something like ‘price checker’.

 

var adGroups = AdWordsApp.adGroups().withCondition(“Status = ENABLED”).withCondition(“LabelNames CONTAINS_ANY [‘Price’]”).get();

 

Now we have the ad groups we need the ads from them just use another selector, selecting the ads in the ad group we already have. Then we need to get the final URL from the ad to get the price from that URL.

 

var ads = adGroup.ads().get();
while (ads.hasNext())
{
     var ad = ads.next();
       
     //gets URL from ad.
     var finalUrl = ad.urls().getFinalUrl();

 

Now to know what we are looking for in the URL we will need to look at the URL’s source code you can view this by pressing CTRL+U or by right clicking the page as an example of what we are looking for i will be looking at an amazon page for a pair of sunglasses if your not used to looking at a sites source code this may look very intimidating but there is an easy way to find what we are looking for just press CTRL+F to open a search box and search for the number of the price of the item on your site for me it is ‘93.76’ and on the second hit i found the correct block.

 

<span id=“priceblock_ourprice” class=“a-size-medium a-color-price”93.76</span>

 

This is just the way Amazon other site have the price of items stored differently. Some other examples of what to look for are

 

<span>&pound;93.76</span>

<span itemprop=“price”>£93.76</span>

<div class=“productprice”>£93.76</div>

 

Use a try catch block to get the source code from the finalURL so it will give us a message if the URL doesn’t work. We need to use regex to get the span block that the price is in

 

try
{
 var sourceCode = UrlFetchApp.fetch(finalUrl).getContentText();
}
catch(e)
{
 Logger.log(“It is not possible to read out this url.”);
}         

 var regex = /<span id=“priceblock_ourprice” class=“a-size-medium    a-color-price”>(.*?)<\/span>/g;  
 var match = sourceCode.match(regex);

 

This should make match have the entire span block that we saw earlier but we only want the price part so we need to get rid of the rest

 

if(match != null)
{
 var value = match[0];
 var value = value.replace(‘<span id=”priceblock_ourprice” class=”a-size-medium a-color-price”>’,);
 var value = value.replace(‘</span>’,);
}
else
{
 var value = ;
}

 

This will check that match is empty and if it’s not replace the unneeded parts with empty strings effectively removing them. After this value should equal “£93.78”. Finally you just need to set param1 in your ad to value this is done by

 

var keyWords = adGroup.keywords().get();
         
while (keyWords.hasNext())
{
 var keyWord = keyWords.next();
 if(value) { keyWord.setAdParam(1,parameter1); }
}  

By saying if(value) it will check that value is not empty this way if it couldn’t find the price then it wont fill your ad with blank space and the param1 will just use the default value you put in it in the beginning.

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

})(jQuery);