Skip to content

Commit

Permalink
plugin-hubtype-analytics: add attributes in nlu events #BLT-1318 (#2960)
Browse files Browse the repository at this point in the history
## Description

@botonic/plugin-hubtype-analytics
- add flow_thread_id, flow_node_id and flow_id in nlu events (keywords,
intents, smart intents and knowledgebase)
- remove nlu_keyword_id, is the same id used for flow_node_id

@botonic/plugin-flow-builder
- add flow_thread_id, flow_node_id and flow_id when generate an events
for keywords, intents, smart intents and knowledgebase

## Testing

Tests updated in boths plugins
  • Loading branch information
Iru89 authored Jan 22, 2025
1 parent 05ae410 commit 2091a9f
Show file tree
Hide file tree
Showing 24 changed files with 183 additions and 46 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ All notable changes to Botonic will be documented in this file.

<details>
<summary>
Changes that have landed in master but are not yet released.
Changes that have landed in master-lts but are not yet released.
Click to see more.
</summary>

Expand Down
20 changes: 2 additions & 18 deletions package-lock.json

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

23 changes: 23 additions & 0 deletions packages/botonic-plugin-flow-builder/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# Changelog

All notable changes to Botonic will be documented in this file.

## [Unreleased]

<details>
<summary>
Changes that have landed in master-lts but are not yet released.
Click to see more.
</summary>

## [0.31.0] - 2024-mm-dd

### Added

### Changed

