Documentation menuStatic site generators: Hugo

Static site generators

Add Loogika to Hugo

Hugo builds every page from the templates under layouts. You put the Loogika tag in a partial template and include that partial from the base template, so every page gets it. The site ID sits in the Hugo config file as a site parameter.

Last updated .

What do you need before you start?

You need the script tag for your site. Loogika shows it in Site Settings, under the Tracking Script tab, with your 12-character site ID in place of YOUR_SITE_ID:

<script src="https://app.loogika.com/api/script.js?siteId=YOUR_SITE_ID" defer></script>

How do you add Loogika to Hugo?

You add one parameter, create one partial and include it from the base template.

  1. Store the site ID in the params section of the Hugo config file. Current Hugo versions name the file hugo.toml, and older ones use config.toml.
  2. Create the file layouts/partials/loogika-analytics.html with the partial code below. Create the layouts/partials folder if it does not exist.
  3. Include the partial inside the head element of your base template, which is typically layouts/_default/baseof.html.
  4. Run hugo to build the site, then upload the public folder it generates.
[params]
  loogikaSiteID = "YOUR_SITE_ID"

The partial prints the tag when two conditions are true. Hugo must not run as a local server, and the site ID parameter must have a value.

{{ if and (not hugo.IsServer) .Site.Params.loogikaSiteID }}
  <script src="https://app.loogika.com/api/script.js?siteId={{ .Site.Params.loogikaSiteID }}" defer></script>
{{ end }}

The base template includes the partial like this:

<head>
  ...
  {{ partial "loogika-analytics.html" . }}
</head>

What if the theme owns the base template?

Copy the baseof.html file of the theme into the layouts/_default folder of your own project and edit the copy. Hugo uses the project file before the theme file, so a theme update leaves your change alone.

You can also include the Loogika partial from a footer partial of the theme, for example layouts/partials/footer.html.

How do you check that Loogika is installed?

Load the deployed Hugo site in a fresh browser tab and visit a few pages. The Loogika dashboard lists the pageviews within a few seconds.

If the dashboard stays empty, open the page source and look for script.js?siteId=. When the tag is on the page, check the Network tab of your browser for requests to /api/track with status 200. Ad blockers can stop those requests, and the proxy guide explains how your own domain can serve the script.

Why is the tag missing from a Hugo page?

CauseFix
You look at the local previewhugo server never prints the tag, because the partial checks hugo.IsServer. Run a production build with hugo and inspect what it writes
You edited a base template that Hugo does not renderA layouts/_default/baseof.html file in your project takes precedence over the copy in the theme. Make sure the partial is included in the file Hugo uses
The site parameter is emptyCheck that loogikaSiteID sits under params in the config file and has your site ID as its value