Automatically stop the campaign when the budget date is exceeded

103 views
Skip to first unread message

Patrick Nguyễn

unread,
Sep 19, 2022, 9:24:44 PM9/19/22
to Google Ads Scripts Forum
Hi,

I want Automatically stop the campaign when the daily budget is exceeded. I use the following code:

function main() {
 
var allowedOverdeliveryPercentage = 0.1; // set percentage as decimal, i.e. 20% should be set as 0.2
var labelName = 'pause-campain';
 
AdWordsApp.createLabel(labelName, 'automatic label needed to reenable campaigns');
 
var campaigns = AdWordsApp.campaigns()
.withCondition('Status = ENABLED')
.withCondition('Cost > 0')
.forDateRange('TODAY');
 
var campaignIterator = campaigns.get();
 
while (campaignIterator.hasNext()) {
var campaign = campaignIterator.next();
var campaignName = campaign.getName();
var budgetAmount = campaign.getBudget().getAmount();
var costToday = campaign.getStatsFor('TODAY').getCost();
 
if(costToday > budgetAmount * (1 + allowedOverdeliveryPercentage)) {
Logger.log(campaignName + ' has spent ' + costToday + ' which is more than allowed.');
campaign.applyLabel(labelName);
campaign.pause();
} else {
Logger.log(campaignName + ' has spent ' + costToday + ' and can continue to run.');
}
}
 
}

=================

In the first time it seems to run fine, the time after that it shows the error occurred: "You are already using this label name"

This error causes the ad campaign that exceeds the daily budget to be unpause and it still works

If I change the label name, in the first run it works fine. From the next time it will show the above error

What can I do to fix this?

Thank you

Sigurd Fabrin

unread,
Sep 20, 2022, 5:05:34 AM9/20/22
to Google Ads Scripts Forum
Hi, it seems you try to create a label with the exact same name every time the script runs. You can only create one label with any given name.

The code below create a new label IF it doesn't already exists
 let labelName = 'Name of label';
  if (!AdsApp.labels().withCondition('LabelName = "' + labelName + '"').get().hasNext()) {
    AdsApp.createLabel(labelName);
  }



Sigurd

Patrick Nguyễn

unread,
Sep 20, 2022, 5:16:11 AM9/20/22
to Google Ads Scripts Forum
Hi,

Please. What exactly can I do with your code above. I'm not good at coding.

As my information earlier. Every time the file is run it shows the label already exists.
Where can I replace your code in my code. Please help me

Thank you


Vào lúc 16:05:34 UTC+7 ngày Thứ Ba, 20 tháng 9, 2022, Sigurd Fabrin đã viết:

Sigurd Fabrin

unread,
Sep 20, 2022, 5:26:46 AM9/20/22
to Google Ads Scripts Forum
"Where can I replace your code in my code"

This part
var labelName = 'pause-campain';
AdWordsApp.createLabel(labelName, 'automatic label needed to reenable campaigns');


Sigurd

Patrick Nguyễn

unread,
Sep 20, 2022, 5:46:49 AM9/20/22
to Google Ads Scripts Forum
Hi,

Sorry, I'm trying. Seems to be going wrong somewhere


function main() {

var allowedOverdeliveryPercentage = 0.1;
 
let labelName = 'Name of label';
  if (!AdsApp.labels().withCondition('LabelName = "' + labelName + '"').get().hasNext()) {
    AdsApp.createLabel(labelName);
  };


var campaigns = AdWordsApp.campaigns()
.withCondition("Status = ENABLED")
.withCondition("Cost > 0")
.forDateRange("TODAY");

var campaignIterator = campaigns.get();

while (campaignIterator.hasNext()) {
var campaign = campaignIterator.next();
var campaignName = campaign.getName();
var budgetAmount = campaign.getBudget().getAmount();
var costToday = campaign.getStatsFor("TODAY").getCost();

if(costToday > budgetAmount * (1 + allowedOverdeliveryPercentage)) {
Logger.log(campaignName + " has spent " + costToday + " which is more than allowed.");
campaign.applyLabel(labelName);
campaign.pause();
} else {
Logger.log(campaignName + " has spent " + costToday + " and can continue to run.");
}
}

}

===========

Can you help me?

Thank you
hình ảnh_2022-09-20_164437204.png



Vào lúc 16:26:46 UTC+7 ngày Thứ Ba, 20 tháng 9, 2022, Sigurd Fabrin đã viết:
Message has been deleted

