SmartConsent install guide

Adding SmartConsent to a Hugo site

Install SmartConsent on a Hugo site, hold back Google Analytics, YouTube, Vimeo and X until visitors agree, and check it in your browser.

Last updated: 25 November 2026

This guide puts the SmartConsent banner on a Hugo site and makes Hugo's built-in Google Analytics, YouTube, Vimeo and X (Twitter) embeds wait until the visitor agrees. You copy a few files from your theme or from Hugo into your own layouts folder and change a line or two in each. You never edit the theme itself, so theme updates keep working.

Every step below was run on a real build: Hugo 0.170.1 with the Ananke theme, and Hugo 0.145.0 for the older folder names. The checks were done in Chrome.

Before you start

  • Your snippet. Sign up for SmartConsent (free) and copy the snippet from your account. It carries your site ID.
  • Your Hugo version. Run hugo version. The folder names depend on it:
HugoPartials folderShortcodes folder
0.146 or laterlayouts/_partials/layouts/_shortcodes/
before 0.146layouts/partials/layouts/shortcodes/

The steps below use the 0.146+ names. On an older Hugo, use the names in the second row.

Step 1: put the snippet first in <head>

  1. Create layouts/_partials/smartconsent.html in your site (not in the theme) with your snippet inside a production check:

    {{- if hugo.IsProduction }}
    <script data-render-head>
    window.dataLayer = window.dataLayer || [];
    window.gtag = window.gtag || function () { window.dataLayer.push(arguments); };
    window.gtag('consent', 'default', {
      ad_storage: 'denied',
      ad_user_data: 'denied',
      ad_personalization: 'denied',
      analytics_storage: 'denied',
      functionality_storage: 'denied',
      personalization_storage: 'denied',
      security_storage: 'granted',
      wait_for_update: 500
    });
    window.smartconsent = window.smartconsent || function () { (window.smartconsent.q = window.smartconsent.q || []).push(arguments); };
    </script>
    <script async src="https://cdn.smartgenie.co.uk/v1/smartconsent.min.js" data-site-id="YOUR_SITE_ID" data-render-head></script>
    {{- end }}
    

    Paste the snippet from your account rather than this copy, so the site ID is yours.

  2. Find the file in your theme that opens <head>:

    grep -rlE '^[[:space:]]*<head([[:space:]]|>)' themes/YOUR_THEME/layouts
    

    For Ananke this prints themes/ananke/layouts/baseof.html. In many themes it is baseof.html (under layouts/ or layouts/_default/); in some it is a head.html partial. If your site already has its own copy of that file under layouts/, use your copy.

  3. Copy that file into your site's layouts folder at the same path. For Ananke:

    cp themes/ananke/layouts/baseof.html layouts/baseof.html
    

    Hugo uses your copy instead of the theme's.

  4. In your copy, add one line straight after <meta charset="utf-8">, above every other script and above any analytics partial:

    <head>
      <meta charset="utf-8">
      {{ partial "smartconsent.html" . }}
    
  5. Build and look at the top of a page:

    hugo
    head -25 public/index.html
    

    You should see <head>, the charset line, then the two SmartConsent scripts, before anything else.

Why the production check. hugo server runs in development, so it leaves the snippet out and your local previews do not show the banner. To see the banner locally, run hugo server -e production. hugo on its own builds for production, so your published site has the snippet.

Step 2: hold back Hugo's Google Analytics

Skip this step if your site does not set services.googleAnalytics.id in its configuration.

Hugo prints its Google Analytics tags from a built-in template. Make your own copy, with both scripts marked so they wait for analytics consent.

  1. Download Hugo's template for your Hugo version (replace 0.170.1 with the number from hugo version):

    V=0.170.1
    mkdir -p layouts/_partials
    curl -fL -o layouts/_partials/google_analytics.html \
      https://raw.githubusercontent.com/gohugoio/hugo/v$V/tpl/tplimpl/embedded/templates/_partials/google_analytics.html
    

    If curl reports a 404, your Hugo keeps the file at the older path .../embedded/templates/google_analytics.html (we checked 0.145.0). On Hugo before 0.146, save it as layouts/partials/google_analytics.html.

  2. In your copy, change the two script tags:

    • <script async src= becomes <script type="text/plain" data-consent-category="analytics" async src=
    • <script> becomes <script type="text/plain" data-consent-category="analytics">
  3. Build and check: grep -n googletagmanager public/index.html should show <script type="text/plain" data-consent-category="analytics" async src="https://www.googletagmanager.com/gtag/js?id=...">.

If your theme uses the old call. Some themes print analytics with {{ template "_internal/google_analytics.html" . }}. Your copy cannot replace that call. With the override installed, Hugo 0.170.1 failed to build and Hugo 0.145.0 rendered unmarked tags, so switch to the partial call below in either case.

Find the theme file that makes the call (grep -rn '_internal/google_analytics' themes/YOUR_THEME/layouts), copy it into your layouts folder at the same path, and change the call in your copy to {{ partial "google_analytics.html" . }}. Then build and check again as in point 3.

Step 3: hold back YouTube and Vimeo videos

