Budget Remaining Notification Script

626 views
Skip to first unread message

Dmytro Konarev

unread,
Mar 21, 2023, 9:52:05 AM3/21/23
to Google Ads Scripts Forum
Dear Google Ads Scripts Forum team,

I hope this message finds you well. I am reaching out to request your assistance with a script that I am using to notify me of the remaining budget in my Google Ads account. While the script has been helpful in keeping track of my budget in the past, it has stopped working completely for the past 3 months and I am no longer receiving any data.

I have recently discovered that the issue may be related to the fact that the script is not compatible with the latest version of Google Ads (v13). I would greatly appreciate it if you could provide me with guidance on how to adjust the script so that it is compatible with v13.

Specifically, I need the script to provide accurate information for all campaigns in my account, as it currently only provides the budget remaining for the first campaign listed in the account. This has resulted in some confusion and mismanagement of my budget in the past, and I am eager to resolve this issue.

Example of the Script:
var CONFIG = {
days : 7,
    email : [' '],
names : [' ']
}

function main() {
  var accountName = AdWordsApp.currentAccount().getName();
  var budgets = AdWordsApp.budgetOrders().withCondition('Status = ACTIVE').get();
 
  try {
    var budget = budgets.next();
    if (budget.getSpendingLimit() !== null ) {
      var startDate = timeFormat(budget.getStartDateTime());
      var cost = AdWordsApp.currentAccount().getStatsFor(startDate,today()).getCost();
      var limit = budget.getSpendingLimit();
      var last7DaysCostByDay =  (AdWordsApp.currentAccount().getStatsFor("LAST_7_DAYS").getCost() / 7).toFixed();
      var remainingDays = rDays(limit, cost, last7DaysCostByDay);
      var adjustments = AdWordsApp.budgetOrders().get().next().getTotalAdjustments();
      var budgetNow = ((limit - cost) - 6761) .toFixed();
      if (budgetNow < 0) {
        var budgetNow = 0;
        }
        else {
          var budgetNow = budgetNow;
        }
      Logger.log([accountName, budgetNow, last7DaysCostByDay, remainingDays, adjustments]);
      if (remainingDays < CONFIG.days) {
        MailApp.sendEmail(CONFIG.email,
                          CONFIG.names +' / Заканчивается бюджет на аккаунте: ' + accountName,
                          'Аккаунт ' + accountName + ' . Текущий остаток = ' + budgetNow +
                          '. Расход в день = ' + last7DaysCostByDay + ' гривень. ' +
                          'Денег хватит на ' + remainingDays + ' дня/дней. В аккаунте заканчиваются средства. Необходимо предупредить PM, клиента. И пополнить баланс.');
        }
      }
    }
  catch (e) {
    Logger.log(e);
    MailApp.sendEmail(CONFIG.email,
                      'Ошибка выполнения скрипта Контроль бюджетов',
                      'Необходимо проверить работу скрипта Контроль бюджетов ' + accountName + ' ' + e);
    }
}

function timeFormat (date) {
 
  var year = date.year.toString();
  var month = date.month.toString();
  var day = date.day.toString();
  if (month.length == 1) {
    month = "0" + month;
  }
  if (day.length == 1) {
    day = "0" + day;
  }
  return [year, month, day].join("");
}

function today () {
  var date = new Date();
  var timeZone = AdWordsApp.currentAccount().getTimeZone();
  var format = 'yyyyMMdd';
  return Utilities.formatDate(date, timeZone, format);
}

function rDays(limit, cost, last7DaysCostByDay) {
  var remainingDays = (((limit - cost) - 6761) / last7DaysCostByDay).toFixed();
 
  if (remainingDays < 1 || remainingDays == "Infinity" || remainingDays == "-Infinity" || remainingDays == -0 ) {
    remainingDays = 0;
  }
 
  return remainingDays;
}


Thank you for your attention to this matter. I look forward to hearing back from you soon.

Best regards,
Dmytro

Google Ads Scripts Forum

unread,
Mar 30, 2023, 7:00:44 AM3/30/23
to Google Ads Scripts Forum
Reposting the last inquiry (https://groups.google.com/g/adwords-scripts/c/_KMP8WQ3Kz0) from the forum as it wasn't routed to our support queue.

Regards,
Google Ads Scripts Team

Google Ads Scripts Forum Advisor

unread,
Mar 30, 2023, 8:10:19 AM3/30/23
to adwords...@googlegroups.com

Hi Dmytro,

 

Thank you for reaching out to the Google Ads Scripts Team. Please excuse us for only getting back now as your message failed to be routed to our support queue.

 

It would be much appreciated if you could provide the following information so we could have a better look at the script you've implemented and guide you accordingly:

  • Google Ads account ID or CID
  • Name of the script
  • Screenshots showing the issue

 

You may send these privately via the Reply to author option. Note that you may need to join the Google Group for you to use this option. If this option is not available on your end still, you may send it through our email (googleadsscr...@google.com) instead.

 

Best regards,

 

Google Logo Google Ads Scripts Team


ref:_00D1U1174p._5004Q2kDLj3:ref
Message has been deleted
Message has been deleted

Google Ads Scripts Forum

unread,
Mar 31, 2023, 6:43:51 AM3/31/23
to Google Ads Scripts Forum
Hi Dmytro,

Thanks for providing the requested information. 

What I would recommend is to store your email address in a variable and then using the .join(, ) method with the sendEmail method for the recipient parameter. This is because the sendEmail method accepts comma separated values and not an array. You may base from the snippets of code below:

var emailAddress = ['YOUR_EMAIL_ADDRESS'];
MailApp.sendEmail(emailAddress.join(','), 'subject', 'body');

Let us know if you have any further clarifications.

Best regards,
Google Ads Scripts Team

On Friday, March 31, 2023 at 6:15:36 PM UTC+8 Google Ads Scripts Forum wrote:
Reposting the last inquiry (https://groups.google.com/g/adwords-scripts/c/_KMP8WQ3Kz0) from the forum as it wasn't routed to our support queue.

Regards,
Google Ads Scripts Team

On Thursday, March 30, 2023 at 9:16:33 PM UTC+8 d.ko...@netpeak.net wrote:
Google Ads Account ID  494-028-0055
Name of the script: Budget Script

чт, 30 мар. 2023 г. в 15:10, Google Ads Scripts Forum on behalf of adsscripts <adwords...@googlegroups.com>:
--
-- You received this message because you are subscribed to the Google Groups AdWords Scripts Forum group. Please do not reply to this email. To post to this group or unsubscribe please visit https://developers.google.com/adwords/scripts/community.
---
You received this message because you are subscribed to a topic in the Google Groups "Google Ads Scripts Forum" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/adwords-scripts/_KMP8WQ3Kz0/unsubscribe.
To unsubscribe from this group and all its topics, send an email to adwords-scrip...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/adwords-scripts/dtoKH000000000000000000000000000000000000000000000RSC1SR00ACDZ-YOsTkyl5GsaikGInA%40sfdc.net.
Reply all
Reply to author
Forward
0 new messages