Script for low account balance

597 views
Skip to first unread message

Gábor Schaffer

unread,
Jan 23, 2023, 2:14:43 AM1/23/23
to Google Ads Scripts Forum
Hi everyone!

I would like to run an ads script if the balance of the current account is below 30.000 HUF. I have generated the following script through Chat GPT:

function main() {
  // Amount
  var threshold = 30000;
 
  // Get all current balance
  var account = AdsApp.currentAccount();
  var accountBalance = account.getBalance();
 
  // If balance is lower, send an email
  if (accountBalance < threshold) {
    var email = 'mye...@gmail.com';
    var subject = 'Google Ads low balance';
    var body = 'Your current balance is: ' + accountBalance + ' Ft.';
    MailApp.sendEmail(email, subject, body);
  }
}

I have tried to run the old and the new script version as well, but it says

TypeError: account.getBalance is not a function at main (Code:7:32)

What is wrong in this case?

Thank you in advance

Google Ads Scripts Forum Advisor

unread,
Jan 23, 2023, 5:47:49 AM1/23/23
to adwords...@googlegroups.com

Hi Gábor,

 

Thanks for reaching out. Yasmin here from the Google Ads Scripts team.

 

Could you please confirm which entity you're trying to retrieve? Is it the account's budget you are looking for? It would be much appreciated if you could provide a screenshot so we're able to further check on our end and assist you properly.

 

Kindly send the requested information via `Reply privately to author` option. If the said option is not available on your end, you may send it through the email alias `googleadsscr...@google.com` instead.

 

Best regards,

 

Google Logo
Yasmin Gabrielle
Google Ads Scripts Team
 


ref:_00D1U1174p._5004Q2iMVqc:ref

Nils Rooijmans

unread,
Oct 8, 2024, 2:56:47 AM10/8/24
to Google Ads Scripts Forum
If you are referring to the spend limits of account budgets you have created, you could try something like this: 


function main() {
  // Constants
  const THRESHOLD_AMOUNT = 1000;  // Threshold in standard currency units (e.g., dollars)
  const ALERT_EMAIL = "al...@example.com";  // Email address to receive the alert
  const SUBJECT = "Account Spending Limit Alert";
 
  // GAQL query to fetch the account budget spending limit
  const QUERY = `
    SELECT account_budget.adjusted_spending_limit_micros
    FROM account_budget
  `;

  // Execute the GAQL query using AdsApp.report
  const report = AdsApp.report(QUERY);
  const rows = report.rows();

  if (rows.hasNext()) {
    const row = rows.next();
    const spendingLimitMicros = parseFloat(row['account_budget.adjusted_spending_limit_micros']);
    const spendingLimit = spendingLimitMicros / 1_000_000;  // Convert micros to standard currency

    // Check if the spending limit is below the threshold
    if (spendingLimit < THRESHOLD_AMOUNT) {
      const message = `
        Alert: Your account spending limit is currently set at $${spendingLimit.toFixed(2)},
        which is below the threshold of $${THRESHOLD_AMOUNT}.
        Please review your budget settings to ensure continued ad performance.
      `;

      // Send email notification
      MailApp.sendEmail(ALERT_EMAIL, SUBJECT, message);
      Logger.log("Alert email sent to " + ALERT_EMAIL);
    } else {
      Logger.log("Spending limit is above the threshold. No alert sent.");
    }
  } else {
    Logger.log("No account budget data found. Check your account's budget setup.");
  }
}



Hope this helps you getting started,

Nils Rooijmans
https://nilsrooijmans.com
See my Google Ads Scripts FAQ to avoid the same mistakes I made: https://nilsrooijmans.com/google-ads-scripts-faq/

Google Ads Scripts Forum Advisor

unread,
Oct 8, 2024, 6:59:16 AM10/8/24
to adwords...@googlegroups.com

Hi,

Thank you for reaching out to the Google Ads Scripts support team.

@Nils - Thank you for providing the solution for the user’s issue.

I would like to inform you that the method “getBalance()” you are using is not a valid one to retrieve the cost. I would suggest that you replace the code with “var accountBalance = account.getStatsFor("ALL_TIME").getCost();”. This will retrieve the cost of the account till now. If you want to get the cost for a specific date range, I would suggest you refer to this document.

I hope this helps! Kindly get back to us if you still face any issues. 

This message is in relation to case "ref:!00D1U01174p.!5004Q02vGchy:ref" (ADR-00270917)

Thanks,
 
Google Logo Google Ads Scripts Team


Maciej Ilczyszyn

unread,
Oct 16, 2024, 10:59:33 AM10/16/24
to Google Ads Scripts Forum
Hi,

I guess the question is about accounts with prepaid payment.
Such a script would be useful, because Google doesn't inform in advance if the balance is low. Only informs when it's gone.  

Google Ads Scripts Forum Advisor

unread,
Oct 16, 2024, 3:25:40 PM10/16/24
to adwords...@googlegroups.com
Hi,

There is no Google Ads script called account with prepaid payment. I would suggest you check this AdsApp.​Account guide to know more about the supported methods. I would like to inform you that the retrieving current balance of a specific account is not available in the Google Ads Scripts. I would recommend you to please follow the blog post for further updates.

As a workaround, we could recommend for you to get the said data via Reports. You may be able to use the fields account_budget.approved_spending_limit_micros and account_budget.adjusted_spending_limit_micros. In addition, it appears that using these as well as the methods BudgetOrder.getSpendingLimit and BudgetOrder.getTotalAdjustments, although it may be sufficient, would return a slightly different value than the Google Ads account (UI) "current balance" as they are calculated in different ways.
Reply all
Reply to author
Forward
0 new messages