Google Adwords dynamic prices with Ad params

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.

JavaScript

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

varCAMPAIGN_IDS=[169951563,169365563,163129223,154580223];

functionmain()

{

for(i=0;i<CAMPAIGN_IDS.length;i++)

{

varcampaignId=CAMPAIGN_IDS[i];

varadGroups=AdWordsApp.adGroups()

.withCondition("CampaignId = '"+campaignId+"'")

.get();

if(!adGroups.hasNext())

{

Logger.log("Campaign: '"+campaignId+"' doesn't exist.");

}

else

{

while(adGroups.hasNext())

{

varadGroup=adGroups.next();

Logger.log("Adgroup: "+adGroup.getName());

varads=adGroup.ads().get();

while(ads.hasNext())

{

varad=ads.next();

varfinalUrl=ad.urls().getFinalUrl();

Logger.log(" FinalUrl: "+ad.urls().getFinalUrl());

try

{

varsourceCode=UrlFetchApp.fetch(finalUrl).getContentText();

}

catch(e)

{

Logger.log(" It is not possible to read out this url.");

}

varregex1=/<span itemprop="price">(.*?)<\/span>/g;

varmatch1=sourceCode.match(regex1);

varregex2=/<div class="stock" id="stock">(.*?)<\/div>/g;

varmatch2=sourceCode.match(regex2);

if(match1!=null)

{

varvalue1=match1[0];

varvalue1=value1.replace('</span>','');

varvalue1=value1.replace('&euro; ','€');

varvalue1=value1.replace('<span itemprop="price">','');

}

else

{

varvalue1='';

}

if(match2!=null)

{

varvalue2=match2[0];

varvalue2=value2.replace('</div>','');

varvalue2=value2.replace('<div class="stock" id="stock">','');

}

else

{

varvalue2='';

}

varparameter1=value1;

varparameter2=value2;

varparameter1=parameter1.replace(/\s/g,"");

varparameter2=parameter2.replace(/\s/g,"");

Logger.log(" Parameter 1: '"+parameter1+"'");

Logger.log(" Parameter 2: '"+parameter2+"'");

varkeyWords=adGroup.keywords().get();

while(keyWords.hasNext())

{

varkeyWord=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:

JavaScript

1

varCAMPAIGN_IDS=[169951563];

In case of multiple campaign Id’s:

JavaScript

1

varCAMPAIGN_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:

XHTML

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:

JavaScript

1

varregex1=/<span itemprop="price">(.*?)<\/span>/g;

This retrieves anything like this from our source code.

XHTML

1

<span itemprop="price">$30,00</span>

Another example could be:

JavaScript

1

varregex1=/<div class="productprice">(.*?)<\/div>/g;

This retrieves anything that looks like this from our source code.

XHTML

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.

JavaScript

1

2

3

varvalue1=value1.replace('</span>','');

varvalue1=value1.replace('&euro; ','€');

varvalue1=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 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