Setting Max CPC at the Keyword level

193 views
Skip to first unread message

SBatLX

unread,
Jul 11, 2019, 1:49:12 PM7/11/19
to Google Ads Scripts Forum
Hello Google Ads Scripts experts,

I am new to Google Ads scripts and I am at a dead end with the script I am trying to form through research.

My goal, with this script, is to automate keyword bid adjustments on a daily basis. I want this to happen only on specific campaign(s).

My idea with this script is:
Every day, once a day, check if keyword max. cpc is > $10

If so, then check if keyword max. cpc is < est. Top of Page Bid
If so, then check if est. Top of Page Bid is < $10
If conditions are met, then set keyword max. cpc to est. Top of Page Bid

If those conditions are not met, then check if keyword max cpc is < est. First of Page Bid
If so, then check if est. First of Page Bid is < $10
If conditions are met, then set keyword max. cpc to est. First of Page Bid

If those conditions are still not met, then set max. cpc to $10


Here is how the script is looking right now but it is still far from the output the script should be accomplishing:


function main() {
      var keywords=AdWordsApp
      .keywords()
      .withCondition("CampaignName CONTAINS_IGNORE_CASE 'Google'")
      .withCondition("CampaignStatus = ENABLED")
      .withCondition("Status = ENABLED")
      .withCondition("MaxCpc < 10.00")
      .forDateRange("TODAY")
      .get();
  
  while(keywords.hasNext())
  {
   
    var keyword=keywords.next();
    var bid=keyword.getMaxCpc();
    var topofPage=keyword.getTopOfPageCpc();
    
        
    if(bid < topofPage)
    if(topofPage < 10.00) {

      Logger.log("The bid for "+keyword.getText()+" will be raised from "+bid+" to "+topofPage);
      keyword.setMaxCpc(topofPage);

    }
  }
}

Google Ads Scripts Forum Advisor Prod

unread,
Jul 11, 2019, 5:11:26 PM7/11/19
to adwords-scripts+apn2wqevqtwyojds...@googlegroups.com, adwords-scripts+apn2wqevqtwyojds...@googlegroups.co, adwords...@googlegroups.com
Hello,

I would suggest verifying that the keyword iterator is not empty. You can verify before the while loop with:

Logger.log(keywords.hasNext());

Which will return 'false' if is empty and 'true' if it is not empty. 

