One of the more powerful features within Adwords is the use of script. These little JavaScript programs can automate accounts very powerfully. However, they seems to be one of the less used and discussed elements in adwords.
Scripts lie in the middle between the simplicity of automated rules, and the complexity and power of full API integration. In many cases scripts can do in a single line of code what would take dozens using the API. They also can take in external data sources and set dynamic parameters in ads.
To illustrate this power, I’ve created a script that will display an updated list of twitter followers in an ad with two dynamic parameters.
First the ad:
And the Script:
var AD_GROUP_NAME = 'ScriptDemo'; function main() { var adGroup = getAdGroup(AD_GROUP_NAME); // If we couldn't find the Ad Group, log an error and halt. if (adGroup) { var twitterData = getTwitterData(); updateAdParameterValues(adGroup, twitterData); } else { Logger.log('Could not locate AdGroup with name ' + AD_GROUP_NAME); } } function getAdGroup(name) { var adGroupIterator = AdWordsApp.adGroups() .withCondition('Name = "' + name + '"') .withLimit(1) .get(); if (adGroupIterator.hasNext()) { return adGroupIterator.next(); } } function updateAdParameterValues(adGroup, twitterData) { var keywordIter = adGroup.keywords().get(); while (keywordIter.hasNext()) { var keyword = keywordIter.next(); keyword.setAdParam(1, twitterData.followers_current); keyword.setAdParam(2, twitterData.followers_current+1); } } function getTwitterData() { var url = 'http://api.twittercounter.com/?apikey=INSERT API HERE&twitter_id=IDHERE'; var json = Utilities.jsonParse(UrlFetchApp.fetch(url).getContentText()); return json; }
First an admission. I’m not a programmer. What I’ve actually done is modify a script that google itself posted on ad parameters for my own purpose. I’m certain there are more efficient and sophisticated ways of accomplishing the goal, so please grade this on a curve. I’m pretty sure I couldn’t have done it without w3schools.
What I’m about to do is attempt to explain some of the basics of the script.
On the adwords side, the setup is pretty clear. Much like keyword insertion, a dynamic element is inserted in curly brackets, with a default value after the colon.
On the script side, there are a few basic things to understand.
The first is the main function. It’s the loop of the process and what will perform the actual update. Everything else is a function called from within it.
The second part is an iterator that runs through the keywords of the adgroup. It’s pretty much unmodifed from the script that I started from.
The actual meat of what happens comes in the last two parts. The function of setAdParam is what changes the parameters. The number at the front specifies which parameter to change, and the second half tells the value. In this case I’m pulling from “followers_current” item in the output of the twitter data. These outputs can have quite a number of potential features to pull and it’s just added to the item after the called variable.
The final part is where the external data source is called. In this case I’m using the API from twitter counter and parsing it into a variable object I can use to set the parameters. I think this is likely an inefficently way to do this as I believe I call the API with every keyword, rather than just once.
Extending this approach
Just by replacing the url to call, it’s now possible to call any API that responds in JSON. Change that variable and the script can now pull the data you desire.
The part of the response needed as a parameter goes at the end of the variable used to set it. In this example it was “followers_current” These items are documented as response parameters. For the twitter counter example, I could have used “average_growth” if I wanted to talk about rate of growth rather than raw number.
From here you can take it as far as your imagination and strategy can take you. If you have other ideas for usage, leave them in the comments.