Use the code below for Google Analytics event tracking with Freshchat:


Code Snippet:


<script>
  window.fcSettings = {
    token: "WEB_CHAT_TOKEN",
    host: "https://wchat.freshchat.com",
    onInit: function() {
      if (typeof(ga) === "function") {
        ga('create', '<GA TRACKING ID>', 'auto', 'FreshchatEvents');
        window.fcWidget.on("widget:loaded", function(resp) {
          window.fcWidget.on("widget:opened", function(resp) {
            ga('FreshchatEvents.send', 'event', 'Freshchat widget', 'GadgetGod', 'Widget Open');
          });
          window.fcWidget.on("widget:closed", function(resp) {
            ga('FreshchatEvents.send', 'event', 'Freshchat widget', 'GadgetGod', 'Widget Close');
          });
          window.fcWidget.on('message:received', function(resp) {
            ga('FreshchatEvents.send', 'event', 'Freshchat widget', 'GadgetGod', 'Received');
          });
          window.fcWidget.on('message:sent', function(resp) {
            ga('FreshchatEvents.send', 'event', 'Freshchat widget', 'GadgetGod', 'Sent');
          });
          window.fcWidget.on('user:statechange', function(resp) {
            if (resp && resp.data && resp.data.userState === "created") {
              ga('FreshchatEvents.send', 'event', 'Freshchat widget', 'GadgetGod', 'User Create');
            }
            if (resp && resp.data && resp.data.userState === "loaded") {
              ga('FreshchatEvents.send', 'event', 'Freshchat widget', 'GadgetGod', 'User Loaded');
            }
            if (resp && resp.data && resp.data.userState === "identified") {
              ga('FreshchatEvents.send', 'event', 'Freshchat widget', 'GadgetGod', 'User Identified');
            }
            if (resp && resp.data && resp.data.userState === "restored") {
              ga('FreshchatEvents.send', 'event', 'Freshchat widget', 'GadgetGod', 'User Restored');
            }
          });
        });
      }
      else {
        console.log('Google Analytics not loaded');
      }
    }
  };
</script>
<script src="https://wchat.freshchat.com/js/widget.js" async></script></span></span>


The above code will load the Freshchat Widget, and only if Google Analytics code is also enabled it will track the following events:

1. Widget Open
2. Widget Closed
3. Messages Sent
4. Messages Received
5. User Create
6. User Loaded
7. User Identified
8. User Restored

You only need to replace two parameters in the script - your Web Chat Token and Google Analytics Tracking ID.


Here's an image for your reference where you need to update your Web Chat Token and Google Analytics Tracking ID in the above code snippet.