// Create campaign via bulk upload because apparently this just isn't a regular supported function? function createCampaign(campaignName, startDate, endDate) { // Specify standard columns // TbD: Start date, End date (YYYY-MM-DD) - also extract location taregeting or set in sheet? var columns = ["Campaign", "Budget", "Budget type", "Status", "Bid Strategy type", "Campaign type", "start date", "end date"];
var upload = AdsApp.bulkUploads().newCsvUpload(columns);
// Add in values for campaign to be created upload.append({ 'Campaign': campaignName, 'Budget': 0.5, 'Budget type': 'Daily', 'Status': 'Paused', 'Bid Strategy type': 'Maximize clicks', 'Campaign type': 'Search', 'start date': startDate, 'end date': endDate });
// Make the campaign upload.forCampaignManagement(); upload.apply();
// Wait a little bit because apparently the upload application is not actually instant Utilities.sleep(500); }// Check if this campaign already exists. If not create it and assign it to the iterator var campaignIterator = AdsApp.campaigns().withCondition('Name = "' + currentCampaign + '"').get(); if (campaignIterator.totalNumEntities() == 0) { createCampaign(currentCampaign, row[44], row[45]);
campaignIterator = AdsApp.campaigns().withCondition('Name = "' + currentCampaign + '"').get();
// Stupid workaround for delay in campaign deployment just keeps trying until it gets the campaign var fail = 0; while (campaignIterator.totalNumEntities() == 0) { Utilities.sleep(500); campaignIterator = AdsApp.campaigns().withCondition('Name = "' + currentCampaign + '"').get(); fail++; if (fail > 10) { // waited 5 seconds and still failing? throw '5 seconds of campaign not appearing'; } }
// Get the active campaign to work with it var campaign = campaignIterator.next();