hugo-toha / toha

A Hugo theme for personal portfolio
https://hugo-toha.github.io
MIT License
1.05k stars 607 forks source link

.Site.GoogleAnalytics was deprecated in Hugo v0.120.0 and will be removed in a future release. Use .Site.Config.Services.GoogleAnalytics.ID instead. #856

Closed eishundo closed 8 months ago

eishundo commented 11 months ago

Question

Heads-up.

I spent some time updating my build from an older version of Hugo and Toha v3.8.0 to the latest Hugo version with Toha v4.0.0 (great work on the theme update!).

I noticed Hugo now reports:

$ hugo server -D --watch --logLevel debug --disableFastRender
INFO  deprecated: .Site.GoogleAnalytics was deprecated in Hugo v0.120.0 and will be removed in a future release. Use .Site.Config.Services.GoogleAnalytics.ID instead.

My GoogleAnalytics script was not being added to the rendered pages at all.

Tactically, I have overridden the theme by creating "layouts/partials/analytics.html" containing the following changes:

--- analytics.orig.html 2023-12-28 13:58:19.508035398 +0000
+++ analytics.html      2023-12-28 13:59:16.984908042 +0000
@@ -4,8 +4,8 @@
         {{ with .services }}
         <!-- Google Analytics -->
         {{ with .google }}
-            {{ $privacyConfig:= dict (slice "Site" "Config" "Privacy" "GoogleAnalytics") $.Site.Config.Privacy.GoogleAnalytics }}
-            {{ $analyticsConfig := dict (slice "Site" "GoogleAnalytics") .id }}
+            {{ $privacyConfig := dict (slice "Site" "Config" "Privacy" "GoogleAnalytics") $.Site.Config.Privacy.GoogleAnalytics }}
+            {{ $analyticsConfig := dict (slice "Site" "Config" "Services" "GoogleAnalytics" "ID") .id }}
             {{ template "_internal/google_analytics.html" (merge $privacyConfig $analyticsConfig) }}
         {{ end }}

@@ -56,6 +56,6 @@
 {{ end }}

 <!-- Keep backwards compatibility and consistency with HUGO defaults -->
-{{ if site.GoogleAnalytics }}
+{{ if site.Config.Services.GoogleAnalytics.ID }}
     {{ template "_internal/google_analytics.html" . }}
 {{ end }}

However, this may break sites using older versions of Hugo, so I didn't want to submit a formal patch.

I also noticed a similar warning for Disqus, but do not use this site so unable to troubleshoot. e.g.

INFO  deprecated: .Site.DisqusShortname was deprecated in Hugo v0.120.0 and will be removed in a future release. Use .Site.Config.Services.Disqus.Shortname instead.

References:

hossainemruz commented 11 months ago

@eishundo Thank you for pointing this out. We can keep backward compatibility by checking Hugo version using hugo.Version function.

javierasping commented 8 months ago

Hello there! I've encountered the same issue. After updating the theme, I lost the functionality of Google Analytics. However, I followed your advice and it's been fixed. In my case, I didn't have the layouts folder in my repository. I downloaded it from the main repository, applied the changes, and it works now.

eishundo commented 8 months ago

I see the fix was released, but I believe we also need to modify line 8 in the file.

{{ $analyticsConfig := dict (slice "Site" "GoogleAnalytics") .id }} Should be: {{ $analyticsConfig := dict (slice "Site" "Config" "Services" "GoogleAnalytics" "ID") .id }}

hossainemruz commented 6 months ago

Should be fixed in #937