Scriptlytics
  • Introduction
    • Welcome to the community
  • Account level scripts
    • Account overview (all time)
    • Account performance (date range)
    • Account performance (daily export)
    • Account performance (last 90 days)
    • Account search query optimizer
    • Account search query optimizer (yesterday)
    • Call asset scheduling
    • Account keyword optimizer (date range)
    • Account keyword optimizer (last 90 days)
    • Match type report (last 360 days)
    • Negative keyword list
  • Campaign level scripts
    • Campaign performance (date range)
    • Campaign performance (daily export)
    • Campaign performance (last 90 days)
    • Multiple campaign performance (last 90 days)
    • Ad groups (date range)
    • Ad groups (last 30 days)
    • Keyword optimizer (date range)
    • Keyword optimizer (last 30 days)
    • Impressions heatmap (date range)
    • Impressions heatmap (last 30 days)
    • Clicks heatmap (date range)
    • Clicks heatmap (last 30 days)
    • CTR heatmap (date range)
    • CTR heatmap (last 30 days)
    • Conversions heatmap (last 30 days)
    • Conversions heatmap (date range)
    • Conversion rate heatmap (last 30 days)
    • Conversion rate heatmap (date range)
    • CPC heatmap (last 30 days)
    • CPC heatmap (date range)
  • pmax scripts
    • Pmax segmentation
    • Pmax search query optimizer
    • Pmax search query optimizer (all time)
  • Call reports
    • Call report (date range)
    • Call report (evergreen)
  • Bulk edit and actions
    • Bulk upload template
    • Swap Base URL's
    • Copy campaign and swap base URL's
  • Alerts and notifications
    • Zero spend alert
  • MCC scripts
    • MCC account performance export
    • Revised MCC account performance export
  • Search ads
    • Keyword match optimizer
  • Google sheets
    • Remove the following keywords
    • Remove keywords that don't contain
    • Remove keywords that don't contain and create negative keyword list
  • Product title modification
Powered by GitBook
On this page
  1. Bulk edit and actions

Swap Base URL's

This is specifically built for responsive search ads

The script automates the process of updating the final URLs of all responsive search ads within a specified campaign in your Google Ads account. This ensures that all RSAs in the campaign point to a new URL, streamlining bulk updates and saving time compared to manual changes.

To set up this script, follow these steps:

  1. Open your Google Ads account.

  2. Go to the "Tools & Settings" menu and select "Scripts".

  3. Click the "Create" or "+" button to create a new script.

  4. Copy and paste the following script.

function main() {
  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
  var campaignName = 'Your Campaign Name'; // Name of the campaign to update

  // Get all campaigns matching the campaign name
  var campaignIterator = AdsApp.campaigns()
    .withCondition('Name = "' + campaignName + '"')
    .get();

  // Check if the campaign exists
  if (!campaignIterator.hasNext()) {
    Logger.log('No campaign found with the name "' + campaignName + '"');
    return;
  }

  // Get the campaign
  var campaign = campaignIterator.next();

  // Get all ad groups in the campaign
  var adGroupIterator = campaign.adGroups().get();

  while (adGroupIterator.hasNext()) {
    var adGroup = adGroupIterator.next();

    // Get all ads in the ad group
    var adIterator = adGroup.ads().get();

    while (adIterator.hasNext()) {
      var ad = adIterator.next();

      // Get the ad's current final URL using the appropriate method
      var currentUrl = ad.urls().getFinalUrl();

      // If the current URL starts with the old base URL, perform the swap
      if (currentUrl && currentUrl.startsWith(oldBaseUrl)) {
        // Extract UTM parameters or anything after the base URL
        var urlParams = currentUrl.substring(oldBaseUrl.length); // Get the part after the base URL

        // Construct the new URL with the new base URL and the existing parameters
        var newUrl = newBaseUrl + urlParams;

        // Create a new ad with the updated URL
        var adBuilder = adGroup
          .expandedTextAds()
          .newAd()
          .withHeadlinePart1(ad.getHeadlinePart1())
          .withHeadlinePart2(ad.getHeadlinePart2())
          .withDescription(ad.getDescription())
          .withFinalUrl(newUrl) // Set the new URL here
          .withPath1(ad.getPath1())
          .withPath2(ad.getPath2());

        // Pause the old ad and create a new one with the updated URL
        ad.pause();
        adBuilder.build();
        Logger.log('Replaced URL in ad: ' + ad.getHeadlinePart1() + ' with new URL: ' + newUrl);
      }
    }
  }
  Logger.log('URL swap complete!');
}
  1. Update the old base URL, new base URL and campaign name.

  2. Save the script.

  3. Review and authorize the script to access your Google Ads and Google Sheets accounts.

  4. Run the script manually for the first time to populate the initial values in the spreadsheet.

  5. (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.

PreviousBulk upload templateNextCopy campaign and swap base URL's

Last updated 8 months ago