Google Analytics - Track Events (Embedded)

Pau Sanchez Updated by Pau Sanchez

This process is only suitable for Embedded landbots. If you're landbot is not embedded in your website, check our guide here

Bot is embedded in a site

When your bot is embedded in one of the four different ways available: Fullpage, Popup, Embed or Livechat (with custom domain).

Google Analytics 4 (GA4)

Important: Do not use the Google Analytics tracking code on the Landbot side (leave the Google Analytics ID field blank on Settings > SEO & Tracking) to make sure it doesn’t duplicate visitors.

You should apply this process when landbot is embedded into your website - using a domain that’s different from “landbot.io”. This process is very useful to track landbot and leads conversions, send events and create goals in Google Analytics with a correct source/medium channel attribution.

Copy tracking code from GA:
  1. You'll need to copy the following snippet to add to your webpage, right after the <head> tag
<script async src="https://www.googletagmanager.com/gtag/js?id=G-XXXX"></script>
<script>
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag('js', new Date());
gtag('config', 'G-XXXX');
</script>
  1. You'll need to change G-XXX in the script (at both places) for your GA4 Measurement ID, you can find it in 'Data Streams' in the Admin section of your Google Analytics account
What to edit in the Site (container):
  1. Paste the snippet in your site in the <head> section

How the code in your site is going to look like with the snippets in these instructions:

<html>

<head>

<script async src="https://www.googletagmanager.com/gtag/js?id=G-XXXX"></script>
<script>
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag('js', new Date());
gtag('config', 'G-XXXX');
</script>
</head>

<body>
</body>



</html>

  1. And of course, add your Landbot to your container’s webpage using the snippet provided by the app:

<script SameSite="None; Secure" src="https://cdn.landbot.io/landbot-3/landbot-3.0.0.js"></script>
<script>
var myLandbot = new Landbot.Livechat({
configUrl: 'https://chats.landbot.io/u/H-XXX-KZP5BY06MTWL17OY/index.json',
});
</script>

What to edit in your bot (Landbot builder):

  1. In your chatbot builder, add this code to a Code block, right where you want the tracking be triggered This is the point in the flow when you want to send the event to Google Analytics. Replace the parameters with your own values:
window.gtag('event', 'start chat')

Change "start chat" for the name of the event desired

Test the event:
  1. Once the setup above is complete you can test the event by using the bot and arrive to the step where the code block is. After few seconds (15 seconds more or less) you should be able to see your event in the real time section:

Here's an example of how we can add GA4 to Wordpress

Optional: Track bot displayed (On Welcome Message displayed)
One thing to keep in mind when using scripts in the welcome message is that they will trigger twice

If you want to track when the bot is displayed to the user, before the conversation is started. You can add a event trigger like this:

<script> if (!window.pushedEvent) { window.gtag('event', 'welcome loaded') window.pushedEvent = true; } </script>

Channel Type compatibility

for Web

This feature is fully compatible.

for WhatsApp

This block/functionality is not compatible with WhatsApp.

for Messenger

This block/functionality is not compatible with Messenger.

How did we do?

Google Analytics - Track Events (Not embedded)

Contact