khusika / FeelIt

A clean, elegant but advanced blog theme for Hugo
https://feelit.khusika.dev
MIT License
164 stars 60 forks source link

[BUG] Google Analytics does not work #61

Closed gatolynx closed 3 years ago

gatolynx commented 3 years ago

Describe the bug

When I add in config.toml

googleAnalytics = "UA-hoge"
[privacy]
  [privacy.googleAnalytics]
    disable = false
    anonymizeIP = true
    respectDoNotTrack = true

Render error occuered, such as

ERROR 2021/08/15 05:31:06 render of "page" failed: execute of template failed: template: _internal/google_analytics.html:1:16: executing "_internal/google_analytics.html" at <.Site.Config.Privacy.GoogleAnalytics>: can't evaluate field Site in type string
ERROR 2021/08/15 05:31:06 render of "page" failed: execute of template failed: template: _internal/google_analytics.html:1:16: executing "_internal/google_analytics.html" at <.Site.Config.Privacy.GoogleAnalytics>: can't evaluate field Site in type string
ERROR 2021/08/15 05:31:06 render of "page" failed: execute of template failed: template: _internal/google_analytics.html:1:16: executing "_internal/google_analytics.html" at <.Site.Config.Privacy.GoogleAnalytics>: can't evaluate field Site in type string
ERROR 2021/08/15 05:31:06 render of "page" failed: execute of template failed: template: _internal/google_analytics.html:1:16: executing "_internal/google_analytics.html" at <.Site.Config.Privacy.GoogleAnalytics>: can't evaluate field Site in type string
ERROR 2021/08/15 05:31:06 Rebuild failed:

Expected behavior

google analytics(google tagmanagers ) tags displayed in header.

Build Environment

Please try to use Hugo extended version before opening the issue.

Preview Environment

Additional Information

I think With is Rebinds the context (.) , Internal Templates can not use arguments correct config.

inbaseof.html

{{- $analytics := .Site.Config.Privacy.GoogleAnalytics -}}
        {{- if not $analytics.Disable }}{{ with .Site.GoogleAnalytics -}}
            {{ template "_internal/google_analytics.html" . }}
        {{- end -}}{{ end -}}

"_internal/google_analytics.html" arguments were rebinded .Site.GoogleAnalytics by With.

I think this should be fix below.

{{- $analytics := .Site.Config.Privacy.GoogleAnalytics -}}
        {{- if not $analytics.Disable }}{{ if .Site.GoogleAnalytics -}}
            {{ template "_internal/google_analytics.html" . }}
        {{- end -}}{{ end -}}
khusika commented 3 years ago

Hi, thanks for your report.

I'll prefer to use if and as conditional statement instead