UNPKG

@lableb/javascript-sdk

Version:

Lableb cloud search client for javascript

219 lines (160 loc) 6.9 kB
# Lableb's Client | Recommend Feedback API After creating a new instance of Lableb's Client, ```js const lablebClient = LablebClient({ APIKey: process.env.API_KEY, platformName: process.env.PLATFORM_NAME, }); ``` You can access the recommend feedback functions at `lablebClient.feedback.recommend`, where you have two types of feedbacks functions: `single` and `batch` Use `lablebClient.feedback.recommend.single` to send one feedback at a time to Lableb, and use `lablebClient.feedback.recommend.batch` to send many feedbacks at the same time to Lableb. # Options ## Single Feedback | field | type | description | | ------------------ | ------ | ----------- | | documentFeedback* | object | feedback object(see below for details) | | platformName | string | your platform name in small-letters | | indexName | string | the used index name for the recommend function | | recommendHandler | string | the used recommend handler for the recommend function | | APIKey | string | your recommend API Key copied from [Lableb Dashboard](https://dashboard.lableb.com) | ## Content of the feedback object: `documentFeedback` | field | type | description | | ----------- | ------ | ----------- | | sourceId* | string | Source data Id that is used to fetch the recommendations | | sourceTitle | string | Source data Title that is used to fetch the recommendations | | sourceUrl | string | Source data URL that is used to fetch the recommendations | | targetId* | string | Target data Id that"s the end-user interact with | | targetTitle | string | Target data Title that"s the end-user interact with | | targetUrl | string | Target data URL that"s the end-user interact with | | itemOrder | string | document"s order clicked on or chosen by the user | | sessionId | string | uniques session id of the end user | | userId | string | end user id | | userIp | string | end user IP address | | userCountry | string | end user country code(ISO-3166) | Example ```js await lablebClient.feedback.recommend.single({ documentFeedback: { sourceId: 141, sourceTitle: "Searching for water", sourceUrl: "https://example.com/articles/searching-for-water", targetId: 984, targetTitle: "How to preserve water consumption by 20%", targetUrl: "https://example.com/articles/preserve-water-consumption", itemOrder: 2, sessionId: "a9VAm22", userCountry: "AE", userId: "um29ava-mv92na-kieyqn-annv1", userIp: "172.165.32.1" } }); ``` ## Batch Feedback Its options basically the same as `single` feedback options except using `documentsFeedbacks` as an array instead of `documentFeedback` as an object. | field | type | description | | ------------------- | ------ | ----------- | | documentsFeedbacks* | array | array of feedback object(see below for details) | | platformName | string | your platform name in small-letters | | indexName | string | the used index name for the recommend function | | recommendHandler | string | the used recommend handler for the recommend function | | APIKey | string | your recommend API Key copied from [Lableb Dashboard](https://dashboard.lableb.com) | Example ```js await lablebClient.feedback.recommend.batch({ documentsFeedbacks: [ { sourceId: 141, sourceTitle: "Searching for water", sourceUrl: "https://example.com/articles/searching-for-water", targetId: 984, targetTitle: "How to preserve water consumption by 20%", targetUrl: "https://example.com/articles/preserve-water-consumption", itemOrder: 2, sessionId: "a9VAm22", userCountry: "AE", userId: "um29ava-mv92na-kieyqn-annv1", userIp: "172.165.32.1" }, { sourceId: 141, sourceTitle: "Searching for water", sourceUrl: "https://example.com/articles/searching-for-water", targetId: 753, targetTitle: "How to purify your water at home", targetUrl: "https://example.com/articles/purify-your-water-at-home", itemOrder: 7, sessionId: "a9VAm22", userCountry: "AE", userId: "um29ava-mv92na-kieyqn-annv1", userIp: "172.165.32.1" }, ... ] }); ``` -------------------- #### Alternative syntax for async code ```js let lablebClient = LablebClient({ APIKey: process.env.API_KEY, platformName: process.env.PLATFORM_NAME, }) lablebClient.feedback.recommend.single({ documentFeedback: { sourceId: 141, sourceTitle: "Searching for water", sourceUrl: "https://example.com/articles/searching-for-water", targetId: 984, targetTitle: "How to preserve water consumption by 20%", targetUrl: "https://example.com/articles/preserve-water-consumption", itemOrder: 2, sessionId: "a9VAm22", userCountry: "AE", userId: "um29ava-mv92na-kieyqn-annv1", userIp: "172.165.32.1" } }) .then(() => { console.log("Recommend feedback has been sent."); }) .catch(console.error); ``` --------------------------- #### Real Usage Example ```js const { code, response, time } = await lablebClient.recommend(options); const recommendResults = response.results; ``` Recommend results is an array of objects representing your platform documents(data), each document automatically has a `feedback` object in it that contains everything you need to send within the feedback request. ```json [ { id: "8112-cmb", feedback: { sourceId: 141, sourceTitle: "Searching for water", sourceUrl: "https://example.com/articles/searching-for-water", targetId: 984, targetTitle: "How to preserve water consumption by 20%", targetUrl: "https://example.com/articles/preserve-water-consumption", itemOrder: 2, sessionId: "a9VAm22", userCountry: "AE", userId: "um29ava-mv92na-kieyqn-annv1", userIp: "172.165.32.1" }, ...fields }, ... ] ``` Now you can use the `feedback` object returned to be sent with the recommend feedback request ```js const { code, response, time } = await lablebClient.recommend(options); const recommendResults = response.results; // later on, the end user clicks on the first result const userInteraction = recommendResults[0]; await lablebClient.feedback.recommend.single({ documentFeedback: userInteraction.feedback }); ```