Also, you can combine the two if statements:

  if(bid < topofPage)
    if(topofPage < 10.00

Into:

if( bid < topofPage && topofPage < 10.00 )
 
Regards,
Matt 
Google Ads Scripts Team


ref:_00D1U1174p._5001U8KpVL:ref

SBatLX

unread,
Jul 12, 2019, 12:31:53 PM7/12/19
to Google Ads Scripts Forum
Appreciate the help Matt!

So we are able to combine the two if statements for topofPage bid. 

How are we able to add/combine in a single script a rule that if the topofPage bid is > $10, then look whether firstPage bid is < $10 and if so, then set the bid to firstPage bid?

Essentially, the script will look whether it can use the topofPage first, if not, then use firstPage and if still not possible, then set bid to ceiling bid of $10.

Google Ads Scripts Forum Advisor Prod

unread,
Jul 12, 2019, 5:07:29 PM7/12/19
to adwords-scripts+apn2wqevqtwyojds...@googlegroups.com, adwords-scripts+apn2wqevqtwyojds...@googlegroups.co, adwords...@googlegroups.com
Hello,

Regarding your question:


How are we able to add/combine in a single script a rule that if the topofPage bid is > $10, then look whether firstPage bid is < $10 and if so, then set the bid to firstPage bid?

For this scenario, you can use the logic:

if(condition1) //topofPage
  //code
else if(condition2) //firstPage
  //code
else //set max to 10
  //code 

SBatLX

unread,
Jul 12, 2019, 5:25:59 PM7/12/19
to Google Ads Scripts Forum
Thanks Matt!

I apologize as I have zero programming knowledge so I am simply trying to follow instructions here. 

So when I modified the original script based on your recommendation, I am getting a syntax error on line 25 which is where the first else statement is:

function main() {
      var keywords=AdWordsApp
      .keywords()
      .withCondition("CampaignName CONTAINS_IGNORE_CASE 'Google'")
      .withCondition("CampaignStatus = ENABLED")
      .withCondition("Status = ENABLED")
      .forDateRange("TODAY")
      .get();
  
  Logger.log(keywords.hasNext());
  
  while(keywords.hasNext())
  {
   
    var keyword=keywords.next();
    var bid=keyword.getMaxCpc();
    var topofPage=keyword.getTopOfPageCpc();
    var topofPage=keyword.getfirstPageCpc();
    
        
    if( bid < topofPage && topofPage < 10 ) {
      Logger.log("The bid for "+keyword.getText()+" will be raised from "+bid+" to "+topofPage);
      keyword.setMaxCpc(topofPage)};
      
    else if( bid < firstPage && firstPage < 10 ) {
      Logger.log("The bid for "+keyword.getText()+" will be raised from "+bid+" to "+firstPage);
      keyword.setMaxCpc(firstPage)};
        
    else
      {
        Logger.log(keyword.setMaxCpc(10))
      };

Google Ads Scripts Forum Advisor Prod

unread,
Jul 15, 2019, 2:05:40 PM7/15/19
to adwords-scripts+apn2wqevqtwyojds...@googlegroups.com, adwords-scripts+apn2wqevqtwyojds...@googlegroups.co, adwords...@googlegroups.com
Hello,

You are getting a syntax error because you have one too many curly braces at the bottom of your script. You'll see that if you click next to a curly brace, its pair will be highlighted in the editor. You will see the second to last curly brace matches up with the main curly brace. Delete the last curly brace to clear the error.

SBatLX

unread,
Jul 17, 2019, 9:57:23 AM7/17/19
to Google Ads Scripts Forum
Hey Matt,

I reviewed the script to make sure I don't have unnecessary curly brackets and I am still getting the same syntax error on line 25 (where my else if statement starts). 

I reviewed the variables I am calling out and I cannot determine what is causing the error.

function main() {
      var keywords=AdWordsApp
      .keywords()
      .withCondition("CampaignName CONTAINS_IGNORE_CASE 'Google'")
      .withCondition("CampaignStatus = ENABLED")
      .withCondition("Status = ENABLED")
      .forDateRange("TODAY")
      .get();
  
  Logger.log(keywords.hasNext());
  
  while(keywords.hasNext())
  {   
    var keyword=keywords.next();
    var bid=keyword.getMaxCpc();
    var topOfPage=keyword.getTopOfPageCpc();
    var firstPage=keyword.getFirstPageCpc();
    var maxBid = 10.00;
  }
            
    if(bid > 10.00 && topOfPage <= 10.00) {
      Logger.log("The bid for "+keyword.getText()+" will be set from "+bid+" to "+topOfPage);
      keyword.setMaxCpc(topOfPage)};
      
    else if (bid > 10.00 && firstPage <= 10.00) {
      Logger.log("The bid for "+keyword.getText()+" will be set from "+bid+" to "+firstPage);
      keyword.setMaxCpc(firstPage)};
        
    else
      {
        Logger.log(keyword.setMaxCpc(maxBid));

Google Ads Scripts Forum Advisor Prod

unread,
Jul 17, 2019, 2:06:31 PM7/17/19
to adwords-scripts+apn2wqevqtwyojds...@googlegroups.com, adwords-scripts+apn2wqevqtwyojds...@googlegroups.co, adwords...@googlegroups.com
Hello,

As I mentioned, you must place the semicolons inside of the curly braces for the if, else if and else statements (e.g. ;} rather than }; ). 

Also, I recommend moving your if, else if and else block inside of the while-loop.

I would recommend learning more basic Javascript concepts before proceeding with scripts.

SBatLX

unread,
Jul 17, 2019, 5:38:04 PM7/17/19
to Google Ads Scripts Forum
Thank you for your patience Matt!

I've applied your recommendations and the script is now running perfectly. 
Reply all
Reply to author
Forward
0 new messages