Скрипт для аварийного отключения всех кампаний в аккаунте Google Ads

StalkerOK

Администратор
Команда форума
Регистрация
5 Фев 2019
Сообщения
480
Код:
/******************************************
* Red Big Button
*
* This script turns off all active campaigns and re-enabled them after second run.
* Script supports all campaign types.
*
* Version         : 1.0
* Date            : 23.03.2020
*
* Created by      : Dmytro Tonkikh, chiliad
*
* Telegram channel: t.me/adwordsscripts
* Site                 : https://chiliad.agency
*
* For donates:
*  Yandex.Money – https://money.yandex.ru/to/410015037212380
*  Visa/MasterCard – https://send.monobank.com.ua/2r4Vye2xV
******************************************/

var dataFile = 'red_big_button.txt'

function main() {

    var campaigns = activeCampaigns();
    var upload = AdsApp.bulkUploads().newCsvUpload(["Campaign", "Campaign ID", "Campaign state"]).forCampaignManagement();

    var files = DriveApp.getFilesByName(dataFile);
    if (!files.hasNext()) {
        var file = DriveApp.createFile(dataFile, "[]");
        Logger.log("Файл '" + dataFile + "' создан.");
    } else {
        var file = files.next();
        if (files.hasNext()) {
            Logger.log("Ошибка - больше одного файла создано '" + dataFile + "'");
            return;
        }
        Logger.log("Файл '" + dataFile + "' прочитан.");
    }

    var data = JSON.parse(file.getBlob().getDataAsString());

    if (data.length == 0 && campaigns.length > 0) {
        Logger.log(data);
        for (var i in campaigns) {
            campaigns[i]["Campaign state"] = "paused";
            campaigns[i]["Action"] = "set";
            upload.append(campaigns[i])
        }
        var content = JSON.stringify(campaigns);
    } else if (data.length > 0) {
        Logger.log(campaigns)
        for (var i in data) {
            data[i]["Campaign state"] = "enabled";
            upload.append(data[i])
        }
        var content = "[]";
    }

    upload.apply()
    file.setContent(content);

}

function activeCampaigns() {
    var retVal = [];
    var report = AdsApp.report(
        "SELECT CampaignName, CampaignId, CampaignStatus " +
        "FROM CAMPAIGN_PERFORMANCE_REPORT " +
        "WHERE CampaignStatus = ENABLED"
    ).rows();

    while (report.hasNext()) {
        var r = report.next();
        retVal.push({ "Campaign": r.CampaignName, "Campaign ID": r.CampaignId, "Campaign state": r.CampaignStatus })
    }
    return retVal;
}
Источник
 
Сверху