Backend frameworks
Add Loogika to Phoenix
The root layout of Phoenix, root.html.heex, holds the html and head elements and wraps each page the app renders. You add the Loogika tag to that file. The site ID comes from application config that config/runtime.exs reads from an environment variable when the app boots.
Last updated .
What do you need before you start?
Copy your script tag from Site Settings in Loogika, under the Tracking Script tab. The 12-character site ID in the tag is the value you set as an environment variable below.
<script src="https://app.loogika.com/api/script.js?siteId=YOUR_SITE_ID" defer></script>
The examples use my_app and MyAppWeb as names. Replace them with the names of your own app.
Where do you store the site ID in Phoenix?
Read the site ID in config/runtime.exs. Phoenix runs that file when the app boots, also inside a release, which makes it the right place to read an environment variable. Give your OTP app a :loogika key:
config :my_app, :loogika,
site_id: System.get_env("LOOGIKA_SITE_ID")
Then set the variable where the app runs:
export LOOGIKA_SITE_ID=YOUR_SITE_ID
How do you make the config available in the layout?
Add a function to the MyAppWeb.Layouts module that returns the config. That module embeds every template from the lib/my_app_web/components/layouts/ folder, so root.html.heex can call the function. The module sits in lib/my_app_web/components/layouts.ex:
defmodule MyAppWeb.Layouts do
use MyAppWeb, :html
embed_templates "layouts/*"
def loogika_config, do: Application.get_env(:my_app, :loogika, [])
end
How do you add the tag to the root layout?
The tag goes in the head element of lib/my_app_web/components/layouts/root.html.heex. Place it below the tags for app.css and app.js that the file already has:
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<meta name="csrf-token" content={get_csrf_token()} />
<link phx-track-static rel="stylesheet" href={~p"/assets/app.css"} />
<script defer phx-track-static type="text/javascript" src={~p"/assets/app.js"}></script>
<script
:if={loogika_config()[:site_id]}
src={"https://app.loogika.com/api/script.js?siteId=#{loogika_config()[:site_id]}"}
defer
></script>
</head>
When LOOGIKA_SITE_ID has no value, the :if attribute leaves the tag out. Development and test environments therefore stay untracked until you define the variable in them.
Does Loogika track LiveView navigation?
Yes, as long as SPA Navigation is on in Site Settings, which is the default. LiveView changes the address through the browser history and keeps the root layout in place, including its head element. The Loogika script watches those history changes and records each one as a pageview.
Live navigation in Phoenix covers <.link navigate>, <.link patch>, push_navigate/2 and push_patch/2. You need no listener for phx: events. A regular <.link href> causes a full page load, which Loogika also records as a pageview.
How do you check that Loogika is installed?
Load the running 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=. The script troubleshooting page shows how to test the script from the browser console and lists common causes.
Why does Phoenix not send data to Loogika?
| Cause | Fix |
|---|---|
| The app still runs with the old value | Phoenix evaluates config/runtime.exs at boot. Restart the app or the release after you change LOOGIKA_SITE_ID, then start iex -S mix and print the value with Application.get_env(:my_app, :loogika) |
| A Content Security Policy blocks the script | If a plug sets a content-security-policy header, 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;