Correlate user data from WordPress and Mailchimp

This query combines the user data from the local WordPress site, and data from those same users from Mailchimp, using the user's email as the common identificator between the two systems.

The result will be the email, name and lastname user data coming from WordPress, augmented with field location from Mailchimp.

query FetchUserDataFromMailchimpList(
# 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!
) {
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]
)
@remove

mailchimpListMembersJSONObject: _sendJSONObjectItemHTTPRequest(input: {
url: $__mailchimpAPIEndpoint,
method: GET,
options: {
auth: {
username: $__mailchimpUsername,
password: $__mailchimpPassword
}
}
})
@remove
mailchimpUserData: _objectProperty(
object: $__mailchimpListMembersJSONObject,
by: { key: "members"}
)
@underEachArrayItem
@objectKeepProperties(keys: ["email_address", "location"])
@export(as: "mailchimpUserData")
}

query FetchUserDataFromWordPress {
users(pagination: { limit: -1 }) {
id
email_address: email
name
lastName
@export(
as: "wordpressUserData",
type: LIST,
affectAdditionalFieldsUnderPos: [1, 2, 3]
)
}
}

query CombineUserDataFromMailchimpAndWordPress
@depends(on: [
"FetchUserDataFromMailchimpList",
"FetchUserDataFromWordPress"
])
{
combinedWordPressAndMailchimpUserData: _arrayInnerJoinJSONObjectProperties(
source: $mailchimpUserData,
target: $wordpressUserData,
index: "email_address"
)
}

And define in wp-config.php:

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

Extensions required for this query permalink

  1. Field Response Removal permalink

    Remove the output of a field from the response.

  2. Field To Input permalink

    Retrieve the value of a field, manipulate it, and input it into another field or directive, all within the same operation.

  3. HTTP Client permalink

    Addition of fields to execute HTTP requests against a webserver and fetch their response.

  4. Multiple Query Execution permalink

    Combine multiple queries into a single query, sharing state across them and executing them in the requested order.

  5. PHP Constants and Environment Variables via Schema permalink

    Query the value from an environment variable or PHP constant.

Bundles containing all extensions required for this query permalink

  1. “All in One Toolbox for WordPress” Bundle permalink

    Achieve all superpowers: All of Gato GraphQL extensions, in a single plugin

  2. “Automated Content Translation & Sync for WordPress Multisite” Bundle permalink

    Automatically create a translation of a newly-published post using the Google Translate API, for every language site on a WordPress multisite

  3. “Better WordPress Webhooks” Bundle permalink

    Easily create webhooks to process incoming data from any source or service using advanced tools, directly within the wp-admin

  4. “Private GraphQL Server for WordPress” Bundle permalink

    Use GraphQL to power your application (blocks, themes and plugins), internally fetching data without exposing a public endpoint

  5. “Selective Content Import, Export & Sync for WordPress” Bundle permalink

    Import hundreds of records into your WordPress site from another site or service (such as Google Sheets), and selectively export entries to another site

  6. “Tailored WordPress Automator” Bundle permalink

    Create workflows to automate tasks (to transform data, automatically caption images, send notifications, and more)

  7. “Versatile WordPress Request API” Bundle permalink

    Interact with any external API and cloud service, posting and fetching data to/from them

Tutorial lessons explaining how the query works permalink