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?

Webhooks in Ghost can be used to start marketing automations. In Ghost webhooks can be triggered on events like a site, post, tag or subscriber change to make HTTP requests. To read more about available Ghost options visit webhooks in Ghost. Cloudflare workers can respond to HTTP requests, can return results and can in turn make subsequent requests to services like Mailchimp's API. Why use a Cloudflare worked and not host a hosted service to do this? A Cloudflare worker lets you run JavaScript functions on a server without the need/overhead/cost for a container to host the application in, also Cloudflare workers are very, very fast.

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.

Process flow diagram showing visitor, website, worker and Mailchimp sequence.

Notes from the subscription process flow

  1. A subscriber is a visitor to the website that registers for the newsletter.
  2. Ghost (website) callback to start Cloudflare worker is triggered when a visitor registers for newsletter.
  3. Add subscriber to Mailchimp is what the Cloudflare worker does.
  4. 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.
  5. 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

For code visit gist showing JavaScript required for the Cloudflare worker. Code is self-explanatory with comments for key steps in the process.

In summary:

  • Comment (1) sets up KV in global name space.
  • Comment (2) handles what worker should do when it first starts, in this case start handleRequest function.
  • Comments (3) and (4) are functions used by worker.
  • Comments (5) through to (12) are for main handleRequest function 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

  1. Set up a Cloudflare account with an unlimited plan so that workers can use KV (key-value storage).
  2. Visit the workers/kv/namespaceson your Cloudflare dash and setup a namespace for variables.
  3. Register for logdna (or a similar logging service) which will be required whilst in development for testing the worker function, add logdna ingestion key and other logdna variables to your workers/kv/namespace.
  4. Create a Mailchimp account, add Mailchimp API Key and Mailchimp ListID to your workers/kv/namespace.
  5. Publish worker to Cloudflare. When worker is published to Cloudflare make a note of URL.

Ghost (website) implementation steps

  1. On Ghost setup new integration.
  2. 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.

Wrapping up

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.

Matt Cameron

Matt Cameron

Reach out with questions or for assistance with digital marketing ideation, creating, developing, doing, coaching and planning.