In this article we will discuss how to use a Cloudflare worker to respond to a Ghost webhook and push subscriber data to Mailchimp.
Motivation for coding solution is that with the new Ghost blog content marketing membership features simply embedding the Mailchimp subscription form into the theme is no longer an option.
Read on if you have a healthy "developer" interest in how this works.
First, what is a Ghost webhook, a Cloudflare worker, what do they have to do with subscriber data in Mailchimp, and why do we care?
All of this is important because it helps to provide a responsive, in the moment, email marketing automation experience.
Subscription process Flow
The following diagram shows the components in the process flow of a subscription.
Notes from the subscription process flow
- A subscriber is a visitor to the website that registers for the newsletter.
- Ghost (website) callback to start Cloudflare worker is triggered when a visitor registers for newsletter.
- Add subscriber to Mailchimp is what the Cloudflare worker does.
- The result is that visitor subscribing receives a welcome email and will get newsletters as they are published. Note that Mailchimp was set up to send welcome and newsletter campaigns separately.
- Results in marketing automation happening in-the-moment when the person visiting is most interested and removes the need for manual time-consuming copy/pasting of subscriber details from website to email marketing platform.
Code required for the Cloudflare worker
- Comment (1) sets up KV in global name space.
- Comment (2) handles what worker should do when it first starts, in this case start
- Comments (3) and (4) are functions used by worker.
- Comments (5) through to (12) are for main
handleRequestfunction and logic.
In a production environment additional logic would be required to handle errors, production logging and scenarios where subscriber has previously registered.
Cloudflare worker implementation steps
- Set up a Cloudflare account with an unlimited plan so that workers can use KV (key-value storage).
- Visit the
workers/kv/namespaceson your Cloudflare dash and setup a namespace for variables.
- Register for logdna (or a similar logging service) which will be required whilst in development for testing the worker function, add
logdna ingestion keyand other logdna variables to your
- Create a Mailchimp account, add
Mailchimp API Keyand
Mailchimp ListIDto your
- Publish worker to Cloudflare. When worker is published to Cloudflare make a note of URL.
Ghost (website) implementation steps
- On Ghost setup new integration.
- Add to new integration callback so that when a new subscriber is add the Cloudflare worker is triggered. Callback will trigger the Cloudflare worker URL.
Most of the articles on websitePlus answer questions from a small business digital marketing perspective on how to do digital marketing. This was one of my personal more "technical" questions that I felt would be of general interest.
This article is intended to be more of an overview. Reach out if you would like to do more of a deep dive into any of the concepts discussed.
To test this out, subscribe to website.