Set up HumanBehavior SDK with Angular applications
npm install humanbehavior-js # or yarn add humanbehavior-js
main.ts
import { platformBrowserDynamic } from '@angular/platform-browser-dynamic'; import { AppModule } from './app/app.module'; import { HumanBehaviorTracker } from 'humanbehavior-js'; // Initialize the tracker once globally const tracker = HumanBehaviorTracker.init('your-api-key-here'); platformBrowserDynamic().bootstrapModule(AppModule) .catch(err => console.error(err));
app.component.ts
import { Component, OnInit } from '@angular/core'; import { Router, NavigationEnd } from '@angular/router'; import { filter } from 'rxjs/operators'; import { HumanBehaviorTracker } from 'humanbehavior-js'; @Component({ selector: 'app-root', template: '<router-outlet></router-outlet>' }) export class AppComponent implements OnInit { private tracker = HumanBehaviorTracker.init('your-api-key-here'); constructor(private router: Router) {} ngOnInit() { // Track route changes this.router.events.pipe( filter(event => event instanceof NavigationEnd) ).subscribe((event: any) => { this.tracker.trackPageView(event.urlAfterRedirects); }); } }
import { Component, OnInit } from '@angular/core'; import { HumanBehaviorTracker } from 'humanbehavior-js'; @Component({ selector: 'app-my-component', template: ` <div> <h1>My Component</h1> <button (click)="handleButtonClick()">Click Me</button> </div> ` }) export class MyComponent implements OnInit { private tracker = HumanBehaviorTracker.init('your-api-key-here'); ngOnInit() { // Track component view this.tracker.customEvent('component_viewed', { componentName: 'MyComponent' }); } async handleButtonClick() { await this.tracker.customEvent('button_clicked', { buttonLocation: 'header' }); } }
src/environments/environment.ts
export const environment = { production: false, humanBehaviorApiKey: 'your-api-key-here' };
import { environment } from '../environments/environment'; import { HumanBehaviorTracker } from 'humanbehavior-js'; const tracker = HumanBehaviorTracker.init(environment.humanBehaviorApiKey);