Queries Library
Queries LibraryRegister a newsletter subscriber from InstaWP to Mailchimp

Register a newsletter subscriber from InstaWP to Mailchimp

InstaWP allows visitors to launch a sandbox site to test a plugin, while allowing them to subscribe to the newsletter:

Launching a testing site in InstaWP to test Gato GraphQL
Launching a testing site in InstaWP to test Gato GraphQL

We can capture these emails by providing a webhook to InstaWP, under the template's "Advanced Options" tab:

Advanced options for InstaWP templates
Advanced options for InstaWP templates

The webhook will be a persisted query with the query below. This query captures the email from the visitors, and subscribes this email to a Mailchimp list.

query HasSubscribedToNewsletter {
  hasSubscriberOptin: _httpRequestHasParam(name: "marketing_optin")
  subscriberOptin: _httpRequestStringParam(name: "marketing_optin")
  isNotSubscriberOptinNAValue: _notEquals(value1: $__subscriberOptin, value2: "NA")
  subscribedToNewsletter: _and(values: [$__hasSubscriberOptin, $__isNotSubscriberOptinNAValue])
    @export(as: "subscribedToNewsletter")
}
 
query RegisterNewsletterSubscribeFromInstaWPToMailchimpList(
  # mailchimpDataCenterCode: Code for the data center of your account on Mailchimp (See: https://mailchimp.com/developer/marketing/docs/fundamentals/#api-structure)
  $mailchimpDataCenterCode: String!
  # Audience ID for the list on Mailchimp to which to subscribe the email
  $mailchimpAudienceID: String!
)
   @depends(on: "HasSubscribedToNewsletter")
   @include(if: $subscribedToNewsletter)
{
  subscriberEmail: _httpRequestStringParam(name: "email")
  
  mailchimpUsername: _env(name: "MAILCHIMP_API_CREDENTIALS_USERNAME")
    @remove
  mailchimpPassword: _env(name: "MAILCHIMP_API_CREDENTIALS_PASSWORD")
    @remove
  
  mailchimpAPIEndpoint: _sprintf(
    string: "https://%s.api.mailchimp.com/3.0/lists/%s/members",
    values: [$mailchimpDataCenterCode, $mailchimpAudienceID]
  )
  
  mailchimpListMembersJSONObject: _sendJSONObjectItemHTTPRequest(input: {
    url: $__mailchimpAPIEndpoint,
    method: POST,
    options: {
      auth: {
        username: $__mailchimpUsername,
        password: $__mailchimpPassword
      },
      json: {
        email_address: $__subscriberEmail,
        status: "subscribed"
      }
    }
  })
}

And define in wp-config.php:

define( 'MAILCHIMP_API_CREDENTIALS_USERNAME', '{ username }' );
define( 'MAILCHIMP_API_CREDENTIALS_PASSWORD', '{ password }' );

Using this webhook, when creating a new testing site on InstaWP, and the user signs up to the newsletter, the visitor's email is automatically added to the Mailchimp list:

Email automatically added to Mailchimp list
Email automatically added to Mailchimp list