Google Ads Library

Account level functions

One of the most fundamental aspects of using Google Ads Scripts is understanding the different functions that are available for managing and optimizing your account. One important category of functions is related to the overall account, which includes retrieving account information or updating account-level settings. By leveraging these functions, advertisers can gain a deeper understanding of their account performance and make strategic changes to improve their campaign outcomes.

In this guide, we will explore the different functions available for managing the overall account in Google Ads scripts, provide real-life examples, and offer insights on how to use these functions to achieve your advertising goals.

1. getAccount(): retrieves the current account

The getAccount() function is used in Google Ads Scripts to retrieve the current account object. This allows scripts to access and manipulate account-level settings and information, such as the account name, currency code, time zone, and customer ID. By using getAccount(), scripts can retrieve valuable information about the account and use it to inform campaign optimizations or other tasks. For example, a script for a dog grooming business could use getAccount() to retrieve the account name and include it in a report or notification.

function main() {
  // Retrieve the current account object.
  var account = AdWordsApp.currentAccount();

  // Log the account name.
  Logger.log('The current account name is ' + account.getName());
}

In this example, the getAccount() function is used to retrieve the current account object, which is then stored in the account variable. The account name is then retrieved using the getName() function, and logged to the script’s execution log using the Logger.log() function. This would output a message like “The current account name is Doggie Grooming Co.” in the script’s logs.

2. getCurrencyCode(): retrieves the currency code

The getCurrencyCode() function is used in Google Ads Scripts to retrieve the currency code for the account. This information is essential for managing bids, setting budgets, and reporting on campaign performance. By using getCurrencyCode(), scripts can access the account-level currency code and use it to inform bidding strategies or other tasks. For example, a script for a dog food business could use getCurrencyCode() to retrieve the currency code and convert cost data to a common currency for analysis or reporting purposes.

// Retrieve the currency code for the account.
var account = AdWordsApp.currentAccount();
var currencyCode = account.getCurrencyCode();

// Log the currency code.
Logger.log('The bark bark currency is ' + currencyCode);

In this example, the script first retrieves the current account object using currentAccount() and stores it in a variable called account. It then uses getCurrencyCode() to retrieve the currency code for the account and store it in a variable called currencyCode.

Next, the script logs the currency code using Logger.log(). The script uses the phrase “bark bark currency” as an example to indicate that this could be the currency used by a dog food business, and includes the actual currency code retrieved from the account. For example, if the account’s currency code were “USD”, the script would log “The bark bark currency is USD”.

3. getTimeZone(): retrieves the time zone

The getTimeZone() function is used in Google Ads Scripts to retrieve the time zone for the account. This allows scripts to schedule campaigns or other tasks to run at the appropriate times for the account’s target audience. By using getTimeZone(), scripts can ensure that their activities are coordinated with the account’s time zone and that their campaigns are active during the hours when the target audience is most likely to be active. For example, a script for a dog food business could use getTimeZone() to retrieve the account’s time zone and schedule ads to run during peak dog food buying times in that time zone.

// Retrieve the current account object and time zone.
var account = AdWordsApp.currentAccount();
var timeZone = account.getTimeZone();

// Schedule a new ad to run at 7:00 PM in the account's time zone.
var ad = AdsApp.ads().withCondition("Type = TEXT_AD").get().next();
var date = new Date();
date.setHours(19, 0, 0); // 7:00 PM
ad.schedules().add({
  "startHour": date.getHours(),
  "startMinute": date.getMinutes(),
  "dayOfWeek": date.getDay(),
  "userTimeZoneId": timeZone
});

// Pause all other ads.
AdsApp.ads().withCondition("Type = TEXT_AD").withCondition("Id != " + ad.getId()).get().forEach(function(ad) {
  ad.pause();
});

In this example, the script first retrieves the current account object and stores it in a variable called account. It then uses getTimeZone() to retrieve the account’s time zone and store it in a variable called timeZone.

Next, the script schedules a new ad to run at 7:00 PM in the account’s time zone. To do this, it retrieves an existing ad of type “TEXT_AD” and uses the schedules().add() method to add a new schedule. The new schedule specifies the start time as 7:00 PM in the account’s time zone, which is retrieved from the timeZone variable.

Finally, the script pauses all other ads of type “TEXT_AD” in the account to ensure that only the scheduled ad is running at the designated time. This ensures that the scheduled ad will receive maximum visibility and engagement during the target audience’s most active hours. For example, if the account were for a dog food business, the script could use getTimeZone() to retrieve the account’s time zone and schedule an ad to run at 7:00 PM in that time zone, which is typically when dog owners are feeding their dogs.

4. getCustomerId(): retrieves the customer ID

The getCustomerId() function is used in Google Ads Scripts to retrieve the customer ID associated with the current account. The customer ID is a unique identifier that is used to manage multiple accounts under a single Google Ads manager account. By using getCustomerId(), scripts can access this identifier and use it to differentiate between multiple accounts or to link to external tools or platforms that require the customer ID. For example, a script for a dog training business that uses a third-party tool to manage ad campaigns could use getCustomerId() to retrieve the customer ID and link it to the third-party tool for more advanced campaign management.

// Retrieve the current account object and customer ID.
var account = AdWordsApp.currentAccount();
var customerId = account.getCustomerId();

// Log the customer ID.
Logger.log('The customer ID is ' + customerId);

In this example, the script first retrieves the current account object using currentAccount() and stores it in a variable called account. It then uses getCustomerId() to retrieve the customer ID associated with the account and stores it in a variable called customerId.

Finally, the script logs the customer ID using Logger.log(). This could be useful for debugging or for including the customer ID in a notification or report generated by the script. For example, if the account were for a dog walking business, the script would log “The customer ID is 9876543210”.

Combination of above account level functions

Here’s an example report that combines the five functions I described earlier to create a useful account-level script:

function main() {
  // Retrieve the current account object for the script.
  var account = AdWordsApp.currentAccount();
  
  // Log the account name.
  var accountName = account.getName();
  Logger.log('Account Name: ' + accountName);
  
  // Log the account customer ID.
  var customerId = account.getCustomerId();
  Logger.log('Customer ID: ' + customerId);
  
  // Log the account currency code.
  var currencyCode = account.getCurrencyCode();
  Logger.log('Currency Code: ' + currencyCode);
  
  // Log the account time zone.
  var timeZone = account.getTimeZone();
  Logger.log('Time Zone: ' + timeZone);
  
  // Get the number of active campaigns in the account.
  var campaignIterator = AdWordsApp.campaigns().withCondition('Status = ENABLED').get();
  var campaignCount = 0;
  while (campaignIterator.hasNext()) {
    campaignCount++;
    campaignIterator.next();
  }
  
  // Log the number of active campaigns.
  Logger.log('Number of Active Campaigns: ' + campaignCount);
}

This script logs the account name, customer ID, currency code, and time zone using the corresponding functions. Additionally, it counts the number of active campaigns in the account and logs that information as well. If the account were for a dog grooming business, the script could output a report that looks something like this:

Account Name: Doggie Spa
Customer ID: 1234567890
Currency Code: USD
Time Zone: America/Los_Angeles
Number of Active Campaigns: 6

This report provides a quick overview of important account-level information, including the account name, customer ID, currency code, time zone, and the number of active campaigns.