Integration with Angular
Angular integration is not much different than Pure JavaScript integration. You only need to add Metricalp script to your index.html file. Then Metricalp will automatically be integrated to your Angular app and will be starting to tracking screen view events. While we do not have an official Angular library yet (planning it for future), you do not even need that. With this simple steps you will be integrated Metricalp's most recent and stable version always and it will be type safe. In this documentation we will also learn how to fire custom events. With that method, you can also disable auto route catch and fire screen_view events manually. To details please check Custom Events & Props
Add Metricalp script:
We assume that you bootstrapped a new Angular project with ng cli like `ng new my-app`. Then inside of your index.html file (src/index.html) add Metricalp script as described in Pure JavaScript Embed
<script src="https://cdn.metricalp.com/event/metricalp.js" data-allow-localhost="true" data-tid="YOUR_TID" defer></script>
- Do not forget to change YOUR_TID with your tid number a.k.a Tracker ID as described in Embed Share Tracker
We added allowLocalhost because defaultly Metricalp does not work in localhost to protect your quota limits. We allowed localhost for testing purposes. You would want to remove it after testing it once to save your quota. For detailed info check allow localhost & exclude me docs.
That's all, you integrated your Angular app with Metricalp, hooray 🚀
Defaultly, Metricalp automatically starts to collecting screen_view events and route changes (it can be disabled with data-disable-auto-route-catch option please check API reference for details and all other customizations).
Fire custom events with type safety:
Let's create a new file metricalp-tracker.ts in root of app (src/app/metricalp-tracker.ts)
type MetricalpEvent = {
type: string;
[key: string]: string | number | boolean | undefined;
};
declare global {
interface Window {
metricalp?: { event: (ev: MetricalpEvent) => void };
}
}
export enum MetricalpEventTypes {
ScreenView = 'screen_view',
ClickSave = 'click_save',
}
export function metricalpEvent(event: MetricalpEvent) {
if (typeof window === 'undefined' || !window['metricalp']?.event) {
return;
}
window.metricalp?.event(event);
}
Now we can use metricalpEvent function to fire custom events. Let's import and use it in our app. For example (src/app/app.component.ts) and fire a custom event when user clicks save button.
import { Component } from '@angular/core';
import { CommonModule } from '@angular/common';
import { RouterOutlet } from '@angular/router';
import { MetricalpEventTypes, metricalpEvent } from './metricalp-tracker';
@Component({
selector: 'app-root',
standalone: true,
imports: [CommonModule, RouterOutlet],
templateUrl: './app.component.html',
styleUrl: './app.component.css',
})
export class AppComponent {
title = 'ng-metricalp-example';
handleClick() {
metricalpEvent({ type: MetricalpEventTypes.ClickSave });
}
}
Edit src/app/app.component.html file and add a button to test it.
<main class="main">
<button (click)="handleClick()">Save</button>
</main>
<router-outlet></router-outlet>
That is all, now you can fire custom events with type safety because we used event types as Enums. You can also disable auto route catch and fire a screen_view event manually with MetricalpEventTypes.ScreenView as we defined above. Also you can attach extra data to event as custom props, for details please check Custom Events & Props
Also please check full API Reference to customize script as you wish: API reference