Backend frameworks
Add Loogika to Ruby on Rails
The Loogika tag goes into the head of app/views/layouts/application.html.erb, the layout Rails puts around every page. The site ID comes from a custom config.x setting that reads an environment variable. No template holds a hard-coded ID, and you can point every environment at a separate site.
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>
Where do you store the site ID in Rails?
Set the site ID as an environment variable. Use the .env file if the app runs dotenv-rails, and the environment settings of your host otherwise:
LOOGIKA_SITE_ID=YOUR_SITE_ID
Next, open config/application.rb and expose the value under the config.x namespace:
module MyApp
class Application < Rails::Application
# ... existing configuration
config.x.loogika.site_id = ENV["LOOGIKA_SITE_ID"]
end
end
How do you add the tag to the layout?
The tag belongs in the head element of app/views/layouts/application.html.erb, below the tags for the stylesheet and the JavaScript:
<head>
<title><%= content_for(:title) || "MyApp" %></title>
<meta name="viewport" content="width=device-width,initial-scale=1">
<%= csrf_meta_tags %>
<%= csp_meta_tag %>
<%= stylesheet_link_tag "application", "data-turbo-track": "reload" %>
<%= javascript_importmap_tags %>
<% if Rails.configuration.x.loogika.site_id.present? %>
<script src="https://app.loogika.com/api/script.js?siteId=<%= Rails.configuration.x.loogika.site_id %>" defer></script>
<% end %>
</head>
Where LOOGIKA_SITE_ID has no value, the if block leaves the tag out. Loogika therefore tracks development and test environments only after you define the variable in them.
What if the Rails app uses several layouts?
Every HTML layout needs the tag, so application.html.erb and admin.html.erb would both get it. A partial saves the repetition. Place the tag in a file like app/views/layouts/_loogika.html.erb, then call <%= render "layouts/loogika" %> in each layout. Mailer layouts do not need the tag.
Does Loogika track Turbo Drive visits?
Yes, as long as SPA Navigation is on in Site Settings, which is the default. For each visit, Turbo Drive swaps the page body, merges the head and adds the new address to the browser history. The Loogika script watches those history changes and records every Turbo visit as a pageview.
You need no listener for turbo:load. The script tag sits in the head and is identical on every page, so Turbo Drive keeps the existing tag and does not load it again.
How do you check that Loogika is installed?
Visit the running app in another browser tab and open a few pages. Loogika's dashboard shows those pageviews within a few seconds.
If nothing arrives, check whether the page source contains script.js?siteId=. When the tag is on the page, check the Network tab of your browser for requests to /api/track with status 200. The script troubleshooting page lists more causes.
Why does Rails not send data to Loogika?
| Cause | Fix |
|---|---|
| The app still runs with the old value | Rails reads config/application.rb once at boot. Restart the server after you change LOOGIKA_SITE_ID. To see the value the app uses, open rails console and print Rails.configuration.x.loogika.site_id |
| A Content Security Policy blocks the script | If config/initializers/content_security_policy.rb sets a policy, add https://app.loogika.com to script_src and connect_src. You can also use a proxy on your own domain, which needs no extra origin |
Without both entries, the browser blocks the script and the requests to /api/track. The resulting policy header contains these two directives:
script-src 'self' https://app.loogika.com; connect-src 'self' https://app.loogika.com;