งบประมาณที่ยืดหยุ่น - บัญชีดูแลจัดการ

ไอคอนเครื่องมือ

สคริปต์นี้จะขยายการใช้งานงบประมาณที่ยืดหยุ่นเพื่อให้ทำงานกับหลายบัญชีภายใต้บัญชีดูแลจัดการบัญชีเดียว งบประมาณที่ยืดหยุ่นสามารถปรับงบประมาณแคมเปญแบบไดนามิกทุกวันด้วยรูปแบบการกระจายงบประมาณที่กำหนดเอง

สคริปต์จะอ่านสเปรดชีตสำหรับแต่ละบัญชี/แคมเปญที่ระบุ และงบประมาณที่เกี่ยวข้อง (ที่เชื่อมโยงกับวันที่เริ่มต้นและวันที่สิ้นสุด) ค้นหาแคมเปญ คำนวณงบประมาณสำหรับวันปัจจุบัน กำหนดเป็นงบประมาณรายวันของแคมเปญ และบันทึกผลลัพธ์ลงในสเปรดชีต และจะไม่สัมผัสแคมเปญ ที่ไม่ได้ระบุในสเปรดชีต

วิธีการทำงาน

สคริปต์จะทำงานในลักษณะเดียวกับสคริปต์งบประมาณแบบยืดหยุ่นของบัญชีเดียว ฟังก์ชันเพิ่มเติมเพียงอย่างเดียวคือระบบรองรับหลายบัญชีผ่านสเปรดชีตที่ระบุ

2 คอลัมน์แรกระบุแคมเปญที่จะคำนวณงบประมาณ คอลัมน์ 3 คอลัมน์ถัดไปจะระบุข้อมูลงบประมาณ และคอลัมน์สุดท้ายจะบันทึกผลของการดำเนินการ

รหัสบัญชีควรเป็นบัญชีผู้ลงโฆษณา ไม่ใช่บัญชีดูแลจัดการ

คุณสามารถมีงบประมาณหลายรายการสำหรับบัญชี/แคมเปญเดียวกันได้ แต่ให้ตรวจสอบว่าคุณมีงบประมาณที่ใช้งานอยู่เพียงครั้งละ 1 รายการ มิเช่นนั้น การคำนวณงบประมาณที่ใหม่กว่าอาจเขียนทับงบประมาณเก่า

หากไม่มีการระบุบัญชี/แคมเปญในสเปรดชีต สคริปต์จะไม่ตั้งงบประมาณที่ยืดหยุ่น

การทดสอบกลยุทธ์งบประมาณ

สคริปต์นี้จะมีโค้ดการทดสอบเพื่อจำลองผลกระทบของการทำงานเป็นเวลาหลายวัน ซึ่งจะช่วยให้คุณเห็นภาพได้ดีขึ้นว่าจะเกิดอะไรขึ้น เมื่อมีการตั้งเวลาให้สคริปต์ทํางานทุกวันในช่วงระยะเวลาหนึ่ง

โดยค่าเริ่มต้น สคริปต์นี้จะจำลองการกระจายงบประมาณที่เท่ากันที่ใช้จ่าย 15, 000 บาทในช่วง 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

ในแต่ละวันจะมีการคำนวณงบประมาณใหม่เพื่อให้แน่ใจว่ามีการใช้งบประมาณอย่างสม่ำเสมอในแต่ละวัน หลังจากใช้จ่ายเกินงบประมาณที่จัดสรรไว้ในตอนแรก ระบบจะตั้งงบประมาณเป็น 0 เพื่อหยุดการใช้จ่าย

คุณสามารถเปลี่ยนกลยุทธ์งบประมาณที่ใช้ได้โดยเปลี่ยนฟังก์ชันที่ใช้ หรือแก้ไขฟังก์ชันเอง สคริปต์มาพร้อมกับกลยุทธ์ 2 แบบที่สร้างไว้ล่วงหน้า ได้แก่ calculateBudgetEvenly และ calculateBudgetWeighted ตัวอย่างก่อนหน้านี้เพิ่งทดสอบกลยุทธ์เดิม คืออัปเดตบรรทัด testBudgetStrategy เพื่อใช้อันหลัง ดังนี้

