A plugin that enables Google Analytics 4 tracking in Godot games. This plugin supports both page views and custom events with session tracking.
-
Enable the plugin in your Godot project:
- Copy the
addons/google_analytics
folder to your project - Go to Project → Project Settings → Plugins
- Enable the "Google Analytics" plugin
- Copy the
For HTML5/Web exports, you'll need to add the GA4 tracking code to your HTML template, this can either be done by adding the tag to the Head Include or by supplying a custom HTML file in the export options.
In the snippet below we disable the default page view tracking that G4A does. If you want to allow G4A to handle page views normally then you can remove the send_page_view
line.
-
Add the GA4 tracking code to your HTML template:
<!-- Google tag (gtag.js) --> <script async src="https://www.googletagmanager.com/gtag/js?id=G-XXXXXXXXXX" ></script> <script> window.dataLayer = window.dataLayer || []; function gtag() { dataLayer.push(arguments); } gtag("js", new Date()); gtag("config", "G-XXXXXXXXXX", { send_page_view: false, allow_google_signals: false, allow_ad_personalization_signals: false, }); </script>
-
Replace
G-XXXXXXXXXX
with your measurement ID -
The plugin will automatically use gtag.js when running in a web context
For desktop and mobile builds you'll need to configure the plugin with your Google Analytics credentials.
- Configure your Google Analytics credentials:
- Go to Project → Project Settings → General
- In the top right enable "Advanced Settings" with the toggle
- Scroll down to "google_analytics" section
- Fill in the following settings:
measurement_id
: Your GA4 measurement ID (format: "G-XXXXXXXXXX")api_secret
: Your GA4 API secret (from GA4 Admin → Data Streams → Choose your stream → Measurement Protocol API secrets)
When the game starts, the plugin will automatically track a 'Game Start' page view. You can also manually track page views to track when players enter different scenes or sections of your game:
# Track a simple page view
Analytics.track_page_view("Main Menu") # This will take the title and generate a location, in this case "app://game/main_menu"
# Track a page view with custom location
Analytics.track_page_view("Level 1", "app://game/levels/level_1") # This will use the provided location instead of generating one
Custom events will be tracked with the current page context.
Track custom events using GA4's recommended event names and parameters:
# Track button clicks
Analytics.track_event("select_content", {
"content_type": "button",
"item_id": "start_game",
"content_category": "main_menu"
})
# Track level completion
Analytics.track_event("level_complete", {
"level_id": "1",
"score": "1000",
"time_spent": "120"
})
# Track item purchases
Analytics.track_event("purchase", {
"currency": "gold",
"value": "500",
"items": "magic_sword"
})
- Automatic session tracking
- Event queuing with rate limiting
- Persistent client ID across game sessions
- Automatic page context for all events
- Support for GA4's recommended event names and parameters
- Error handling and debug logging
- Events are automatically queued and sent with a minimum delay between them
- Each event includes session information and current page context
- Client ID is generated once and stored persistently
- All numeric values are automatically converted to strings (GA4 requirement)
- Debug logging helps track event sending and any issues
-
Use GA4's recommended event names when possible:
select_content
level_complete
purchase
tutorial_begin
tutorial_complete
- etc.
-
Track meaningful game events:
- Scene/level changes (using
page_view
) - Button clicks and UI interactions
- Game progression events
- Achievement unlocks
- Resource gathering/spending
- Player preferences
- Scene/level changes (using
-
Include relevant parameters:
- Always include meaningful context
- Use consistent parameter names
- Follow GA4's parameter naming conventions
The plugin includes detailed logging prefixed with "[GA]". To enable debug logs:
- Run Godot with the
--verbose
flag, or - Set "Debug > Stdout > Verbose" in your Godot Editor Settings
When verbose logging is enabled, you'll see:
- Client initialization details
- Page view tracking information
- Event preparation and parameters
- HTTP request details and responses
- Success/failure status of each event
Error messages (prefixed with "[GA] Error:") are always shown regardless of verbose mode.
- Maximum of 25 custom parameters per event
- Parameter names must use snake_case
- Parameter values must be strings
MIT License - Feel free to use in any project, commercial or otherwise.