obsly-sdk-js-full
Version:
This is the Obsly SDK for javascript...
865 lines (830 loc) • 26.1 kB
JavaScript
/* eslint-disable no-undef */
import "./main.bundle";
const initializeTimeout = 1500;
const attachedEvent = "obsly_sdk_attached";
const readyEvent = "obsly_sdk_ready";
/**
* Initialize the Obsly SDK with the provided parameters.
* @param {InitParameters} params The initialization parameters for Obsly SDK.
* @returns {Promise<void>}
*/
export async function init(params) {
if (ObslySDK) {
ObslySDK.init(params);
} else {
console.log("Obsly SDK is not ready... waiting a second for execute init.");
return new Promise((resolve, reject) => {
const timeout = setTimeout(() => {
reject(
new Error(
`[Obsly SDK] [init] Obsly SDK initialization timed out after waiting for ${initializeTimeout} milliseconds.`
)
);
}, initializeTimeout);
window.addEventListener(attachedEvent, () => {
clearTimeout(timeout);
ObslySDK.init(params);
resolve();
});
});
}
}
/**
* Set an identifier for an user in Obsly.
* @param {string} userID User Identifier.
*/
export const setUserID = function (userID) {
if (ObslySDK) {
ObslySDK.setUserID(userID);
} else {
console.log(
"Obsly SDK is not ready... waiting a second for execute setUserID."
);
return new Promise((resolve, reject) => {
const timeout = setTimeout(() => {
reject(
new Error(
`[Obsly SDK] [setUserID] Obsly SDK initialization timed out after waiting for ${initializeTimeout} milliseconds.`
)
);
}, initializeTimeout);
window.addEventListener(attachedEvent, () => {
clearTimeout(timeout);
ObslySDK.setUserID(userID);
resolve();
});
});
}
};
/**
* Set an identifier for a person in Obsly.
* @param {string} personID Person Identifier.
*/
export const setPersonID = function (personID) {
if (ObslySDK) {
ObslySDK.setPersonID(personID);
} else {
console.log(
"Obsly SDK is not ready... waiting a second for execute setPersonID."
);
return new Promise((resolve, reject) => {
const timeout = setTimeout(() => {
reject(
new Error(
`[Obsly SDK] [setPersonID] Obsly SDK initialization timed out after waiting for ${initializeTimeout} milliseconds.`
)
);
}, initializeTimeout);
window.addEventListener(attachedEvent, () => {
clearTimeout(timeout);
ObslySDK.setPersonID(personID);
resolve();
});
});
}
};
/**
* Set an identifier for a Passport in Obsly.
* @param {string} passportID Passport Identifier.
*/
export const setPassportID = function (passportID) {
if (ObslySDK) {
ObslySDK.setPassportID(passportID);
} else {
console.log(
"Obsly SDK is not ready... waiting a second for execute setPassportID."
);
return new Promise((resolve, reject) => {
const timeout = setTimeout(() => {
reject(
new Error(
`[Obsly SDK] [setPassportID] Obsly SDK initialization timed out after waiting for ${initializeTimeout} milliseconds.`
)
);
}, initializeTimeout);
window.addEventListener(attachedEvent, () => {
clearTimeout(timeout);
ObslySDK.setPassportID(passportID);
resolve();
});
});
}
};
/**
* Set an identifier for a contract in Obsly.
* @param {string} contractID Contract Identifier.
*/
export const setContractID = function (contractID) {
if (ObslySDK) {
ObslySDK.setContractID(contractID);
} else {
console.log(
"Obsly SDK is not ready... waiting a second for execute setContractID."
);
return new Promise((resolve, reject) => {
const timeout = setTimeout(() => {
reject(
new Error(
`[Obsly SDK] [setContractID] Obsly SDK initialization timed out after waiting for ${initializeTimeout} milliseconds.`
)
);
}, initializeTimeout);
window.addEventListener(attachedEvent, () => {
clearTimeout(timeout);
ObslySDK.setContractID(contractID);
resolve();
});
});
}
};
/**
* Set a name for identify your App in Obsly.
* @param {string} appName App Name Identifier.
*/
export const setAppName = function (appName) {
if (ObslySDK) {
ObslySDK.setAppName(appName);
} else {
console.log(
"Obsly SDK is not ready... waiting a second for execute setAppName."
);
return new Promise((resolve, reject) => {
const timeout = setTimeout(() => {
reject(
new Error(
`[Obsly SDK] [setAppName] Obsly SDK initialization timed out after waiting for ${initializeTimeout} milliseconds.`
)
);
}, initializeTimeout);
window.addEventListener(attachedEvent, () => {
clearTimeout(timeout);
ObslySDK.setAppName(appName);
resolve();
});
});
}
};
/**
* Set a name for identify your App in Obsly.
* @param {string} appVersion App Version.
*/
export const setAppVersion = function (appVersion) {
if (ObslySDK) {
ObslySDK.setAppVersion(appVersion);
} else {
console.log(
"Obsly SDK is not ready... waiting a second for execute setAppVersion."
);
return new Promise((resolve, reject) => {
const timeout = setTimeout(() => {
reject(
new Error(
`[Obsly SDK] [setAppVersion] Obsly SDK initialization timed out after waiting for ${initializeTimeout} milliseconds.`
)
);
}, initializeTimeout);
window.addEventListener(attachedEvent, () => {
clearTimeout(timeout);
ObslySDK.setAppVersion(appVersion);
resolve();
});
});
}
};
/**
* Set log Level for SDK Obsly.
* @param {string} logLevel Allowed: null, error, warn, log, debug.
*/
export const setLogLevel = function (logLevel) {
if (ObslySDK) {
ObslySDK.setLogLevel(logLevel);
} else {
console.log(
"Obsly SDK is not ready... waiting a second for execute setLogLevel."
);
return new Promise((resolve, reject) => {
const timeout = setTimeout(() => {
reject(
new Error(
`[Obsly SDK] [setLogLevel] Obsly SDK initialization timed out after waiting for ${initializeTimeout} milliseconds.`
)
);
}, initializeTimeout);
window.addEventListener(attachedEvent, () => {
clearTimeout(timeout);
ObslySDK.setLogLevel(logLevel);
resolve();
});
});
}
};
/**
* Initializes the capture and sending of events according to the established configuration.
*/
export const startSession = function () {
if (ObslySDK) {
ObslySDK.startSession();
} else {
console.error("[Obsly SDK] [startSession] ObslySDK is not ready yet.");
}
};
/**
* Ends the current session. createNewSession can be called to start a new custom session; otherwise, a new session will be generated automatically.
*/
export const closeCurrentSession = function () {
if (ObslySDK) {
ObslySDK.closeCurrentSession();
} else {
console.error(
"[Obsly SDK] [closeCurrentSession] ObslySDK is not ready yet."
);
}
};
/**
* Initiates a new Obsly session with a custom name. Events will be sent with this additional data for filtering in the backend.
* @param {string} customSessionId Session Name.
*/
export const createNewSession = function (customSessionId) {
if (ObslySDK) {
ObslySDK.createNewSession(customSessionId);
} else {
console.error("[Obsly SDK] [createNewSession] ObslySDK is not ready yet.");
}
};
/**
* Pause capturing and sending events.
*/
export const pauseTracker = function () {
if (ObslySDK) {
ObslySDK.pauseTracker();
} else {
console.error("[Obsly SDK] [pauseTracker] ObslySDK is not ready yet.");
}
};
/**
* Resets event capture and sending.
*/
export const resumeTracker = function () {
if (ObslySDK) {
ObslySDK.resumeTracker();
} else {
console.error("[Obsly SDK] [resumeTracker] ObslySDK is not ready yet.");
}
};
/**
* Adds a blacklist of request URLs.
* @param {Array[string]} [blacklist=[]] URL Blacklist
*/
export const setRequestsBlacklist = function (blacklist) {
if (ObslySDK) {
ObslySDK.setRequestsBlacklist(blacklist);
} else {
console.log(
"Obsly SDK is not ready... waiting a second for execute addTag."
);
return new Promise((resolve, reject) => {
const timeout = setTimeout(() => {
reject(
new Error(
`[Obsly SDK] [setRequestsBlacklist] Obsly SDK initialization timed out after waiting for ${initializeTimeout} milliseconds.`
)
);
}, initializeTimeout);
window.addEventListener(attachedEvent, () => {
clearTimeout(timeout);
ObslySDK.setRequestsBlacklist(blacklist);
resolve();
});
});
}
};
/**
* Allows to send Key / Value extra data inmediatly.
* @param {Array} [tags=[]] Array of Tags. A tag must be an Object {key, value}
* @param {string} category Category.
*/
export const addTag = async function (tags, category) {
var date = Date.now();
if (ObslySDK) {
await ObslySDK.addTag(tags, category);
} else {
console.log(
"Obsly SDK is not ready... waiting a second for execute addTag."
);
return new Promise((resolve, reject) => {
const timeout = setTimeout(() => {
reject(
new Error(
`[Obsly SDK] [addTag] Event timed out after waiting for ${initializeTimeout} milliseconds. Have you execute the init function?`
)
);
}, initializeTimeout);
window.addEventListener(readyEvent, () => {
clearTimeout(timeout);
ObslySDK.addTag(tags, category, date);
resolve();
});
});
}
};
/**
* @deprecated This function is deprecated. Please use `addTag` instead.
*/
export const addObslyTag = async function (tags, category) {
console.warn("This function is deprecated. Please use `addTag` instead.");
return addTag(tags, category);
};
/**
* @deprecated This function is deprecated. Please use `addScreenshot` instead.
*/
export const addObslyScreenshot = async function () {
console.warn(
"This function is deprecated. Please use `addScreenshot` instead."
);
return addScreenshot();
};
/**
* Creates a browser screenshot event.
*/
export const addScreenshot = function () {
console.log("Screenshot is not available in this SDK version")
return;
// var date = Date.now();
// if (ObslySDK) {
// ObslySDK.addScreenshot();
// } else {
// console.log(
// "Obsly SDK is not ready... waiting a second for execute addScreenshot."
// );
// return new Promise((resolve, reject) => {
// const timeout = setTimeout(() => {
// reject(
// new Error(
// `[Obsly SDK] [addScreenshot] Event timed out after waiting for ${initializeTimeout} milliseconds. Have you execute the init function?`
// )
// );
// }, initializeTimeout);
// window.addEventListener(readyEvent, () => {
// clearTimeout(timeout);
// ObslySDK.addScreenshot(date);
// resolve();
// });
// });
// }
};
/**
* Start a Performance Event.
* @param {String} name Performance Event Name (Must be unique).
* @param {String} description (Optional) Description for Performance event (Debug field).
* @param {Number} startNanoTime (Optional) Number higher than 0, set the start moment.
* @param {Number} autofinishWithStepsCount (Optional) Number higher than 0, set it if you want to close automatically your performance event after N steps.
*/
export const startTransaction = async function (
name,
description = null,
startNanoTime = null,
autofinishWithStepsCount = null
) {
if (ObslySDK) {
await ObslySDK.startTransaction(
name,
description,
startNanoTime,
autofinishWithStepsCount
);
} else {
console.log(
"Obsly SDK is not ready... waiting a second for execute startTransaction."
);
return new Promise((resolve, reject) => {
const timeout = setTimeout(() => {
reject(
new Error(
`[Obsly SDK] [startTransaction] Event timed out after waiting for ${initializeTimeout} milliseconds. Have you execute the init function?`
)
);
}, initializeTimeout);
window.addEventListener(readyEvent, () => {
clearTimeout(timeout);
ObslySDK.startTransaction(
name,
description,
startNanoTime,
autofinishWithStepsCount
);
resolve();
});
});
}
};
/**
* Ends a Performance Event.
* @param {String} name Performance event name.
* @param {String} updatedDescription (Optional) Updated description for perfomance event.
*/
export const endTransaction = async function (name, updatedDescription = null) {
if (ObslySDK) {
await ObslySDK.endTransaction(name, updatedDescription);
} else {
console.log(
"Obsly SDK is not ready... waiting a second for execute endTransaction."
);
return new Promise((resolve, reject) => {
const timeout = setTimeout(() => {
reject(
new Error(
`[Obsly SDK] [endTransaction] Event timed out after waiting for ${initializeTimeout} milliseconds. Have you execute the init function?`
)
);
}, initializeTimeout);
window.addEventListener(readyEvent, () => {
clearTimeout(timeout);
ObslySDK.endTransaction(name, updatedDescription);
resolve();
});
});
}
};
/**
* Starts a step for an existent performance event.
* @param {*} name Performance Event Name.
* @param {*} transactionName Step Name.
* @param {*} description (Optional) Step description.
* @param {*} startNanoTime (Optional) Number higher than 0, set the start moment.
*/
export const startStep = async function (
name,
transactionName,
description = null,
startNanoTime = null
) {
if (ObslySDK) {
await ObslySDK.startStep(name, transactionName, description, startNanoTime);
} else {
console.log(
"Obsly SDK is not ready... waiting a second for execute startStep."
);
return new Promise((resolve, reject) => {
const timeout = setTimeout(() => {
reject(
new Error(
`[Obsly SDK] [startStep] Event timed out after waiting for ${initializeTimeout} milliseconds. Have you execute the init function?`
)
);
}, initializeTimeout);
window.addEventListener(readyEvent, () => {
clearTimeout(timeout);
ObslySDK.startStep(name, transactionName, description, startNanoTime);
resolve();
});
});
}
};
/**
* Ends an step from an existent Performance event.
* @param {*} name Performance event name.
* @param {*} transactionName Step Name.
* @param {*} updatedDescription (Optional) An updated description for the step.
*/
export const finishStep = async function (
name,
transactionName,
updatedDescription = null
) {
if (ObslySDK) {
await ObslySDK.finishStep(name, transactionName, updatedDescription);
} else {
console.log(
"Obsly SDK is not ready... waiting a second for execute finishStep."
);
return new Promise((resolve, reject) => {
const timeout = setTimeout(() => {
reject(
new Error(
`[Obsly SDK] [finishStep] Event timed out after waiting for ${initializeTimeout} milliseconds. Have you execute the init function?`
)
);
}, initializeTimeout);
window.addEventListener(readyEvent, () => {
clearTimeout(timeout);
ObslySDK.finishStep(name, transactionName, updatedDescription);
resolve();
});
});
}
};
/**
* Retrieves session information.
*/
export const getSessionInfo = async function () {
if (ObslySDK) {
return await ObslySDK.getSessionInfo();
} else {
throw new Error("[Obsly SDK] [getSessionInfo] ObslySDK is not ready yet.");
}
};
/**
* Returns a Base64 image string of a screenshot.
* @returns str Base64 encoded image.
*/
export const getScreenshot = function () {
return new Promise((resolve, reject) => {
if (ObslySDK) {
return resolve(ObslySDK.getScreenshot());
} else {
console.log(
"[Obsly SDK] [getScreenshot] ObslySDK is not ready yet. Waiting for the SDK to attach..."
);
const handleSdkAttached = () => {
window.removeEventListener("obsly_sdk_attached", handleSdkAttached);
resolve(ObslySDK.getScreenshot());
};
window.addEventListener("obsly_sdk_attached", handleSdkAttached);
}
});
};
/**
* Add a new feedback event with a rating, a message and an image.
* @param {String} rating
* @param {String} message
* @param {String} image
* @returns
*/
export const addFeedback = async function (rating, message, image) {
let date = Date.now();
if (ObslySDK) {
try {
return await ObslySDK.addFeedback(rating, message, image);
} catch (e) {
throw new Error("There was a problem during creating feedback event.");
}
} else {
console.log(
"Obsly SDK is not ready... waiting a second for execute addFeedback."
);
return new Promise((resolve, reject) => {
const timeout = setTimeout(() => {
reject(
new Error(
`[Obsly SDK] [addFeedback] Event timed out after waiting for ${initializeTimeout} milliseconds. Have you execute the init function?`
)
);
}, initializeTimeout);
window.addEventListener(readyEvent, () => {
clearTimeout(timeout);
ObslySDK.addFeedback(rating, message, image, date);
resolve();
});
});
}
};
/**
* Sets the current view.
* @param {string} name view name.
* @returns void
*/
export async function setView(name) {
if (ObslySDK) {
return await ObslySDK.setView(name);
} else {
console.log(
"Obsly SDK is not ready... waiting a second for execute setView."
);
return new Promise((resolve, reject) => {
const timeout = setTimeout(() => {
reject(
new Error(
`[Obsly SDK] [setView] Event timed out after waiting for ${initializeTimeout} milliseconds. Have you execute the init function?`
)
);
}, initializeTimeout);
window.addEventListener(attachedEvent, () => {
clearTimeout(timeout);
ObslySDK.setView(name);
resolve();
});
});
}
}
/**
* Sets the current operation.
* @param {string} name operation name.
* @returns void
*/
export async function setOperation(name) {
if (ObslySDK) {
return await ObslySDK.setOperation(name);
} else {
console.log(
"Obsly SDK is not ready... waiting a second for execute setOperation."
);
return new Promise((resolve, reject) => {
const timeout = setTimeout(() => {
reject(
new Error(
`[Obsly SDK] [setOperation] Event timed out after waiting for ${initializeTimeout} milliseconds. Have you execute the init function?`
)
);
}, initializeTimeout);
window.addEventListener(attachedEvent, () => {
clearTimeout(timeout);
ObslySDK.setOperation(name);
resolve();
});
});
}
}
/**
* Sets the current Functional Block.
* @param {string} name functional block name.
* @returns void
*/
export async function setFunctionalBlock(name) {
if (ObslySDK) {
return await ObslySDK.setFunctionalBlock(name);
} else {
console.log(
"Obsly SDK is not ready... waiting a second for execute setFunctionalBlock."
);
return new Promise((resolve, reject) => {
const timeout = setTimeout(() => {
reject(
new Error(
`[Obsly SDK] [setFunctionalBlock] Event timed out after waiting for ${initializeTimeout} milliseconds. Have you execute the init function?`
)
);
}, initializeTimeout);
window.addEventListener(attachedEvent, () => {
clearTimeout(timeout);
ObslySDK.setFunctionalBlock(name);
resolve();
});
});
}
}
/**
* Increments the value of a counter by 1 with the given key. Optional parameters can provide additional context.
* @param {string} key The key or name of the metric to increment.
* @param {string} fbl (Optional) The functional block label where the increment is being executed.
* @param {string} operation (Optional): The operation being performed.
* @param {string} view (Optional) The view where the increment is being executed.
* @param {string} state (Optional) The state of the operation, e.g., "OK" or "KO".
* @returns
*/
export function incCounter(key, fbl, operation, view, state) {
let date = Date.now();
if (ObslySDK) {
return ObslySDK.incCounter(key, fbl, operation, view, state);
} else {
console.log(
"Obsly SDK is not ready... waiting a second for execute incCounter."
);
return new Promise((resolve, reject) => {
const timeout = setTimeout(() => {
reject(
new Error(
`[Obsly SDK] [incCounter] Event timed out after waiting for ${initializeTimeout} milliseconds. Have you execute the init function?`
)
);
}, initializeTimeout);
window.addEventListener(readyEvent, () => {
clearTimeout(timeout);
ObslySDK.incCounter(key, fbl, operation, view, state, date);
resolve();
});
});
}
}
/**
* Sets the value of the metric with the given key and value. Optional parameters provide additional context.
* @param {string} key The key or name of the metric to increment.
* @param {number} value The new value to set for the metric.
* @param {string} fbl (Optional) The functional block label where the increment is being executed.
* @param {string} operation (Optional): The operation being performed.
* @param {string} view (Optional) The view where the increment is being executed.
* @param {string} state The state of the operation, e.g., "OK" or "KO".
* @returns
*/
export function setGauge(key, value, fbl, operation, view, state) {
let date = Date.now();
if (ObslySDK) {
return ObslySDK.setGauge(key, value, fbl, operation, view, state);
} else {
console.log(
"Obsly SDK is not ready... waiting a second for execute setGauge."
);
return new Promise((resolve, reject) => {
const timeout = setTimeout(() => {
reject(
new Error(
`[Obsly SDK] [setGauge] Event timed out after waiting for ${initializeTimeout} milliseconds. Have you execute the init function?`
)
);
}, initializeTimeout);
window.addEventListener(readyEvent, () => {
clearTimeout(timeout);
ObslySDK.setGauge(key, value, fbl, operation, view, state, date);
resolve();
});
});
}
}
/**
* Starts a HistogramTimer with the specified key. Optional parameters provide additional context.
* @param {string} key The key or name of the metric to increment.
* @param {string} fbl (Optional) The functional block label where the increment is being executed.
* @param {string} operation (Optional): The operation being performed.
* @param {string} view (Optional) The view where the increment is being executed.
* @returns
*/
export function startHistogramTimer(key, fbl, operation, view) {
let date = Date.now();
if (ObslySDK) {
return ObslySDK.startHistogramTimer(key, fbl, operation, view);
} else {
console.log(
"Obsly SDK is not ready... waiting a second for execute startHistogramTimer."
);
return new Promise((resolve, reject) => {
const timeout = setTimeout(() => {
reject(
new Error(
`[Obsly SDK] [startHistogramTimer] Event timed out after waiting for ${initializeTimeout} milliseconds. Have you execute the init function?`
)
);
}, initializeTimeout);
window.addEventListener(readyEvent, () => {
clearTimeout(timeout);
ObslySDK.startHistogramTimer(key, fbl, operation, view, date);
resolve();
});
});
}
}
/**
* Ends a HistogramTimer with the specified key. Optional parameters provide additional context.
* @param {string} key The key or name of the metric to increment.
* @param {string} fbl (Optional) The functional block label where the increment is being executed.
* @param {string} operation (Optional): The operation being performed.
* @param {string} view (Optional) The view where the increment is being executed.
* @param {string} state The state of the operation, e.g., "OK" or "KO".
* @returns
*/
export function endHistogramTimer(key, fbl, operation, view, state) {
let date = Date.now();
if (ObslySDK) {
return ObslySDK.endHistogramTimer(key, fbl, operation, view, state);
} else {
console.log(
"Obsly SDK is not ready... waiting a second for execute endHistogramTimer."
);
return new Promise((resolve, reject) => {
const timeout = setTimeout(() => {
reject(
new Error(
`[Obsly SDK] [endHistogramTimer] Event timed out after waiting for ${initializeTimeout} milliseconds. Have you execute the init function?`
)
);
}, initializeTimeout);
window.addEventListener(readyEvent, () => {
clearTimeout(timeout);
ObslySDK.endHistogramTimer(key, fbl, operation, view, state, date);
resolve();
});
});
}
}
export default {
init,
setUserID,
setPersonID,
setPassportID,
setContractID,
setAppName,
setAppVersion,
setLogLevel,
startSession,
closeCurrentSession,
createNewSession,
pauseTracker,
resumeTracker,
setRequestsBlacklist,
addTag,
addObslyTag,
addObslyScreenshot,
addScreenshot,
startTransaction,
endTransaction,
startStep,
finishStep,
getSessionInfo,
getScreenshot,
addFeedback,
setView,
setOperation,
setFunctionalBlock,
incCounter,
setGauge,
startHistogramTimer,
endHistogramTimer,
};