When I finally decided to push the hugo version of this website live and overwrite the wordpress version, I made the decision to put Google Analytics on it. My wordpress site had been using Cloudflare DNS, and therefore had low level analytics for free through Cloudflare. It was enough for me. For the hugo deploy I went with Netlify and Netlify DNS. Not wanting to pay $9/month for Netlify analytics at that time, but still desiring some type of analytics, i went with GA as it is monetarily cost-free.
At the time of deploy, Hugo had not addressed the new Google Analytics 4 implementation. (It seems they are working on it, however.) GA4 uses a Measurement ID in place of the long-present Tracking ID. This Measurement ID is embedded in a website in a different way than the Tracking ID has been. I found a workaround by looking into the GA docs, and the implementation can be found in the Github Gist that I wrote or following along below.
Everything is pretty straightforward. I changed the names of parameters and files to prevent crashing with hugo internal templates.
First, place the Measurement ID into the root config file.
Lastly, the partial needs to be called within the head of every page in which you would like tracking. Google suggests that the script be placed directly after the opening head tag. I called the partial (with an if statement checking for the existence of googleAnalyticsID) in
baseof.html so it is implemented sitewide.
That’s it. Like I said, pretty straightforward. Hope this can help someone out while we wait for it to be integrated into Hugo.