A Google Ads script that uses GPT to write RSAs

Posted On 14 Apr 2023
Comment: Off

This script can help you leverage GPT’s API to use the maximum number of RSA assets and, in turn, boost your paid search campaigns.

Google Ads scripts have long been one of my favorites because they are completely customizable, scale reasonably well, and are included for free with any Google Ads account.

But there’s a new kid on the block – GPT. Could we combine generative AI with Google Ads scripts?

That was the question I set out to answer, and the result is my first Google Ads script that uses GPT.

It identifies opportunities for responsive search ads (RSAs) and uses GPT to help generate additional creative assets to capitalize on the opportunity.

You can grab the script code at the end of this article and try it on your own account.

Why you should use the maximum number of assets for RSAs

Responsive search ads (RSAs) are a type of ad format on Google Ads that allow advertisers to create multiple headlines and descriptions for a single ad.

Google then uses machine learning to test different combinations of headlines and descriptions to determine which ones perform best for different search queries and user contexts.

This helps improve ad relevance and performance and allows advertisers to reach a wider audience.

We found that RSAs drive 4X the impressions of expanded text ads, and ads with more headline variants got more impressions per ad than those with fewer variants.

RSA impressions

Google allows advertisers to submit 15 versions of their headline and four versions of their description for every RSA.

While that doesn’t mean that 43,000 variations of your assets will gather equal impressions, it’s always a good idea to feed the machine the maximum allowed number of assets so that its algorithms can show your ads to every interested user.

But let’s face it, writing 15 great headlines and four long descriptions for every ad group in your account can get tedious. So it’s no surprise that many advertisers have gaps in their RSAs.

PPC management tools make it easy to find and fix this issue. But there are also ways to do this at scale without paying for additional software.

Google Ads scripts are among the best free tools for bulk operations on ad accounts.

Last year, I wrote a script with Matt Umbro, a long-time PPC thought leader and founder of #ppcchat on Twitter, to create a list of RSA ads with missing assets.

But that was before ChatGPT raised public awareness of generative AI. So I decided to see if I could combine scripts and GPT’s API to improve an ad account.

Using the script

The script creates a spreadsheet with one RSA on every row and column for every headline and description asset.

Optmyzr Google Ads - RSA script - spreadsheet screenshot

When an RSA is not using the maximum number of variations allowed by Google, it calls the GPT API to suggest additional ad text variations.

These AI-generated suggestions are then placed in the spreadsheet to be bulk-uploaded back to Google to easily create the missing assets.

To make it easy to see what was generated by GPT, those cells are automatically colored green.

Optmyzr Google Ads - GPT-generated RSA assets

In order to run this script, you will need to get an API key from OpenAI’s website and add the key to the script around line 39 where it says:

var OPEN_AI_API_KEY = ''; // get your own API key at https://platform.openai.com/account/api-keys 

You can run the script in preview mode and look at the logs to get the URL of the new spreadsheet.

Script preview

Every time the script runs, it will use the OpenAI API. This costs money, so do not put this script on an automatic schedule.

Download the script

Grab a copy of the code here:

