Documentation menuJavaScript frameworks: Angular

JavaScript frameworks

Add Loogika to Angular

You add Loogika to an Angular app by pasting one script tag into src/index.html. An Angular CLI project serves that single HTML file for every route, so the script loads once and covers the whole app.

Last updated .

What do you need before you start?

Copy your script tag from Site Settings in the Loogika dashboard, under the Tracking Script tab. The tag carries your site ID, a 12-character string, 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 the Loogika script to Angular?

The tag goes in src/index.html, just before the closing </head> tag. The file then looks like this:

<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <title>MyAngularApp</title>
  <base href="/">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <script src="https://app.loogika.com/api/script.js?siteId=YOUR_SITE_ID" defer></script>
</head>
<body>
  <app-root></app-root>
</body>
</html>

The tag also works just before the closing </body> tag. Inside the head element the browser starts to fetch the script sooner.

Does Loogika track Angular Router navigation?

Yes. Loogika records a pageview each time the Angular Router changes the URL, and you write no extra code for it. The setting behind it is SPA Navigation in Site Settings, which is on for a new site.

The Loogika script waits 500 milliseconds after a route change before it records the pageview. A redirect inside the Angular app is then counted once, not twice.

How do you track custom events in Angular?

Any component or service can send a custom event with window.loogika.event(). TypeScript does not know the loogika object, so declare it on window once. Check that the object exists before you call it, because a blocked script would otherwise throw an error.

import { Component } from "@angular/core";

declare global {
  interface Window {
    loogika?: {
      event: (eventName: string, eventData?: Record<string, any>) => void;
      pageview: () => void;
    };
  }
}

@Component({
  selector: "app-feature",
  template: `<button (click)="onFeatureClick()">Use Feature</button>`,
})
export class FeatureComponent {
  onFeatureClick(): void {
    if (window.loogika) {
      window.loogika.event("feature_used", { featureName: "Export" });
    }
  }
}

For a plain click you can skip the TypeScript. Put data-loogika-event="feature_used" on the button in the template, and Loogika sends the event on each click. The event tracking page lists the limits for names and properties.

How do you check that Loogika is installed?

Open the deployed Angular app in another tab and move between a few routes. The pageviews appear in the Loogika dashboard within a few seconds.

If the dashboard stays empty, open the page source and look for script.js?siteId=. In the Network tab of the browser, script.js should return status 200 and POST requests should go to /api/track.

Why are pageviews missing after an Angular route change?

Missing pageviews after navigation are rare in Angular. First check that SPA Navigation is still on in Site Settings, under the Tracking tab. With that setting off, Loogika only counts full page loads.

If the setting is on and route changes still go missing, record them yourself. Listen to Router.events and keep only the NavigationEnd events. For each one, call window.loogika.pageview(). Skip the first NavigationEnd, because the script has already counted the landing page.

An ad blocker can also stop the script for some visitors. The proxy guide shows how your own domain can serve the script.