Now I’m trying to make script to delete and duplicate targeted ads which I give below as Ad ID
.
Before deleting the ads, copy it and duplicate it to under the same ads group which was belonging to.
For example, here’s targeted Ad information.
targetAccountId:’123-456-7890′
campaignId:’1234567890′
adGroupId:’0987654321′
adId:’8901234567′
I want to delete adId:’8901234567′ and duplicate it to under adGroupId.
Also, I will use MCC account.
Type of ad is “responsive video ads”
*here’s a reference; Support for Responsive Video Ads in Scripts
I wrote script as below.
However, everytime I got an error
“Error: No more values at main (Code:23:8)“
although ad adGroupId and adId is existing and active.
function main() {
var targetAccountId = '123-456-7890'; // Account ID
var campaignId = '1234567890'; // Campaign ID to be added
var adGroupId = '0987654321'; // Ad group ID
var adId = '8901234567'; // Ad ID
// Select and iterate over accounts within the MCC.
var accountIterator = AdsManagerApp.accounts()
.withIds([targetAccountId])
.get();
while (accountIterator.hasNext()) {
var account = accountIterator.next();
AdsManagerApp.select(account); // Selecting the child account.
// Log for clarity.
Logger.log('Processing account: ' + account.getCustomerId());
// Attempt to find and process the specific campaign, ad group, and ad.
var campaign = AdsApp.campaigns()
.withIds([campaignId])
.get()
.next();
var adGroup = campaign.adGroups()
.withIds([adGroupId])
.get()
.next();
var adIterator = adGroup.ads()
.withIds([adId])
.get();
if (adIterator.hasNext()) {
var adToDuplicate = adIterator.next();
// Ensure it's a responsive video ad before proceeding.
if (adToDuplicate.getType() === 'VIDEO_AD') { // This type check might need to be adjusted.
// Duplicate ad logic goes here. As an example:
var newAdOperation = adGroup.newVideoAd().responsiveVideoAdBuilder()
// Builder methods to replicate the ad's properties.
// Note: Actual methods will vary based on the ad's specifics.
.build();
if (newAdOperation.isSuccessful()) {
// Remove the original ad after successful duplication.
adToDuplicate.remove();
Logger.log('Ad duplicated and original ad removed for account ' + account.getCustomerId());
} else {
Logger.log('Failed to create duplicate ad for account ' + account.getCustomerId());
}
} else {
Logger.log('The specified ad is not a responsive video ad for account ' + account.getCustomerId());
}
} else {
Logger.log('Ad not found in account ' + account.getCustomerId());
}
}
}