I couldn’t resist the hype surrounding ChatGPT, so I’ve decided to jump in on the trend and see how it could be used by marketers in Salesforce Marketing Cloud.
ChatGPT claims that some of the common use cases for generative AI in email marketing are:
- Personalization: ChatGPT can be trained on customer data, such as purchase history, browsing behavior, and demographic information, to generate personalized messages that resonate with each recipient. Marketers can use this information to create more targeted and relevant email content, such as product recommendations or special offers.
- Content generation: ChatGPT can be used to generate content for email campaigns, such as subject lines, email copy, and calls to action. By using a language model like ChatGPT, marketers can save time and resources on content creation while still delivering high-quality messages.
- Chatbots: Marketers can use ChatGPT to create chatbots that can interact with customers in real-time through email. Chatbots can be used to answer customer questions, provide product recommendations, and even process orders directly within the email.
- A/B testing: ChatGPT can be used to generate multiple variations of email content, which can then be tested against each other to determine which version performs best. By using a language model to generate these variations, marketers can easily create and test multiple versions of email content without the need for additional resources.
I’ve decided to check how much effort it would take to set up a mix of 1 & 2: an automated email that would generate personalized content for each subscriber on the fly, based on an attribute such as the subscriber’s interests – and to my surprise, it turned out to be fairly easy. Think of it as dynamic content with AI-generated text. Let’s get to it!
Get an OpenAI API Key
The only prerequisite you need to fulfill to set up a ChatGPT integration is obtaining an API key from the OpenAI API website:
- Go to the OpenAI API website: https://openai.com/api/
- Click on the “Get API Key” button
- Sign up for an OpenAI account (if you haven’t already)
- Choose a plan that best fits your needs (there is a free plan which I have been using for testing purposes)
- Follow the instructions to generate an API key
The Chat Completion API endpoint
In order to generate personalized content for each subscriber, we will use the Chat Completion API endpoint. It has many parameters that you can use to fine-tune the responses, but the basic payload you need to send to the API endpoint has two parameters, the AI model to be used and details of the content you’d like it to generate:
My knowledge of AI models is very limited, so I used the default one and skipped the remaining settings, but one thing that you need to keep in mind is the usage of tokens, which are ChatGPT’s “currency”, just like super messages in Marketing Cloud: tokens are essentially access codes that allow you to make API calls to the GPT model. When you sign up for OpenAI’s API service, you will be assigned a certain number of tokens based on the plan you choose. Each API call to the GPT model consumes a certain number of tokens, which are deducted from your account. The number of tokens consumed per API call can vary depending on the length and complexity of the text generated by the model. You can control the usage of tokens by adding an additional parameter to your payload, specifying the maximum number of tokens to be used to generate the chat completion, eg.
Embed the API call in the email body
Let’s start with a disclaimer – do not try to use this for bulk sends. The way I’ve set it up, each email sent will have unique content, but it also means that each email will make a separate API call to ChatGPT. Therefore, each email sent will take more time than it would to send out a generic email, and it will cost you “tokens”.
You can paste the below SSJS directly into a Marketing Cloud email. Note, that you will need to change the following to make it work:
- In line 5, link the interest variable to the data/attributes you want to use as the basis for personalizing content
- In line 9, add your API key
- In line 11, add your prompt
To see how it works in practice, visit the CloudPage I created here. Once you fill in the form, it will send you an email with AI-generated content based on your interest.
While the technical aspects of using ChatGTP API to dynamically generate email content are relatively simple, the true difficulty lies in crafting effective prompts that lead to meaningful output and understanding the various AI models and settings available for use in API calls. Other factors to take into account include performance, cost, and legal considerations. In conclusion, I think it’s best to wait until Salesforce’s native generative AI, Einstein GPT, becomes available for use with Marketing Cloud.
Leave a comment below or email me at firstname.lastname@example.org.
6 thoughts on “ChatGPT and Marketing Cloud Integration: Real-Time Email Content Generation with ChatGPT API”
My name is Leny, I work with MC for quite some time and I follow you page etc..
Thank you so much for this content. Really great.
However, did you really manage to make it work inside email preview? If so, may I ask how? Cause I am doing that, and in the cloud pages, it works well, but in the email, it doesn’t. It shows the message Unable to generate preview.
Do you know any specific reason to not work inside email? At least in my case?
Hi Leny, yes, it works in an email just fine – I am not sure why it would fail, but I did notice that if you’re on the free ChatGPT plan, it’s not always 100% reliable. Can you please try again, or try sending a test email to see if it works that way?
I guess the issue may be then for using the free plan. Cause I also did a test send, and it did not work
Can you email me your whole script to email@example.com and I’ll have a look?
I’ll share tomorrow! Thanks
Regards Leny ________________________________
an SSJS into an email is a nice quick solution but at scale it is risky.
I see it performing better if you make it run in a Journey with a custom activity that stores Chat GPT results into a Data Extension and then uses that Data in the Email send step in the journey.
The same implementation is possible using Automation Studio with an SSJS step to prepare the user data but keep in mind you risk running into a timeout error if you have a lot of data to be sent to Open AI.