Angular provides several approaches for integrating third-party scripts. Here are the recommended methods for adding Visitors analytics to your Angular application.
YOUR_TOKEN with your project token.The simplest approach is to add the script tag directly to your src/index.html file.
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Angular App</title>
<base href="/">
<meta name="viewport" content="width=device-width, initial-scale=1">
<script
src="https://cdn.visitors.now/v.js"
data-token="YOUR_TOKEN">
</script>
</head>
<body>
<app-root></app-root>
</body>
</html>For more control, you can inject the script programmatically in your app component:
import { Component, Inject, OnInit } from '@angular/core';
import { DOCUMENT } from '@angular/common';
@Component({
selector: 'app-root',
templateUrl: './app.component.html'
})
export class AppComponent implements OnInit {
constructor(@Inject(DOCUMENT) private document: Document) {}
ngOnInit(): void {
const script = this.document.createElement('script');
script.src = 'https://cdn.visitors.now/v.js';
script.setAttribute('data-token', 'YOUR_TOKEN');
script.async = true;
this.document.head.appendChild(script);
}
}