# Replace Hugo's inbuilt Google Analytics template with Minimal Analytics (GA4)

**URL:** <https://discourse.gohugo.io/t/replace-hugos-inbuilt-google-analytics-template-with-minimal-analytics-ga4/43704>\
**Category:** tips & tricks\
**Tags:** analytics\
**Created:** [April 1, 2023, 5:12pm UTC](https://discourse.gohugo.io/t/replace-hugos-inbuilt-google-analytics-template-with-minimal-analytics-ga4/43704 "2023-04-01T17:12:43Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Arif](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/arif/32/20931_2.png) [@Arif](https://discourse.gohugo.io/u/Arif)\
**Post date:** [April 1, 2023, 5:12pm UTC](https://discourse.gohugo.io/t/replace-hugos-inbuilt-google-analytics-template-with-minimal-analytics-ga4/43704/1 "2023-04-01T17:12:43Z")

</div>

This implementation is based on the following [tutorial](https://discourse.gohugo.io/t/39016) and [comment](https://discourse.gohugo.io/t/39016/2).

We shall override the internal template [configuration](https://gohugo.io/templates/internal/#use-the-google-analytics-template) so that `googleAnalytics` value is used by our custom template instead.

1. Create a Google Analytics file `/layouts/_internal/google_analytics.html`

```auto
└───layouts
    └───_internal
            google_analytics.html

```

Copy-paste the following code into it–

```auto
{{- with site.GoogleAnalytics }}
  {{- if hugo.IsProduction }}
  {{- $ga4 := resources.GetRemote "https://cdn.jsdelivr.net/npm/@minimal-analytics/ga4/dist/index.js" }}
  {{- $track := resources.Get "js/track.js" }}
  {{- $opts := dict "params" (dict "trackingId" site.GoogleAnalytics) }}
  {{- $track = $track | js.Build $opts -}}
  {{- $stats := slice $track $ga4 | resources.Concat "js/analytics.js" | minify | fingerprint }}
  <script src="{{ $stats.RelPermalink }}" integrity="{{ $stats.Data.Integrity }}"></script>
  {{- end }}
{{- end -}}

```

1. Create the tracking file in `assets/js/track.js`.

```auto
assets
    └───js
            track.js

```

Copy-paste the code below into it–

```auto
import params from "@params";

window.minimalAnalytics = {
  trackingId: params.trackingId,
  autoTrack: true, // <-- init tracking
  defineGlobal: true,
};

```

1. In your `config.toml` somewhere higher up e.g below the `title = ""` entry, add the GA tracking code. Replace `G-XXXXXXXXXX` with your own tracking code.

```auto
googleAnalytics = "G-XXXXXXXXXX"

```

1. Call the template in the `head.html` file or just before the closing `</body>` tag (in `baseof.html` file) depending on your preference.

```auto
{{- template "_internal/google_analytics.html" . -}}

```

Confirm after some time if the Real Time section of your GA4 account is populating stats.

---

<div class="post-metadata">

**Author:** ![Arif](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/arif/32/20931_2.png) [@Arif](https://discourse.gohugo.io/u/Arif)\
**Post date:** [November 12, 2023, 8:42am UTC](https://discourse.gohugo.io/t/replace-hugos-inbuilt-google-analytics-template-with-minimal-analytics-ga4/43704/2 "2023-11-12T08:42:18Z")

</div>

**UPDATE**. [Hugo v0.120.0](https://github.com/gohugoio/hugo/releases/tag/v0.120.0) deprecated `googleAnalytics`, so here is the updated code–

In step 1

```auto
{{- with site.Config.Services.GoogleAnalytics.ID }}
  {{- if hugo.IsProduction }}
  {{- $ga4 := resources.GetRemote "https://cdn.jsdelivr.net/npm/@minimal-analytics/ga4/dist/index.js" }}
  {{- $track := resources.Get "js/track.js" }}
  {{- $opts := dict "params" (dict "trackingId" . ) }}
  {{- $track = $track | js.Build $opts -}}
  {{- $stats := slice $track $ga4 | resources.Concat "js/analytics.js" | minify | fingerprint }}
  <script src="{{ $stats.RelPermalink }}" integrity="{{ $stats.Data.Integrity }}"></script>
  {{- end }}
{{- end -}}

```

In step 3

```auto
[services]
  [services.googleAnalytics]
    ID = 'G-MEASUREMENT_ID'

```

---

<div class="post-metadata">

**Author:** ![system](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/system/32/1_2.png) [@system](https://discourse.gohugo.io/u/system)\
**Post date:** [November 14, 2023, 8:42am UTC](https://discourse.gohugo.io/t/replace-hugos-inbuilt-google-analytics-template-with-minimal-analytics-ga4/43704/3 "2023-11-14T08:42:55Z")

</div>

This topic was automatically closed 2 days after the last reply. New replies are no longer allowed.
