EXPERT GOOGLE-ADS-SCRIPTS

Generate Google Ads Headlines Using Chat GPT

Published: August 21, 2023

Script Type: Manager

blog-header-image

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
    .get();

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

    let query = `SELECT ad_group_ad.ad.responsive_search_ad.headlines FROM ad_group_ad WHERE ad_group_ad.ad.type = 'RESPONSIVE_SEARCH_AD' AND metrics.clicks > 10 AND segments.date DURING LAST_30_DAYS`;
    let report = AdsApp.report(query);
    let rows = report.rows();

    while (rows.hasNext()) {
      let row = rows.next();
      let headlines = row["ad_group_ad.ad.responsive_search_ad.headlines"];

      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 = "https://api.openai.com/v1/chat/completions"

    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}`)
    console.log(choices[0].message.content)

  }
}

Share Article

blog-hero

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.

blog-hero

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.

blog-hero

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.