JavaScript form events

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 error
success      // fired if form is submitted successfully
subscribed   // fired if form is used to subscribe
unsubscribed // fired if form is used to unsubscribe

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.

mc4wp.forms.on('success', function(form) {
   // your code goes here
   console.log("Form successfully submitted.");

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.

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

Was this article helpful? ·