Add Google Analytics to Hugo Blog
Add Google Analytics to Hugo Blog
Contents
Google Analytics
-
Register a google analytics account
-
Get your measurement ID that looks like below
G-6ED0IWOAJK
-
Get gtag js script
|
|
Hugo blog
- Find config.toml file, enable google analytics as below
|
|
This is my settings above, you may just need
googleAnalytics = "G-6ED0IWOAJK"
-
Add gtag script to path:
/themes/your-theme/layouts/_internal/google_analytics_async.html
- You may need to create a new folder and new html file
- Paste gtag js script above to the html file
- Add template code to path: /themes/your-theme/layouts/partials/header.html
|
|
- You only need the second line but the if statement prevents local builds being tracked by GA
Google Analytics should be about to work!
If it doesn’t work, wait for a while. I added GA late at night and it didn’t make an effect immediately.
The next day I woke up, It worked! 😁