Text Ad Builder Script

Script to generate Google Ads text ads from spreadsheet data

Text Ad Builder Script illustration

Looking for scripts to automate text ad generation?

Close the automation circle by building text ads. The “Text Ad Builder” script generates text ads based on external spreadsheet data. You can use this spreadsheet template but we strongly suggest using our “Shopping Explorer” script instead to identify successful Shopping queries which can then be added as keywords — a light version of our Keyword Sourcing module. Depending on their length, well performing search terms will also be pushed into headline 1, headline 2 and/or the description, while the price retrieved from the exploration script can be used as adParam.

Download script and start optimizing

No commitments & no hidden costs — Just a great script

Access and read our
privacy policy.

Success image

Thank you!

You can get the script code below

Arrow down icon
/**
 * Text Ad Builder Script        . * ・ 。゚☆━੧[ ✪ ᗜ ✪ ]⊃
 *
 * Overview: Close the automation circle by building text ads from the hidden
 * gems in your Google Shopping account. This script generates text ads based on
 * an external spreadsheet. We suggest the prior use of our “Shopping Explorer”
 * script to identify successful Shopping queries which can then be used as
 * keywords and in the ad text.
 *
 * Author: smec Team [smarter-ecommerce.com]
 *
 * Version: 1.0
 * Changelog:
 * - version 1.0
 *  - Released initial version.
 * Updated: 9-05-2018
 *
 * Twitter: @smec
 * Email: [email protected]
 * (c) Smarter Ecommerce GmbH. All rights reserved.
 * License: MIT
 */

/* ============================== Configuration ============================= */

//adjust the textAds to your needs in the textAdBuilder-section marked below

// Campaign you want the Adgroups and TextAds to be created in
var CAMPAIGN_ID = "YOUR_CAMPAIGN_ID";
// Google-Spreadsheet where the exploration-script saved the item-data
var SPREADSHEET_ID = "YOUR_SPREADSHEET_ID";

// Fallback ad title in case the query from the source is too long.
var FALLBACK_HEADLINE_PART_1 = "YOUR_FALLBACK_HEADLINE_PART_1_HERE";

// Sets the second part of the new ad's headline to the specified value.
var HEADLINE_PART_2 = "YOUR_HEADLINE_PART_2_HERE"; //(e.g. "Buy now at our store")

// Adds additional text to the new ad's description.
// Additionally to this text, the script appends AdParam with price at the end.
var DESCRIPTION_ADDITION = "YOUR_DESCRIPTION_ADDITION"; // (E.g. " by BrandName. Buy now for ")
var CURRENCY_FOR_ADPARAM = "€";
// Example final ad description: <Search query> by BrandName. Buy now for 25€ !

// Sets the first path that appears with the new ad's displayed URL.
var PATH_1 = "YOUR_PATH_1"; //(E.g. Soccer)
var PATH_2 = "YOUR_PATH_2"; //(E.g. Store)

/* =============================== Execution ================================ */

function main() {
  var spreadsheet = SpreadsheetApp.openById(SPREADSHEET_ID);
  var data = spreadsheet.getDataRange().getValues();

  var campaignIterator = AdWordsApp.campaigns().withIds([CAMPAIGN_ID]).get();
  if (campaignIterator.totalNumEntities() > 1) {
    throw new Error("Provided an invalid number of Campaign Ids");
  }

  while (campaignIterator.hasNext()) {
    var campaign = campaignIterator.next();
    for (var i = 1; i < data.length; i++) {
      var cpc = parseFloat(data[i][11]);
      if (cpc !== undefined && cpc > 0) {
        var query = data[i][3];
        var itemId = data[i][2];
        var adGroupName = itemId + " - " + query;

        var checkAdGroup = campaign.adGroups()
          .withCondition("Name = '" + adGroupName + "'").get();

        // Check if adGroup already exists.
        if (checkAdGroup.totalNumEntities() === 0) {
          var adGroupOperation = campaign.newAdGroupBuilder()
          .withName(adGroupName)
          .withCpc(cpc)
          .build();
          var adGroup = adGroupOperation.getResult();
          var keywordOperation = adGroup.newKeywordBuilder()
          .withText(query)
          .withCpc(cpc)
          .build();

          var keyword = keywordOperation.getResult();
          keyword.setAdParam(1, data[i][9] + CURRENCY_FOR_ADPARAM);

          // Build text ads
          if (query.length < 30) {
            // Default case (query is below 30 symbols).
            adGroup.newAd()
            .expandedTextAdBuilder()
            .withHeadlinePart1(capitalizeString(query))
            .withHeadlinePart2(HEADLINE_PART_2)
            .withDescription(capitalizeString(query) + DESCRIPTION_ADDITION + "{param1:less money} !")
            .withPath1(PATH_1)
            .withPath2(PATH_2)
            .withFinalUrl(data[i][10])
            .build();
          } else {
            // Fallback case when the query is too long.
            adGroup.newAd()
            .expandedTextAdBuilder()
            .withHeadlinePart1(FALLBACK_HEADLINE_PART_1)
            .withHeadlinePart2(HEADLINE_PART_2)
            .withDescription(capitalizeString(query) + DESCRIPTION_ADDITION + "{param1:less money} !")
            .withPath1(PATH_1)
            .withPath2(PATH_2)
            .withFinalUrl(data[i][10])
            .build();
          }
        }
      }
    }
  }
}

function capitalizeString(string) {
  return string.replace(/\b\w/g, function(l){ return l.toUpperCase(); });
}

The code was successfully copied to your Clipboard.

Popular Google Ads Scripts

Martin Röttgerding

“Automation can take your PPC efforts to the next level – if you’re doing it right.”

Martin Röttgerding, Head of SEA – Bloofusion Germany

Book a personalized demo

Find out what the AdEngine can do for your business

Logo of Great Day for PPC Automation by smec
smec Headquarters at Tabakfabrik, Linz
Join industry leaders as they share new developments in paid search automation
Watch Livestream