Adwords Script – Double your Targeted Keyword Portfolio

7 Aaron Falloon

Keywords are a pretty important part of paid search marketing (that’s an understatement and a half). In short, good keyword selection equals higher quality traffic which leads to £££s.

Google know this too and, of late, have become increasingly restrictive with the data they supply about keywords … well, unless you pay lofty sums for the privilege. So, without any “reliable” data source to turn to, marketers can find themselves stifled when it comes to thinking of new keywords to target.

However, PPCers do have an abundance of data to use at their disposal – that being their Adwords accounts and, therein, the keywords they already target.

How to quickly generate new keywords

Below is a free and easy-to-use Adwords script that uses a campaign’s existing keywords to create thousands of new keywords in seconds. Here’s a few notes on how the script works before you start running it.

  • creates a duplicate of the base keyword that include a user-specified word, then replaces said word with a substitute (which is also specified by the user)
  • automatically populates the new keyword within the most relevant AdGroup
    • if one doesn’t already exist, a new AdGroup will be created for the new keyword (note, that no advert will be created)
  • user must set the campaign label so that the script knows which campaigns to run on
  • all new keywords Max CPC is set at 80% of the base AdGroup’s Max CPC
  • all new keywords and AdGroups  are initially paused for your consideration and labelled “Replaced Word Entity”
// This Adwords Scripts was created by Gravytrain Ltd. for public use.
// To find out more about Gravytrain visit https://www.gravytrain.co.uk/

function main() {
  
  // PARAMETERS SET BY USER
  var campaignLabel = ""; 
  var KWIncludes = ""; // THE WORD THE BASE KEYWORDS INCLUDE
  var KWReplacement = ""; // THE SUBSTITUTE WORD

  // LEAVE REST OF SCRIPT AS IT IS
  // CREATE NEW ENTITY LABEL ON FIRST RUN
  var newEntityLabel = "Replaced Word Entity";
  var labelTotal = AdWordsApp.labels()
  .withCondition("Name = '" + newEntityLabel + "'")
  .get().totalNumEntities();
   if (labelTotal < 1) { AdWordsApp.createLabel(newEntityLabel); };

  // TITLE CASE FUNCTION
  function toTitleCase(str)
  {
    return str.replace(/\w\S*/g, function(txt){
      return txt.charAt(0).toUpperCase() + txt.substr(1).toLowerCase();
    });
  }

  // ITERATE THRU CAMPAIGNS
  var campaignIter = AdWordsApp.campaigns()
  .withCondition("Status = ENABLED")
  .withCondition("LabelNames CONTAINS_ANY ['"  + campaignLabel + "']")
  .get();
  while (campaignIter.hasNext()) {
    var campaign = campaignIter.next();

    // GET ALL KWREPLACEMENT KEYWORDS
    var existingKWs = [];
    var KWIter = campaign.keywords().withCondition("AdGroupStatus = ENABLED")
    .withCondition("Text CONTAINS_IGNORE_CASE '" + KWReplacement + "'")
    .get();
    while (KWIter.hasNext()) {
      var KW = KWIter.next();
      var KWText = KW.getText();
      existingKWs.push(KWText);
    }

    // ITERATE THRU KWINCLUDES KWS
    var KWIter = campaign.keywords()
    .withCondition("AdGroupStatus = ENABLED")
    .withCondition("Status = ENABLED")
    .withCondition("Text CONTAINS_IGNORE_CASE '" + KWIncludes + "'")
    .get();
    while (KWIter.hasNext()) {
      var KW = KWIter.next();
      var KWText = KW.getText();
      
      // REPLACE TEXT & CHECK AGAINST EXISTING
      var newKWText = KWText.toLowerCase().replace(KWIncludes, KWReplacement);
      if (existingKWs.indexOf(newKWText) < 0) {
        existingKWs.push(newKWText);

        // CHECK IF MATCHING ADGROUP EXISTS
        var newBid =  KW.getAdGroup().bidding().getCpc() * 0.8;
        var newAGName = KW.getAdGroup()
        .getName().toLowerCase().replace(KWIncludes, KWReplacement);
        var AGIter = campaign.adGroups()
        .withCondition("Name CONTAINS_IGNORE_CASE '" + newAGName + "'")
        .get();
        var totalAGs = AGIter.totalNumEntities();
        
        // IF YES MATCHING ADGROUP, ADD NEW KEYWORD
        if (totalAGs > 0) {
 
          while (AGIter.hasNext()) {
            var AG = AGIter.next();
            var KWOperation = AG.>newKeywordBuilder()
            .withText(newKWText)
            .withStatus("PAUSED")
            .withCpc(newBid)
            .build();
            var newKW = KWOperation.getResult();
            newKW.applyLabel(newEntityLabel);
            
            break;
          }
        
        } else {
          // IF NO MATCHING ADGROUP, CREATE ONE - POPULATE WITH KW & AD
          newAGName = toTitleCase(newAGName);

          var AGOperation = campaign.newAdGroupBuilder()
          .withName(newAGName)
          .withStatus("PAUSED")
          .withCpc(newBid)
          .build();
          var newAG = AGOperation.getResult();
          newAG.applyLabel(newEntityLabel);
          
          var KWOperation = newAG.newKeywordBuilder()
          .withText(newKWText)
          .withStatus("PAUSED")
          .withCpc(newBid)
          .build();
          var newKW = KWOperation.getResult();
          newKW.applyLabel(newEntityLabel);
          
          break;
            
        }
      }
    }
  }
}

 

Our PPC solutions team is able to create customised digital marketing services to optimise your results. Contact our team now 020 3813 7545.