It is possible in Google Adwords to update your ads with parameters. These parameters can contain numbers, valuta’s and more. Instead of updating your ads over and over again manually every time your stock or product prices are changing, you could automate this with an Adwords Script. This makes updating your ad copy child’s play.
Unfortunately, it isn’t possible to use words in your parameters (valuta signs like $ and € are allowed). Besides of that, you don’t lose historical data anymore and it’s no longer necessary to approve your ads by Google.
Some examples
- Automatically updating your product prices.
- Show up-to-date temperature’s in ads for flight destinations.
- Someone is searching for ice-skates in Groningen, show them the ice thickness in mm.
- Show the actual exchange rates for bitcoins or other valuta’s for currency exchanges.
- Show the number of products sold or the number of available.
How to use
In my example we are going to update our product price in our ad description.
Step 1
Put one or more parameters in your ad description or ad title.
For parameter 1 use:
1 |
{param1:fallbackvalue} |
And if you would like to use a second parameter set:
1 |
{param2:another value} |
Our script is going to scrape the destination url of the ad. If there’s the information found we need, we are going to update the ad parameter. If the information we need doesn’t exist, the ad shows up our fallback value.
Step 2
The maximum execution time of an Google Adwords script is 30 minutes. To avoid our script is running out of time we could define the campaigns we would like to affect. The best way to do this is defining the campaign Id’s of the campaigns instead of using campaign names (that are likely to change at a later moment). We could find the campaign Id’s by adding this column at the campaign report tab. Select Campaign Id at the modify column section.
In this case the Campaign Id is 126900143.
Step 3
Open the script libary.
Add the script in the Google Adwords interface and authorize it.
And allow the script to manage your account. For unexplained reasons sometimes you have to do this twice :S
Add this script. We have to change it a bit to make it fit into your setup.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 |
var CAMPAIGN_IDS = [169951563,169365563,163129223,154580223]; function main() { for (i = 0; i < CAMPAIGN_IDS.length; i++) { var campaignId = CAMPAIGN_IDS[i]; var adGroups = AdWordsApp.adGroups() .withCondition("CampaignId = '" + campaignId + "'") .get(); if (!adGroups.hasNext()) { Logger.log("Campaign: '" + campaignId + "' doesn't exist."); } else { while (adGroups.hasNext()) { var adGroup = adGroups.next(); Logger.log("Adgroup: " +adGroup.getName()); var ads = adGroup.ads().get(); while (ads.hasNext()) { var ad = ads.next(); var finalUrl = ad.urls().getFinalUrl(); Logger.log(" FinalUrl: " +ad.urls().getFinalUrl()); try { var sourceCode = UrlFetchApp.fetch(finalUrl).getContentText(); } catch(e) { Logger.log(" It is not possible to read out this url."); } var regex1 = /<span itemprop="price">(.*?)<\/span>/g; var match1 = sourceCode.match(regex1); var regex2 = /<div class="stock" id="stock">(.*?)<\/div>/g; var match2 = sourceCode.match(regex2); if(match1 != null) { var value1 = match1[0]; var value1 = value1.replace('</span>',''); var value1 = value1.replace('€ ','€'); var value1 = value1.replace('<span itemprop="price">',''); } else { var value1 = ''; } if(match2 != null) { var value2 = match2[0]; var value2 = value2.replace('</div>',''); var value2 = value2.replace('<div class="stock" id="stock">',''); } else { var value2 = ''; } var parameter1 = value1; var parameter2 = value2; var parameter1 = parameter1.replace(/\s/g, ""); var parameter2 = parameter2.replace(/\s/g, ""); Logger.log(" Parameter 1: '" +parameter1 +"'"); Logger.log(" Parameter 2: '" +parameter2 +"'"); var keyWords = adGroup.keywords().get(); while (keyWords.hasNext()) { var keyWord = keyWords.next(); if(parameter1) { keyWord.setAdParam(1,parameter1); } if(parameter2) { keyWord.setAdParam(2,parameter2); } } } } } } } |
Step 4
We have to change line 1. Here we could give up our campaign Id’s.
In case of one campaign Id:
1 |
var CAMPAIGN_IDS = [169951563]; |
In case of multiple campaign Id’s:
1 |
var CAMPAIGN_IDS = [169951563,169365563,163129223,154580223]; |
Step 5
This is the most difficult step. First we are going to explore our source code. Go to the destination url of your ad and open te source code (Right click somewhere on the page and click on Show source code). Look in the source code for the line that is representing your product price. In my case it is:
1 |
<span itemprop="price">$30,00</span> |
Now we have to find the right regular expression to scrape the desired information from our destination url. A regular expression can be used to find our information based on a pattern from the source code of our destination url. I am using this line for parameter 1:
1 |
var regex1 = /<span itemprop="price">(.*?)<\/span>/g; |
This retrieves anything like this from our source code.
1 |
<span itemprop="price">$30,00</span> |
Another example could be:
1 |
var regex1 = /<div class="productprice">(.*?)<\/div>/g; |
This retrieves anything that looks like this from our source code.
1 |
<div class="productprice">$30,00</div> |
Because this part is different for any of those, my advise is to ask a developer to take a look over your shoulder.
Next steps are removing unwanted code that’s retrieved by the regular expression and format our found information. By replacing unwanted html with nothing we finally get our goal.
1 2 3 |
var value1 = value1.replace('</span>',''); var value1 = value1.replace('€ ','€'); var value1 = value1.replace('<span itemprop="price">',''); |
You have to keep trying. Run some previews and read the logs to find out everything is working properly.
Ask me for help in the comments if you get stuck.
Step 6
To make sure our prices are up to date we have to schedule our script. A daily frequency is ok!
Hi, I tried implementing this script, but if my parameter includes a space like “€2,40 / Kilogram” it will trigger an error. I tried the one below, but this doesn’t fix it.
var value2 = value2.replace(‘ ‘,’ ‘);
Do you have any idea how to fix this?
Hi Marthijn, thanks for the article. I am trying to use your script but I encounter 2 errors. The first one is that adwords doesn’t find the price I want to get from teh page source. Here is a sample of the source where the price is indicated:
Prix généralement constaté :1 499,00 €1 239,00 €
Where our_price_display is the one I want to have in the ads.
The second error is ReferenceError: “els” is not defined. (line 55)
I would be very grateful if you can help me with this.
Many thanks in advance,
Best regards,
Kiril
Sorry, the source snippet I’ve sent is not displayed, it turned to Prix généralement constaté :1 499,00 €1 239,00 €…
Here it is again :
Prix généralement constaté :1 499,00 €1 239,00 €
Hey bud!
Thanks for the epic script, however i can not make it work.
I replaced all the undwanted data what i have left is the following
Change to:
{1}: {{{data.formatted_final_total}}} (!) Value is too long
Any advice? thanks
I tried to shorten the variable now i get this:
Invalid insertion text
any ideas why?
Thx