For website or web application
This is a technical documentation of the APIFor more user-case oriented examples see Website widget: API examples.
Installation
window.onUsersnapLoad = function(api) {
api.init();
}
var script = document.createElement('script');
script.defer = 1;
script.src = 'https://widget.usersnap.com/global/load/<GLOBAL_API_KEY>?onload=onUsersnapLoad';
document.getElementsByTagName('head')[0].appendChild(script);API object
interface GlobalApi {
init: (options: InitOptions) => Promise<void>,
destroy: () => Promise<void>,
logEvent: (eventName: string) => Promise<void>,
on: (eventName: string, callback: (event: any) => void) => void,
off: (eventName: string, callback: (event: any) => void) => void,
}init()
api.init({
custom: {
applicationLanguage : "de",
},
user: {
userId: "123",
email: "[email protected]",
},
locale: 'de',
});The init function initializes the global api. It must be called before calling any other api method.
Params
optionsoptions.custom: Record<string, any>β An object holding custom data. This data will be passed along with every feedbackoptions.userβ An object holding information about the current useroptions.user.email: stringβ Will be pre-filled in the widget and attached to every feedbackoptions.user.userId: stringβ Can be used for determining whether to show a widget or notoptions.locale: stringβ Language in which widgets should be displayedoptions.useSystemFonts: booleanβ Can be used to disable loading external fonts. Defaults tofalseoptions.useLocalStorage: booleanβ Can be used to disable usage of localStorage. Defaults totrueoptions.nativeScreenshot: booleanβ Can be used to enable the usage of the native screenshot feature. Defaults tofalseoptions.collectGeoLocation: 'all' | 'none'β can be used to disable the collection of IP and geo location. Defaults to'all'
Returns Promise<void>
Promise<void>destroy()
api.destroy()Destroys the global api and removes all widgets from the DOM of the webpage.
After calling destroy() it is necessary to call init() before calling any other method.
Returns Promise<void>
Promise<void>logEvent()
api.logEvent('my-event') // will open a widget if one was configured for this eventCan be used to trigger widgets by specific event names.
Params
eventName: stringβ the name of the event
Returns Promise<void>
Promise<void>on()
function handleSubmit() {
console.log('Widget was submitted')
}
api.on('submit', handleSubmit)Registers an event handler for widget events.
For a detailed list of available events and how they can be used see section Widget Events
Params
eventName: stringβ the name of the widget event you want to listen tocallback: (event: WidgetEvent) => voidβ callback to handle the event. The structure of the received object depends on the event.
Returns void
voidoff()
api.off('submit', handleSubmit)Removes an event handler
Params
eventName: stringβ the name of the widget event want to remove the listener fromcallback: (event: WidgetEvent) => voidβ same callback that was used for registering the event
Returns void
voidWidget Events
openβ A widget was opened.closeβ A widget was closedbeforeSubmitβ Before form values are submittedsubmitβ After form values were submitted
open event
open eventtype ValueKey = 'assignee' | 'custom' | 'labels' | 'visitor'
interface OpenEvent {
apiKey: string,
api: {
setValue: (key: ValueKey, value: any) => void,
},
}apiKeyβ API key of the widget that was openedapi.setValueβ method to set form values
Example
function handleWidgetOpened(event) {
// You can check whether a specific widget was called
if (event.apiKey !== '<PROJECT_API_KEY>') return;
// Set Assignee. A User with this email has to exist in your Usersnap account
event.api.setValue('assignee', '[email protected]')
// Set custom data
event.api.setValue('custom', { applicationLanguage : 'de' })
// Set labels
event.api.setValue('labels', ['bug', 'critical'])
// Set email
event.api.setValue('visitor', '[email protected]')
}
api.on('open', handleWidgetOpened)close event
close eventinterface CloseEvent {
apiKey: string,
isCancel: boolean,
}apiKeyβ API key of the widget that was closedisCancelβ istruein case the widget was closed before submitting a feedback item
Example
function handleWidgetClosed(event) {
if (event.isCancel) {
// do something
}
}
api.on('close', handleWidgetClosed)beforeSubmit event
beforeSubmit eventtype ValueKey = 'assignee' | 'custom' | 'labels' | 'visitor'
interface BeforeSubmitEvent {
apiKey: string,
api: {
setValue (key: ValueKey, value: any) => void,
}
values: {
assignee?: string,
custom?: any,
labels?: string,
visitor: string,
}
}apiKeyβ API key of the widget that will be submittedapi.setValueβ method to set form values. Allows changing values before they are submitted.valuesβ values that will be submitted
Example
function handleBeforeSubmit(event) {
var labels = event.values.labels
if (labels && labels.includes('bug')) {
event.api.setValue('assignee', '[email protected]')
}
}
api.on('beforeSubmit', handleBeforeSubmit)submit event
submit eventinterface CloseEvent {
apiKey: string,
}apiKeyβ API key of the widget that was submitted
Example
function handleSubmit() {
// do something
}
api.on('submit', handleSubmit)