Skip to content

Instantly share code, notes, and snippets.

@norisk-marketing
Last active December 10, 2021 21:43
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save norisk-marketing/77de7919114ba4e9208ac5d4f69714ee to your computer and use it in GitHub Desktop.
Save norisk-marketing/77de7919114ba4e9208ac5d4f69714ee to your computer and use it in GitHub Desktop.
CampaignCreation Service for AdWords Scripts via BulkUpload
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
}

nrCampaignUpload.js

Simple AdWords Scripts helper method that auto-uploads campaigns via the bulk upload service. Bulk upload is a generally recommended best practices by Google for larger entity operations: https://developers.google.com/adwords/scripts/docs/best-practices#consider_using_bulk_uploads_for_large_updates

Installation

  • Copy the 'NEW_CAMPAIGN_CONFIG' to the beginning of your script.
  • Copy the two methods createCampaigns and applyLabels to the END of your script

Required values

  • Modify the NEW_CAMPAIGN_CONFIG variable as neeeded.
  • Pass an array of campaign names into createCampaigns method.

IMPORTANT NOTES.

  • The bulk service can NOT (!) set campaign language and location. Please always upload as paused and update the settings.
  • The bulk operation will not show as adwords scripts operation. Either go to the bulk upload section or your the campaign overview your AdWords account
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment