Static site generators
Add Loogika to Eleventy
Eleventy builds each page with a layout from the _includes folder. You add the Loogika tag to the head of your base layout, and a global data file holds the site ID. The tag is then part of every page that uses the layout.
Last updated .
What do you need before you start?
Copy your script tag from Site Settings in Loogika, under the Tracking Script tab. The site ID in the tag is a 12-character string, shown here as YOUR_SITE_ID:
<script src="https://app.loogika.com/api/script.js?siteId=YOUR_SITE_ID" defer></script>
How do you add Loogika to Eleventy?
You store the site ID in a data file and print the tag from the base layout. A data file under _data is available in every template, so no layout holds a hard-coded ID.
- Create the file
_data/loogika.jsonwith the content below. Create the_datafolder first if your project has none. - Open your base layout, for example
_includes/base.njkor_includes/base.liquid, and add the tag inside the head element. - Run the build command and deploy the generated
_sitefolder.
The data file holds one value. Eleventy names the data after the file, so templates read it as loogika.siteId.
{
"siteId": "YOUR_SITE_ID"
}
The layout code is the same in Nunjucks and in Liquid:
<head>
...
{% if eleventy.env.runMode == "build" and loogika.siteId %}
<script src="https://app.loogika.com/api/script.js?siteId={{ loogika.siteId }}" defer></script>
{% endif %}
</head>
Build the site with this command:
npx @11ty/eleventy
Why does the layout check the run mode?
The run mode check keeps your own visits during development out of the statistics. Eleventy sets eleventy.env.runMode to build for a plain build, and to another value when you start it with --serve or --watch. The second half of the condition leaves the tag out when the data file has no site ID.
How do you check that Loogika is installed?
Open the deployed site in another tab and visit a few pages. The pageviews show up in the Loogika dashboard within a few seconds.
If the dashboard stays empty, open the page source and look for script.js?siteId=. When the tag is there, open the Network tab of your browser and look for requests to /api/track with status 200. The script troubleshooting page lists more causes, including ad blockers.
Why is the tag missing from an Eleventy page?
| Cause | Fix |
|---|---|
| You look at the local preview | npx @11ty/eleventy --serve sets the run mode to serve, and the condition removes the tag. Look at what a plain build produces instead |
| The site runs on Eleventy 1.x | eleventy.env.runMode arrived in Eleventy 2.0. On older versions, remove the run mode condition and keep the loogika.siteId check |
| The project uses its own data folder | When dir.data is set in your config, loogika.json belongs in the folder it names |
| The page uses another layout | The tag reaches a page only if the layout you edited is the last link in that page's layout chain. Add the tag to other base layouts and to pages with layout: none |