Hello and welcome to our community! Is this your first visit?
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    New to the CF scene
    Join Date
    Mar 2014
    Thanked 0 Times in 0 Posts

    Question [Newbie] Short Script, If You Could Have a Quick Look

    Hi guys,

    I'm new of this forum and I'm honest, I don't know anything about JS. I use occasionally some js script for my business (I'm working in advertising) and I can't find a solution for this small one. I have some knowledge of programming and I tried using Dreamweaver to fix the problem with no success. Basically this script has to run every morning and set up budget on different AdWords campaign.
    If you could help me out would be very much appreciated, thanks in advance for your kindness.

    /* Modified Flexible Budget Script 
    ** @param (number) TOTAL_BUDGET - the total sum to be spent in a single month
    ** Schedule to run around 1am (before "day" starts)
    var TOTAL_BUDGET = 2900;
    var d = new Date();
    var thisMonth=d.getMonth();
    var thisYear=d.getFullYear();
    // DAYS_SO_FAR is -1 because "today" hasn't happened yet
    var DAYS_SO_FAR = d.getDate() - 1;
    var TOTAL_DAYS = daysInMonth(thisMonth + 1, thisYear);
    var START_DATE = new Date(thisYear, thisMonth, 1);
    var END_DATE = new Date(thisYear, thisMonth, TOTAL_DAYS);
    function main() {
      setNewBudget(calculateBudgetEvenly, TOTAL_BUDGET, START_DATE, END_DATE);
    function setNewBudget(budgetFunction, totalBudget, start, end) {
      var costSoFar = 0;
      var campaignsToSet = [];
      var campaigns = AdWordsApp.campaigns()
        //operate only on Campaigns labelled with "FlexBudget"
        .withCondition('LabelNames CONTAINS_ANY ["FlexBudget"]')
      while(campaigns.hasNext()) {
        var campaign=campaigns.next();
        var thisCost = campaign.getStatsFor(dateToString(start), dateToString(end)).getCost();
        costSoFar += thisCost;
      for (var i=0;i;
           var thisCampaign=campaignsToSet[i]
          // Ensure only the "percentage" Label is selected - you may need to add others if you use them
          .withCondition('Name NOT_IN ["FlexBudget"]')
        var percentage = cLabels.next().getName();
        var newBudget = budgetFunction(costSoFar, totalBudget, percentage);
    function calculateBudgetEvenly(costSoFar, totalBudget, percentage) {
      var daysRemaining = (TOTAL_DAYS - DAYS_SO_FAR);
      //set budget based upon percentage Label
      var budgetRemaining = (totalBudget - costSoFar)*(percentage / 100);
      if (daysRemaining <= 0) {
        return budgetRemaining;
      } else {
        return budgetRemaining / daysRemaining;
    function daysInMonth(month, year) {
      //returns number of days in a given month of a given year
      return new Date(year, month, 0).getDate();
    function dateToString(date) {
      return date.getFullYear() + zeroPad(date.getMonth() + 1) + zeroPad(date.getDate());
    function zeroPad(n) {
      if (n < 10) {
        return '0' + n;
      } else {
        return '' + n;

  • #2
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    London, England
    Thanked 2,536 Times in 2,514 Posts
    So what is the problem? If you want to update various variables each day you should use server-side scripting. Javascript cannot know what proportion of your budget has already been spent.

    All advice is supplied packaged by intellectual weight, and not by volume. Contents may settle slightly in transit.

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.


    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts