Set AdParams from a Google Spreadsheet

105 views
Skip to first unread message

Shany Haimy

unread,
Dec 6, 2016, 8:04:56 AM12/6/16
to AdWords Scripts Forum
I would like to take this script and change it to not look for campaign and ad group name - just change the param1 and param2 in all ads in my account (if they have param in the text), how can I modify the script?

/************************************************
* Update Ad Params by Ad Groups
* Version 1.1
* ChangeLog v1.1
*  - Added the ability to enable param1 or 2 individually
*  - Looks for Keywords on all sheets
*  - Runs much faster
* Created By: Russ Savage
* FreeAdWordsScripts.com
************************************************/
var SPREADSHEET_URL = "PUT YOUR SPREADSHEET URL HERE";
var SET_PARAM1 = true;
var SET_PARAM2 = false;
var DATA_RANGE = 'A:D'; // A - CampaignName, B - AdGroupName,
                        // C - Param1, D - Param2
function main() {
 var spreadsheet = SpreadsheetApp.openByUrl(SPREADSHEET_URL);
 var allSheets = spreadsheet.getSheets();
 var allData = [];
 for(var i in allSheets) {
   var sheet = allSheets[i];
   var data = sheet.getRange(DATA_RANGE).getValues();
   data.shift(); //get rid of headers
   allData = allData.concat(data);
 }
 
  var allDataHash = {};
 for(var i in allData) {
   var row = allData[i];
   if(row[0] === '') { continue; }
   var rowKey = [row[0],row[1]].join('~~!~~');
   allDataHash[rowKey] = { param1 : row[2], param2: row[3] };
 }
 
  var kwIter = AdWordsApp.keywords()
   .withCondition('CampaignStatus = ENABLED')
   .withCondition('AdGroupStatus = ENABLED')
   .withCondition('Status = ENABLED')
   .get();
 
  while(kwIter.hasNext()) {
    var kw = kwIter.next();
   var campName = kw.getCampaign().getName();
   var adGroupName = kw.getAdGroup().getName();
   var rowKey = [campName,adGroupName].join('~~!~~');
   if(allDataHash[rowKey]) {
     if(SET_PARAM1) { kw.setAdParam(1, allDataHash[rowKey].param1); }
     if(SET_PARAM2) { kw.setAdParam(2, allDataHash[rowKey].param2); }
   }
 }
}


Tyler Sidell (AdWords Scripts Team)

unread,
Dec 6, 2016, 10:55:33 AM12/6/16
to AdWords Scripts Forum
Hi Shany,

You can use the following script to get started.  Basically we removed all instances of campaign and ad group names:

/************************************************
* Update Ad Params by Ad Groups
* Version 1.1
* ChangeLog v1.1
*  - Added the ability to enable param1 or 2 individually
*  - Looks for Keywords on all sheets
*  - Runs much faster
* Created By: Russ Savage
* FreeAdWordsScripts.com
************************************************/

var SPREADSHEET_URL = "SPREADSHEET URL WITH ONLY TWO PARAM COLUMNS";
var SET_PARAM1 = true;
var SET_PARAM2 = true;
var DATA_RANGE = 'A:B'; // A - Param1, B - Param2

 
function main() {
 
var spreadsheet = SpreadsheetApp.openByUrl(SPREADSHEET_URL);
 
var allSheets = spreadsheet.getSheets();
 
var allData = [];
 
for(var i in allSheets) {
   
var sheet = allSheets[i];
   
var data = sheet.getRange(DATA_RANGE).getValues();
    data
.shift(); //get rid of headers
    allData
= allData.concat(data);
 
}
   
 
var allDataHash = {};
 
for(var i in allData) {
   
var row = allData[i];
   
if(row[0] === '') { continue; }
   
var rowKey = [row[0],row[1]].join('~~!~~');

    allDataHash
[rowKey] = { param1 : row[0], param2: row[1] };

 
}
   
 
var kwIter = AdWordsApp.keywords()
   
.withCondition('CampaignStatus = ENABLED')
   
.withCondition('AdGroupStatus = ENABLED')
   
.withCondition('Status = ENABLED')
   
.get();
   
 
while(kwIter.hasNext()) {
   
var kw = kwIter.next();

     
if(SET_PARAM1) { kw.setAdParam(1, allDataHash[rowKey].param1); }
     
if(SET_PARAM2) { kw.setAdParam(2, allDataHash[rowKey].param2); }
 
}
}

Thanks,
Tyler Sidell
AdWords Scripts Team

Code License: https://developers.google.com/open-source/devplat

Shany Haimy

unread,
Dec 7, 2016, 4:07:07 AM12/7/16
to AdWords Scripts Forum
Thank you, it's working.
Does it have to be in the keywords level, as there's a 50,000 records limitation?
Can it be in the ad group level?

Tyler Sidell (AdWords Scripts Team)

unread,
Dec 7, 2016, 10:17:52 AM12/7/16
to AdWords Scripts Forum
Hi Shany,

Even if we were to run it on the ad group level, you would still have a 50,000 entity limit.  My suggestion would be to run this script on a smaller sample size.  You can achieve this by editing your keyword selector

Example:

var kwIter = AdWordsApp.keywords()
.withCondition('CampaignStatus = ENABLED')
.withCondition('AdGroupStatus = ENABLED')
.withCondition('Status = ENABLED')
.withCondition("AdGroupName CONTAINS_IGNORE_CASE 'Name of Ad Group'")
.get();

Thanks,
Tyler Sidell
AdWords Scripts Team

Reply all
Reply to author
Forward
0 new messages