Documentation menuDesktop, mobile and extensions: Flutter web

Desktop, mobile and extensions

Add Loogika to Flutter web

You add Loogika to a Flutter web app by pasting the script tag into the head of web/index.html. Flutter copies that template to build/web/index.html when you run flutter build web. Pageviews and events you send from Dart work as usual, while features that read HTML elements do not, because Flutter draws the interface into a canvas.

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>

How do you add Loogika to a Flutter web app?

The tag goes into the HTML template that Flutter serves the app from.

  1. In the Flutter project, find the file web/index.html and open it.
  2. Paste the tag inside the head element. Keep it above the flutter_bootstrap.js script, which Flutter generates itself.
  3. Build the app with flutter build web, then upload the build/web folder to your host.
<head>
  <meta charset="UTF-8" />
  <title>My App</title>
  <script src="https://app.loogika.com/api/script.js?siteId=YOUR_SITE_ID" defer></script>
</head>

Does Loogika track Flutter route changes?

Yes, with both URL strategies that Flutter offers. Route tracking depends on SPA Navigation in Site Settings, which is on for a new site.

URL strategyExample addressWhat Loogika records
Hash, the Flutter default/#/settingsThe path /settings. Loogika treats a hash that starts with #/ as a path
Path, set with usePathUrlStrategy()/settingsThe path /settings. The Navigator uses the History API, which the Loogika script listens to

How do you track events from Dart?

Dart reaches window.loogika.event() through dart:js_interop. The data-loogika-event attribute has nothing to attach to, because a Flutter widget is not an HTML element.

Create a small file that binds the JavaScript function to a Dart function:

// lib/analytics.dart
import 'dart:js_interop';

@JS('loogika.event')
external void _loogikaEvent(String name, [JSAny? properties]);

void trackEvent(String name, [Map<String, Object?>? properties]) {
  _loogikaEvent(name, properties?.jsify());
}

Then call trackEvent where the user acts, for example in a button handler:

ElevatedButton(
  onPressed: () {
    trackEvent('signup_click', {'plan': 'pro'});
  },
  child: const Text('Sign up'),
)

When the same code is also compiled for mobile or desktop, wrap the call in a kIsWeb check. The Loogika script only exists in the web build.

Which Loogika features do not work in Flutter web?

Features that observe HTML elements record nothing in a Flutter web app. The CanvasKit and Skwasm renderers paint the whole interface into a single canvas element, so the script finds no buttons or links to watch.

  • Session replay
  • Button click autocapture
  • Outbound link tracking
  • Click tracking with data-loogika-event attributes

Send the clicks you care about as events from Dart, as shown above.

How do you check that Loogika is installed?

Open the deployed app in a new tab and move between a few routes. 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=. If the tag is missing, run flutter build web again and deploy the new build/web folder.

What are common problems with Loogika in Flutter web?

SymptomCause and fix
loogika is undefined in DartThe tag uses defer, so the script is not ready while main() runs. Send events from user actions, not from app start-up
Deep links return 404 after a switch to usePathUrlStrategy()Path URLs only work when the web server answers each route with index.html. In development Flutter handles that rewrite itself, and production hosting must be set up for it
Clicks, outbound links and replays show nothingFlutter paints into a canvas. Use the Dart function for events