/******************************************
* RSA Report
* @version: 3.0
* @authors: Naman Jindal (Optmyzr), Frederick Vallaeys (Optmyzr)
* ——————————-
* Install this script in your Google Ads account (not an MCC account)
* to generate a Google Sheet with a list of all your responsive search ads
* and their headlines and descriptions.
* For RSAs that are not using the maximum number of allowed variations,
* this script will suggest new variations for headlines and descriptions
* using the OpenAI GPT API.
* The resulting sheet can be bulk uploaded back into Google Ads.
* ——————————–
* For more PPC tools, visit www.optmyzr.com.
******************************************/
// If Blank script will create a new Google sheet everytime it runs.
var SS_URL = ”;
// Name of the tab in the Google sheet.
var TAB_NAME = ‘RSA’;
// Flag to decide if the script checks only ads in active campaigns and active ad groups
var INCLUDE_PAUSED = false;
// only include ads with this many or fewer headlines on the output spreadsheet (defaults to 15)
var MAX_HEADLINES = 15;
// only include ads with this many or fewer descriptions on the output spreadsheet (defaults to 4)
var MAX_DESCRIPTIONS = 4;
// Multiple emails can be added sepearated by comma (,)
// Used for access to spreadsheet and for sending email
var EMAIL = ”;
// Set to true if you want to recieve the report on Email.
var SEND_EMAIL = false;
var OPEN_AI_API_KEY = ”; // get your own API key at https://platform.openai.com/account/api-keys
var GPT_MODEL = ‘gpt-3.5-turbo’;
// Do not edit anything below this line
function main() {
var output = [[
‘Account ID’, ‘Account Name’, ‘Campaign’, ‘Ad Group’, ‘Ad ID’, ‘# Headlines’, ‘# Descriptions’, ‘Ad Strength’,
‘Headline 1’, ‘Headline 2’, ‘Headline 3’, ‘Headline 4’, ‘Headline 5’, ‘Headline 6’, ‘Headline 7’, ‘Headline 8’, ‘Headline 9’,
‘Headline 10’, ‘Headline 11’, ‘Headline 12’, ‘Headline 13’, ‘Headline 14’, ‘Headline 15’,
‘Description Line 1’, ‘Description Line 2’, ‘Description Line 3’, ‘Description Line 4’
]];
var columCount = output[0].length;
var backgroupHeader = [];
while(backgroupHeader.length < columCount) {
backgroupHeader.push(‘#ffffff’);
}
var backgrounds = [backgroupHeader];
var accId = AdsApp.currentAccount().getCustomerId(),
accName = AdsApp.currentAccount().getName();
var query = [
‘SELECT campaign.name, ad_group.name, ad_group_ad.ad.id, ad_group_ad.ad_strength,’,
‘ad_group_ad.ad.responsive_search_ad.headlines, ad_group_ad.ad.responsive_search_ad.descriptions’,
‘FROM ad_group_ad WHERE ad_group_ad.ad.type = RESPONSIVE_SEARCH_AD AND metrics.impressions >= 0’,
INCLUDE_PAUSED ? ” : ‘AND ad_group_ad.status = ENABLED AND campaign.status = ENABLED and ad_group.status = ENABLED’,
‘AND segments.date DURING LAST_7_DAYS’
].join(‘ ‘);
var rows = AdsApp.report(query).rows();
while(rows.hasNext()) {
var row = rows.next();
var headlines = row[‘ad_group_ad.ad.responsive_search_ad.headlines’];
var headlineCount = headlines.length;
var descriptions = row[‘ad_group_ad.ad.responsive_search_ad.descriptions’];
var descriptionCount = descriptions.length;
if(headlineCount > MAX_HEADLINES || descriptionCount > MAX_DESCRIPTIONS) { continue; }
var out = [
accId, accName, row[‘campaign.name’], row[‘ad_group.name’], row[‘ad_group_ad.ad.id’],
headlineCount, descriptionCount, row[‘ad_group_ad.ad_strength’]
];
var bgRow = [‘#ffffff’, ‘#ffffff’, ‘#ffffff’, ‘#ffffff’, ‘#ffffff’, ‘#ffffff’, ‘#ffffff’, ‘#ffffff’];
var headlinesText = [];
for(var z in headlines) {
headlinesText.push(headlines[z].text);
bgRow.push(‘#ffffff’);
}
var diff = 15 – headlinesText.length;
var autoHeadlines = [];
if(diff > 0) {
autoHeadlines = generateTextOpenAI(‘Find ‘+diff+’ more ad headlines under 30 characters that are similar to these:\n’, headlinesText);
}
if(autoHeadlines.length) {
headlinesText = headlinesText.concat(autoHeadlines);
for(var i = 0; i < autoHeadlines.length; i++) {
bgRow.push(‘#d9ead3’);
}
}
while(headlinesText.length < 15) {
headlinesText.push(”);
bgRow.push(‘#fffff’)
}
while(headlinesText.length > 15) {
headlinesText.pop();
bgRow.pop();
}
var descriptionsText = [];
for(var z in descriptions) {
descriptionsText.push(descriptions[z].text);
bgRow.push(‘#ffffff’);
}
var diff = 4 – descriptions.length;
var autoDescriptions = [];
if(diff > 0) {
autoDescriptions = generateTextOpenAI(‘Find ‘+diff+’ more ad descriptions under 90 characters that are similar to these:\n’, descriptionsText);
}
if(autoDescriptions.length) {
descriptionsText = descriptionsText.concat(autoDescriptions);
for(var i = 0; i < autoDescriptions.length; i++) {
bgRow.push(‘#d9ead3’);
}
}
while(descriptionsText.length < 4) {
descriptionsText.push(”);
bgRow.push(‘#ffffff’);
}
while(descriptionsText.length > 4) {
descriptionsText.pop();
bgRow.pop();
}
out = out.concat(headlinesText).concat(descriptionsText);
backgrounds.push(bgRow);
output.push(out);
}
if(!SS_URL) {
var ss = SpreadsheetApp.create(accName + ‘: RSA Report’);
SS_URL = ss.getUrl();
if(EMAIL) {
ss.addEditors(EMAIL.split(‘,’));
}
}
Logger.log(‘Report URL: ‘ + SS_URL);
var ss = SpreadsheetApp.openByUrl(SS_URL);
var tab = ss.getSheetByName(TAB_NAME);
if(!tab) {
tab = ss.getSheetByName(‘Sheet1’);
if(!tab) {
tab = ss.insertSheet(TAB_NAME);
} else {
tab.setName(TAB_NAME)
}
}
tab.clearContents();
tab.setFrozenRows(1);
tab.getRange(1,1,output.length,output[0].length).setValues(output).setBackgrounds(backgrounds).setFontFamily(‘Calibri’);
if(EMAIL && SEND_EMAIL) {
MailApp.sendEmail(EMAIL, accName + ‘ RSA Report is ready’, ‘Report is available at below link:\n’+SS_URL);
}
}
function getGoogleAdsFormattedDate(d, format){
var date = new Date();
date.setDate(date.getDate() – d);
return Utilities.formatDate(date,AdsApp.currentAccount().getTimeZone(),format);
}
function generateTextOpenAI(question, texts) {
//texts.pop();
var prompt = question + texts.join(‘\n’);
var messages= [
{“role”: “user”, “content”: prompt}
];
var payload = {
“model”: GPT_MODEL,
“messages”: messages
};
var httpOptions = {
“method” : “POST”,
“muteHttpExceptions”: true,
“contentType”: “application/json”,
“headers” : {
“Authorization” : ‘Bearer ‘ + OPEN_AI_API_KEY
},
‘payload’: JSON.stringify(payload)
};
//Logger.log(JSON.stringify(payload));
var response = JSON.parse(UrlFetchApp.fetch(‘https://api.openai.com/v1/chat/completions’, httpOptions));
var choices = response[‘choices’];
var texts = [];
if(choices[0] && choices[0][‘message’]) {
var output = choices[0][‘message’][‘content’].split(‘\n’);
for(var z in output) {
if(!output[z].trim()) { continue; }
var parts = output[z].split(‘. ‘);
if(parts.length > 1) {
parts.shift();
}
texts.push(parts.join(‘. ‘));
}
}
return texts;
}

If you’re new to Google Ads Scripts, you can follow the instructions here to set it up for the first time.

Limitations of GPT for Google Ads

While GPT is great at writing, it’s notoriously bad at counting. I asked it why, and here’s what it explained:

“As an AI language model, GPT (Generative Pre-trained Transformer) is not inherently bad at math. However, it may not perform as well as specialized math models because it was not specifically trained on mathematical tasks. GPT is primarily designed for natural language processing, which involves understanding and generating human language. While it can perform basic mathematical operations, it may struggle with more complex calculations.”

For our PPC use case, you’ll have to double-check the length of assets it suggests to ensure they fit in Google’s character limits.

When I asked GPT to add character counts next to each suggested headline, you can see it usually comes close but gets it wrong:

GPT character counts

Experience has taught me that GPT tends to follow patterns found in the prompt when completing a task.

This means it’s more likely to write headlines of an appropriate length when working with a list of existing headlines like the ones you currently have in your account.

Hence, the script will work best for completing RSAs that are missing just a few elements rather than most or all elements.

As you can see in the earlier screenshot, it also sometimes tends to give numbered lists of suggestions, and that doesn’t make for great ads when each of your headlines starts with ‘1),’ ‘2),’ etc.

I would not let GPT auto-generate ads because I’m hesitant to let some of Google’s suggestions go on automation.

When I accidentally turned on automatically applied recommendations for redundant keywords, it removed my brand keyword, Optmyzr, from my account.

Take GPT’s work as a suggestion to help speed up generating new ad variants.

Combining Google Ads with generative AI

GPT is one of the most exciting new technologies since the invention of the internet.

It’s mind-blowing how well it understands questions and how confident (though not always accurate) it is in its responses.

I am excited to have another fantastic tool in my PPC toolkit to help my ads outperform the competition.

I hope this script gives you a taste of what is possible when you combine Google Ads with generative AI like GPT.

About the Author