Skip to content

Instantly share code, notes, and snippets.

@jrjames83
Created March 16, 2016 16:00
Show Gist options
  • Save jrjames83/34d560e8177164104aec to your computer and use it in GitHub Desktop.
Save jrjames83/34d560e8177164104aec to your computer and use it in GitHub Desktop.
adwords bid2
function main() {
// make sure single digit dd's don't appear without a leading zero
Number.prototype.padZero= function(len){
var s= String(this), c= '0';
len= len || 2;
while(s.length < len) s= c + s;
return s;
}
// date to usable string
Date.prototype.yyyymmdd = function() {
var yyyy = this.getFullYear().toString();
var mm = (this.getMonth()+1).padZero().toString();
var dd = this.getDate().padZero().toString();
return "".concat(yyyy).concat(mm).concat(dd);
};
// make the dates using this
function makeDate(daysAgo) {
var today = new Date();
var priorDate = new Date().setDate(today.getDate()-daysAgo)
return new Date(priorDate).yyyymmdd();
}
// Set-up Lookback Period - discuss optimal look-back for cookie window
var daysago7 = makeDate(7);
var daysago14 = makeDate(14);
// create the query for the keywords iterator
// useful condition guide https://developers.google.com/adwords/scripts/docs/reference/adwordsapp/adwordsapp_adselector#withCondition_1
var keywordSelector = AdWordsApp
.keywords()
.withCondition("CampaignName DOES_NOT_CONTAIN 'Brand'") // filter on type of campaign
//.withCondition("Clicks > 100") conditions chainable if desired - can handle requirement of converted clicks here
.forDateRange(daysago14,daysago7) // from makeDate func earlier in code
.orderBy("Clicks DESC");
// instantiate the iterator object
var keywordIterator = keywordSelector.get();
// Set-up constraints for any bidding adjustments down the line
var max = 100;
var max_down = .25;
var mid = 50;
var mid_down = .15;
var low = 33;
var low_down = .10;
// go through all keywords and build some metrics
while (keywordIterator.hasNext()) {
var keyword = keywordIterator.next();
var stats = keyword.getStatsFor(daysago14,daysago7)
var conv = stats.getConvertedClicks()
var cost = stats.getCost();
var cpa = (cost / conv).toFixed(2);
// Outstanding cases: (handle no converisons to avoid > 100 = infinity - check logs of existing script)
if(cpa > max) {
// keyword.applyLabel("KW Bid Script: " + max_down)
// keyword.setMaxCpc(keyword.getMaxCpc() * (1+max_down));
Logger.log("More than 100 " + cpa);
} else if (cpa > mid) {
// keyword.applyLabel("KW Bid Script: " + mid_down)
Logger.log("More than 50 " + cpa);
// keyword.setMaxCpc(keyword.getMaxCpc() * (1+mid_down));
} else {
// keyword.applyLabel("KW Bid Script: " + low_down)
Logger.log("Last Condition Hit " + cpa);
// keyword.setMaxCpc(keyword.getMaxCpc() * (1+low_down));
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment