@pocketping/widget 0.1.0 → 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +89 -2
- package/dist/index.d.mts +191 -1
- package/dist/index.d.ts +191 -1
- package/dist/index.global.js +264 -4
- package/dist/index.js +272 -5
- package/dist/index.mjs +264 -4
- package/package.json +8 -4
package/README.md
CHANGED
|
@@ -7,7 +7,7 @@ Embeddable chat widget for PocketPing. Drop-in customer support chat that connec
|
|
|
7
7
|
### Via CDN (Recommended)
|
|
8
8
|
|
|
9
9
|
```html
|
|
10
|
-
<script src="https://cdn.
|
|
10
|
+
<script src="https://cdn.pocketping.io/widget.js"></script>
|
|
11
11
|
<script>
|
|
12
12
|
PocketPing.init({
|
|
13
13
|
endpoint: 'https://yoursite.com/pocketping'
|
|
@@ -260,18 +260,96 @@ PocketPing.close();
|
|
|
260
260
|
// Toggle the chat
|
|
261
261
|
PocketPing.toggle();
|
|
262
262
|
|
|
263
|
+
// Send a message programmatically
|
|
264
|
+
PocketPing.sendMessage('Hello!');
|
|
265
|
+
|
|
263
266
|
// Destroy the widget
|
|
264
267
|
PocketPing.destroy();
|
|
265
268
|
```
|
|
266
269
|
|
|
267
270
|
---
|
|
268
271
|
|
|
272
|
+
## Custom Events
|
|
273
|
+
|
|
274
|
+
PocketPing supports bidirectional custom events between your website and the backend. This enables powerful interactions like triggering alerts when users take specific actions.
|
|
275
|
+
|
|
276
|
+
### Triggering Events (Widget → Backend)
|
|
277
|
+
|
|
278
|
+
Send events to your backend when users perform actions:
|
|
279
|
+
|
|
280
|
+
```javascript
|
|
281
|
+
// Notify when user clicks pricing
|
|
282
|
+
PocketPing.trigger('clicked_pricing', { plan: 'pro', source: 'header' });
|
|
283
|
+
|
|
284
|
+
// Track user interactions
|
|
285
|
+
PocketPing.trigger('viewed_demo');
|
|
286
|
+
PocketPing.trigger('downloaded_pdf', { name: 'whitepaper.pdf' });
|
|
287
|
+
|
|
288
|
+
// Report errors
|
|
289
|
+
PocketPing.trigger('error_occurred', { code: 500, page: window.location.pathname });
|
|
290
|
+
```
|
|
291
|
+
|
|
292
|
+
Events are forwarded to your backend and displayed in Telegram/Discord/Slack with full context.
|
|
293
|
+
|
|
294
|
+
### Listening for Events (Backend → Widget)
|
|
295
|
+
|
|
296
|
+
Your backend can send events to the widget. Subscribe to handle them:
|
|
297
|
+
|
|
298
|
+
```javascript
|
|
299
|
+
// Subscribe to an event
|
|
300
|
+
const unsubscribe = PocketPing.onEvent('show_offer', (data) => {
|
|
301
|
+
showPopup(`Special offer: ${data.discount}% off!`);
|
|
302
|
+
});
|
|
303
|
+
|
|
304
|
+
// Unsubscribe when done
|
|
305
|
+
unsubscribe();
|
|
306
|
+
|
|
307
|
+
// Or use offEvent
|
|
308
|
+
PocketPing.onEvent('announcement', handleAnnouncement);
|
|
309
|
+
PocketPing.offEvent('announcement', handleAnnouncement);
|
|
310
|
+
```
|
|
311
|
+
|
|
312
|
+
### Event Callback
|
|
313
|
+
|
|
314
|
+
You can also use an `onEvent` callback in the init config:
|
|
315
|
+
|
|
316
|
+
```javascript
|
|
317
|
+
PocketPing.init({
|
|
318
|
+
endpoint: 'https://yoursite.com/pocketping',
|
|
319
|
+
onEvent: (event) => {
|
|
320
|
+
console.log('Received event:', event.name, event.data);
|
|
321
|
+
}
|
|
322
|
+
});
|
|
323
|
+
```
|
|
324
|
+
|
|
325
|
+
### Use Cases
|
|
326
|
+
|
|
327
|
+
| Event | Use Case |
|
|
328
|
+
|-------|----------|
|
|
329
|
+
| `clicked_pricing` | Alert sales team when visitor shows interest |
|
|
330
|
+
| `error_spike` | Get notified of frontend errors |
|
|
331
|
+
| `cart_abandoned` | Trigger follow-up message |
|
|
332
|
+
| `show_offer` | Display personalized offer |
|
|
333
|
+
| `request_demo` | Open demo scheduling modal |
|
|
334
|
+
| `announcement` | Show system-wide notification |
|
|
335
|
+
|
|
336
|
+
---
|
|
337
|
+
|
|
269
338
|
## TypeScript Support
|
|
270
339
|
|
|
271
340
|
Full TypeScript definitions are included:
|
|
272
341
|
|
|
273
342
|
```typescript
|
|
274
|
-
import {
|
|
343
|
+
import {
|
|
344
|
+
init,
|
|
345
|
+
trigger,
|
|
346
|
+
onEvent,
|
|
347
|
+
offEvent,
|
|
348
|
+
PocketPingConfig,
|
|
349
|
+
Message,
|
|
350
|
+
CustomEvent,
|
|
351
|
+
CustomEventHandler
|
|
352
|
+
} from '@pocketping/widget';
|
|
275
353
|
|
|
276
354
|
const config: PocketPingConfig = {
|
|
277
355
|
endpoint: 'https://yoursite.com/pocketping',
|
|
@@ -281,6 +359,15 @@ const config: PocketPingConfig = {
|
|
|
281
359
|
};
|
|
282
360
|
|
|
283
361
|
init(config);
|
|
362
|
+
|
|
363
|
+
// Type-safe events
|
|
364
|
+
trigger('clicked_pricing', { plan: 'pro' });
|
|
365
|
+
|
|
366
|
+
const handler: CustomEventHandler = (data, event: CustomEvent) => {
|
|
367
|
+
console.log(event.name, data);
|
|
368
|
+
};
|
|
369
|
+
|
|
370
|
+
onEvent('show_offer', handler);
|
|
284
371
|
```
|
|
285
372
|
|
|
286
373
|
---
|
package/dist/index.d.mts
CHANGED
|
@@ -59,6 +59,8 @@ interface PocketPingConfig {
|
|
|
59
59
|
onConnect?: (sessionId: string) => void;
|
|
60
60
|
/** Called when connection fails */
|
|
61
61
|
onError?: (error: Error) => void;
|
|
62
|
+
/** Called when a version mismatch is detected */
|
|
63
|
+
onVersionWarning?: (warning: VersionWarning) => void;
|
|
62
64
|
}
|
|
63
65
|
type MessageStatus = 'sending' | 'sent' | 'delivered' | 'read';
|
|
64
66
|
interface Message {
|
|
@@ -78,6 +80,8 @@ interface Session {
|
|
|
78
80
|
visitorId: string;
|
|
79
81
|
operatorOnline: boolean;
|
|
80
82
|
messages: Message[];
|
|
83
|
+
/** User identity if identified via PocketPing.identify() */
|
|
84
|
+
identity?: UserIdentity;
|
|
81
85
|
}
|
|
82
86
|
interface PresenceResponse {
|
|
83
87
|
online: boolean;
|
|
@@ -89,6 +93,55 @@ interface PresenceResponse {
|
|
|
89
93
|
aiEnabled?: boolean;
|
|
90
94
|
aiActiveAfter?: number;
|
|
91
95
|
}
|
|
96
|
+
type VersionWarningSeverity = 'info' | 'warning' | 'error';
|
|
97
|
+
interface VersionWarning {
|
|
98
|
+
/** Severity level of the warning */
|
|
99
|
+
severity: VersionWarningSeverity;
|
|
100
|
+
/** Human-readable warning message */
|
|
101
|
+
message: string;
|
|
102
|
+
/** Current widget version */
|
|
103
|
+
currentVersion: string;
|
|
104
|
+
/** Minimum supported version (if applicable) */
|
|
105
|
+
minVersion?: string;
|
|
106
|
+
/** Latest available version (if applicable) */
|
|
107
|
+
latestVersion?: string;
|
|
108
|
+
/** Whether the widget should still function */
|
|
109
|
+
canContinue: boolean;
|
|
110
|
+
/** URL to upgrade instructions */
|
|
111
|
+
upgradeUrl?: string;
|
|
112
|
+
}
|
|
113
|
+
/** Custom event sent from widget to backend or vice versa */
|
|
114
|
+
interface CustomEvent {
|
|
115
|
+
/** Event name (e.g., 'clicked_pricing', 'show_offer') */
|
|
116
|
+
name: string;
|
|
117
|
+
/** Event payload */
|
|
118
|
+
data?: Record<string, unknown>;
|
|
119
|
+
/** Timestamp of the event */
|
|
120
|
+
timestamp: string;
|
|
121
|
+
}
|
|
122
|
+
/** Handler for custom events */
|
|
123
|
+
type CustomEventHandler = (data: Record<string, unknown> | undefined, event: CustomEvent) => void;
|
|
124
|
+
/**
|
|
125
|
+
* User identity data for identifying visitors
|
|
126
|
+
* @example
|
|
127
|
+
* PocketPing.identify({
|
|
128
|
+
* id: 'user_123',
|
|
129
|
+
* email: 'john@example.com',
|
|
130
|
+
* name: 'John Doe',
|
|
131
|
+
* plan: 'pro',
|
|
132
|
+
* company: 'Acme Inc'
|
|
133
|
+
* })
|
|
134
|
+
*/
|
|
135
|
+
interface UserIdentity {
|
|
136
|
+
/** Required unique user identifier */
|
|
137
|
+
id: string;
|
|
138
|
+
/** User's email address */
|
|
139
|
+
email?: string;
|
|
140
|
+
/** User's display name */
|
|
141
|
+
name?: string;
|
|
142
|
+
/** Any custom fields (plan, company, etc.) */
|
|
143
|
+
[key: string]: unknown;
|
|
144
|
+
}
|
|
92
145
|
|
|
93
146
|
type Listener<T> = (data: T) => void;
|
|
94
147
|
declare class PocketPingClient {
|
|
@@ -97,6 +150,7 @@ declare class PocketPingClient {
|
|
|
97
150
|
private ws;
|
|
98
151
|
private isOpen;
|
|
99
152
|
private listeners;
|
|
153
|
+
private customEventHandlers;
|
|
100
154
|
private reconnectAttempts;
|
|
101
155
|
private maxReconnectAttempts;
|
|
102
156
|
private reconnectTimeout;
|
|
@@ -108,6 +162,33 @@ declare class PocketPingClient {
|
|
|
108
162
|
sendTyping(isTyping?: boolean): Promise<void>;
|
|
109
163
|
sendReadStatus(messageIds: string[], status: MessageStatus): Promise<void>;
|
|
110
164
|
getPresence(): Promise<PresenceResponse>;
|
|
165
|
+
/**
|
|
166
|
+
* Identify the current user with metadata
|
|
167
|
+
* Call after user logs in or when user data becomes available
|
|
168
|
+
* @param identity - User identity data with required id field
|
|
169
|
+
* @example
|
|
170
|
+
* PocketPing.identify({
|
|
171
|
+
* id: 'user_123',
|
|
172
|
+
* email: 'john@example.com',
|
|
173
|
+
* name: 'John Doe',
|
|
174
|
+
* plan: 'pro',
|
|
175
|
+
* company: 'Acme Inc'
|
|
176
|
+
* })
|
|
177
|
+
*/
|
|
178
|
+
identify(identity: UserIdentity): Promise<void>;
|
|
179
|
+
/**
|
|
180
|
+
* Reset the user identity and optionally start a new session
|
|
181
|
+
* Call on user logout to clear user data
|
|
182
|
+
* @param options - Optional settings: { newSession: boolean }
|
|
183
|
+
*/
|
|
184
|
+
reset(options?: {
|
|
185
|
+
newSession?: boolean;
|
|
186
|
+
}): Promise<void>;
|
|
187
|
+
/**
|
|
188
|
+
* Get the current user identity
|
|
189
|
+
* @returns UserIdentity or null if not identified
|
|
190
|
+
*/
|
|
191
|
+
getIdentity(): UserIdentity | null;
|
|
111
192
|
getSession(): Session | null;
|
|
112
193
|
getMessages(): Message[];
|
|
113
194
|
isConnected(): boolean;
|
|
@@ -116,14 +197,45 @@ declare class PocketPingClient {
|
|
|
116
197
|
toggleOpen(): void;
|
|
117
198
|
on<T>(event: string, listener: Listener<T>): () => void;
|
|
118
199
|
private emit;
|
|
200
|
+
/**
|
|
201
|
+
* Trigger a custom event to the backend
|
|
202
|
+
* @param eventName - The name of the event (e.g., 'clicked_pricing', 'viewed_demo')
|
|
203
|
+
* @param data - Optional payload to send with the event
|
|
204
|
+
* @example
|
|
205
|
+
* PocketPing.trigger('clicked_cta', { button: 'signup', page: '/pricing' })
|
|
206
|
+
*/
|
|
207
|
+
trigger(eventName: string, data?: Record<string, unknown>): void;
|
|
208
|
+
/**
|
|
209
|
+
* Subscribe to custom events from the backend
|
|
210
|
+
* @param eventName - The name of the event to listen for (e.g., 'show_offer', 'open_chat')
|
|
211
|
+
* @param handler - Callback function when event is received
|
|
212
|
+
* @returns Unsubscribe function
|
|
213
|
+
* @example
|
|
214
|
+
* const unsubscribe = PocketPing.onEvent('show_offer', (data) => {
|
|
215
|
+
* showPopup(data.message)
|
|
216
|
+
* })
|
|
217
|
+
*/
|
|
218
|
+
onEvent(eventName: string, handler: CustomEventHandler): () => void;
|
|
219
|
+
/**
|
|
220
|
+
* Unsubscribe from a custom event
|
|
221
|
+
* @param eventName - The name of the event
|
|
222
|
+
* @param handler - The handler to remove
|
|
223
|
+
*/
|
|
224
|
+
offEvent(eventName: string, handler: CustomEventHandler): void;
|
|
225
|
+
private emitCustomEvent;
|
|
119
226
|
private connectWebSocket;
|
|
120
227
|
private handleWebSocketEvent;
|
|
228
|
+
private handleVersionWarning;
|
|
121
229
|
private scheduleReconnect;
|
|
122
230
|
private startPolling;
|
|
123
231
|
private fetch;
|
|
232
|
+
private checkVersionHeaders;
|
|
124
233
|
private getOrCreateVisitorId;
|
|
125
234
|
private getStoredSessionId;
|
|
126
235
|
private storeSessionId;
|
|
236
|
+
private getStoredIdentity;
|
|
237
|
+
private storeIdentity;
|
|
238
|
+
private clearIdentity;
|
|
127
239
|
private generateId;
|
|
128
240
|
}
|
|
129
241
|
|
|
@@ -133,6 +245,77 @@ declare function open(): void;
|
|
|
133
245
|
declare function close(): void;
|
|
134
246
|
declare function toggle(): void;
|
|
135
247
|
declare function sendMessage(content: string): Promise<Message>;
|
|
248
|
+
/**
|
|
249
|
+
* Trigger a custom event to the backend
|
|
250
|
+
* @param eventName - The name of the event (e.g., 'clicked_pricing', 'viewed_demo')
|
|
251
|
+
* @param data - Optional payload to send with the event
|
|
252
|
+
* @example
|
|
253
|
+
* PocketPing.trigger('clicked_cta', { button: 'signup', page: '/pricing' })
|
|
254
|
+
*/
|
|
255
|
+
declare function trigger(eventName: string, data?: Record<string, unknown>): void;
|
|
256
|
+
/**
|
|
257
|
+
* Subscribe to custom events from the backend
|
|
258
|
+
* @param eventName - The name of the event to listen for
|
|
259
|
+
* @param handler - Callback function when event is received
|
|
260
|
+
* @returns Unsubscribe function
|
|
261
|
+
* @example
|
|
262
|
+
* const unsubscribe = PocketPing.onEvent('show_offer', (data) => {
|
|
263
|
+
* showPopup(data.message)
|
|
264
|
+
* })
|
|
265
|
+
*/
|
|
266
|
+
declare function onEvent(eventName: string, handler: CustomEventHandler): () => void;
|
|
267
|
+
/**
|
|
268
|
+
* Unsubscribe from a custom event
|
|
269
|
+
* @param eventName - The name of the event
|
|
270
|
+
* @param handler - The handler to remove
|
|
271
|
+
*/
|
|
272
|
+
declare function offEvent(eventName: string, handler: CustomEventHandler): void;
|
|
273
|
+
/**
|
|
274
|
+
* Identify the current user with metadata
|
|
275
|
+
* Call after user logs in or when user data becomes available
|
|
276
|
+
* @param identity - User identity data with required id field
|
|
277
|
+
* @example
|
|
278
|
+
* PocketPing.identify({
|
|
279
|
+
* id: 'user_123',
|
|
280
|
+
* email: 'john@example.com',
|
|
281
|
+
* name: 'John Doe',
|
|
282
|
+
* plan: 'pro',
|
|
283
|
+
* company: 'Acme Inc'
|
|
284
|
+
* })
|
|
285
|
+
*/
|
|
286
|
+
declare function identify(identity: UserIdentity): Promise<void>;
|
|
287
|
+
/**
|
|
288
|
+
* Reset the user identity and optionally start a new session
|
|
289
|
+
* Call on user logout to clear user data
|
|
290
|
+
* @param options - Optional settings: { newSession: boolean }
|
|
291
|
+
* @example
|
|
292
|
+
* // Clear identity but keep session
|
|
293
|
+
* PocketPing.reset()
|
|
294
|
+
*
|
|
295
|
+
* // Clear everything and start fresh
|
|
296
|
+
* PocketPing.reset({ newSession: true })
|
|
297
|
+
*/
|
|
298
|
+
declare function reset(options?: {
|
|
299
|
+
newSession?: boolean;
|
|
300
|
+
}): Promise<void>;
|
|
301
|
+
/**
|
|
302
|
+
* Get the current user identity
|
|
303
|
+
* @returns UserIdentity or null if not identified
|
|
304
|
+
*/
|
|
305
|
+
declare function getIdentity(): UserIdentity | null;
|
|
306
|
+
/**
|
|
307
|
+
* Subscribe to internal widget events
|
|
308
|
+
* @param eventName - Event name: 'versionWarning', 'message', 'connect', 'typing', etc.
|
|
309
|
+
* @param handler - Callback function
|
|
310
|
+
* @returns Unsubscribe function
|
|
311
|
+
* @example
|
|
312
|
+
* PocketPing.on('versionWarning', (warning) => {
|
|
313
|
+
* if (warning.severity === 'error') {
|
|
314
|
+
* showUpgradeNotice(warning.message);
|
|
315
|
+
* }
|
|
316
|
+
* })
|
|
317
|
+
*/
|
|
318
|
+
declare function on<T>(eventName: string, handler: (data: T) => void): () => void;
|
|
136
319
|
declare const _default: {
|
|
137
320
|
init: typeof init;
|
|
138
321
|
destroy: typeof destroy;
|
|
@@ -140,6 +323,13 @@ declare const _default: {
|
|
|
140
323
|
close: typeof close;
|
|
141
324
|
toggle: typeof toggle;
|
|
142
325
|
sendMessage: typeof sendMessage;
|
|
326
|
+
trigger: typeof trigger;
|
|
327
|
+
onEvent: typeof onEvent;
|
|
328
|
+
offEvent: typeof offEvent;
|
|
329
|
+
on: typeof on;
|
|
330
|
+
identify: typeof identify;
|
|
331
|
+
reset: typeof reset;
|
|
332
|
+
getIdentity: typeof getIdentity;
|
|
143
333
|
};
|
|
144
334
|
|
|
145
|
-
export { type Message, type PocketPingConfig, close, _default as default, destroy, init, open, sendMessage, toggle };
|
|
335
|
+
export { type CustomEvent, type CustomEventHandler, type Message, type PocketPingConfig, type UserIdentity, type VersionWarning, close, _default as default, destroy, getIdentity, identify, init, offEvent, on, onEvent, open, reset, sendMessage, toggle, trigger };
|
package/dist/index.d.ts
CHANGED
|
@@ -59,6 +59,8 @@ interface PocketPingConfig {
|
|
|
59
59
|
onConnect?: (sessionId: string) => void;
|
|
60
60
|
/** Called when connection fails */
|
|
61
61
|
onError?: (error: Error) => void;
|
|
62
|
+
/** Called when a version mismatch is detected */
|
|
63
|
+
onVersionWarning?: (warning: VersionWarning) => void;
|
|
62
64
|
}
|
|
63
65
|
type MessageStatus = 'sending' | 'sent' | 'delivered' | 'read';
|
|
64
66
|
interface Message {
|
|
@@ -78,6 +80,8 @@ interface Session {
|
|
|
78
80
|
visitorId: string;
|
|
79
81
|
operatorOnline: boolean;
|
|
80
82
|
messages: Message[];
|
|
83
|
+
/** User identity if identified via PocketPing.identify() */
|
|
84
|
+
identity?: UserIdentity;
|
|
81
85
|
}
|
|
82
86
|
interface PresenceResponse {
|
|
83
87
|
online: boolean;
|
|
@@ -89,6 +93,55 @@ interface PresenceResponse {
|
|
|
89
93
|
aiEnabled?: boolean;
|
|
90
94
|
aiActiveAfter?: number;
|
|
91
95
|
}
|
|
96
|
+
type VersionWarningSeverity = 'info' | 'warning' | 'error';
|
|
97
|
+
interface VersionWarning {
|
|
98
|
+
/** Severity level of the warning */
|
|
99
|
+
severity: VersionWarningSeverity;
|
|
100
|
+
/** Human-readable warning message */
|
|
101
|
+
message: string;
|
|
102
|
+
/** Current widget version */
|
|
103
|
+
currentVersion: string;
|
|
104
|
+
/** Minimum supported version (if applicable) */
|
|
105
|
+
minVersion?: string;
|
|
106
|
+
/** Latest available version (if applicable) */
|
|
107
|
+
latestVersion?: string;
|
|
108
|
+
/** Whether the widget should still function */
|
|
109
|
+
canContinue: boolean;
|
|
110
|
+
/** URL to upgrade instructions */
|
|
111
|
+
upgradeUrl?: string;
|
|
112
|
+
}
|
|
113
|
+
/** Custom event sent from widget to backend or vice versa */
|
|
114
|
+
interface CustomEvent {
|
|
115
|
+
/** Event name (e.g., 'clicked_pricing', 'show_offer') */
|
|
116
|
+
name: string;
|
|
117
|
+
/** Event payload */
|
|
118
|
+
data?: Record<string, unknown>;
|
|
119
|
+
/** Timestamp of the event */
|
|
120
|
+
timestamp: string;
|
|
121
|
+
}
|
|
122
|
+
/** Handler for custom events */
|
|
123
|
+
type CustomEventHandler = (data: Record<string, unknown> | undefined, event: CustomEvent) => void;
|
|
124
|
+
/**
|
|
125
|
+
* User identity data for identifying visitors
|
|
126
|
+
* @example
|
|
127
|
+
* PocketPing.identify({
|
|
128
|
+
* id: 'user_123',
|
|
129
|
+
* email: 'john@example.com',
|
|
130
|
+
* name: 'John Doe',
|
|
131
|
+
* plan: 'pro',
|
|
132
|
+
* company: 'Acme Inc'
|
|
133
|
+
* })
|
|
134
|
+
*/
|
|
135
|
+
interface UserIdentity {
|
|
136
|
+
/** Required unique user identifier */
|
|
137
|
+
id: string;
|
|
138
|
+
/** User's email address */
|
|
139
|
+
email?: string;
|
|
140
|
+
/** User's display name */
|
|
141
|
+
name?: string;
|
|
142
|
+
/** Any custom fields (plan, company, etc.) */
|
|
143
|
+
[key: string]: unknown;
|
|
144
|
+
}
|
|
92
145
|
|
|
93
146
|
type Listener<T> = (data: T) => void;
|
|
94
147
|
declare class PocketPingClient {
|
|
@@ -97,6 +150,7 @@ declare class PocketPingClient {
|
|
|
97
150
|
private ws;
|
|
98
151
|
private isOpen;
|
|
99
152
|
private listeners;
|
|
153
|
+
private customEventHandlers;
|
|
100
154
|
private reconnectAttempts;
|
|
101
155
|
private maxReconnectAttempts;
|
|
102
156
|
private reconnectTimeout;
|
|
@@ -108,6 +162,33 @@ declare class PocketPingClient {
|
|
|
108
162
|
sendTyping(isTyping?: boolean): Promise<void>;
|
|
109
163
|
sendReadStatus(messageIds: string[], status: MessageStatus): Promise<void>;
|
|
110
164
|
getPresence(): Promise<PresenceResponse>;
|
|
165
|
+
/**
|
|
166
|
+
* Identify the current user with metadata
|
|
167
|
+
* Call after user logs in or when user data becomes available
|
|
168
|
+
* @param identity - User identity data with required id field
|
|
169
|
+
* @example
|
|
170
|
+
* PocketPing.identify({
|
|
171
|
+
* id: 'user_123',
|
|
172
|
+
* email: 'john@example.com',
|
|
173
|
+
* name: 'John Doe',
|
|
174
|
+
* plan: 'pro',
|
|
175
|
+
* company: 'Acme Inc'
|
|
176
|
+
* })
|
|
177
|
+
*/
|
|
178
|
+
identify(identity: UserIdentity): Promise<void>;
|
|
179
|
+
/**
|
|
180
|
+
* Reset the user identity and optionally start a new session
|
|
181
|
+
* Call on user logout to clear user data
|
|
182
|
+
* @param options - Optional settings: { newSession: boolean }
|
|
183
|
+
*/
|
|
184
|
+
reset(options?: {
|
|
185
|
+
newSession?: boolean;
|
|
186
|
+
}): Promise<void>;
|
|
187
|
+
/**
|
|
188
|
+
* Get the current user identity
|
|
189
|
+
* @returns UserIdentity or null if not identified
|
|
190
|
+
*/
|
|
191
|
+
getIdentity(): UserIdentity | null;
|
|
111
192
|
getSession(): Session | null;
|
|
112
193
|
getMessages(): Message[];
|
|
113
194
|
isConnected(): boolean;
|
|
@@ -116,14 +197,45 @@ declare class PocketPingClient {
|
|
|
116
197
|
toggleOpen(): void;
|
|
117
198
|
on<T>(event: string, listener: Listener<T>): () => void;
|
|
118
199
|
private emit;
|
|
200
|
+
/**
|
|
201
|
+
* Trigger a custom event to the backend
|
|
202
|
+
* @param eventName - The name of the event (e.g., 'clicked_pricing', 'viewed_demo')
|
|
203
|
+
* @param data - Optional payload to send with the event
|
|
204
|
+
* @example
|
|
205
|
+
* PocketPing.trigger('clicked_cta', { button: 'signup', page: '/pricing' })
|
|
206
|
+
*/
|
|
207
|
+
trigger(eventName: string, data?: Record<string, unknown>): void;
|
|
208
|
+
/**
|
|
209
|
+
* Subscribe to custom events from the backend
|
|
210
|
+
* @param eventName - The name of the event to listen for (e.g., 'show_offer', 'open_chat')
|
|
211
|
+
* @param handler - Callback function when event is received
|
|
212
|
+
* @returns Unsubscribe function
|
|
213
|
+
* @example
|
|
214
|
+
* const unsubscribe = PocketPing.onEvent('show_offer', (data) => {
|
|
215
|
+
* showPopup(data.message)
|
|
216
|
+
* })
|
|
217
|
+
*/
|
|
218
|
+
onEvent(eventName: string, handler: CustomEventHandler): () => void;
|
|
219
|
+
/**
|
|
220
|
+
* Unsubscribe from a custom event
|
|
221
|
+
* @param eventName - The name of the event
|
|
222
|
+
* @param handler - The handler to remove
|
|
223
|
+
*/
|
|
224
|
+
offEvent(eventName: string, handler: CustomEventHandler): void;
|
|
225
|
+
private emitCustomEvent;
|
|
119
226
|
private connectWebSocket;
|
|
120
227
|
private handleWebSocketEvent;
|
|
228
|
+
private handleVersionWarning;
|
|
121
229
|
private scheduleReconnect;
|
|
122
230
|
private startPolling;
|
|
123
231
|
private fetch;
|
|
232
|
+
private checkVersionHeaders;
|
|
124
233
|
private getOrCreateVisitorId;
|
|
125
234
|
private getStoredSessionId;
|
|
126
235
|
private storeSessionId;
|
|
236
|
+
private getStoredIdentity;
|
|
237
|
+
private storeIdentity;
|
|
238
|
+
private clearIdentity;
|
|
127
239
|
private generateId;
|
|
128
240
|
}
|
|
129
241
|
|
|
@@ -133,6 +245,77 @@ declare function open(): void;
|
|
|
133
245
|
declare function close(): void;
|
|
134
246
|
declare function toggle(): void;
|
|
135
247
|
declare function sendMessage(content: string): Promise<Message>;
|
|
248
|
+
/**
|
|
249
|
+
* Trigger a custom event to the backend
|
|
250
|
+
* @param eventName - The name of the event (e.g., 'clicked_pricing', 'viewed_demo')
|
|
251
|
+
* @param data - Optional payload to send with the event
|
|
252
|
+
* @example
|
|
253
|
+
* PocketPing.trigger('clicked_cta', { button: 'signup', page: '/pricing' })
|
|
254
|
+
*/
|
|
255
|
+
declare function trigger(eventName: string, data?: Record<string, unknown>): void;
|
|
256
|
+
/**
|
|
257
|
+
* Subscribe to custom events from the backend
|
|
258
|
+
* @param eventName - The name of the event to listen for
|
|
259
|
+
* @param handler - Callback function when event is received
|
|
260
|
+
* @returns Unsubscribe function
|
|
261
|
+
* @example
|
|
262
|
+
* const unsubscribe = PocketPing.onEvent('show_offer', (data) => {
|
|
263
|
+
* showPopup(data.message)
|
|
264
|
+
* })
|
|
265
|
+
*/
|
|
266
|
+
declare function onEvent(eventName: string, handler: CustomEventHandler): () => void;
|
|
267
|
+
/**
|
|
268
|
+
* Unsubscribe from a custom event
|
|
269
|
+
* @param eventName - The name of the event
|
|
270
|
+
* @param handler - The handler to remove
|
|
271
|
+
*/
|
|
272
|
+
declare function offEvent(eventName: string, handler: CustomEventHandler): void;
|
|
273
|
+
/**
|
|
274
|
+
* Identify the current user with metadata
|
|
275
|
+
* Call after user logs in or when user data becomes available
|
|
276
|
+
* @param identity - User identity data with required id field
|
|
277
|
+
* @example
|
|
278
|
+
* PocketPing.identify({
|
|
279
|
+
* id: 'user_123',
|
|
280
|
+
* email: 'john@example.com',
|
|
281
|
+
* name: 'John Doe',
|
|
282
|
+
* plan: 'pro',
|
|
283
|
+
* company: 'Acme Inc'
|
|
284
|
+
* })
|
|
285
|
+
*/
|
|
286
|
+
declare function identify(identity: UserIdentity): Promise<void>;
|
|
287
|
+
/**
|
|
288
|
+
* Reset the user identity and optionally start a new session
|
|
289
|
+
* Call on user logout to clear user data
|
|
290
|
+
* @param options - Optional settings: { newSession: boolean }
|
|
291
|
+
* @example
|
|
292
|
+
* // Clear identity but keep session
|
|
293
|
+
* PocketPing.reset()
|
|
294
|
+
*
|
|
295
|
+
* // Clear everything and start fresh
|
|
296
|
+
* PocketPing.reset({ newSession: true })
|
|
297
|
+
*/
|
|
298
|
+
declare function reset(options?: {
|
|
299
|
+
newSession?: boolean;
|
|
300
|
+
}): Promise<void>;
|
|
301
|
+
/**
|
|
302
|
+
* Get the current user identity
|
|
303
|
+
* @returns UserIdentity or null if not identified
|
|
304
|
+
*/
|
|
305
|
+
declare function getIdentity(): UserIdentity | null;
|
|
306
|
+
/**
|
|
307
|
+
* Subscribe to internal widget events
|
|
308
|
+
* @param eventName - Event name: 'versionWarning', 'message', 'connect', 'typing', etc.
|
|
309
|
+
* @param handler - Callback function
|
|
310
|
+
* @returns Unsubscribe function
|
|
311
|
+
* @example
|
|
312
|
+
* PocketPing.on('versionWarning', (warning) => {
|
|
313
|
+
* if (warning.severity === 'error') {
|
|
314
|
+
* showUpgradeNotice(warning.message);
|
|
315
|
+
* }
|
|
316
|
+
* })
|
|
317
|
+
*/
|
|
318
|
+
declare function on<T>(eventName: string, handler: (data: T) => void): () => void;
|
|
136
319
|
declare const _default: {
|
|
137
320
|
init: typeof init;
|
|
138
321
|
destroy: typeof destroy;
|
|
@@ -140,6 +323,13 @@ declare const _default: {
|
|
|
140
323
|
close: typeof close;
|
|
141
324
|
toggle: typeof toggle;
|
|
142
325
|
sendMessage: typeof sendMessage;
|
|
326
|
+
trigger: typeof trigger;
|
|
327
|
+
onEvent: typeof onEvent;
|
|
328
|
+
offEvent: typeof offEvent;
|
|
329
|
+
on: typeof on;
|
|
330
|
+
identify: typeof identify;
|
|
331
|
+
reset: typeof reset;
|
|
332
|
+
getIdentity: typeof getIdentity;
|
|
143
333
|
};
|
|
144
334
|
|
|
145
|
-
export { type Message, type PocketPingConfig, close, _default as default, destroy, init, open, sendMessage, toggle };
|
|
335
|
+
export { type CustomEvent, type CustomEventHandler, type Message, type PocketPingConfig, type UserIdentity, type VersionWarning, close, _default as default, destroy, getIdentity, identify, init, offEvent, on, onEvent, open, reset, sendMessage, toggle, trigger };
|