Skip to content

Commit

Permalink
Blt 992 plugin hubtype analytics create new web event endpoint (#2878)
Browse files Browse the repository at this point in the history
## Description

**@botonic/core**
- Remove `bot_interaction_id` from HandoffBuilder.withBotEvent. This
value is obtained by the backend when generate the event.

**@botonic/plugin-hubtype-analytics**
- Use the new endpoint for webevents created from frontend. This new
endpoint does not use the `bot_id` and uses the session.user.id of the
webchat which is the `provider_id` instead of the chat_id from the
lambda.
- Fixes a typo in feedback_knowledge_base_event
- `webviewStepNumber` is required in EventWebviewStep 

**@botonic/react**
- Saves the botInteractionId in the localStorage message
- Use the webchat session to generate the knowledge base feedback event.

**@botonic/plugin-flow-builder**
- Add HandoffBuilder.withExtraData({language}), to be able to use
automatic translations on the desk
  • Loading branch information
Iru89 authored Jul 31, 2024
1 parent c9b75e8 commit 14d758a
Show file tree
Hide file tree
Showing 21 changed files with 6,806 additions and 8,834 deletions.
10,705 changes: 6,711 additions & 3,994 deletions package-lock.json

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"clean": "rimraf packages/**/lib"
},
"dependencies": {
"axios": "^1.6.8"
"axios": "^1.7.2"
},
"devDependencies": {
"@babel/cli": "^7.23.9",
Expand Down
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.28.1",
"version": "0.28.2",
"license": "MIT",
"description": "Build Chatbots using React",
"main": "./lib/cjs/index.js",
Expand Down
1 change: 0 additions & 1 deletion packages/botonic-core/src/handoff.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ export type HandoffExtraData = {
interface BotEventData {
language: string
country: string
bot_interaction_id?: string
}

function contextDefaults(context: any): BackendContext {
Expand Down
1,180 changes: 0 additions & 1,180 deletions packages/botonic-plugin-flow-builder/package-lock.json

This file was deleted.

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.28.1",
"version": "0.28.2",
"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.28.1",
"@botonic/react": "^0.28.2",
"axios": "^1.7.2",
"uuid": "^9.0.1"
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,11 +52,16 @@ export class FlowHandoff extends ContentFieldsBase {
}

if (this.queue) {
const language = request.session.user.extra_data.language
const country = request.session.user.extra_data.country

handOffBuilder.withQueue(this.queue.id)
handOffBuilder.withBotEvent({
language: request.session.user.extra_data.language,
country: request.session.user.extra_data.country,
bot_interaction_id: request.input.bot_interaction_id,
language,
country,
})
handOffBuilder.withExtraData({
language,
})
this.isTestIntegration = request.session.is_test_integration
await handOffBuilder.handOff()
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.28.1",
"version": "0.28.2",
"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.28.1",
"@botonic/core": "^0.28.2",
"axios": "^1.7.2"
},
"devDependencies": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { HtEvent } from './ht-event'

export class HtEventFeedbackKnowledgebase extends HtEvent {
action: EventAction.FeedbackKnowledgebase
knowledgebase_inference_id: string
knowledge_base_inference_id: string
feedback_target_bot_interaction_id: string
feedback_target_id: string
feedback_group_id: string
Expand All @@ -22,7 +22,7 @@ export class HtEventFeedbackKnowledgebase extends HtEvent {
super(event, requestData)
this.type = EventType.WebEvent
this.action = event.action
this.knowledgebase_inference_id = event.knowledgebaseInferenceId
this.knowledge_base_inference_id = event.knowledgebaseInferenceId
this.feedback_target_bot_interaction_id = event.feedbackBotInteractionId
this.feedback_target_id = event.feedbackTargetId
this.feedback_group_id = event.feedbackGroupId
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ export class HtEventWebviewStep extends HtEvent {
flow_thread_id?: string
webview_thread_id: string
webview_name: string
webview_step_name?: string
webview_step_n?: number
webview_step_name: string
webview_step_n: number

constructor(event: EventWebviewStep, requestData: RequestData) {
super(event, requestData)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,8 @@
/* eslint-disable @typescript-eslint/naming-convention */
import { EventAction, EventType, HtEventProps, RequestData } from '../types'

const excludedEvents = [
EventAction.FeedbackCase,
EventAction.FeedbackConversation,
EventAction.FeedbackKnowledgebase,
EventAction.FeedbackWebview,
EventAction.WebviewStep,
EventAction.WebviewEnd,
]

export class HtEvent {
chat_id: string
chat_id?: string
type: EventType
chat_language: string
chat_country?: string
Expand All @@ -26,9 +18,6 @@ export class HtEvent {
this.chat_country = requestData.country
this.format_version = 2
this.action = event.action

if (!excludedEvents.includes(event.action)) {
this.bot_interaction_id = requestData.botInteractionId
}
this.bot_interaction_id = requestData.botInteractionId
}
}
72 changes: 52 additions & 20 deletions packages/botonic-plugin-hubtype-analytics/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { BotRequest, Plugin } from '@botonic/core'
import axios from 'axios'
import { send } from 'process'

import { HtEvent } from './event-models'
import { EventType, HtEventProps, RequestData } from './types'
Expand All @@ -19,11 +20,12 @@ function getDefaultCountry(request: BotRequest): string {
}

export default class BotonicPluginHubtypeAnalytics implements Plugin {
baseUrl: string
eventsBaseUrl: string
getLanguage: (request: BotRequest) => string
getCountry: (request: BotRequest) => string
constructor(options?: HubtypeAnalyticsOptions) {
this.baseUrl = process.env.HUBTYPE_API_URL || 'https://api.hubtype.com'
const hubtypeUrl = process.env.HUBTYPE_API_URL || 'https://api.hubtype.com'
this.eventsBaseUrl = `${hubtypeUrl}/external/v2/conversational_apps`
this.getLanguage = options?.getLanguage || getDefaultLanguage
this.getCountry = options?.getCountry || getDefaultCountry
}
Expand All @@ -32,39 +34,69 @@ export default class BotonicPluginHubtypeAnalytics implements Plugin {
return
}

getUrl(request: BotRequest, eventType: EventType) {
const endpoint =
eventType === EventType.BotEvent ? 'bot_event' : 'web_event'
const botId = request.session.bot.id
return `${this.baseUrl}/external/v2/conversational_apps/${botId}/${endpoint}/`
async trackEvent(request: BotRequest, htEventProps: HtEventProps) {
if (request.session.is_test_integration) {
return Promise.resolve({
data: 'Event not sent because it is a test integration',
})
}

const requestData = this.getRequestData(request)
const event = createHtEvent(requestData, htEventProps)
return this.sendEvent(request, event)
}

getRequestData(request: BotRequest): RequestData {
return {
language: this.getLanguage(request),
country: this.getCountry(request),
userId: request.session.user.id,
userId: this.isLambdaEvent(request) ? request.session.user.id : undefined,
botInteractionId: request.input?.bot_interaction_id,
}
}

async trackEvent(request: BotRequest, htEventProps: HtEventProps) {
if (request.session.is_test_integration) {
return Promise.resolve({
data: 'Event not sent because it is a test integration',
})
private isLambdaEvent(request: BotRequest): boolean {
return request.session?.bot?.id !== undefined
}

private sendEvent(request: BotRequest, event: HtEvent) {
if (event.type === EventType.BotEvent) {
return this.sendBotEvent(request, event)
}

const requestData = this.getRequestData(request)
const event = createHtEvent(requestData, htEventProps)
return this.sendEvent(request, event)
return this.sendWebEvent(request, event)
}

private sendEvent(request: BotRequest, event: HtEvent) {
const url = this.getUrl(request, event.type)
const headers = { Authorization: `Bearer ${request.session._access_token}` }
const config = event.type !== EventType.WebEvent ? { headers } : undefined
private sendBotEvent(request: BotRequest, event: HtEvent) {
const botId = request.session.bot.id
const url = `${this.eventsBaseUrl}/${botId}/bot_event/`
const config = {
headers: { Authorization: `Bearer ${request.session._access_token}` },
}
return axios.post(url, event, config)
}

private sendWebEvent(request: BotRequest, event: HtEvent) {
if (this.isLambdaEvent(request)) {
return this.sendWebEventByBotId(request, event)
}

return this.sendWebEventByProviderId(request, event)
}

private sendWebEventByBotId(request: BotRequest, event: HtEvent) {
const botId = request.session.bot.id
const url = `${this.eventsBaseUrl}/${botId}/web_event/`
return axios.post(url, event)
}

private sendWebEventByProviderId(request: BotRequest, event: HtEvent) {
const url = `${this.eventsBaseUrl}/web_event/`
const config = {
params: {
provider_id: request.session.user.id,
},
}
return axios.post(url, event, config)
}
}
Expand Down
4 changes: 2 additions & 2 deletions packages/botonic-plugin-hubtype-analytics/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ export interface EventWebviewStep extends HtBaseEventProps {
webviewThreadId: string
webviewName: string
webviewStepName: string
webviewStepNumber?: number
webviewStepNumber: number
}

export interface EventWebviewEnd extends HtBaseEventProps {
Expand Down Expand Up @@ -175,6 +175,6 @@ export type HtEventProps =
export interface RequestData {
language: string
country: string
userId: string
userId?: string
botInteractionId: string
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,13 @@ describe('Create feedback knowledgebase event', () => {
feedback_target_id: 'messageIdTest',
feedback_group_id: 'groupIdTest',
feedback_target_bot_interaction_id: 'testInteractionId',
knowledgebase_inference_id: 'knowledgebaseInferenceIdTest',
knowledge_base_inference_id: 'knowledgebaseInferenceIdTest',
possible_options: ['thumbs_down', 'thumbs_up'],
possible_values: [0, 1],
option: 'thumbs_down',
value: 0,
type: EventType.WebEvent,
bot_interaction_id: 'testInteractionId',
})
})
})
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ describe('Create feedback event', () => {
option: '**',
value: 2,
type: EventType.WebEvent,
bot_interaction_id: 'testInteractionId',
})
})

Expand Down Expand Up @@ -57,6 +58,7 @@ describe('Create feedback event', () => {
value: 2,
comment: 'This is a comment',
type: EventType.WebEvent,
bot_interaction_id: 'testInteractionId',
})
})
})
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ describe('Create webview events', () => {
webview_step_name: 'step1',
webview_step_n: 1,
type: EventType.WebEvent,
bot_interaction_id: 'testInteractionId',
})
})

Expand Down Expand Up @@ -50,6 +51,7 @@ describe('Create webview events', () => {
webview_end_fail_type: 'canceled_by_user',
webview_end_fail_message: 'Closed by user',
type: EventType.WebEvent,
bot_interaction_id: 'testInteractionId',
})
})
})
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ export function createRequest(args?: RequestArgs): BotRequest {
_access_token: 'fake_access_token',
_hubtype_api: 'https://api.hubtype.com',
is_test_integration: false,
flow_thread_id: 'testFlowThreadId',
},
input: {
data: 'Hola',
Expand Down
Loading

0 comments on commit 14d758a

Please sign in to comment.