Documentation menuContent management systems: Craft CMS

Content management systems

Add Loogika to Craft CMS

In Craft CMS, pages come from the Twig templates stored in the templates folder. Your section templates extend a base template, and the Loogika tag goes there, right above the closing head tag. Every page built on that base template then loads the script.

Last updated .

What do you need before you start?

Copy your script tag from Site Settings in Loogika, under the Tracking Script tab. It looks like this, with your own site ID in place of YOUR_SITE_ID:

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

Which Craft CMS template holds the head?

The answer depends on how far the project has come. A fresh craftcms/craft project ships one file, templates/index.twig, with the full HTML document in it.

Most Craft projects move that HTML into a base template that every page extends. The file name of a base template starts with an underscore, for example templates/_layout.twig.

How do you add Loogika to the base template?

Open the template that holds your <head> and add the tag before </head>. A base template with the tag in place looks like this:

<!DOCTYPE html>
<html lang="{{ currentSite.language }}">
<head>
    <meta charset="utf-8">
    <title>{{ siteName }}</title>
    <script src="https://app.loogika.com/api/script.js?siteId=YOUR_SITE_ID" defer></script>
</head>
<body>
    {% block content %}{% endblock %}
</body>
</html>

A section template that extends the base template loads the script without any change of its own. The entry template below is an example:

{% extends "_layout" %}

{% block content %}
    <h1>{{ entry.title }}</h1>
    {{ entry.body }}
{% endblock %}

How do you store the site ID outside the template?

Read the site ID from your .env file. Add a line such as LOOGIKA_SITE_ID=YOUR_SITE_ID there, and each environment can carry its own value. The template then prints the value with getenv:

<script src="https://app.loogika.com/api/script.js?siteId={{ getenv('LOOGIKA_SITE_ID') }}" defer></script>

You can also store the site ID in a field of a Global Set and print that field in the template.

How do you check that Loogika is installed?

Open your site in another tab and visit a few pages. The pageviews show up in the Loogika dashboard within a few seconds. If nothing arrives, open the page source and look for script.js?siteId=.

Why does Craft CMS not show the tag?

Template caching is one cause. When {% cache %} tags surround the head, Craft keeps serving the old version until the cache expires. The command php craft clear-caches/all empties the cache at once.

A second cause is a section built on a different base template, such as _layout-landing.twig. Every base template needs the tag. You can also move the tag into a partial and pull that partial in from every base template with {% include %}.

Does Live Preview count as visits?

Yes, unless you exclude it. Live Preview in the control panel shows your template inside an iframe and adds x-craft-live-preview to the query string. A condition around the tag leaves editor previews out of your data:

{% if not craft.app.request.isPreview %}
    <script src="https://app.loogika.com/api/script.js?siteId=YOUR_SITE_ID" defer></script>
{% endif %}

The Craft control panel itself is never tracked. The pages under /admin use the templates of Craft, so the Loogika tag does not run there.