תקציבים גמישים – חשבון ניהול

לוחצים על סמל מפתח הברגים

הסקריפט מרחיב את ההפעלה של תקציבים גמישים לכמה חשבונות שמנוהלים על ידי חשבון ניהול אחד. תקציבים גמישים יכולים לשנות באופן דינמי את תקציב הקמפיין מדי יום, באמצעות סכימת התפלגות מותאמת אישית של התקציב.

הסקריפט קורא גיליון אלקטרוני לכל חשבון/קמפיין ולכל תקציב תואם (שמשויך לתאריך ההתחלה ולתאריך הסיום), למצוא את הקמפיין, לחשב את התקציב ליום הנוכחי, להגדיר אותו כתקציב היומי של הקמפיין ולתעד את התוצאה בגיליון האלקטרוני. היא לא תיגע בקמפיינים שלא צוינו בגיליון האלקטרוני.

איך זה עובד

הסקריפט פועל באותו אופן שבו פועל הסקריפט 'תקציב גמיש' בחשבון יחיד. הפונקציונליות הנוספת היחידה היא שיש בה תמיכה במספר חשבונות באמצעות הגיליון האלקטרוני שצוין.

שתי העמודות הראשונות מציינות את הקמפיין שעבורו יחושב התקציב, בשלוש העמודות הבאות מצוין פרטי התקציב ובשתי העמודות האחרונות מופיעה תוצאת הביצוע.

מספר החשבון צריך להיות חשבון מפרסם, ולא חשבון ניהול.

אפשר להגדיר כמה תקציבים לאותו חשבון או קמפיין, אבל חשוב לוודא שיש רק תקציב פעיל אחד בכל פעם, אחרת חישוב תקציב חדש יותר עשוי להחליף תקציב ישן יותר

אם חשבון/קמפיין לא מצוינים בגיליון האלקטרוני, הסקריפט לא יגדיר עבורם תקציב גמיש.

בדיקה של שיטות התקציב

הסקריפט כולל קוד בדיקה שמדמה את השפעות הריצה במשך מספר ימים. כך תוכלו להבין טוב יותר מה קורה כשהסקריפט מתוזמן לפעול מדי יום לאורך תקופת זמן מסוימת.

כברירת מחדל, הסקריפט הזה מדמה חלוקת תקציב שווה של 500 $שהוצאתם במשך 10 ימים.

אפשר להריץ את קוד הבדיקה על ידי קריאה ל-testBudgetStrategy במקום ל-setNewBudget בשיטה הראשית:

function main() {
  testBudgetStrategy(calculateBudgetEvenly, 10, 500);
  //  setNewBudget(calculateBudgetWeighted);
}

מתקבלת תגובה לקריאה לפונקציה setNewBudget, מה שמצביע על כך שהסקריפט מריץ את קוד הבדיקה. הנה הפלט מהדוגמה:

Day 1.0 of 10.0, new budget 50.0, cost so far 0.0
Day 2.0 of 10.0, new budget 50.0, cost so far 50.0
Day 3.0 of 10.0, new budget 50.0, cost so far 100.0
Day 4.0 of 10.0, new budget 50.0, cost so far 150.0
Day 5.0 of 10.0, new budget 50.0, cost so far 200.0
Day 6.0 of 10.0, new budget 50.0, cost so far 250.0
Day 7.0 of 10.0, new budget 50.0, cost so far 300.0
Day 8.0 of 10.0, new budget 50.0, cost so far 350.0
Day 9.0 of 10.0, new budget 50.0, cost so far 400.0
Day 10.0 of 10.0, new budget 50.0, cost so far 450.0
Day 11.0 of 10.0, new budget 0.0, cost so far 500.0

בכל יום מחושב תקציב חדש כדי להבטיח ניצול שווה של התקציב בכל יום. אחרי חריגה מההקצאה הראשונית של התקציב, התקציב מוגדר לאפס כדי להפסיק את ההוצאה.

כדי לשנות את שיטת התקציב שמשמשת אותך, אפשר לשנות את הפונקציה שבה נעשה שימוש או לשנות את הפונקציה עצמה. הסקריפט כולל שתי אסטרטגיות מוגדרות מראש: calculateBudgetEvenly ו-calculateBudgetWeighted; הדוגמה הקודמת בדקה את השנייה - עדכנו את השורה testBudgetStrategy כדי להשתמש באפשרות השנייה:

testBudgetStrategy(calculateBudgetWeighted, 10, 500);

לוחצים על Preview (תצוגה מקדימה) ובודקים את פלט היומן. שימו לב ששיטת התקציב הזו מקצה פחות תקציב בתחילת התקופה ומגדילה אותו לימים הבאים.

ניתן לך להשתמש בשיטת הבדיקה הזו כדי לדמות שינויים בפונקציות חישוב התקציב, ולנסות גישה משלך לחלוקת התקציב.

הקצאת תקציב

נבחן את שיטת התקציב calculateBudgetWeighted בצורה מפורטת יותר:

// One calculation logic that distributes remaining budget in a weighted manner
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);
  }
}

