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:
| Hugo | Partials folder | Shortcodes folder |
|---|---|---|
| 0.146 or later | layouts/_partials/ | layouts/_shortcodes/ |
| before 0.146 | layouts/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>
-
Create
layouts/_partials/smartconsent.htmlin 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.
-
Find the file in your theme that opens
<head>:grep -rlE '^[[:space:]]*<head([[:space:]]|>)' themes/YOUR_THEME/layoutsFor Ananke this prints
themes/ananke/layouts/baseof.html. In many themes it isbaseof.html(underlayouts/orlayouts/_default/); in some it is ahead.htmlpartial. If your site already has its own copy of that file underlayouts/, use your copy. -
Copy that file into your site's
layoutsfolder at the same path. For Ananke:cp themes/ananke/layouts/baseof.html layouts/baseof.htmlHugo uses your copy instead of the theme's.
-
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" . }} -
Build and look at the top of a page:
hugo head -25 public/index.htmlYou 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.
-
Download Hugo's template for your Hugo version (replace
0.170.1with the number fromhugo 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.htmlIf
curlreports 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 aslayouts/partials/google_analytics.html. -
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">
-
Build and check:
grep -n googletagmanager public/index.htmlshould 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.
-
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 doneIf
curlreports 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 inlayouts/shortcodes/. -
In each copy, find the
<iframetag and itssrc="attribute. Put this in front ofsrc=":srcdoc="<p>This video loads after you accept marketing cookies.</p>" data-consent-so
src="becomesdata-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
<iframetags; change both. -
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> -
Build and check a page with a video: its iframe has
data-consent-src="https://www.youtube.com/embed/..."(orhttps://player.vimeo.com/video/...) and nosrc, 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.
-
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.htmlIf
curlreports a 404, use the older path.../embedded/templates/shortcodes/x.html. On Hugo before 0.146, createlayouts/shortcodes/instead and save it there. -
In the line that builds the query, add
"omit_script" true:{{- $query := querify "url" $url "dnt" .dnt "omit_script" true -}} -
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> -
Build and check a page with a post: the
<blockquote class="twitter-tweet">is followed by the markedwidgets.jsscript 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.comorplatform.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:
| Directive | Add | Why |
|---|---|---|
script-src | https://cdn.smartgenie.co.uk and 'unsafe-inline' | the engine, and the snippet's inline part (the marked scripts above are inline too) |
connect-src | https://smartconsentsettings.blob.core.windows.net | your site's banner settings |
connect-src | https://cdn.smartgenie.co.uk | banner languages |
connect-src | https://api.smartgenie.co.uk | consent records |
style-src | 'unsafe-inline' | the banner's styles |
img-src | your logo's address | only 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.