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.
- Store the site ID in the
paramssection of the Hugo config file. Current Hugo versions name the filehugo.toml, and older ones useconfig.toml. - Create the file
layouts/partials/loogika-analytics.htmlwith the partial code below. Create thelayouts/partialsfolder if it does not exist. - Include the partial inside the head element of your base template, which is typically
layouts/_default/baseof.html. - Run
hugoto build the site, then upload thepublicfolder 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?
| Cause | Fix |
|---|---|
| You look at the local preview | hugo 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 render | A 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 empty | Check that loogikaSiteID sits under params in the config file and has your site ID as its value |