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 Analytics by Yoast plugin.

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

Use the following code if you are using GTM.

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

If you are still using the older ga.js instead of analytics.js you will need to migrate to analytics.js for event tracking to work.

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.

Was this article helpful? ·