Change shared budget script - Google Ads Scripts

323 views
Skip to first unread message

Jonathan Svensson

unread,
Nov 23, 2022, 6:16:15 AM11/23/22
to Google Ads Scripts Forum
Hello,

We are running a budget script that changes the budget of each campaign on a daily bases to make sure the monthly budget is used effectively. This script does not include Shared budgets and I am curious if this is possible to add?

I am no scripter or programmer and I don't know how to include this myself.
But I would be really happy if anyone here could help out by either give tips if this is possible or adjust the script to include Shared budgets as well.

Just like we add the Google sheet link and sheet name, I believe we will have to include the name of the Shared budget as well? There is a part in the script that skips the process if it's part of a shared budget. But I would love to change this so it actually changes shared budget too.

Part:       
if (campaign.getBudget().isExplicitlyShared() == 1 ) {
        Logger.log("Campaign " + campaign.getName() + " was found but is part of shared budget, skipping...")

See full script below ---

  var SETTINGS_SHEET_NAME = "Campaigns"; // the name of the tab where the filters and date range are specified
  var SPREADSHEET_URL = "https://docs.google.com/spreadsheets/d/xxxxxxxxx"; // The URL to the Google spreadsheet with your report template


function main() {
 
   var currentSetting = new Object();
   currentSetting.ss = SPREADSHEET_URL;
 
    var settingsSheet = SpreadsheetApp.openByUrl(currentSetting.ss).getSheetByName(SETTINGS_SHEET_NAME);
    var dataRow = settingsSheet.getLastRow();
    var data = settingsSheet.getRange(1, 1,dataRow,2).getValues();

  Logger.log("Starting run!")
  Logger.log("Number of rows in document: "+ dataRow)
  var campaignCount = 0;
 
    var campaignIterator = AdsApp.campaigns().withCondition("CampaignStatus = ENABLED").get();
    while (campaignIterator.hasNext())
   
     
     
    {
    var campaign = campaignIterator.next();
   
     for (i = 1; i < dataRow; i++){
     
    var driveCampaign = data[i][0];
    var driveBudget = data[i][1];
      if (driveCampaign == campaign.getName()){
      if (campaign.getBudget().isExplicitlyShared() == 1 ) {
        Logger.log("Campaign " + campaign.getName() + " was found but is part of shared budget, skipping...")
      }
        else {
          if (isNaN(driveBudget)) {
            Logger.log("Campaign " + campaign.getName() + " was found but is not a number, skipping... ")}
         
          else {
            if (driveBudget == "") {
              Logger.log("Campaign " + campaign.getName() + " was found but is missing a budget, skipping...")}
            else {
            if (campaign.getBudget().getAmount() == driveBudget) {
              Logger.log("Campaign named " + campaign.getName() + " was found but no change in budget, skipping...");
            }
             
            else {
        Logger.log("Campaign named "+campaign.getName() + " was found! Current budget on "+ campaign.getBudget().getAmount() + " " + AdsApp.currentAccount().getCurrencyCode() + " is updated to "+ driveBudget  + " " + AdsApp.currentAccount().getCurrencyCode()) ;
     
              campaign.getBudget().setAmount(driveBudget)
              campaignCount++;
              break;
        }
        }
        }
  }
    if (campaign.getName() != driveCampaign){
       Logger.log(campaign.getName() + "Kampanj finns inte" + driveCampaign);
    }
    }

    }
    }
   
    Logger.log("Run complete! Number of changes made: " + campaignCount)
}

Google Ads Scripts Forum

unread,
Jan 25, 2023, 3:07:05 AM1/25/23
to Google Ads Scripts Forum
Reposting the last inquiry (https://groups.google.com/g/adwords-scripts/c/2r26X2_-uJw) from the forum as it wasn't routed to our support queue.

Regards,
Yasmin
Google Ads Scripts Team

Google Ads Scripts Forum

unread,
Jan 25, 2023, 6:06:04 AM1/25/23
to Google Ads Scripts Forum
Hello Jonathan,

This is Yasmin from the Google Ads scripts team. Please excuse us for only getting back now as your message failed to be routed to our support queue.

Could you confirm if you still need further assistance to this concern? If yes, you would just need to update that line and remove this condition from the if statement such that the script would not check if the budget of the campaign satisfies the condition of being explicitly shared. The if statement would then start from what's included in the else statement below it.

Let me know if you have any questions or encounter any issues.


Regards,
Yasmin
Google Ads Scripts Team

Reply all
Reply to author
Forward
0 new messages