Last active
April 15, 2024 12:44
-
-
Save TimBHowe/93767add564332085b2eb76fe40ac9d5 to your computer and use it in GitHub Desktop.
Mailchimp Popup Tracking
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<script id="mcjs"> | |
!function(c,h,i,m,p){m = c.createElement(h), p = c.getElementsByTagName(h)[0], m.async = 1, m.src = i, p.parentNode.insertBefore(m, p)}(document,"script","https://chimpstatic.com/mcjs-connected/js/users/XXXXX/XXXXX.js"); | |
window.dataLayer = window.dataLayer || []; | |
var mailchimpObserver = new MutationObserver(function(mutations_list) { | |
mutations_list.forEach(function (mutation) { | |
mutation.addedNodes.forEach(function (added_node) { | |
if (added_node.id == 'PopupSignupForm_0') { | |
window.dataLayer.push({ event: 'mailchimp_popup_shown' }); | |
document.querySelector('#PopupSignupForm_0 div.mc-modal iframe').contentDocument.querySelector('form').addEventListener('submit', function (event) { | |
window.dataLayer.push({ event: 'mailchimp_popup_submit' }); | |
}); | |
mailchimpObserver.disconnect(); | |
} | |
}); | |
}); | |
}); | |
mailchimpObserver.observe(document.querySelector("body"), {subtree: false, childList: true }); | |
</script> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hi @TimBHowe! Your code works well, but I wanted some additional functionalities for our tracking setup:
..._shown
event only if the modal was actually visible.f_id
) and attach it to thedataLayer
event.Therefore I needed to create some additional functionality. Maybe this comment is helpful for other people who need to handle these "pesky" dynamic MailChimp forms.
Example
dataLayer
event (result):The code below should be ready for copy&paste, if anyone has the same challenge.