From: How to Exclude Google Analytics When Running Under Hugo Local Server from 2018 with love :)
If you are not using a theme that already excludes Google Analytics, then you should use the following piece of hugo code.
In my case, I added this code into layouts/partials/custom-header.html
{{- if not .Site.IsServer -}}
{{ template "_internal/google_analytics.html" . }}
{{- end -}}