הפונקציה הזאת מקבלת את הארגומנטים הבאים:

  • costSoFar: איזו עלות צברה הקמפיין הזה מתאריך startDate ועד היום.
  • totalBudget: כמה להוציא מ-startDate עד endDate.
  • daysSoFar: כמה ימים חלפו מ-startDate עד היום.
  • totalDays: מספר הימים הכולל בין startDate ל-endDate.

אתם יכולים לכתוב פונקציה משלכם כל עוד היא מקבלת את הארגומנטים האלה. באמצעות הערכים האלה, תוכלו להשוות בין הסכום שהוצאתם עד כה לבין סכום ההוצאה הכולל ולקבוע איפה אתם נמצאים כרגע במסגרת לוח הזמנים של התקציב כולו.

באופן ספציפי, שיטת התקציב הזו קובעת כמה תקציב נותר (totalBudget - costSoFar) ומחלקת אותו פי שניים ממספר הימים שנותרו. הערך הזה משקל את חלוקת התקציב לקראת סוף הקמפיין. כשמשתמשים בעלות מאז startDate, המערכת מביאה בחשבון גם "ימים איטיים" שבהם לא מנצלים את כל התקציב שהגדרתם.

תקציב בסביבת הייצור

אחרי שתהיו מרוצים משיטת התקציב שבחרתם, תצטרכו לבצע כמה שינויים לפני שתוכלו לתזמן את הסקריפט כך שיפעל מדי יום.

תחילה, מעדכנים את הגיליון האלקטרוני כך שיכלול חשבון, קמפיין, תקציב, תאריך התחלה ותאריך סיום – שורה אחת לכל תקציב של קמפיין.

  • מספר חשבון: מספר החשבון (בפורמט xxx-xxx-xxxx) של הקמפיין שעליו רוצים להחיל את שיטת התקציב.
  • שם הקמפיין: שם הקמפיין שעליו רוצים להחיל את שיטת התקציב.
  • תאריך התחלה: תאריך ההתחלה של שיטת התקציב. התאריך הזה צריך להיות התאריך הנוכחי או תאריך שחל בעבר.
  • תאריך סיום: היום האחרון שבו רוצים לפרסם עם התקציב הזה.
  • תקציב כולל: הסכום הכולל שאתם מנסים להוציא. הערך הזה הוא במטבע שמוגדר בחשבון, ואפשר לחרוג ממנו בהתאם למועד שבו הסקריפט מתוזמן לפעול.

בשלב הבא, משביתים את הבדיקה ומפעילים את הלוגיקה לשינוי התקציב בפועל:

function main() {
  //  testBudgetStrategy(calculateBudgetEvenly, 10, 500);
  setNewBudget(calculateBudgetWeighted);
}

התוצאה של כל קמפיין מתועדת בעמודה תוצאת ביצוע.

תזמון

תזמנו את הסקריפט כך שיפעל מדי יום, בחצות או זמן קצר לאחר מכן באזור הזמן המקומי, כדי לנתב כמה שיותר מהתקציב של היום הבא. עם זאת, שימו לב שנתוני דוחות שמאוחזרים, כמו עלות, עשויים להתעכב בכ-3 שעות, ולכן הפרמטר costSoFar עשוי להתייחס לסכום הכולל מאתמול של סקריפט שמתוזמן לפעול אחרי חצות.

הגדרה

קוד מקור

// Copyright 2015, Google Inc. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

/**
 * @name MCC Flexible Budgets
 *
 * @overview The MCC Flexible Budgets script dynamically adjusts campaign budget
 *     daily for accounts under an MCC account with a custom budget distribution
 *     scheme. See
 *     https://developers.google.com/google-ads/scripts/docs/solutions/manager-flexible-budgets
 *     for more details.
 *
 * @author Google Ads Scripts Team [adwords-scripts@googlegroups.com]
 *
 * @version 2.1
 *
 * @changelog
 * - version 2.1
 *   - Split into info, config, and code.
 *  - version 2.0
 *   - Updated to use new Google Ads scripts features.
 * - version 1.0.4
 *   - Add support for video and shopping campaigns.
 * - version 1.0.3
 *   - Added validation for external spreadsheet setup.
 * - version 1.0.2
 *   - Fix a minor bug in variable naming.
 *   - Use setAmount on the budget instead of campaign.setBudget.
 * - version 1.0.1
 *   - Improvements to time zone handling.
 * - version 1.0
 *   - Released initial version.
 */
/**
 * Configuration to be used for the Flexible Budgets script.
 */
CONFIG = {
  // URL of the default spreadsheet template. This should be a copy of
  // https://docs.google.com/spreadsheets/d/17wocOgrLeRWF1Qi_BjEigCG0qVMebFHrbUS-Vk_kpLg/copy
  // Make sure the sheet is owned by or shared with same Google user executing the script
  'spreadsheet_url': 'YOUR_SPREADSHEET_URL',

  'advanced_options': {
    // Please fix the following variables if you need to reformat the
    // spreadsheet
    // column numbers of each config column. Column A in your spreadsheet has
    // column number of 1, B has number of 2, etc.
    'column': {
      'accountId': 2,
      'campaignName': 3,
      'startDate': 4,
      'endDate': 5,
      'totalBudget': 6,
      'results': 7
    },

    // Actual config (without header and margin) starts from this row
    'config_start_row': 5
  }
};

