Flexible Budget Script is not Running

148 views
Skip to first unread message

Jordan Imdieke

unread,
Oct 17, 2022, 10:13:14 AM10/17/22
to Google Ads Scripts Forum
Hello everyone, 

I have recently picked up scripts for work and have yet to run a successful flexible budget. I entered the information I believe is required to run the script (I highlighted it) and previewed the script before running it. It has yet to run over multiple days. Any help would be greatly appreciated!!

Config.gs
CONFIG = {
  'total_budget': 1221,
  'campaign_name': 'Advertising Budget',
  'start_date': 'October 12, 2022 0:00:00 -0500',
  'end_date': 'October 31, 2022 0:00:00 -0500'

Code.gs
const TOTAL_BUDGET = CONFIG.total_budget;
const CAMPAIGN_NAME = CONFIG.campaign_name;
const START_DATE = new Date(CONFIG.start_date);
const END_DATE = new Date(CONFIG.end_date);

function main() {
  testBudgetStrategy(calculateBudgetEvenly, 17, 1221);
//  setNewBudget(calculateBudgetEvenly, CAMPAIGN_NAME, TOTAL_BUDGET,
//      START_DATE, END_DATE);
}

function setNewBudget(budgetFunction, campaignName, totalBudget, start, end) {
  const today = new Date();
  if (today < start) {
    console.log('Not ready to set budget yet');
    return;
  }
  const campaign = getCampaign(campaignName);
  const costSoFar = campaign.getStatsFor(
        getDateStringInTimeZone('yyyyMMdd', start),
        getDateStringInTimeZone('yyyyMMdd', end)).getCost();
  const daysSoFar = datediff(start, today);
  const totalDays = datediff(start, end);
  const newBudget = budgetFunction(costSoFar, totalBudget, daysSoFar,
                                   totalDays);
  campaign.getBudget().setAmount(newBudget);
}

function calculateBudgetEvenly(costSoFar, totalBudget, daysSoFar, totalDays) {
  const daysRemaining = totalDays - daysSoFar;
  const budgetRemaining = totalBudget - costSoFar;
  if (daysRemaining <= 0) {
    return budgetRemaining;
  } else {
    return budgetRemaining / daysRemaining;
  }
}

function calculateBudgetWeighted(costSoFar, totalBudget, daysSoFar,
    totalDays) {
  const daysRemaining = totalDays - daysSoFar;
  const budgetRemaining = totalBudget - costSoFar;
  if (daysRemaining <= 0) {
    return budgetRemaining;
  } else {
    return budgetRemaining / (2 * daysRemaining - 1);
  }
}

function testBudgetStrategy(budgetFunc, totalDays, totalBudget) {
  let daysSoFar = 0;
  let costSoFar = 0;
  while (daysSoFar <= totalDays + 2) {
    const newBudget = budgetFunc(costSoFar, totalBudget, daysSoFar, totalDays);
    console.log(`Day ${daysSoFar + 1} of ${totalDays}, new budget ` +
                `${newBudget}, cost so far ${costSoFar}`);
    costSoFar += newBudget;
    daysSoFar += 1;
  }
}

/**
 * Returns number of days between two dates, rounded up to nearest whole day.
 */
function datediff(from, to) {
  const millisPerDay = 1000 * 60 * 60 * 24;
  return Math.ceil((to - from) / millisPerDay);
}

function getDateStringInTimeZone(format, date, timeZone) {
  date = date || new Date();
  timeZone = timeZone || AdsApp.currentAccount().getTimeZone();
  return Utilities.formatDate(date, timeZone, format);
}

/**
 * Finds a campaign by name, whether it is a regular, video, or shopping
 * campaign, by trying all in sequence until it finds one.
 *
 * @param {string} campaignName The campaign name to find.
 * @return {Object} The campaign found, or null if none was found.
 */
function getCampaign(campaignName) {
  const selectors = [AdsApp.campaigns(), AdsApp.videoCampaigns(),
      AdsApp.shoppingCampaigns()];
  for (const selector of selectors) {
    const campaignIter = selector
        .withCondition(`CampaignName = "${campaignName}"`)
        .get();
    if (campaignIter.hasNext()) {
      return campaignIter.next();
    }
  }
  throw new Error(`Could not find specified campaign: ${campaignName}`);
}

Google Ads Scripts Forum

unread,
Nov 8, 2022, 3:33:39 AM11/8/22
to Google Ads Scripts Forum
Reposting the last inquiry (https://groups.google.com/g/adwords-scripts/c/16ZyJAsQbvQ) from the forum as it wasn't routed to our support queue. 

Cheers, 
James
Google Ads Scripts Team

Google Ads Scripts Forum Advisor

unread,
Nov 8, 2022, 11:30:55 PM11/8/22
to adwords...@googlegroups.com

Hello,

James here, a member of the Google Ads scripts support team. Thank you for reaching out to us.

Based on the given code snippet, it appears that you have implemented our Flexible Budgets - Single Account solution script. Having said that, can you please provide more details of your concern so that we can assist you accordingly? It would be helpful if you can also specify what assistance you’re requesting from us. 

Moreover, kindly share with us the following items below for our investigation.

  • Google Ads account ID / CID
  • Script name
  • Screenshot of the issue.

Regards,

Google Logo
James Howell
Google Ads Scripts Team
 


ref:_00D1U1174p._5004Q2g1NbQ:ref
Reply all
Reply to author
Forward
0 new messages