Mailchimp for WordPress triggers various JavaScript events whenever your visitors interact with a sign-up form on your site. These events can be used to run custom code, for example to track form sign-ups with Google Analytics.

You can include this code directly in the form editor if you like. The mc4wp reference is only registered on pages where our form is inserted via a shortcode or widget.

Available form events

The following event names are available.

started fired once user starts filling in a form
submitted fired once user submits form
error fired if form is submitted with errors
success fired if form is submitted successfully
subscribed fired if form is used to subscribe
unsubscribed fired if form is used to unsubscribe
updated_subscriber fired if form was used to update an existing subscriber

Hooking into a form event

The following code hooks into the “success” event. This example simply writes something to the browser’s console, but you can run whatever code you want in the function callback.

<script type="text/javascript">
mc4wp.forms.on('subscribed', function(form) {
  // gtag.js
  gtag('event', 'Sign-up', {'event_category': 'Mailchimp', 'event_label': 'Name: ' + form.name + ' ID: ' + form.id});
});
</script>

If your form is set to update existing subscribers the third function parameter will be set to true if the submitted email did already exist in your audience.

<script type="text/javascript">
mc4wp.forms.on('subscribed', function(form, data, updated) {
  if(!updated) {
    console.log('This is new subscriber');
  } else {
    console.log('This is an updated subscriber');
  }
})
</script>

Hooking into one specific form

The above code will trigger whenever any form on your website is successfully submitted. If you want to limit this to a certain form, prefix the event name with the numerical form ID followed by a dot.

<script>
mc4wp.forms.on('15.success', function(form, data) {
   // your code goes here
   console.log("Form with ID 15 successfully submitted.");
});
</script>