Skip to content

Instantly share code, notes, and snippets.

@cristacheda
Last active January 27, 2022 14:47
Show Gist options
  • Save cristacheda/de0667512e84480cc0000a10752de446 to your computer and use it in GitHub Desktop.
Save cristacheda/de0667512e84480cc0000a10752de446 to your computer and use it in GitHub Desktop.
Track Tawk.to chats, send events to Google Analytics, gtag.js implementation
<!--Start of Tawk.to Script-->
<script type="text/javascript">
// Load Tawk.to script
var Tawk_API=Tawk_API||{}, Tawk_LoadStart=new Date();
(function(){
var s1=document.createElement("script"),s0=document.getElementsByTagName("script")[0];
s1.async=true;
s1.src="https://embed.tawk.to/REPLACE-WITH-YOUR-TAWK-TO-ID/default";
s1.charset="UTF-8";
s1.setAttribute("crossorigin","*");
s0.parentNode.insertBefore(s1,s0);
})();
//Track chat starting event
Tawk_API.onChatStarted = function(){
gtag('event', 'chatstarted', {
'event_category': 'tawkto',
});
};
//Track chat ending event
Tawk_API.onChatEnded = function(){
gtag('event', 'chatended', {
'event_category': 'tawkto',
});
};
//Track offline message
Tawk_API.onOfflineSubmit = function(data){
gtag('event', 'offlinechat', {
'event_category': 'tawkto',
});
};
//More tawk.to events triggered by the tawk.to API https://www.tawk.to/javascript-api/
//Google Analytics gtag.js send event documentation https://developers.google.com/analytics/devguides/collection/gtagjs/events
</script>
<!--End of Tawk.to Script-->
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment