matsuyoshi30 / harbor

Simple and minimal personal blog theme.
MIT License
186 stars 68 forks source link

Google Analytics Not Working #21

Closed ghost closed 4 years ago

ghost commented 4 years ago

I set my tracking key using googleAnalytics = "<key>" in the config.toml file.

However, there's no google analytics <script> in the generated index.html.

If I add this in config.toml:

[params]
    googleAnalytics = "<key>"

And change analytics.html to:

{{ if not .Site.IsServer }}
  {{ with .Site.Params.googleAnalytics }}
<script async src="https://www.googletagmanager.com/gtag/js?id={{ . }}"></script>
  <script>
    window.dataLayer = window.dataLayer || [];
    function gtag(){dataLayer.push(arguments);}
    gtag('js', new Date());
    gtag('config', '{{ . }}');
  </script>
  {{ end }}
{{ end }}

The google analytics <script> is added correctly.

matsuyoshi30 commented 4 years ago

Hi @jakehell!

Thank you for letting me know about that and PR! I really appreciate your help, but I want to know more details about that issue.

Could you show me your config.toml when doesn't generate google analytics script in the index.html?

ghost commented 4 years ago

@matsuyoshi30

https://github.com/jakehell/blog/blob/f9b9443baaf1d0e121052c91f73ce42154cd651c/config.toml

This is what it looked like.

matsuyoshi30 commented 4 years ago

@jakehell

Thank you for sharing, and I figured out what happens. If you set top-level configuration like google analytics, you need to move your top-level options in config.toml to the beginning of the config file.

ref: https://stackoverflow.com/questions/46162211/google-analytics-does-not-work-with-blogdown/46207854#46207854

theme = "harbor"

[markup.goldmark.renderer]
  unsafe = true

googleAnalytics = "XXXX"

You change config.toml like below.

theme = "harbor"
googleAnalytics = "XXXX"

[markup.goldmark.renderer]
  unsafe = true

Please try it! If you still have a problem, let me know.

ghost commented 4 years ago

I see, thanks for the information. I will close this issue and the PR.

matsuyoshi30 commented 4 years ago

@jakehell

I updated README from your issue. https://github.com/matsuyoshi30/harbor/commit/454f24eeb3687a516d27878ea655afb790c1cf29

Thank you for letting me know!