Documentation menuBackend frameworks: Flask

Backend frameworks

Add Loogika to Flask

A Flask app builds its pages from Jinja templates, and those templates usually share one base layout. You add the Loogika tag to the head of that layout. Flask gives every template access to app.config under the name config, so you store the site ID there.

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, and you set it as an environment variable in the next step.

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

Where do you store the site ID in Flask?

Set the site ID in the environment of the app. With python-dotenv installed, flask run picks the value up from a .env file too:

LOOGIKA_SITE_ID=YOUR_SITE_ID

Copy the value into app.config in the file that creates the app:

import os

from flask import Flask, render_template

app = Flask(__name__)
app.config["LOOGIKA_SITE_ID"] = os.environ.get("LOOGIKA_SITE_ID")


@app.route("/")
def index():
    return render_template("index.html")

Some apps hold their configuration in a Python class or file. In that case, put LOOGIKA_SITE_ID in it and let app.config.from_object() load the value.

Why should you not use from_prefixed_env for the site ID?

The app.config.from_prefixed_env() method parses each value as JSON, and that can change a site ID. An ID such as 123456e78901 reads as a number in scientific notation, so Flask stores a float that no longer matches your site. Reading the variable with os.environ keeps the ID as text.

How do you add the tag to the base layout?

Open the template your pages extend and add the tag inside the head element. Most projects name that file templates/base.html or templates/layout.html.

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

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

The config object is part of the standard template context in Flask, so you need no context processor. When LOOGIKA_SITE_ID has no value, the {% if %} block leaves the tag out, which keeps local development out of the statistics.

A template that does not extend the base layout 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 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=. When the tag is present, check the Network tab of your browser for requests to /api/track with status 200. The script troubleshooting page lists more causes.

Why does Flask not send data to Loogika?

CauseFix
The app still runs with the old valueFlask reads the environment when the app is created. Restart the server after you change LOOGIKA_SITE_ID. To see the value the app holds, print app.config["LOOGIKA_SITE_ID"] from flask shell
A macro prints the tag from an imported templateAn imported template does not receive config, because Flask only places it in the render context. Import the macro with context
A Content Security Policy blocks the scriptIf Flask-Talisman or a similar extension sets a policy, allow the Loogika domain with the directives below, or use 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;