Hugo's youtube and vimeo shortcodes print an <iframe> that loads the video player at once. Your copies print the same iframe without its address, with a short message inside it, and a marked script that fills in the address when the visitor accepts marketing cookies.

  1. Download Hugo's two shortcodes for your version:

    V=0.170.1
    mkdir -p layouts/_shortcodes
    for f in youtube vimeo; do
      curl -fL -o layouts/_shortcodes/$f.html \
        https://raw.githubusercontent.com/gohugoio/hugo/v$V/tpl/tplimpl/embedded/templates/_shortcodes/$f.html
    done
    

    If curl reports a 404, your Hugo keeps them at the older path .../embedded/templates/shortcodes/ (we checked 0.145.0). On Hugo before 0.146, save them in layouts/shortcodes/.

  2. In each copy, find the <iframe tag and its src=" attribute. Put this in front of src=":

    srcdoc="<p>This video loads after you accept marketing cookies.</p>" data-consent-
    

    so src=" becomes data-consent-src=". On Hugo 0.170.1, YouTube's line

    {{- with $src }} src="{{ . }}" {{- end }}
    

    becomes

    {{- with $src }} srcdoc="<p>This video loads after you accept marketing cookies.</p>" data-consent-src="{{ . }}" {{- end }}
    

    Some older Vimeo templates have two <iframe tags; change both.

  3. Add this line at the very end of each copy:

    <script type="text/plain" data-consent-category="marketing">document.querySelectorAll("iframe[data-consent-src]").forEach(function (f) { f.removeAttribute("srcdoc"); f.src = f.getAttribute("data-consent-src"); f.removeAttribute("data-consent-src"); });</script>
    
  4. Build and check a page with a video: its iframe has data-consent-src="https://www.youtube.com/embed/..." (or https://player.vimeo.com/video/...) and no src, followed by the marked script.

Everything else in Hugo's shortcodes (start time, autoplay, class, privacy settings) stays as Hugo wrote it, because the rest of the file is Hugo's own.

If privacy.vimeo.simple = true, the browser requests a Vimeo thumbnail before consent, so set it to false and apply this override if all Vimeo requests must wait.

Step 4: hold back X (Twitter) posts

Hugo's x shortcode fetches the post when you build and prints it with X's widgets.js script. Your copy asks X for the post without the script and adds a marked copy of it, so the post shows as a plain quote until the visitor accepts marketing cookies, and as X's styled card after that.

This step requires Hugo 0.141.0 or later and applies to x calls, so migrate any twitter or tweet calls to x with named user and id arguments.

  1. Download Hugo's shortcode:

    V=0.170.1
    mkdir -p layouts/_shortcodes
    curl -fL -o layouts/_shortcodes/x.html \
      https://raw.githubusercontent.com/gohugoio/hugo/v$V/tpl/tplimpl/embedded/templates/_shortcodes/x.html
    

    If curl reports a 404, use the older path .../embedded/templates/shortcodes/x.html. On Hugo before 0.146, create layouts/shortcodes/ instead and save it there.

  2. In the line that builds the query, add "omit_script" true:

    {{- $query := querify "url" $url "dnt" .dnt "omit_script" true -}}
    
  3. Straight after the line {{- (. | transform.Unmarshal).html | safeHTML -}}, add:

    <script type="text/plain" data-consent-category="marketing" async src="https://platform.x.com/widgets.js" charset="utf-8"></script>
    
  4. Build and check a page with a post: the <blockquote class="twitter-tweet"> is followed by the marked widgets.js script and no other script.

X's simple mode (privacy.x.simple = true) already prints the post with no X script, so it needs no change.

Step 5: check it in a browser

Publish (or run hugo server -e production). Open pages containing each tracker you use with the Network tab open, test Accept and Reject in separate fresh private sessions, and reload in the Accept session to check saved consent.

  • Before you choose: the banner shows, and nothing is requested from googletagmanager.com, youtube.com, vimeo.com or platform.x.com. Video boxes show "This video loads after you accept marketing cookies."
  • After Accept, the browser requested all four tested hosts and the two video iframes received their player URLs.
  • After Reject: none of them load.
  • On your next visit, SmartConsent remembers an Accept and loads them straight away.

That is what our test build showed: before and after Accept and after Reject on both Hugo versions, and the next visit on Hugo 0.170.1.

Other scripts on your site

The steps above cover Hugo's own templates. Any other tracking script in your theme, partials or content (Meta Pixel, Hotjar, ad tags) needs the same two attributes: type="text/plain" and a data-consent-category (analytics, functional or marketing). See Script Blocking in the SmartConsent docs for examples. Scripts without them run as normal.

If your host sets a Content Security Policy

Hugo sets none, but some hosts and setups add one (a _headers file on Netlify or Cloudflare Pages, for example). SmartConsent needs these in the policy:

DirectiveAddWhy
script-srchttps://cdn.smartgenie.co.uk and 'unsafe-inline'the engine, and the snippet's inline part (the marked scripts above are inline too)
connect-srchttps://smartconsentsettings.blob.core.windows.netyour site's banner settings
connect-srchttps://cdn.smartgenie.co.ukbanner languages
connect-srchttps://api.smartgenie.co.ukconsent records
style-src'unsafe-inline'the banner's styles
img-srcyour logo's addressonly if you show a logo in the banner

Your trackers need their own entries too (for example https://www.googletagmanager.com in script-src, and the video hosts in frame-src).

Our test page used this policy:

default-src 'self'; script-src 'self' 'unsafe-inline' https://cdn.smartgenie.co.uk; connect-src 'self' https://smartconsentsettings.blob.core.windows.net https://cdn.smartgenie.co.uk https://api.smartgenie.co.uk; style-src 'self' 'unsafe-inline'; img-src 'self' https:

The banner appeared and the browser allowed the settings request with zero CSP violations on our test page, which used an unregistered site ID and fallback settings.

With the settings address left out of connect-src, the browser blocked the settings file and reported it.

What this guide does not do

  • It does not make your site compliant. No tool does that by being installed; what your site does with consent matters.
  • It covers Hugo's built-in Google Analytics, YouTube, Vimeo and X templates only. Theme-specific hooks (for example a theme's own "extend head" partial) were not tested; the copy-the-file route above was tested on Ananke and on a plain layout.

Our side: checked against the SmartConsent code on 25 Nov 2026.