Hugo: analytics: Disable GA on localhost

Created on 24 Jun 2020  ยท  7Comments  ยท  Source: gohugoio/hugo

Problem

Built-in templates for Google Analytics are enabled on localhost, aka development server.

Solution

Disable GA templates as done in Disqus if localhost is detected.

What version of Hugo are you using (hugo version)?

$ hugo version
v0.71.1

Does this issue reproduce with the latest release?

Yes

Most helpful comment

I think you can accomplish this with configuration settings. See https://gohugo.io/getting-started/configuration/#configuration-directory.

โ”œโ”€โ”€ config
โ”‚ย ย  โ”œโ”€โ”€ _default
โ”‚ย ย  โ”‚ย ย  โ””โ”€โ”€ config.toml
โ”‚ย ย  โ””โ”€โ”€ production
โ”‚ย ย      โ””โ”€โ”€ config.toml

config/_default/config.toml:

googleAnalytics = ""

config/production/config.toml:

googleAnalytics = "your-live-code-here"

All 7 comments

I'm more than happy to send a patch fixing this, as long as someone says it's ok to do so.

I think you can accomplish this with configuration settings. See https://gohugo.io/getting-started/configuration/#configuration-directory.

โ”œโ”€โ”€ config
โ”‚ย ย  โ”œโ”€โ”€ _default
โ”‚ย ย  โ”‚ย ย  โ””โ”€โ”€ config.toml
โ”‚ย ย  โ””โ”€โ”€ production
โ”‚ย ย      โ””โ”€โ”€ config.toml

config/_default/config.toml:

googleAnalytics = ""

config/production/config.toml:

googleAnalytics = "your-live-code-here"

I'm sure _someone_ is running hugo locally behind a proxy in a production environment. While we don't recommend that setup, this feature request would break that setup. I'd prefer to see you use the existing configuration options mentioned above by @jmooring.

That needs some documentation I think at the place of the analytics documentation. At least a little notice :)

Actually, it's easier than I originally proposed. Two methods:

  1. Conditionally include the template in baseof.html or wherever:

    {{ if not .Site.IsServer }}
     {{ template "_internal/google_analytics.html" . }}
    {{ end }}
    
  2. The template will not be rendered if googleAnalytics is not present in config.toml. So...

    โ”œโ”€โ”€ config
    โ”‚   โ”œโ”€โ”€ _default
    โ”‚   โ”‚   โ””โ”€โ”€ config.toml
    โ”‚   โ””โ”€โ”€ production
    โ”‚       โ””โ”€โ”€ config.toml
    

    config/_default/config.toml:

    # googleAnalytics = "your-live-code-here" (or eliminate the entry)
    

    config/production/config.toml:

    googleAnalytics = "your-live-code-here"
    

Thanks for getting in touch, but it feels like this is a question that would be better suited to the Hugo Discussion Forum. We prefer to use GitHub issues only for bugs and enhancements. Feel free to update this issue with a link to the re-posted question (so that other people can find it) or add some more details if you feel this is a genuine bug. You may also find the answers you search for in Hugo Documentation.

All comments are valid, but as I mentioned, Disqus does this in the Javascript side, so, why one and not others? :thinking:

Keeping behavior equal across multiple items is desired.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

carandraug picture carandraug  ยท  3Comments

artelse picture artelse  ยท  3Comments

marekr picture marekr  ยท  3Comments

vielmetti picture vielmetti  ยท  3Comments

geddski picture geddski  ยท  3Comments