Skip to content

Commit

Permalink
plugin-hubtype-analytics: change events to use new endpoint v2 (#2827)
Browse files Browse the repository at this point in the history
## Description

**@botonic/plugin-hubtype-analytics**
- Add Jest config
- Add github action workflow to launch tests

Accumulation of all PRs of new hubtype-analytics events with endpoint v2
Apply events within the **botonic-plugin-flow-builder**
Update Session and Input types within **botonic-core**
Add the withBotEvent option to the **botonic-core** HandoffBuilder to
tell the backend to track the handoff-success

## Context
There are two types of events: botevent and webevent
- botevent: these are events that are always done in the bot's lambda
and use an authenticated endpoint with token.
- webevent: these events that can be sent from the bot's frontend
(webchat, webviews) or from the bot's lambda, it uses an unauthenticated
endpoint.

## Approach taken / Explain the design

Different actions of the bot can be tracked, for now we have the
following ones:
- with botevent type: FlowNode, HandoffOption, HandoffSuccess,
HandoffFail, Keyword, Intent, IntentSmart, Knowledgebase, and Fallback
- with webevent type: FeedbackCase, FeedbackMessage,
FeedbackConversation, FeedbackWebview, WebviewStep, WebviewEnd and
Custom

The **plugin-flow-builder** takes care of flow tracking and exports the
function, trackFlowContent to facilitate the tracking of content
displayed in the bot. Apart from tracking the content displayed by the
bot, it tracks Keywords, IntentSmart, IntentKnowledge base and Fallback.
It also uses the HandoffBuilder with the parameter with BotEvent for the
backend to track the handoff-success.

## To document / Usage example
In the bot code we can declare a function like this and then pass it
through the flow builder constructor to make it work
```typescript

type EventArgs = { [key: string]: any }

export async function trackEvent(
  request: BotRequest,
  eventName: string,
  args?: EventArgs
): Promise<void> {
  if (Object.values(EventAction).includes(eventName as EventAction)) {
    const htEventProps = {
      action: eventName as EventAction,
      ...args,
    } as HtEventProps
    const hubtypeAnalyticsPlugin = request.plugins.hubtypeAnalytics
    await hubtypeAnalyticsPlugin.trackEvent(request, htEventProps)
  }
  return
}
```

## Testing

- Add tests to check how each event is created

---------

Co-authored-by: Adrià Sastre <[email protected]>
Co-authored-by: Marc Rabat <[email protected]>
  • Loading branch information
3 people authored Jun 11, 2024
1 parent 0507eaf commit 2bd349b
Show file tree
Hide file tree
Showing 70 changed files with 1,513 additions and 683 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/botonic-plugin-flow-builder-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ on:
workflow_dispatch:

jobs:
botonic-react-tests:
botonic-plugin-flow-builder-tests:
uses: ./.github/workflows/botonic-common-workflow.yml
secrets: inherit #pragma: allowlist secret
with:
Expand Down
18 changes: 18 additions & 0 deletions .github/workflows/botonic-plugin-hubtype-analytics-tests.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
name: Botonic plugin hubtype analytics tests

on:
push:
paths:
- 'packages/botonic-plugin-hubtype-analytics/**'
- '.github/workflows/botonic-plugin-hubtype-analytics-tests.yml'
workflow_dispatch:

jobs:
botonic-plugin-hubtype-analytics-tests:
uses: ./.github/workflows/botonic-common-workflow.yml
secrets: inherit #pragma: allowlist secret
with:
PACKAGE_NAME: Botonic plugin hubtype analytics tests
PACKAGE: botonic-plugin-hubtype-analytics
BUILD_COMMAND: 'cd ../botonic-core && npm run build && cd ../botonic-plugin-hubtype-analytics && npm run build'
NEEDS_CODECOV_UPLOAD: 'yes'
27 changes: 13 additions & 14 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,15 @@ All notable changes to Botonic will be documented in this file.
Click to see more.
</summary>

## [0.25.X] - 2024-xx-xx
## [0.27.X] - 2024-xx-xx

### Changed

### Fixed

</details>

## [0.27.0] - tbd

### Added

Expand All @@ -30,27 +38,18 @@ All notable changes to Botonic will be documented in this file.
- [Remove types `HubtypeSession` and use `Session`](https://github.com/hubtype/botonic/pull/2812)

- [@botonic/plugin-knowledge-bases](https://www.npmjs.com/package/@botonic/plugin-knowledge-bases)
- [Remove types `HubtypeSession` and use `Session`](https://github.com/hubtype/botonic/pull/2812)

### Fixed
- [Remove types `HubtypeSession` and use `Session`](https://github.com/hubtype/botonic/pull/2812)

- [@botonic/plugin-flow-builder](https://www.npmjs.com/package/@botonic/plugin-flow-builder)

- [Fix `bot action payload` limit characters](https://github.com/hubtype/botonic/pull/2817)

</details>

## [0.27.0] - tbd

### Added
- [Modified Smart Intents api request to call smart intents inference v2](https://github.com/hubtype/botonic/pull/2825)

### Changed
### Fixed

- [@botonic/plugin-flow-builder](https://www.npmjs.com/package/@botonic/plugin-flow-builder)

- [Modified Smart Intents api request to call smart intents inference v2](https://github.com/hubtype/botonic/pull/2825)

### Fixed
- [Fix `bot action payload` limit characters](https://github.com/hubtype/botonic/pull/2817)

## [0.26.0] - 2024-04-30

Expand Down
101 changes: 13 additions & 88 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion packages/botonic-core/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@botonic/core",
"version": "0.26.1-alpha.0",
"version": "0.27.0-alpha.1",
"license": "MIT",
"description": "Build Chatbots using React",
"main": "./lib/cjs/index.js",
Expand Down
21 changes: 19 additions & 2 deletions packages/botonic-core/src/handoff.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,11 @@ export type HandoffExtraData = {
location?: string
}

interface BotEventData {
language: string
country: string
}

function contextDefaults(context: any): BackendContext {
return {
timeoutMs: context.timeoutMs || 10000,
Expand Down Expand Up @@ -68,6 +73,7 @@ export class HandOffBuilder {
_autoIdleMessage: string
_shadowing: boolean
_extraData: HandoffExtraData
_bot_event: BotEventData

constructor(session: Session) {
this._session = session
Expand Down Expand Up @@ -133,6 +139,11 @@ export class HandOffBuilder {
return this
}

withBotEvent(botEvent: BotEventData): this {
this._bot_event = botEvent
return this
}

async handOff(): Promise<void> {
return _humanHandOff(
this._session,
Expand All @@ -146,7 +157,8 @@ export class HandOffBuilder {
this._note,
this._autoIdleMessage,
this._shadowing,
this._extraData
this._extraData,
this._bot_event
)
}
}
Expand Down Expand Up @@ -183,6 +195,7 @@ interface HubtypeHandoffParams {
shadowing?: boolean
on_finish?: string
case_extra_data?: HandoffExtraData
bot_event?: BotEventData
}
async function _humanHandOff(
session: Session,
Expand All @@ -196,7 +209,8 @@ async function _humanHandOff(
note = '',
autoIdleMessage = '',
shadowing = false,
extraData: HandoffExtraData | undefined = undefined
extraData: HandoffExtraData | undefined = undefined,
botEvent: BotEventData
) {
const params: HubtypeHandoffParams = {}

Expand Down Expand Up @@ -232,6 +246,9 @@ async function _humanHandOff(
if (extraData) {
params.case_extra_data = extraData
}
if (botEvent) {
params.bot_event = botEvent
}
session._botonic_action = `create_case:${JSON.stringify(params)}`
}

Expand Down
2 changes: 2 additions & 0 deletions packages/botonic-core/src/models/legacy-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,7 @@ export interface Input extends Partial<NluResult> {
context?: {
campaign?: Campaign
}
message_id?: string
}

export interface Campaign {
Expand Down Expand Up @@ -201,6 +202,7 @@ export interface Session {
_shadowing?: boolean
external?: any
zendesk_ticket_id?: string
flow_thread_id?: string
}

export type InputMatcher = (input: Input) => boolean
Expand Down
4 changes: 2 additions & 2 deletions packages/botonic-plugin-flow-builder/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@botonic/plugin-flow-builder",
"version": "0.26.2-alpha.0",
"version": "0.27.0-alpha.4",
"main": "./lib/cjs/index.js",
"module": "./lib/esm/index.js",
"description": "Use Flow Builder to show your contents",
Expand All @@ -14,7 +14,7 @@
"lint_core": "../../node_modules/.bin/eslint_d --cache --quiet 'src/**/*.ts*'"
},
"dependencies": {
"@botonic/react": "^0.26.2-alpha.0",
"@botonic/react": "^0.27.0-alpha.1",
"axios": "^1.6.8",
"uuid": "^9.0.1"
},
Expand Down
Loading

0 comments on commit 2bd349b

Please sign in to comment.