This guide shows you how to add Heyo to your Angular application. Works with all Angular versions including the latest releases.
Install the SDK for full TypeScript support:
npm install @heyo.so/js
Then initialize it in your app.component.ts:
import { Component, OnInit } from '@angular/core';
import HEYO from '@heyo.so/js'; // or: import { HEYO } from '@heyo.so/js'
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css']
})
export class AppComponent implements OnInit {
title = 'my-app';
ngOnInit() {
HEYO.init({ projectId: 'YOUR_PROJECT_ID' });
}
}
Load the script dynamically in your app.component.ts:
import { Component, OnInit, Renderer2, Inject } from '@angular/core';
import { DOCUMENT } from '@angular/common';
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css']
})
export class AppComponent implements OnInit {
constructor(
private renderer: Renderer2,
@Inject(DOCUMENT) private document: Document
) {}
ngOnInit() {
const script = this.renderer.createElement('script');
script.src = 'https://heyo.so/embed/script';
script.defer = true;
script.setAttribute('data-project-id', 'YOUR_PROJECT_ID');
this.renderer.appendChild(this.document.head, script);
}
}
The Heyo SDK includes full TypeScript definitions. Add type declarations if needed:
declare global {
interface Window {
HEYO: {
init: (config: { projectId: string; user?: any }) => void;
show: () => void;
hide: () => void;
toggle: () => void;
};
}
}
export {};
Create services/chat.service.ts:
import { Injectable } from '@angular/core';
import HEYO from '@heyo.so/js'; // or: import { HEYO } from '@heyo.so/js'
@Injectable({
providedIn: 'root'
})
export class ChatService {
constructor() {
HEYO.init({ projectId: 'YOUR_PROJECT_ID' });
}
show() {
HEYO.show();
}
hide() {
HEYO.hide();
}
toggle() {
HEYO.toggle();
}
setUser(user: { name: string; email: string }) {
HEYO.init({
projectId: 'YOUR_PROJECT_ID',
user
});
}
}
Usage in a component:
import { Component } from '@angular/core';
import { ChatService } from './services/chat.service';
@Component({
selector: 'app-support',
template: '<button (click)="openChat()">Need Help?</button>'
})
export class SupportComponent {
constructor(private chatService: ChatService) {}
openChat() {
this.chatService.show();
}
}
Use RxJS for reactive chat control:
import { Injectable } from '@angular/core';
import { BehaviorSubject } from 'rxjs';
import HEYO from '@heyo.so/js'; // or: import { HEYO } from '@heyo.so/js'
@Injectable({
providedIn: 'root'
})
export class ChatService {
private chatVisible$ = new BehaviorSubject<boolean>(false);
constructor() {
HEYO.init({ projectId: 'YOUR_PROJECT_ID' });
}
get isChatVisible$() {
return this.chatVisible$.asObservable();
}
toggle() {
const newState = !this.chatVisible$.value;
if (newState) {
HEYO.show();
} else {
HEYO.hide();
}
this.chatVisible$.next(newState);
}
}
import { Component, OnInit } from '@angular/core';
import { AuthService } from './services/auth.service';
import HEYO from '@heyo.so/js'; // or: import { HEYO } from '@heyo.so/js'
@Component({
selector: 'app-root',
templateUrl: './app.component.html'
})
export class AppComponent implements OnInit {
constructor(private authService: AuthService) {}
ngOnInit() {
this.authService.user$.subscribe(user => {
if (user) {
HEYO.init({
projectId: 'YOUR_PROJECT_ID',
user: {
name: user.name,
email: user.email
}
});
}
});
}
}
Show/hide chat based on current route:
import { Component, OnInit } from '@angular/core';
import { Router, NavigationEnd } from '@angular/router';
import { filter } from 'rxjs/operators';
@Component({
selector: 'app-root',
templateUrl: './app.component.html'
})
export class AppComponent implements OnInit {
constructor(private router: Router) {}
ngOnInit() {
this.router.events
.pipe(filter(event => event instanceof NavigationEnd))
.subscribe((event: NavigationEnd) => {
// Hide on checkout page
if (event.url.includes('/checkout')) {
window.HEYO?.hide();
} else {
window.HEYO?.show();
}
});
}
}
For server-side rendering, ensure Heyo loads only on the client:
import { Component, OnInit, PLATFORM_ID, Inject } from '@angular/core';
import { isPlatformBrowser } from '@angular/common';
import HEYO from '@heyo.so/js'; // or: import { HEYO } from '@heyo.so/js'
@Component({
selector: 'app-root',
templateUrl: './app.component.html'
})
export class AppComponent implements OnInit {
constructor(@Inject(PLATFORM_ID) private platformId: Object) {}
ngOnInit() {
if (isPlatformBrowser(this.platformId)) {
HEYO.init({ projectId: 'YOUR_PROJECT_ID' });
}
}
}
AppComponent for site-wide availabilityngOnInit)@heyo.so/js for proper type definitionsinit() in multiple components