<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>Google Analytics on Daniel Zhang</title><link>https://zhangdapeng.dev/tags/google-analytics/</link><description>Recent content in Google Analytics on Daniel Zhang</description><generator>Hugo -- gohugo.io</generator><language>en-us</language><lastBuildDate>Sun, 01 Nov 2026 16:24:00 +0800</lastBuildDate><atom:link href="https://zhangdapeng.dev/tags/google-analytics/index.xml" rel="self" type="application/rss+xml"/><item><title>How to Add Google Analytics (GA4) to Hugo: A Step-by-Step Guide</title><link>https://zhangdapeng.dev/p/hugo-google-analytics-setup/</link><pubDate>Sun, 01 Nov 2026 16:24:00 +0800</pubDate><guid>https://zhangdapeng.dev/p/hugo-google-analytics-setup/</guid><description>&lt;p&gt;When building a personal blog or static website, tracking visitor traffic and user behavior is essential. Fortunately, Hugo comes with built-in support for &lt;strong&gt;Google Analytics 4 (GA4)&lt;/strong&gt;, making the integration quick and seamless.&lt;/p&gt;
&lt;p&gt;This guide will walk you through setting up Google Analytics on your Hugo site step by step.&lt;/p&gt;
&lt;h2 id="1-obtain-your-measurement-id"&gt;1. Obtain Your Measurement ID
&lt;/h2&gt;&lt;p&gt;Before configuring Hugo, ensure you have a Google Analytics account and an active Data Stream:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;Log in to the &lt;a class="link" href="https://analytics.google.com/" target="_blank" rel="noopener"
 &gt;Google Analytics Console&lt;/a&gt;.&lt;/li&gt;
&lt;li&gt;Navigate to &lt;strong&gt;Admin -&amp;gt; Data Streams&lt;/strong&gt; and select your web stream.&lt;/li&gt;
&lt;li&gt;Copy your &lt;strong&gt;Measurement ID&lt;/strong&gt; (it usually looks like &lt;code&gt;G-XXXXXXXXXX&lt;/code&gt;).&lt;/li&gt;
&lt;/ol&gt;
&lt;h2 id="2-update-hugo-configuration"&gt;2. Update Hugo Configuration
&lt;/h2&gt;&lt;p&gt;Add your GA4 Measurement ID to your site&amp;rsquo;s main configuration file located in the root directory.&lt;/p&gt;
&lt;h3 id="recommended-configuration-modern-hugo"&gt;Recommended Configuration (Modern Hugo)
&lt;/h3&gt;&lt;p&gt;For &lt;code&gt;hugo.toml&lt;/code&gt; or &lt;code&gt;config.toml&lt;/code&gt;:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;"&gt;&lt;code class="language-toml" data-lang="toml"&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;[&lt;span style="color:#a6e22e"&gt;services&lt;/span&gt;]
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; [&lt;span style="color:#a6e22e"&gt;services&lt;/span&gt;.&lt;span style="color:#a6e22e"&gt;googleAnalytics&lt;/span&gt;]
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#a6e22e"&gt;id&lt;/span&gt; = &lt;span style="color:#e6db74"&gt;&amp;#39;G-XXXXXXXXXX&amp;#39;&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;For &lt;code&gt;hugo.yaml&lt;/code&gt; or &lt;code&gt;config.yaml&lt;/code&gt;:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;"&gt;&lt;code class="language-yaml" data-lang="yaml"&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#f92672"&gt;services&lt;/span&gt;:
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#f92672"&gt;googleAnalytics&lt;/span&gt;:
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#f92672"&gt;id&lt;/span&gt;: &lt;span style="color:#e6db74"&gt;&amp;#39;G-XXXXXXXXXX&amp;#39;&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;h3 id="legacy-configuration-older-hugo-versions"&gt;Legacy Configuration (Older Hugo Versions)
&lt;/h3&gt;&lt;p&gt;If you are using an older version of Hugo, you can set the key directly at the top level of your configuration file:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;"&gt;&lt;code class="language-toml" data-lang="toml"&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#a6e22e"&gt;googleAnalytics&lt;/span&gt; = &lt;span style="color:#e6db74"&gt;&amp;#39;G-XXXXXXXXXX&amp;#39;&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;h2 id="3-verify-theme-compatibility"&gt;3. Verify Theme Compatibility
&lt;/h2&gt;&lt;p&gt;Most modern Hugo themes include GA4 support out of the box. If tracking does not work automatically, check your theme&amp;rsquo;s head template (typically located at &lt;code&gt;layouts/partials/head.html&lt;/code&gt;) to ensure the built-in internal template is included:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;"&gt;&lt;code class="language-html" data-lang="html"&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;{{ template &amp;#34;_internal/google_analytics.html&amp;#34; . }}
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;h2 id="4-important-troubleshooting--development-notes"&gt;4. Important Troubleshooting &amp;amp; Development Notes
&lt;/h2&gt;&lt;ol&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Disabled in Local Development&lt;/strong&gt;
When running &lt;code&gt;hugo server&lt;/code&gt; locally, Hugo automatically &lt;strong&gt;omits&lt;/strong&gt; the Google Analytics tracking script. This prevents your local debugging and development activity from skewing production analytics data.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Enabled in Production Builds&lt;/strong&gt;
The analytics script will only be injected when building for the production environment:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;"&gt;&lt;code class="language-bash" data-lang="bash"&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;hugo --environment production
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Verifying Your Setup&lt;/strong&gt;
After deploying your site, visit it in a browser while keeping the &lt;strong&gt;Realtime Report&lt;/strong&gt; tab open in Google Analytics to verify that active sessions are recorded.&lt;/p&gt;
&lt;/li&gt;
&lt;/ol&gt;
&lt;h2 id="conclusion"&gt;Conclusion
&lt;/h2&gt;&lt;p&gt;Thanks to Hugo&amp;rsquo;s built-in template mechanism, adding GA4 requires nothing more than adding your Measurement ID to the config file. This clean approach ensures simplicity without needing to hardcode external scripts into your site templates.&lt;/p&gt;</description></item></channel></rss>