Retrieving data from your integration source
Before you can start sending any data into Dimension, you will first need to collect (fetch) your data, and then format the data. Whether you want to collect your data from applications or a database, you will need to simply look at the documentation of whatever source you are looking to gather your data from, and determine which API endpoints are relevant for retrieving data that can be then analyzed
To give you an example of what this might look like, we've created this Github repository that shows an example of fetching data from the popular chatbot vendor, ADA (we also have our integration guide for more information).
Below is the sample code and our commentary pasted from the same example Github repository above:
import { fetchAdaMessagesFromAdaAPI, fetchStaticAdaMessages } from "./fetch-ada-data.js";
const DIMENSION_API_KEY = 'API_KEY_HERE'
const TRACKER_URL_PREFIX = 'https://tracker.dimensionlabs.io/track?platform=universal&v=10.1.1-rest'
/**
* Main method for this example program.
*
* -- Fetches Ada messages
* -- Converts to Dimension Labs Universal format
* -- Sends to Dimension Labs via their message tracker endpoint for data ingestion
*/
const handleAdaToUniveralConversion = async (shouldUseExampleData) => {
// Fetch messages from Ada
let adaMessages
if (shouldUseExampleData) {
adaMessages = fetchStaticAdaMessages()
} else {
adaMessages = await fetchAdaMessagesFromAdaAPI()
}
const messageDataArray = adaMessages.data
// Convert to the Dimension Labs Univeral format
const universalData = messageDataArray.map((adaMessage) => {
switch (adaMessage.message_data._type) {
case 'text':
return {
incoming: adaMessage.sender === 'ada' || adaMessage.sender === 'bot',
dimension_timestamp: new Date(adaMessage.date_created).getTime(),
text: adaMessage.message_data.body,
conversationId: adaMessage.conversation_id,
userId: adaMessage.chatter_id,
platformJson: { ...adaMessage } // PlatformJson is a space for any additional data you want to send to Dimension Labs
}
break;
default: // Note that events, pictures, and other attachments and types are supported. Please see the Dimension Labs API documentation for more information.
console.trace("Unsupported message type: " + adaMessage.message_data._type)
break;
}
}).filter((message) => message !== undefined)
// The conversion is done. This is simply illustrative of how to get this over to the Dimension Labs system.
await sendToDimension(universalData)
}
/**
* Sends data to Dimension Labs via the message tracker endpoint. Note that at this point, you could also write this array to
* a file in JSONL format and upload via the file upload functionality.
*
* @param universalData Data translated to the universal format
*/
const sendToDimension = async (universalData) => {
for (const message of universalData) {
const data = JSON.stringify(message)
const url =`${TRACKER_URL_PREFIX}&apiKey=${DIMENSION_API_KEY}&type=${message.incoming ? 'incoming' : 'outgoing'}`
const res = await fetch(url, {
method: 'POST',
headers: {
'accept': 'application/json',
'Content-Type': 'application/json',
},
body: data
})
if (res.status === 200) {
console.trace("Yay! We sent a message to Dimension Labs!")
} else {
console.error("Oh no! Something went wrong. Check the configs and possible contact support.")
}
}
}
// Change the parameter to false if you want to test with live data. You will need to fill in your own Ada API information.
await handleAdaToUniveralConversion(true)