This is specifically built for responsive search ads
The script automates the process of copying an existing campaign, renaming it, and updating the base URLs of all responsive search ads within the new campaign. This ensures that you can create a modified version of an existing campaign with updated URLs efficiently, saving time compared to manual duplication and modification
To set up this script, follow these steps:
Open your Google Ads account.
Go to the "Tools & Settings" menu and select "Scripts".
Click the "Create" or "+" button to create a new script.
Copy and paste the following script.
function main() {
var oldCampaignName = 'Original Campaign Name'; // Name of the campaign to copy
var newCampaignName = 'New Campaign Name'; // Name for the copied campaign
var oldBaseUrl = 'https://www.old-url.com'; // The base URL to replace
var newBaseUrl = 'https://www.new-url.com'; // The new base URL to use
// Get the original campaign to copy
var campaignIterator = AdsApp.campaigns()
.withCondition('Name = "' + oldCampaignName + '"')
.get();
// Check if the original campaign exists
if (!campaignIterator.hasNext()) {
Logger.log('No campaign found with the name "' + oldCampaignName + '"');
return;
}
// Get the original campaign
var oldCampaign = campaignIterator.next();
// Create a new campaign by copying settings from the old campaign
var newCampaign = AdsApp.campaigns()
.newCampaignBuilder()
.withName(newCampaignName)
.withBudget(oldCampaign.getBudget())
.withStatus(oldCampaign.isEnabled() ? 'ENABLED' : 'PAUSED') // Copy status
.withBiddingStrategy(oldCampaign.getBiddingStrategyType()) // Copy bidding strategy
.build()
.getResult();
Logger.log('Campaign "' + newCampaignName + '" created.');
// Copy each ad group and its ads
var adGroupIterator = oldCampaign.adGroups().get();
while (adGroupIterator.hasNext()) {
var oldAdGroup = adGroupIterator.next();
// Create a new ad group with the same settings as the old one
var newAdGroup = newCampaign
.newAdGroupBuilder()
.withName(oldAdGroup.getName())
.withCpc(oldAdGroup.bidding().getCpc()) // If CPC bidding, copy CPC
.withCpm(oldAdGroup.bidding().getCpm()) // If CPM bidding, copy CPM
.withMaxCpa(oldAdGroup.bidding().getMaxCpa()) // If CPA bidding, copy CPA
.withStatus(oldAdGroup.isEnabled() ? 'ENABLED' : 'PAUSED')
.build()
.getResult();
Logger.log('Ad group "' + newAdGroup.getName() + '" created.');
// Copy and modify ads from the old ad group to the new ad group
var adIterator = oldAdGroup.ads().get();
while (adIterator.hasNext()) {
var oldAd = adIterator.next();
var currentUrl = oldAd.urls().getFinalUrl();
// If the ad's URL starts with the old base URL, modify it with the new base URL
if (currentUrl && currentUrl.startsWith(oldBaseUrl)) {
var urlParams = currentUrl.substring(oldBaseUrl.length); // Get everything after the base URL
var newUrl = newBaseUrl + urlParams;
// Copy ad and apply the new URL in the new ad group
var adBuilder = newAdGroup
.expandedTextAds()
.newAd()
.withHeadlinePart1(oldAd.getHeadlinePart1())
.withHeadlinePart2(oldAd.getHeadlinePart2())
.withDescription(oldAd.getDescription())
.withFinalUrl(newUrl) // Set the new URL here
.withPath1(oldAd.getPath1())
.withPath2(oldAd.getPath2())
.build();
Logger.log('Ad "' + oldAd.getHeadlinePart1() + '" copied with new URL: ' + newUrl);
}
}
}
Logger.log('Campaign copy and URL swap complete!');
}
Add the old campaign name, new campaign name, old base URL and new base URL.
Save the script.
Review and authorize the script to access your Google Ads and Google Sheets accounts.
Run the script manually for the first time to populate the initial values in the spreadsheet.
(Optional) Schedule the script to run on a specific cadence (ie: daily, weekly, monthly, etc.)
Note: Ensure that you have the necessary permissions to access and modify the target Google Spreadsheet.