Generate Google Ads Headlines Using Chat GPT

Published: August 21, 2023

Script Type: Manager


Generate Google Ads headlines using Chat GPT

Writing new Google ads copy is no-ones favourite task. This script will take your exisitng headlines and use chat GPT to generate new ideas for you. These will be printed in the console upon completion.

This script will only use headlines from responsive search ads that have had > 10 clicks in the last 30 days.

You will need your own Open AI API developer token in order to use this script.

function main() {
  // API credentials and token usage
  const apiKey = ``; //Add api key here
  const maxTokens = 1000; // Max number of API tokens to use

  const headlineSuggestions = 10;
  const systemMessage = `You are a copywriter and need to produce new Google Ads headlines to A/B test. Write '${headlineSuggestions}' new headlines using our current headlines as a guide. These should be a maximum of 30 characters long. Return a numbered list.`;

  const accountIDs = ["CID1", "CID2"]; // comma separated list of accounts

  //** Do not change anything below **//
  let accountIterator = MccApp.accounts()
    .withIds(accountIDs) // comma seperated list of accounts

  while (accountIterator.hasNext()) {
    let allHeadlines = [];
    let account =;;
    let accountName = account.getName();

    let query = `SELECT FROM ad_group_ad WHERE = 'RESPONSIVE_SEARCH_AD' AND metrics.clicks > 10 AND DURING LAST_30_DAYS`;
    let report =;
    let rows = report.rows();

    while (rows.hasNext()) {
      let row =;
      let headlines = row[""];

      headlines.forEach((e) => {
        let headline = e.text;

        allHeadlines.indexOf(headline) === -1
          ? allHeadlines.push(headline)
          : "";

    let urlString = allHeadlines.join(",");

    let json = {
      model: "gpt-3.5-turbo",
      messages: [
        { role: "system", content: systemMessage },
        { role: "user", content: "" },
      temperature: 0,
      max_tokens: maxTokens,

    json.messages[1].content = urlString;

    let authHeader = `Bearer ${apiKey}`;
    const API_URL = "";

    let options = {
      method: "POST",
      headers: { Authorization: authHeader },
      contentType: "application/json",
      payload: JSON.stringify(json),

    let response = UrlFetchApp.fetch(API_URL, options);
    let parsedResponse = JSON.parse(response.getContentText());
    let choices = parsedResponse.choices;

    console.log(`Headlines generated for account: ${accountName}`);

Share Article


Generate Google Ads Headlines Using Chat GPT

Aug 21, 2023

A free Google Ads sctipt to generate new headlines from your existing ad copy using chat GPT.


Google Ads Script - Check for Search Term Changes

Jan 17, 2023

A manager level Google Ads script to check for changes in spend and cost per click for search terms.


Google Ads Script - Check for Disapproved Ads

Nov 19, 2022

A manager level Google Ads script to check for disapproved ads in active campaigns across your accounts.

Ready to grow your business?

Book a free consultation with the team at Digital Expanse and let’s discuss how we can help you achieve your goals.