Sometimes, we put Facebook Chat on our website so users can chat with us. If we want to see how users interact with it, we can use Google Analytics. But it’s tricky because Facebook Chat comes in an iframe, which makes it hard to track with Google Tag Manager and Google Analytics.
To solve this problem, I’ve created a tool that can track Facebook Chat using GTM and GA4.
Facebook Chat GTM DataLayer
<script>
/**
* Author: Md Hasanuzzamna
* Linkedin: https://linkedin.com/in/md-h
* Youtube: https://youtube.com/@leomeasure
* Email: info@leomeasure.com
*/
window.addEventListener('message', function (event) {
if(event.origin.includes('facebook.com') && event.data.includes('%22euit%22%')) {
window.dataLayer = window.dataLayer || [];
dataLayer.push({event: 'fb_chat_click'});
}
});
</script>