Getting Started
Konten ini belum tersedia dalam bahasa Anda.
-
Install the package
Terminal window npm i @capgo/capacitor-crispTerminal window pnpm add @capgo/capacitor-crispTerminal window yarn add @capgo/capacitor-crispTerminal window bun add @capgo/capacitor-crisp -
Sync with native projects
Terminal window npx cap syncTerminal window pnpm cap syncTerminal window yarn cap syncTerminal window bunx cap sync -
Get your Crisp Website ID
- Sign up at crisp.chat
- Create a website/project
- Find your Website ID in Settings > Setup Instructions
Basic Usage
Import the plugin and configure it with your Website ID:
import { CapacitorCrisp } from '@capgo/capacitor-crisp';
// Configure Crisp with your website IDconst configureCrisp = async () => { await CapacitorCrisp.configure({ websiteID: 'YOUR_WEBSITE_ID' });};
// Open the chatconst openChat = async () => { await CapacitorCrisp.openMessenger();};
// Set user informationconst setUserInfo = async () => { await CapacitorCrisp.setUser({ email: 'user@example.com', nickname: 'John Doe', phone: '+1234567890', avatar: 'https://example.com/avatar.jpg' });};
// Reset user session (logout)const logout = async () => { await CapacitorCrisp.resetChatSession();};
API Reference
configure(options)
Configure Crisp with your website ID and optional settings.
interface ConfigureOptions { websiteID: string; locale?: string; // e.g., 'en', 'fr', 'es' tokenID?: string; // For authenticated sessions}
await CapacitorCrisp.configure({ websiteID: 'YOUR_WEBSITE_ID', locale: 'en'});
openMessenger()
Opens the Crisp chat interface.
await CapacitorCrisp.openMessenger();
setUser(options)
Sets user information for the chat session.
interface UserOptions { email?: string; nickname?: string; phone?: string; avatar?: string;}
await CapacitorCrisp.setUser({ email: 'user@example.com', nickname: 'John Doe'});
setUserCompany(options)
Sets company information for business users.
interface CompanyOptions { name: string; url?: string; description?: string; employment?: { title?: string; role?: string; }; geolocation?: { city?: string; country?: string; };}
await CapacitorCrisp.setUserCompany({ name: 'Acme Corp', url: 'https://acme.com', employment: { title: 'CEO', role: 'Leadership' }});
pushEvent(options)
Send custom events to track user actions.
interface EventOptions { name: string; color?: 'red' | 'orange' | 'yellow' | 'green' | 'blue' | 'purple' | 'pink' | 'brown' | 'grey' | 'black'; data?: { [key: string]: any };}
await CapacitorCrisp.pushEvent({ name: 'checkout_completed', color: 'green', data: { price: 99.99, currency: 'USD' }});
setSessionSegment(segment)
Set a segment to categorize the chat session.
await CapacitorCrisp.setSessionSegment('premium_customer');
resetChatSession()
Reset the current chat session (useful for logout).
await CapacitorCrisp.resetChatSession();
Advanced Features
Custom User Data
// Set multiple custom data fieldsawait CapacitorCrisp.setSessionData({ key: 'plan', value: 'premium'});
await CapacitorCrisp.setSessionData({ key: 'signup_date', value: '2024-01-15'});
Message Preset
Set a pre-filled message in the chat input:
await CapacitorCrisp.setMessageText( "Hello, I need help with my order #12345");
Chat Availability
Control when the chat widget is available:
// Hide chat temporarilyawait CapacitorCrisp.setTokenID('user_token_12345');
Complete Example
import { CapacitorCrisp } from '@capgo/capacitor-crisp';
export class ChatService { async initialize() { // Configure Crisp await CapacitorCrisp.configure({ websiteID: 'YOUR_WEBSITE_ID', locale: 'en' }); }
async loginUser(user: any) { // Set user information await CapacitorCrisp.setUser({ email: user.email, nickname: user.name, phone: user.phone, avatar: user.avatarUrl });
// Set custom data await CapacitorCrisp.setSessionData({ key: 'user_id', value: user.id });
await CapacitorCrisp.setSessionData({ key: 'account_type', value: user.accountType });
// Set segment if (user.isPremium) { await CapacitorCrisp.setSessionSegment('premium'); }
// Track login event await CapacitorCrisp.pushEvent({ name: 'user_login', color: 'blue', data: { method: 'email' } }); }
async openSupport(context?: string) { if (context) { await CapacitorCrisp.setMessageText( `I need help with: ${context}` ); }
await CapacitorCrisp.openMessenger(); }
async logout() { await CapacitorCrisp.resetChatSession(); }}
Styling and Customization
Crisp automatically adapts to your app’s theme, but you can customize it further through the Crisp dashboard:
- Go to your Crisp dashboard
- Navigate to Settings > Website Settings > Chatbox & Email Settings
- Customize colors, position, and behavior
Best Practices
-
Initialize early Configure Crisp during app initialization for immediate availability:
import { App } from '@capacitor/app';App.addListener('appStateChange', async ({ isActive }) => {if (isActive) {await CapacitorCrisp.configure({websiteID: 'YOUR_WEBSITE_ID'});}}); -
Set user context Always provide user information when available for better support:
if (user.isAuthenticated) {await CapacitorCrisp.setUser({email: user.email,nickname: user.name});} -
Track important events Use events to provide context to support agents:
await CapacitorCrisp.pushEvent({name: 'error_occurred',color: 'red',data: {error: error.message,screen: 'checkout'}}); -
Handle logout properly Always reset the session when users log out:
async logout() {await CapacitorCrisp.resetChatSession();// Your other logout logic}
Platform Notes
iOS
- Requires iOS 10.0+
- Uses native Crisp iOS SDK
- Supports push notifications (configure in Crisp dashboard)
Android
- Requires Android 5.0 (API 21)+
- Uses native Crisp Android SDK
- Material Design compliant
Web
- Falls back to Crisp JavaScript SDK
- Full feature parity with native platforms