Skip to content

Instantly share code, notes, and snippets.

@tonkikh

tonkikh/Red Big Button.gs Secret

Last active Oct 22, 2020
Embed
What would you like to do?
Script for emergency shutdown of all campaigns in the Google Ads account.
/******************************************
* 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 : http://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;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.