Reputation: 1032
I'm attempting to track submissions on a embedded MailChimp form. The problem is that no form submit event is fired when the form is submitted. I think this has to do with the JQuery being used to validate the input. Here's the embed code for the form:
<!-- Begin MailChimp Signup Form -->
<link href="//cdn-images.mailchimp.com/embedcode/classic-10_7.css" rel="stylesheet" type="text/css">
<style type="text/css">
#mc_embed_signup{background:#fff; clear:left; font:14px Helvetica,Arial,sans-serif; }
/* Add your own MailChimp form style overrides in your site stylesheet or in this style block.
We recommend moving this block and the preceding CSS link to the HEAD of your HTML file. */
</style>
<div id="mc_embed_signup">
<form action="https://carilionclinic.us16.list-manage.com/subscribe/post?u=#####;id=######" method="post" id="mc-embedded-subscribe-form" name="mc-embedded-subscribe-form" class="validate" target="_blank" novalidate>
<div id="mc_embed_signup_scroll">
<h2>Subscribe to our mailing list</h2>
<div class="indicates-required"><span class="asterisk">*</span> indicates required</div>
<div class="mc-field-group">
<label for="mce-EMAIL">Email Address <span class="asterisk">*</span>
</label>
<input type="email" value="" name="EMAIL" class="required email" id="mce-EMAIL">
</div>
<div class="mc-field-group">
<label for="mce-MMERGE1">first name </label>
<input type="text" value="" name="MMERGE1" class="" id="mce-MMERGE1">
</div>
<div class="mc-field-group">
<label for="mce-MMERGE2">last name </label>
<input type="text" value="" name="MMERGE2" class="" id="mce-MMERGE2">
</div>
<div class="mc-field-group">
<label for="mce-MMERGE3">postal code </label>
<input type="text" value="" name="MMERGE3" class="" id="mce-MMERGE3">
</div>
<div class="mc-field-group">
<label for="mce-MMERGE4">Salutation </label>
<input type="text" value="" name="MMERGE4" class="" id="mce-MMERGE4">
</div>
<div class="mc-field-group">
<label for="mce-MMERGE5">Organization </label>
<input type="text" value="" name="MMERGE5" class="" id="mce-MMERGE5">
</div>
<div id="mce-responses" class="clear">
<div class="response" id="mce-error-response" style="display:none"></div>
<div class="response" id="mce-success-response" style="display:none"></div>
</div> <!-- real people should not fill this in and expect good things - do not remove this or risk form bot signups-->
<div style="position: absolute; left: -5000px;" aria-hidden="true"><input type="text" name="b_137cf6679807eccaef7f6f9d1_ae2345011d" tabindex="-1" value=""></div>
<div class="clear"><input type="submit" value="Subscribe" name="subscribe" id="mc-embedded-subscribe" class="button"></div>
</div>
</form>
</div>
<script type='text/javascript' src='//s3.amazonaws.com/downloads.mailchimp.com/js/mc-validate.js'></script><script type='text/javascript'>(function($) {window.fnames = new Array(); window.ftypes = new Array();fnames[0]='EMAIL';ftypes[0]='email';fnames[1]='MMERGE1';ftypes[1]='text';fnames[2]='MMERGE2';ftypes[2]='text';fnames[3]='MMERGE3';ftypes[3]='text';fnames[4]='MMERGE4';ftypes[4]='text';fnames[5]='MMERGE5';ftypes[5]='text';}(jQuery));var $mcj = jQuery.noConflict(true);</script>
<!--End mc_embed_signup-->
If you inspect mc-validate.js, you'll find that the form uses the JQuery Validation plugin. I believe it may be the issue? I can always track clicks on the submit button, but that's certainly not as accurate as a form submission would be. Is there an alternative method I can use?
Upvotes: 3
Views: 3637
Reputation: 51
The issue with this one is that if a bot or someone clicks the subscribe button, without actually subscribing, it will still fire off your event. What I ended up doing is watching for the success response that the Mailchimp form gives, rather than doing all the validation stuff, as found here.
(function() {
var eventName = 'DOMNodeInserted'; // Look for changes in the element
var el = document.querySelector('#mce-success-response') || document;
var useCapture = true;
el.addEventListener(eventName, function( event ){
window.dataLayer.push({
"event" : "mailchimpSubmission" // push custom event to the data layer.
})
} , useCapture);
})();
Upvotes: 3
Reputation: 646
You can trigger a call to your google analytics or GTM function like this,
$('form#mc-embedded-subscribe-form').submit(function(e) {
//Your event goes here ex. dataLayer.push({'event':'eventname'});
return true;
});
This will trigger a call to track form submissions. Do validations(if required) before triggering the event.
If you need to submit form only if the form is validated, then stop the form submission using e.preventDefault();
inside submit function.
Upvotes: 2