- [create nlu and knowledgebase events with `flow_thread_id, flow_node_id and flow_id attributes`](https://github.com/hubtype/botonic/pull/2960)

### Fixed

</details>
27 changes: 23 additions & 4 deletions packages/botonic-plugin-flow-builder/src/action/knowledge-bases.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { ActionRequest } from '@botonic/react'

import { FlowContent, FlowKnowledgeBase } from '../content-fields'
import { HtNodeWithContent } from '../content-fields/hubtype-fields/nodes'
import { EventAction, KnowledgebaseFailReason, trackEvent } from '../tracking'
import { KnowledgeBaseFunction, KnowledgeBaseResponse } from '../types'
import { inputHasTextData } from '../utils'
Expand Down Expand Up @@ -32,6 +33,9 @@ export async function getContentsByKnowledgeBase({
}

const sourceIds = knowledgeBaseContent.sourcesData.map(source => source.id)
const flowId = cmsApi.getNodeById<HtNodeWithContent>(
knowledgeBaseContent.id
).flow_id

if (
flowBuilderPlugin.getKnowledgeBaseResponse &&
Expand All @@ -43,7 +47,8 @@ export async function getContentsByKnowledgeBase({
flowBuilderPlugin.getKnowledgeBaseResponse,
request,
contents,
sourceIds
knowledgeBaseContent,
flowId
)

if (contentsWithKnowledgeResponse) {
Expand All @@ -58,14 +63,21 @@ async function getContentsWithKnowledgeResponse(
getKnowledgeBaseResponse: KnowledgeBaseFunction,
request: ActionRequest,
contents: FlowContent[],
sourceIds: string[]
knowledgeBaseContent: FlowKnowledgeBase,
flowId: string
): Promise<FlowContent[] | undefined> {
const sourceIds = knowledgeBaseContent.sourcesData.map(source => source.id)
const knowledgeBaseResponse = await getKnowledgeBaseResponse(
request,
request.input.data!,
sourceIds
)
await trackKnowledgeBase(knowledgeBaseResponse, request, sourceIds)
await trackKnowledgeBase(
knowledgeBaseResponse,
request,
knowledgeBaseContent,
flowId
)

if (
!knowledgeBaseResponse.hasKnowledge ||
Expand Down Expand Up @@ -94,12 +106,16 @@ function updateContentsWithResponse(
async function trackKnowledgeBase(
response: KnowledgeBaseResponse,
request: ActionRequest,
sourceIds: string[]
knowledgeBaseContent: FlowKnowledgeBase,
flowId: string
) {
const sourceIds = knowledgeBaseContent.sourcesData.map(source => source.id)
const knowledgebaseInferenceId = response.inferenceId
const knowledgebaseSourcesIds = sourceIds
const knowledgebaseChunksIds = response.chunkIds
const knowledgebaseMessageId = request.input.message_id
const flowThreadId = request.session.flow_thread_id
const flowNodeId = knowledgeBaseContent.id

let knowledgebaseFailReason: KnowledgebaseFailReason | undefined

Expand All @@ -118,5 +134,8 @@ async function trackKnowledgeBase(
knowledgebaseChunksIds,
knowledgebaseMessageId,
userInput: request.input.data,
flowThreadId,
flowId,
flowNodeId,
})
}
3 changes: 3 additions & 0 deletions packages/botonic-plugin-flow-builder/src/user-input/intent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@ async function trackIntentEvent(
nluIntentThreshold: intentNode?.content.confidence,
nluIntentMessageId: request.input.message_id,
userInput: request.input.data,
flowThreadId: request.session.flow_thread_id,
flowId: intentNode.flow_id,
flowNodeId: intentNode.id,
}
await trackEvent(request, EventAction.Intent, eventArgs)
}
Expand Down
13 changes: 11 additions & 2 deletions packages/botonic-plugin-flow-builder/src/user-input/keyword.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@ import { ActionRequest } from '@botonic/react'

import { FlowBuilderApi } from '../api'
import { REG_EXP_PATTERN } from '../constants'
import { HtKeywordNode } from '../content-fields/hubtype-fields'
import {
HtKeywordNode,
HtNodeWithContent,
} from '../content-fields/hubtype-fields'
import { EventAction, trackEvent } from '../tracking'

interface KeywordProps {
Expand All @@ -17,6 +20,7 @@ export class KeywordMatcher {
public isRegExp: boolean
public matchedKeyword?: string
public keywordNodeId?: string
public flowId?: string

constructor({ cmsApi, locale, request }: KeywordProps) {
this.cmsApi = cmsApi
Expand Down Expand Up @@ -50,6 +54,9 @@ export class KeywordMatcher {
const result = node.content.keywords.find(keywords => {
if (keywords.locale === this.locale) {
this.keywordNodeId = node.id
this.flowId = this.cmsApi.getNodeById<HtNodeWithContent>(
node.id
).flow_id
return this.inputMatchesAnyKeyword(userInput, keywords.values)
}

Expand Down Expand Up @@ -87,11 +94,13 @@ export class KeywordMatcher {

private async trackKeywordEvent() {
const eventArgs = {
nluKeywordId: this.keywordNodeId,
nluKeywordName: this.matchedKeyword,
nluKeywordIsRegex: this.isRegExp,
nluKeywordMessageId: this.request.input.message_id,
userInput: this.request.input.data,
flowThreadId: this.request.session.flow_thread_id,
flowId: this.flowId,
flowNodeId: this.keywordNodeId,
}
await trackEvent(this.request, EventAction.Keyword, eventArgs)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ export class SmartIntentsApi {
constructor(
public cmsApi: FlowBuilderApi,
public currentRequest: ActionRequest,
public smartIntentsConfig: SmartIntentsInferenceConfig
public smartIntentsConfig: SmartIntentsInferenceConfig,
public flowId?: string
) {}

async getNodeByInput(): Promise<HtSmartIntentNode | undefined> {
Expand All @@ -50,6 +51,9 @@ export class SmartIntentsApi {
nluIntentSmartNumUsed: response.data.smart_intents_used.length,
nluIntentSmartMessageId: this.currentRequest.input.message_id,
userInput: this.currentRequest.input.data,
flowThreadId: this.currentRequest.session.flow_thread_id,
flowId: smartIntentNode.flow_id,
flowNodeId: smartIntentNode.id,
})
return smartIntentNode
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ export function createRequest({
_access_token: 'fake_access_token',
_hubtype_api: 'https://api.hubtype.com',
is_test_integration: false,
flow_thread_id: 'testFlowThreadId',
},
input: {
bot_interaction_id: 'testInteractionId',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,9 @@ describe('Check tracked events when a contents are displayed', () => {
expect.anything(),
'nlu_keyword',
{
nluKeywordId: '8ec6a479-dca5-4623-8bab-41fa49c9d6e8',
flowId: '43a736f8-4837-4fbb-a661-021291749b4f',
flowNodeId: '8ec6a479-dca5-4623-8bab-41fa49c9d6e8',
flowThreadId: 'testFlowThreadId',
nluKeywordName: 'flowText',
nluKeywordIsRegex: false,
nluKeywordMessageId: expect.anything(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,9 @@ describe('Check tracked events when a bot generates a response using a knowledge
knowledgebaseChunksIds: ['sourceChunkId1', 'sourceChunkId2'],
knowledgebaseMessageId: 'testMessageId',
userInput: 'What is Flow Builder?',
flowId: '4c8acc81-accd-529e-8bb6-d17f4cafafea',
flowNodeId: 'b2ac9457-6928-41ea-9474-911133a75ff4',
flowThreadId: 'testFlowThreadId',
}
)
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@ describe('Check tracked events when a contents are displayed after match with sm
nluIntentSmartNumUsed: 2,
nluIntentSmartMessageId: expect.anything(),
userInput: 'How can I add a bag to my booking?',
flowId: '8d527e7d-ea6d-5422-b810-5b4c8be7657b',
flowNodeId: 'a962b2e5-9424-4fe5-81bd-8cb398b59875',
flowThreadId: 'testFlowThreadId',
}
)
expect(trackEventMock).toHaveBeenNthCalledWith(
Expand Down
23 changes: 23 additions & 0 deletions packages/botonic-plugin-hubtype-analytics/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# Changelog

All notable changes to Botonic will be documented in this file.

## [Unreleased]

<details>
<summary>
Changes that have landed in master-lts but are not yet released.
Click to see more.
</summary>

## [0.31.0] - 2024-mm-dd

### Added

### Changed

- [`add flow_thread_id, flow_node_id and flow_id` in nlu and knowledgebase events](https://github.com/hubtype/botonic/pull/2960)

### Fixed

</details>
24 changes: 13 additions & 11 deletions packages/botonic-plugin-hubtype-analytics/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,19 @@ npm i @botonic/plugin-hubtype-analytics
You can define two optional functions to obtain the language and the country.
By default if you do not define these functions it will use the language defined in request.session.user.extra_data.language and country defined in request.session.user.extra_data.country

```
export const plugins = [
{
id: 'hubtype-analytics',
resolve: require('@botonic/plugin-hubtype-analytics'),
options: {
getLaguange?: (request: BotRequest) => request.session.user.extra_data.lang
getCountry?: (request: BotRequest) => request.session.user.extra_data.store
},
```typescript
export const plugins = [
{
id: 'hubtype-analytics',
resolve: require('@botonic/plugin-hubtype-analytics'),
options: {
getLaguange: (request: BotRequest) =>
request.session.user.extra_data.lang,
getCountry: (request: BotRequest) =>
request.session.user.extra_data.store,
},
]
},
]
```

## Plugin Options
Expand Down Expand Up @@ -60,7 +62,7 @@ export class WelcomeAction extends FlowBuilderMultichannelAction {
}
```

- To track a handoff, you can use an instance of `HandOffBuilder` from `@botonic/core` `handoffBuilder.withBotEvent()` so thaht the backend will create the event after the handoff has been done correctly.
- To track a handoff, you can use an instance of `HandOffBuilder` from `@botonic/core` `handoffBuilder.withBotEvent()` so that the backend will create the event after the handoff has been done correctly.

```typescript
const handOffBuilder = new HandOffBuilder(request.session)
Expand Down
4 changes: 2 additions & 2 deletions packages/botonic-plugin-hubtype-analytics/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@botonic/plugin-hubtype-analytics",
"version": "0.30.2",
"version": "0.31.0-alpha.0",
"description": "Plugin for tracking in the Hubtype backend to see the results in the Hubtype Dashbord",
"main": "./lib/cjs/index.js",
"module": "./lib/esm/index.js",
Expand All @@ -14,7 +14,7 @@
},
"dependencies": {
"@babel/runtime": "^7.21.0",
"@botonic/core": "^0.30.0",
"@botonic/core": "0.31.0-alpha.1",
"axios": "^1.7.2"
},
"devDependencies": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ export class HtEventIntentSmart extends HtEvent {
nlu_intent_smart_num_used: number
nlu_intent_smart_message_id: string
user_input: string
flow_thread_id: string
flow_id: string
flow_node_id: string

constructor(event: EventIntentSmart, requestData: RequestData) {
super(event, requestData)
Expand All @@ -15,5 +18,8 @@ export class HtEventIntentSmart extends HtEvent {
this.nlu_intent_smart_num_used = event.nluIntentSmartNumUsed
this.nlu_intent_smart_message_id = event.nluIntentSmartMessageId
this.user_input = event.userInput
this.flow_thread_id = event.flowThreadId
this.flow_id = event.flowId
this.flow_node_id = event.flowNodeId
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ export class HtEventIntent extends HtEvent {
nlu_intent_threshold: number
nlu_intent_message_id: string
user_input: string
flow_thread_id: string
flow_id: string
flow_node_id: string

constructor(event: EventIntent, requestData: RequestData) {
super(event, requestData)
Expand All @@ -17,5 +20,8 @@ export class HtEventIntent extends HtEvent {
this.nlu_intent_threshold = event.nluIntentThreshold
this.nlu_intent_message_id = event.nluIntentMessageId
this.user_input = event.userInput
this.flow_thread_id = event.flowThreadId
this.flow_id = event.flowId
this.flow_node_id = event.flowNodeId
}
}
Loading

0 comments on commit 2091a9f

Please sign in to comment.