Hypertekt › Docs › Guides

Analytics, other scripts and secrets

How to add analytics and other services' code to a site, how consent, previews and speed work for that code, and how to keep a site's secrets.

Every published site already counts its visitors, with no script to add and no consent to ask for: see Count visitors and what they do. For anything more, there are three ways to add a script, from the simplest to the most open: an integration, a snippet, or a <script> in a template. For an integration or a snippet, Hypertekt asks visitors for consent where the law requires it, and keeps the script out of previews.

Integrations

Well-known services take just an ID in site.yaml. Hypertekt writes their code on every page, in the right place:

integrations:
  googleAnalytics: G-XXXXXXXXXX
  plausible: example.com
  googleSiteVerification: rXz3Q9vB2kLmN7pT4yWc8dHfJ6sE1aUo0iGqV5xYbKw

The services are Google Analytics (googleAnalytics), Google Ads (googleAds), Google Tag Manager (googleTagManager), the Meta Pixel (metaPixel), Microsoft Clarity (clarity), Plausible (plausible), Fathom (fathom) and GoatCounter (goatCounter). There are also the verification tags that Google Search Console and Bing Webmaster Tools ask for (googleSiteVerification, bingSiteVerification). Hypertekt checks that each ID has the right format for its service. The reference has a table of the keys, with an example ID and the consent category of each.

When a service tells you to paste its code, and Hypertekt has an integration for that service, use the integration instead: it handles consent and previews correctly.

Snippets

For other code that says “paste this code into your site”, like a chat widget or a booking button, put the code in snippets/ exactly as it was pasted. The snippet’s front matter says where the code goes. snippets/chat.html:

---
label: Chat widget
place: body-end
pages: all
consent: functional
---
<script src="https://widget.example.com/loader.js" data-account="4821" async></script>
  • place is head, body-start or body-end (the default).
  • pages is all, or a list of paths, like [/contact, /menu/*].
  • consent is required. It’s none for code that stores nothing on the visitor’s device, and necessary for code the site can’t work without. Code in functional, analytics or marketing waits for the visitor to agree.

A snippet is never treated as a template, so a {{ in a snippet is written to the page as it is.

Scripts in templates

A <script> in a layout or page is part of how the site works. It runs everywhere, including in previews. Hypertekt doesn’t ask for consent for a script written this way, so don’t use one for analytics or advertising: use an integration or a snippet. A template’s values are written into a script with the json pipe:

<script type="module">
  const hours = {{ site.business.hours | json }};
</script>

Keep a library, like a carousel or a map, as files in assets/. Don’t load the library from someone else’s server, where its version can change. Before adding a library, check the built-in components: ht/youtube, ht/map, ht/dialog and ht/picture.

  • Consent. Visitors from the EU, the UK and Switzerland see a cookie banner, and so does any visitor whose country can’t be determined. Integrations and snippets in functional, analytics or marketing wait until the visitor agrees to their category. Everywhere else, those scripts run right away, with no banner. Google’s tags get Consent Mode signals. A link with data-consent-settings, like “Cookie settings” in the footer, opens the banner again.
  • Not in previews. Integrations and snippets don’t run in the preview (the Preview tab and the preview link) or in tekt start, so your own visits aren’t counted. tekt start --published builds the site the way it is published, with its integrations and snippets. Add --country to choose the visitor’s country, so you can see the banner.
  • Speed. A <script src> in the <head> that isn’t async, defer or a module holds up the page. The checker warns about it.

Secrets

A secret, like an API key a server function uses, is never in the site’s files. On Hypertekt, a secret is kept with the site, and nobody can read its value back once it’s set:

tekt secrets                     # which are set, by name, and which the site's files ask for
tekt secrets set LASTFM_API_KEY  # typed at a prompt that doesn't show it
tekt secrets push                # sets the ones in .env that the site's files ask for
tekt secrets delete LASTFM_API_KEY

Secrets can also be set in the site’s Settings › Secrets. On your own computer, secrets are in the site’s .env. tekt keeps .env out of git, and Hypertekt turns away a push that adds a .env file.