const SPREADSHEET_URL = CONFIG.spreadsheet_url;
const COLUMN = CONFIG.advanced_options.column;
const CONFIG_START_ROW = CONFIG.advanced_options.config_start_row;

function main() {
  // Uncomment the following function to test your budget strategy function
  // testBudgetStrategy(calculateBudgetEvenly, 10, 500);
  setNewBudget(calculateBudgetWeighted);
}

// Core logic for calculating and setting campaign daily budget
function setNewBudget(budgetFunc) {
  console.log(`Using spreadsheet - ${SPREADSHEET_URL}.`);
  const spreadsheet = validateAndGetSpreadsheet(SPREADSHEET_URL);
  spreadsheet.setSpreadsheetTimeZone(AdsApp.currentAccount().getTimeZone());
  const sheet = spreadsheet.getSheets()[0];

  const endRow = sheet.getLastRow();

  const mccAccount = AdsApp.currentAccount();
  sheet.getRange(2, 6, 1, 2).setValue(mccAccount.getCustomerId());

  const today = new Date();

  for (let i = CONFIG_START_ROW; i <= endRow; i++) {
    console.log(`Processing row ${i}`);

    const accountId = sheet.getRange(i, COLUMN.accountId).getValue();
    const campaignName = sheet.getRange(i, COLUMN.campaignName).getValue();
    const startDate = new Date(sheet.getRange(i, COLUMN.startDate).getValue());
    const endDate = new Date(sheet.getRange(i, COLUMN.endDate).getValue());
    const totalBudget = sheet.getRange(i, COLUMN.totalBudget).getValue();
    const resultCell = sheet.getRange(i, COLUMN.results);

    const accountIter = AdsManagerApp.accounts().withIds([accountId]).get();
    if (!accountIter.hasNext()) {
      resultCell.setValue('Unknown account');
      continue;
    }
    const account = accountIter.next();
    AdsManagerApp.select(account);

    const campaign = getCampaign(campaignName);
    if (!campaign) {
      resultCell.setValue('Unknown campaign');
      continue;
    }

    if (today < startDate) {
      resultCell.setValue('Budget not started yet');
      continue;
    }
    if (today > endDate) {
      resultCell.setValue('Budget already finished');
      continue;
    }

    const costSoFar = campaign
                          .getStatsFor(
                              getDateStringInTimeZone('yyyyMMdd', startDate),
                              getDateStringInTimeZone('yyyyMMdd', endDate))
                          .getCost();
    const daysSoFar = datediff(startDate, today);
    const totalDays = datediff(startDate, endDate);
    const newBudget = budgetFunc(costSoFar, totalBudget, daysSoFar, totalDays);
    campaign.getBudget().setAmount(newBudget);
    console.log(
        `AccountId=${accountId}, CampaignName=${campaignName}, ` +
        `StartDate=${startDate}, EndDate=${endDate}, ` +
        `CostSoFar=${costSoFar}, DaysSoFar=${daysSoFar}, ` +
        `TotalDays=${totalDays}, NewBudget=${newBudget}'`);
    resultCell.setValue(`Set today's budget to ${newBudget}`);
  }

  // update "Last execution" timestamp
  sheet.getRange(1, 3).setValue(today);
  AdsManagerApp.select(mccAccount);
}

// One calculation logic that distributes remaining budget evenly
function calculateBudgetEvenly(costSoFar, totalBudget, daysSoFar, totalDays) {
  const daysRemaining = totalDays - daysSoFar;
  const budgetRemaining = totalBudget - costSoFar;
  if (daysRemaining <= 0) {
    return budgetRemaining;
  } else {
    return budgetRemaining / daysRemaining;
  }
}

// One calculation logic that distributes remaining budget in a weighted manner
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);
  }
}

// Test function to verify budget calculation logic
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;
  }
}

// Return 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);
}

// Produces a formatted string representing a given date in a given time zone.
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();
    }
  }
  return null;
}

/**
 * Validates the provided spreadsheet URL to make sure that it's set up
 * properly. Throws a descriptive error message if validation fails.
 *
 * @param {string} spreadsheeturl The URL of the spreadsheet to open.
 * @return {Spreadsheet} The spreadsheet object itself, fetched from the URL.
 * @throws {Error} If the spreadsheet URL hasn't been set
 */
function validateAndGetSpreadsheet(spreadsheeturl) {
  if (spreadsheeturl == 'YOUR_SPREADSHEET_URL') {
    throw new Error(
        'Please specify a valid Spreadsheet URL. You can find' +
        ' a link to a template in the associated guide for this script.');
  }
  return SpreadsheetApp.openByUrl(spreadsheeturl);
}