testBudgetStrategy(calculateBudgetWeighted, 10, 500);

คลิกแสดงตัวอย่าง และตรวจสอบเอาต์พุตของตัวบันทึก โปรดสังเกตว่ากลยุทธ์งบประมาณนี้จัดสรรงบประมาณน้อยลงในช่วงต้นของช่วงเวลาและเพิ่มงบประมาณในช่วง 2-3 วันข้างหน้า

คุณสามารถใช้วิธีทดสอบนี้เพื่อจําลองการเปลี่ยนแปลงฟังก์ชันการคํานวณงบประมาณ และลองใช้วิธีกระจายงบประมาณของคุณเอง

การจัดสรรงบประมาณ

มาดูรายละเอียดเกี่ยวกับกลยุทธ์งบประมาณ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) และหารด้วยจำนวนวันที่เหลือ 2 เท่า ซึ่งจะถ่วงน้ำหนักการกระจายงบประมาณในช่วงท้ายของแคมเปญ การใช้ต้นทุนตั้งแต่ startDate จะพิจารณา "วันที่ที่ช้า" ด้วยเช่นกัน ที่คุณไม่ได้ใช้จ่ายงบประมาณทั้งหมดที่ตั้งไว้

การตั้งงบประมาณในเวอร์ชันที่ใช้งานจริง

เมื่อพอใจกับกลยุทธ์งบประมาณแล้ว คุณจะต้องทำการเปลี่ยนแปลงบางอย่างก่อนจึงจะตั้งเวลาให้สคริปต์นี้ทำงานทุกวันได้

ก่อนอื่นให้อัปเดตสเปรดชีตเพื่อระบุบัญชี แคมเปญ งบประมาณ วันที่เริ่มต้น และวันที่สิ้นสุด โดยในแต่ละงบประมาณแคมเปญจะต้องมีแถว 1 แถว

  • รหัสบัญชี: รหัสบัญชี (รูปแบบ xxx-xxx-xxxx) ของแคมเปญที่จะใช้กลยุทธ์งบประมาณ
  • ชื่อแคมเปญ: ชื่อแคมเปญที่จะใช้กลยุทธ์งบประมาณ
  • วันที่เริ่มต้น: วันที่เริ่มต้นของกลยุทธ์งบประมาณ ซึ่งควรเป็นวันที่ปัจจุบันหรือวันที่ผ่านมาแล้ว
  • วันที่สิ้นสุด: วันสุดท้ายที่คุณต้องการโฆษณาโดยใช้งบประมาณนี้
  • งบประมาณรวม: จำนวนเงินทั้งหมดที่คุณพยายามจะใช้จ่าย ค่านี้เป็นสกุลเงินของบัญชีและอาจมีการใช้จ่ายเกิน ทั้งนี้ขึ้นอยู่กับกำหนดเวลาที่ให้สคริปต์ทำงาน

ถัดไป ให้ปิดใช้การทดสอบ แล้วเปิดตรรกะเพื่อเปลี่ยนงบประมาณ โดยทำดังนี้

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

ผลของแต่ละแคมเปญจะบันทึกอยู่ในคอลัมน์ผลการดำเนินการ

Scheduling

ตั้งเวลาให้สคริปต์นี้ทำงานทุกวัน ในเวลาเที่ยงคืนหรือหลังจากนั้นเล็กน้อยในเขตเวลาท้องถิ่น เพื่อจัดสรรงบประมาณของวันถัดไปให้ได้มากที่สุด อย่างไรก็ตาม โปรดทราบว่าการดึงข้อมูลรายงาน เช่น ค่าใช้จ่าย อาจล่าช้าประมาณ 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);
}