Documentation menuBackend frameworks: FastAPI

Backend frameworks

Add Loogika to FastAPI

FastAPI serves HTML through Jinja2Templates, so the Loogika tag belongs in the head of the base template that the other pages build on. The site ID lives in a pydantic-settings class, and one Jinja global hands it to the templates. Every template can then print the tag without help from the routes.

Last updated .

Which FastAPI routes can Loogika track?

Loogika tracks the routes that return HTML rendered from a template, because its script needs a browser to run in. A JSON endpoint has no page for the script to run on.

What do you need before you start?

Copy your script tag from Site Settings in Loogika, under the Tracking Script tab. You need the 12-character site ID from the tag for the .env file below.

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

Where do you store the site ID in FastAPI?

The site ID belongs in a settings class. That class takes its values from the environment, and from a .env file when one exists. The project needs the pydantic-settings and jinja2 packages, so add whichever is missing:

pip install pydantic-settings jinja2

Define the settings in a file such as config.py:

from pydantic_settings import BaseSettings, SettingsConfigDict


class Settings(BaseSettings):
    model_config = SettingsConfigDict(env_file=".env")

    loogika_site_id: str = ""


settings = Settings()

Then add the site ID to the .env file:

LOOGIKA_SITE_ID=YOUR_SITE_ID

How do you pass the site ID to the templates?

Register the site ID as a Jinja global. Jinja2Templates exposes its Jinja environment as templates.env, so you set the global once, next to the line that creates the templates object:

from fastapi import FastAPI, Request
from fastapi.responses import HTMLResponse
from fastapi.templating import Jinja2Templates

from config import settings

app = FastAPI()
templates = Jinja2Templates(directory="templates")
templates.env.globals["loogika_site_id"] = settings.loogika_site_id


@app.get("/", response_class=HTMLResponse)
async def index(request: Request):
    return templates.TemplateResponse(request=request, name="index.html")

How do you add the tag to the base template?

The tag goes in the head element of the shared base template, which most projects name templates/base.html:

<head>
    <meta charset="utf-8">
    <title>{% block title %}My App{% endblock %}</title>

    {% if loogika_site_id %}
    <script src="https://app.loogika.com/api/script.js?siteId={{ loogika_site_id }}" defer></script>
    {% endif %}
</head>

With an empty LOOGIKA_SITE_ID, the {% if %} block leaves the tag out. Local development therefore stays untracked until you give the variable a value on your machine. A template that does not extend the base template needs the same block in its own head element.

How do you check that Loogika is installed?

Load the deployed app in a fresh browser tab and visit a few HTML pages. The pageviews appear in the Loogika dashboard within a few seconds.

If nothing arrives, open the page source and look for script.js?siteId=. The script troubleshooting page shows how to test the script from the browser console and lists common causes.

Why does FastAPI not send data to Loogika?

CauseFix
The app still runs with the old valueSettings() reads the environment when Python imports the module. Restart Uvicorn, or your process manager, after you change LOOGIKA_SITE_ID
The app has several Jinja2Templates instancesJinja globals belong to one environment. When each router builds a separate Jinja2Templates object, give every object the global, or let all routers import a single shared instance
A Content Security Policy blocks the scriptIf middleware sets a policy, allow the Loogika domain with the directives below, or load the script through a proxy on your own domain

A Content Security Policy must allow Loogika in two directives:

script-src 'self' https://app.loogika.com; connect-src 'self' https://app.loogika.com;