Track form events with Google Analytics

The plugin makes it easy for you to interact with the various form events and run a piece of code, like tracking the event with Google Analytics.

Say you want to send an event to Google Analytics every time someone used a form to subscribe? You can do that by adding the following snippet to the bottom of your form mark-up.

Use the following code if you are using gtag.js

<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>

Use the following code if you are using analytics.js

<script type="text/javascript">
mc4wp.forms.on('subscribed', function(form) {
	// analytics.js
	ga && ga('send', 'event', 'Forms', 'Sign-up', 'Name: ' + form.name + ' ID: ' + form.id);
});
</script>

Use the following code if you are using Google Tag Manager.

<script type="text/javascript">
mc4wp.forms.on('subscribed', function(form) {
	// GTM Data Layer push
	window.dataLayer = window.dataLayer || []; window.dataLayer.push({ 'event' : 'Sign-up', 'name' : form.name + ' ID: ' + form.id });
});
</script>

Don’t forget to create a Trigger (custom event with name “Sign-up”) and tag in Google Tag Manager!

The code above will only send the event to Google Analytics if the sign-up request actually succeeded, so failed attempts will not be tracked.

It can take up to 24 hours before results show up in Google Analytics reports.