Thanks to visit codestin.com
Credit goes to github.com

Skip to content
This repository was archived by the owner on Apr 23, 2024. It is now read-only.

Commit a2c76cc

Browse files
authored
Add documentation for pub-sub event callbacks
2 parents d6714e2 + 85b6c96 commit a2c76cc

File tree

1 file changed

+72
-0
lines changed

1 file changed

+72
-0
lines changed

source/includes/_embed2.md.erb

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -276,6 +276,20 @@ Meta field keys should not include whitespace, emojis, special characters or per
276276

277277
When `true`, this will prevent the bot from loading until the `adaEmbed.start(...)` method is called.
278278

279+
#### onAdaEmbedLoad
280+
281+
```js
282+
window.adaSettings = {
283+
onAdaEmbedLoad: () => {
284+
window.adaEmbed.subscribeEvent("ada:campaigns:message_received", (data) => {
285+
console.log("Message received from campaign with key:", data.campaignKey);
286+
});
287+
}
288+
};
289+
```
290+
291+
Called when the embed script has been loaded, and before it is initialized. This is useful for subscribing to events (see `subscribeEvent`) - subscribing here ensures subscriptions are in place before events are triggered.
292+
279293
#### parentElement
280294

281295
```html
@@ -433,6 +447,64 @@ Used to initialize Embed2 on your page. This action is triggered by default inte
433447

434448
Removes Embed2 from your page.
435449

450+
### subscribeEvent
451+
`subscribeEvent(eventKey: string, callback: (data: object, context: object) => void): Promise<number>`
452+
453+
```js
454+
window.adaEmbed.subscribeEvent("ada:campaigns:message_received", (data) => {
455+
console.log("Message received from campaign with key:", data.campaignKey);
456+
});
457+
```
458+
459+
```js
460+
window.adaEmbed.subscribeEvent("ada:campaigns", (data, context) => {
461+
const { eventKey } = context;
462+
463+
if (eventKey === "ada:campaigns:message_received") {
464+
console.log("Message received from campaign with key:" data.campaignKey);
465+
}
466+
467+
if (eventKey === "ada:campaigns:opened") {
468+
console.log("Chat opened after being shown campaign with key:" data.campaignKey);
469+
}
470+
471+
if (eventKey === "ada:campaigns:message_received") {
472+
console.log("Message received from campaign with key:" data.campaignKey);
473+
}
474+
});
475+
```
476+
477+
When certain events happen in Ada, events will be triggered. Each event consists of an event key and a data payload. Using `subscribeEvent`, you can define callbacks that get called when a specific event is triggered.
478+
479+
Callbacks will be triggered whenever an event _starts with_ the `eventKey` provided to `subscribeEvent`. This allows subscribing to all events of a certain category - for example, the callback in a subscription to `ada:campaigns` will be called on `ada:campaigns:message_received`, `ada:campaigns:opened`, and any other events beginning with `ada:campaigns`.
480+
481+
Two arguments will be provided to each callback when it is called: `data` and `context`. `data` is specific to each event (see table below). `context` is an object with a single property `eventKey`, which is the event key of the event that triggered the callback to be called.
482+
483+
`subscribeEvent` returns a number `subscriptionId` which can be used to unsubscribe later (see `unsubscribeEvent` below).
484+
485+
It is strongly recommended that initial subscriptions to events be placed in the `onAdaEmbedLoad` function. This ensures that the embed script has been loaded (so it is available to accept subscriptions), and that subscriptions happen before any events are triggered so that no events are missed.
486+
487+
The following is a list of the currently available events that can be subscribed to. More events may be added in the future.
488+
489+
Event key | Data | Description
490+
-------------- | -------------- | --------------
491+
`ada:campaigns:message_received` | `campaignKey` - the key of the campaign which triggered the received message | Triggered when the chat application receives a message that originates from a campaign.
492+
`ada:campaigns:opened` | `campaignKey` - the key of the last campaign shown before chat was opened | Triggered when chat is opened after a proactive campaign has been shown.
493+
`ada:campaigns:engaged` | `campaignKey` - the key of the last campaign shown before the conversation was engaged | Triggered when the chatter engages a conversation (sends a message) after being shown a campaign in the same session.
494+
495+
### unsubscribeEvent
496+
`unsubscribeEvent(subscriptionId: number): void;`
497+
498+
```js
499+
const subscriptionId = await window.adaEmbed.subscribeEvent("ada:campaigns:message_received", (data) => {
500+
console.log("Message received from campaign with key:", data.campaignKey);
501+
});
502+
503+
window.adaEmbed.unsubscribeEvent(subscriptionId);
504+
```
505+
506+
This function can be used to remove subscriptions created with the `subscribeEvent` function above. It takes a single parameter `subscriptionId`, which is the id of the subscription to be removed.
507+
436508
#### toggle
437509
`toggle(): Promise<void>;`
438510

0 commit comments

Comments
 (0)