|
var NEW_CAMPAIGN_CONFIG = { |
|
"autoCreateCampaignsByUpload" : 1, // Set "1" and new campaigns will auto-added, 0 for No |
|
"newcampSettings": { "Budget" : 1, }, // Note: new campaigns will ALWAYS be uploaded as paused |
|
"uploadWithoutPreview" : 1, // With "1", the preview mode will be skipped |
|
|
|
"Campaign type" : "Search Only", // Optional, default "Search Only" |
|
"Campaign state" : "paused", // Optional, default "paused" |
|
"Campaign subtype" : "Standard", // Optional |
|
"Bid Strategy Type" : "cpc", // Optional , default "cpc" |
|
"labels" : ["someLabel1","someLabel2"] |
|
}; |
|
|
|
/** |
|
* @param {array} campaignsToCreate contains the names of all campaigns to create. |
|
* @param {object} NEW_CAMPAIGN_CONFIG object |
|
* @return {void} |
|
* @throws {error} MissingNewCampaignConfigError |
|
*/ |
|
function createCampaigns(campaignsToCreate) { |
|
|
|
if (NEW_CAMPAIGN_CONFIG["autoCreateCampaignsByUpload"] == 1) { |
|
|
|
Logger.log("Starting to create the following campaigns: " + campaignsToCreate); |
|
if(!NEW_CAMPAIGN_CONFIG) {throw new Error("MissingNewCampaignConfigError: NEW_CAMPAIGN_CONFIG + is undefined" );} |
|
|
|
var columns = ["Campaign", "Budget", "Campaign state", "Campaign type", "Campaign subtype", "Start Date", "End Date", "Bid Strategy Type"]; |
|
var upload = AdWordsApp.bulkUploads().newCsvUpload(columns); |
|
|
|
for (var i = 0; i < campaignsToCreate.length; i++) { |
|
var row = NEW_CAMPAIGN_CONFIG.newcampSettings; |
|
row["Campaign"] = campaignsToCreate[i]; |
|
upload.append(row); |
|
} |
|
|
|
if (NEW_CAMPAIGN_CONFIG.uploadWithoutPreview) { |
|
upload.apply(); |
|
Logger.log("*******."); Logger.log("Applying upload done. Waiting for upload to complete. Please run the script again, the campaigns will be ready to be filled."); Logger.log(" "); |
|
} else { |
|
Logger.log("Previewing upload."); |
|
upload.preview(); |
|
Logger.log("*******."); Logger.log("Done. Go to the bulk upload section of your AdWords account to preview the changes. "); |
|
} |
|
} else { |
|
Logger.log("No campaigns will be created due to autoCreateCampaignsByUpload configuration.") |
|
} |
|
Utilities.sleep(10000); |
|
this.applyLabels(campaignsToCreate); |
|
} |
|
|
|
|
|
/** |
|
* Applies given lables to newly created campaigns. |
|
* @param {array} campaignsToCreate contains the names of all campaigns to create. |
|
* @return {void} |
|
*/ |
|
function applyLabels(campaignsToCreate) { |
|
var labels; |
|
if (typeof NEW_CAMPAIGN_CONFIG.labels != "undefined") { |
|
labels = NEW_CAMPAIGN_CONFIG.labels; |
|
} else return; |
|
|
|
labels.push("Add Language & Location"); |
|
for (var i = 0; i < campaignsToCreate.length; i++) { |
|
var campaign; |
|
try { |
|
campaign = AdWordsApp.campaigns().withCondition('Name = "' + campaignsToCreate[i] + '"').get().next(); |
|
for (var j = 0; j < labels.length; j++) { |
|
var labelSelector = AdWordsApp.labels().withCondition("Name = '" + labels[j] + "'").get(); |
|
if (labelSelector.totalNumEntities() > 0) { |
|
campaign.applyLabel(labels[j]); |
|
} else { |
|
AdWordsApp.createLabel(labels[j]); |
|
campaign.applyLabel(labels[j]); |
|
} |
|
} |
|
} catch (e) {Logger.log("CampaignLabelOperationException: " + e); Logger.log(" ");} |
|
} // END FOR loop campaigns |
|
} |