Tracking AI chatbots and search engine crawlers in Google Analytics 4 (GA4) ensures better data accuracy by separating bot traffic from real user activity. Follow the steps below to set up bot detection and reporting in GA4.
Step 1: Create a Custom Dimension in GA4
- Go to Admin in your GA4 property.
- Select Custom definitions.
- Click Create custom dimension.
- Configure the dimension as follows:
- Name:
is_bot
- Scope:
Event
- Event parameter:
is_bot
(Typeis_bot
manually; it may not appear in the dropdown.)
- Name:
- Save the custom dimension.
Step 2: Add User Agent Detection Script
Embed the following JavaScript snippet on your website, before the closing </head>
tag or within your tag manager. This script checks the visitor's user agent string and identifies known bots, including popular AI and search engine bots.
html
- Replace
G-XXXXXXXXXX
with your actual Google Analytics 4 Measurement ID.
Step 3: Analyze Bot Traffic in GA4
After completing the above steps:
- The
is_bot
custom dimension will be available in your GA4 reports. - Use this dimension to segment and filter bot vs. human traffic in your analytics dashboards.
- Optionally, add
bot_name
as a custom dimension to identify specific bots accessing your site.