Ensure All Keywords Have a Final URL - Google Ads Script

Following on our scripts series, we had another request for a very quick and simple script that ensures all your keywords have a final URL that matches what's already in the account. If you're adding loads of keywords it's quite possible to miss a final URL, and for this particular client adding URLs at the ad level was not possible.


This script goes through all of your ad groups, finds the existing final URL that keywords have, and then adds that URL to any keywords in the ad group that currently don't have a URL. Worth noting that it won't work if none of the keywords have a final URL, as it won't know what URL to apply.


This script does actually make changes to your account, so please make sure to preview the script before running it and to thoroughly look through the changes it's making as well as the logs to ensure it's doing what you expect it to do!


There's only one parameter to change, and it only applies if you're running this in an MCC: the account IDs for all of the accounts in the MCC that you want the script to run on. If you're running this in a regular account, no need to make any changes - just paste and preview!



/*

    _   ___ _   _ _____ ___   _     
   /_\ / __| | | |_   _/ _ \ (_)___ 
  / _ \ (__| |_| | | || (_) || / _ \
 /_/ \_\___|\___/  |_| \___(_)_\___/
                                    

*/


var config = {
  
  // If using this script on an MCC, enter the account IDs for the accounts you want this to run on here. If you're running this on a single account, ignore this!
  accounts: ['123-456-7890']
  
}

function main() {  
  try {
    AdsManagerApp.accounts().withIds(config.accounts).executeInParallel('checkKeywordURLs');
  } catch(e) {
    checkKeywordURLs();
  }

}

function checkKeywordURLs() {
  var adGroups = AdsApp.adGroups().withCondition('Status = ENABLED').get();
  while (adGroups.hasNext()) {
    var adGroup = adGroups.next();
    var keywords = adGroup.keywords().withCondition('Status != REMOVED').get();
    var adGroupUrl = null;

    try {
      while (!adGroupUrl) {
        var kw = keywords.next();
        var finalUrl = kw.urls().getFinalUrl();
        if (finalUrl) adGroupUrl = finalUrl;
      }
    } catch(e) {
      Logger.log('Ad group "' + adGroup.getName() + "\" doesn't have any keywords with a final URL - please add at least one so that the script can change all keyword URLs to match!");
    }

    if (adGroupUrl) {
      while (keywords.hasNext()) {
        var keyword = keywords.next();
        var finalUrl = keyword.urls().getFinalUrl();
        if (!finalUrl) {
          keyword.urls().setFinalUrl(adGroupUrl)
        }
      }
    }
  }
} 

25 views0 comments

Recent Posts

See All