Sigurd Fabrin

unread,
Sep 20, 2022, 6:26:14 AM9/20/22
to Google Ads Scripts Forum
It works fine when I try.

Here's a prettified version so it's easier to see what's doing what:
function main() {
  let allowedOverdeliveryPercentage = 0.1;
  let labelName = 'Name of label';
  if (!AdsApp.labels().withCondition('LabelName = "' + labelName + '"').get().hasNext()) {
    AdsApp.createLabel(labelName);
  }
  let campaigns = AdsApp.campaigns()

    .withCondition("Status = ENABLED")
    .withCondition("Cost > 0")
    .forDateRange("TODAY");
  let campaignIterator = campaigns.get();
  while (campaignIterator.hasNext()) {
    let campaign = campaignIterator.next();
    let campaignName = campaign.getName();
    let budgetAmount = campaign.getBudget().getAmount();
    let costToday = campaign.getStatsFor("TODAY").getCost();

    if(costToday > budgetAmount * (1 + allowedOverdeliveryPercentage)) {
      console.log(campaignName + " has spent " + costToday + " which is more than allowed.");
      campaign.applyLabel(labelName);
      campaign.pause();
    }
    else {
      console.log(campaignName + " has spent " + costToday + " and can continue to run.");
    }
  }
}


Sigurd

Patrick Nguyễn

unread,
Sep 20, 2022, 10:12:58 AM9/20/22
to Google Ads Scripts Forum
Hi,

I am using your script. It is working.

I used the label name before, no errors occurred.

I will follow up in the next 24 hours. Wherether Error or not, I will feedback.

I'm really grateful to you and wish good luck to you. So sorry my english is not good.

Thank you so much.





Vào lúc 17:26:14 UTC+7 ngày Thứ Ba, 20 tháng 9, 2022, Sigurd Fabrin đã viết:

Google Ads Scripts Forum Advisor

unread,
Sep 21, 2022, 2:38:16 AM9/21/22
to adwords...@googlegroups.com

Hello Patrick,

I’m James from the Google Ads scripts support team. We’ve been notified about your concern.

Can you please provide us with the following items below so that we can further investigate and troubleshoot the issue?

  • Google Ads account ID / CID
  • Script name

Regards,

Google Logo
James Howell
Google Ads Scripts Team
 


ref:_00D1U1174p._5004Q2eQM1T:ref

Patrick Nguyễn

unread,
Sep 21, 2022, 4:24:33 AM9/21/22
to Google Ads Scripts Forum
Hi  James,

I was supported by Sigurd Fabrin. However, I still urge you to check on an account with CID 402-407-3349 - Script name: pause camp

Currently this script is still working fine. It automatically pauses the campaign when it exceeds the budget and does not generate an error.

It's good to have you and Sigurd Fabrin to help

Regards



Vào lúc 13:38:16 UTC+7 ngày Thứ Tư, 21 tháng 9, 2022, adsscripts đã viết:

Patrick Nguyễn

unread,
Sep 21, 2022, 4:26:07 AM9/21/22
to Google Ads Scripts Forum
Hi Sigurd Fabrin,

I would like to respond:

For the last 24h the script worked fine and not produced an error.

Once again, I thank you so much.

Regards

Vào lúc 17:26:14 UTC+7 ngày Thứ Ba, 20 tháng 9, 2022, Sigurd Fabrin đã viết:

Google Ads Scripts Forum Advisor

unread,
Sep 22, 2022, 1:16:10 AM9/22/22
to adwords...@googlegroups.com

Hello Patrick,

I'm glad that your script is now working fine and not produced any errors.

Based on my further checking, I observed that your script implementation is fine. However, I noticed that you’re executing your script in the new script experience version but you haven’t migrated the codes within your script to the new script experience setup. I would like to inform you that the new script experience is now based on Google Ads API infrastructure, therefore enabling or toggling on the new script experience button within your script editor will not automatically convert or migrate your existing script. However, as we have mentioned in our blog post, you should manually move your scripts over to the new experience before then to ensure continued functionality.

As I mentioned above, you need to make some adjustments to your code snippet for it to work continuously in the new script experience version. To migrate your script, you may follow our migration guide, specifically under the `Organizing selectors` section.

You may navigate to the following links below for additional references:

Reply all
Reply to author
Forward
0 new messages