Skrypt Google Ads: PMax Total Budget

Skrypt Google Ads PMax Total Budget to nowa wersja skryptu opublikowanego w 2020 roku:
https://liveads.pl/skrypt-google-ads-total-budget

Rozwiązanie pozwala na monitorowanie budżetu i automatyczną pauzę kampanii, gdy wydatki przekroczą określoną kwotę. Skrypt wykorzystuje selektor kampanii Performance Max, aby wykluczyć pozostałe typy kampanii.

Po konfiguracji skryptu i dodaniu go na koncie Google Ads skrypt monitoruje wybraną kampanię i wysyła powiadomienie e-mail oraz wstrzymuje kampanię, jeśli wydatki przekroczą określony budżet.

Harmonogram: co godzinę

Konfiguracja:
1. Wprowadź nazwę kampanii do zmiennej CAMPAIGN_NAME
2. Wprowadź datę rozpoczęcia kampanii do zmiennej START_DATE
3. Wprowadź kwotę budżetu do zmiennej TOTAL_BUDGET
4. Wprowadź swój e-mail do zmiennej EMAIL


Po więcej na temat Skryptów zapraszam na grupę FB o automatyzacji:
https://www.facebook.com/groups/skrypty.google.ads


/*

Copyright 2023 Krzysztof Bycina, www.LiveAds.pl
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.

*/


// --------------------------------------- Configuration:
var TOTAL_BUDGET = 2000; // TOTAL BUDGET
var START_DATE = "20230101"; // Enter the start date in this format: YYYYMMDD e.g. 20230101
var CAMPAIGN_NAME = "CAMPAIGN NAME HERE";
var EMAIL = "YOUR E-MAIL HERE";
var PERFORMANCE_MAX_ONLY = true; // Set to true to only check Performance Max campaigns
// --------------------------------------- End of the configuration

function main() {
  validateEmail(EMAIL);
  checkSpend();
}

function checkSpend() {
  var todaysDate = Utilities.formatDate(new Date(), AdsApp.currentAccount().getTimeZone(), "yyyyMMdd");
  var campaigns;

  if (PERFORMANCE_MAX_ONLY) {
    campaigns = AdsApp
      .performanceMaxCampaigns()
      .withCondition("Status = ENABLED")
      .withCondition("Name = '" + CAMPAIGN_NAME + "'")
      .get();
  } else {
    campaigns = AdsApp
      .campaigns()
      .withCondition("Status = ENABLED")
      .withCondition("Name = '" + CAMPAIGN_NAME + "'")
      .get();
  }

  while (campaigns.hasNext()) {
    var theCampaign = campaigns.next();
    var stats = theCampaign.getStatsFor("" + START_DATE + "", todaysDate);
  }

  var currentCost = stats.getCost().toFixed(2);

  //Is the budget exceeded?
  if (currentCost >= TOTAL_BUDGET) {
    sendEmails(currentCost);
    //Pause the campaign
    theCampaign.pause();
  } else {
    Logger.log("Your campaign: " + CAMPAIGN_NAME + " spend " + currentCost + " which is less than your total budget: " + TOTAL_BUDGET);
    Logger.log("No action has been taken.");
  }
}

function sendEmails(currentCost) {
  MailApp.sendEmail(
    EMAIL,
    CAMPAIGN_NAME + " exceeded your total budget.",
    "Your campaign: " +
      CAMPAIGN_NAME +
      " spend " +
      currentCost +
      " which is more than your total budget: " +
      TOTAL_BUDGET
  );
  Logger.log("Your campaign: " + CAMPAIGN_NAME + " spend " + currentCost + " which is more than your total budget: " + TOTAL_BUDGET);
  Logger.log("The campaign has been paused.");
}

function validateEmail(email) {
  var key = /^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;
  if (key.test(email) === false) {
    throw new Error("You must enter a valid email address to run the script.");
  }
}

Dodaj komentarz

Twój adres e-mail nie zostanie opublikowany. Wymagane pola są oznaczone *