Add Google Analytics in HUGO 🌟
how to add google analytics in hugo To add Google Analytics to your Hugo site, follow these clear steps: 1. Create a Google Analytics Account and Get Your Measurement ID Go to Google Analytics and sign in or create an account. Set up a new property for your website and note your Measurement ID (e.g., G-XXXXXXXXXX for GA4 or UA-XXXXXXXX-X for Universal Analytics)12. 2. Configure Your Hugo Site A. Add Your Measurement ID to config.toml Open your site’s config.toml file and add this line at the top level (replace with your actual ID):

HUGO - This article is part of a series.
how to add google analytics in hugo
To add Google Analytics to your Hugo site, follow these clear steps:
1. Create a Google Analytics Account and Get Your Measurement ID
- Go to Google Analytics and sign in or create an account.
- Set up a new property for your website and note your Measurement ID (e.g.,
G-XXXXXXXXXX
for GA4 orUA-XXXXXXXX-X
for Universal Analytics)12.
2. Configure Your Hugo Site
A. Add Your Measurement ID to config.toml
Open your site’s config.toml
file and add this line at the top level (replace with your actual ID):
googleAnalytics = "G-XXXXXXXXXX"
B. Ensure the Analytics Template is Included in Your Theme
Hugo has a built-in template for Google Analytics. You need to include it in your site’s <head>
section.
If Your Theme Already Supports Analytics:
- Most modern Hugo themes automatically include the Google Analytics template if the
googleAnalytics
value is set inconfig.toml
. - No further action needed.
If Your Theme Does NOT Include It:
- Copy your theme’s
header.html
(often atthemes/yourtheme/layouts/partials/header.html
) to your site’s ownlayouts/partials/header.html
to avoid editing theme files directly32. - Add the following line inside the
<head>
section:
{{ template "_internal/google_analytics.html" . }}
- For GA4, you may also add:
{{ template "_internal/google_analytics_async.html" . }}
3. Build and Deploy Your Site
- Run
hugo
to build your site. - Deploy as usual.
4. Verify Tracking
- Visit your site and check the Real-Time section in Google Analytics to confirm data is being received.
- If you use browser privacy plugins, try in an incognito window to ensure scripts aren’t blocked3.
Optional: Custom GA4 Integration
If you want more control or your theme doesn’t support Hugo’s built-in templates, you can create a custom partial (e.g., analytics-gtag.html
) and include the GA4 script manually4:
In config.toml
:
[params]
googleAnalyticsID = "G-XXXXXXXXXX"
In /layouts/partials/analytics-gtag.html
:
<script async src="https://www.googletagmanager.com/gtag/js?id={{ .Site.Params.googleAnalyticsID }}"></script>
<script>
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag('js', new Date());
gtag('config', '{{ .Site.Params.googleAnalyticsID }}');
</script>
Then call this partial in your base template (baseof.html
):
{{ if .Site.Params.googleAnalyticsID }}
{{ partial "analytics-gtag.html" . }}
{{ end }}
Summary Table
Step | Action |
---|---|
1. Get Measurement ID | From Google Analytics account |
2. Edit config.toml | Add googleAnalytics = "G-XXXXXXXXXX" |
3. Check theme header | Ensure {{ template "_internal/google_analytics.html" . }} is included |
4. Build & Deploy | Run hugo and deploy your site |
5. Verify | Check Google Analytics dashboard |
That’s it! Your Hugo site will now track visitors using Google Analytics1324.