Using JavaScript to target surveys
When using a Code Trigger to target a Digital survey using JavaScript, you use the onsite API to trigger the survey you want based on its FormID
. Exactly how you code the targeting on your site is specific to your situation, and we cannot cover all of the possibilities. Instead, this section shows how to use the onsite API to trigger the survey.
The code on your website must do the following:
- Check the Digital embed code is loaded.
- On page display, load the form:
- if the form is available then display the button or link
- if the form is not available then do not display the button or link
- Show the Form when button or link is clicked.
The form can be loaded at any time after the web page has completed loading or immediately when it is triggered by your code. Loading the form in advance ensures there is no delay when the form is triggered to be displayed. If the form is not loaded in advance, the Form iFrame
displays a spinner icon while the form is loading. The following example builds up the parts described above using HTML and JavaScript. Before your start you need to have designed a survey and note its FormID
.
Check Code Loaded First
Any site where you want to collect feedback using Digital requires the Digital embed.js
tag deployed. The embed.js
tag is bootstrap code and it subsequently loads another JavaScript file with the name generic_xxxxxx.js
where xxxxxx is a random number generated by the system each time your script is updated. The generic_xxxxxx.js
script contains all the core functions and APIs of the Digital system.
The functions defined all begin with KAMPYLE_
. The name KAMPYLE
is left over from the original name of the company before they were acquired by Medallia.
There are instances when you want to know that the scripts are loaded and available. This can be achieved by simply checking whether one of the main functions is available.
//if the KAMPYLE_ONSITE_SDK function is available
if (window.KAMPYLE_ONSITE_SDK) {
// add your own code or functions here
myCustomCode();
} else {
// add a listener for the event neb_OnsiteLoaded which will occur once all the
// Digital code has been executed by the browser
window.addEventListener('neb_OnsiteLoaded', myCustomCode);
}
Before you load the form, your code should check that the Digital embed code has loaded by:
- checking the existence of the
KAMPYLE_ONSITE_SDK
function - listening for the
neb_OnsiteLoaded
event, which fires on successful loading of theKAMPYLE_ONSITE_SDK
API components.
Check code loaded example:
<script>if (window.KAMPYLE_ONSITE_SDK) {
//Check Digital code has been loaded
onsiteLoaded();
//your custom function } else {
// On the neb_OnsiteLoaded event call onsiteLoaded function
window.addEventListener('neb_OnsiteLoaded', onsiteLoaded);
}
</script>
Load the Form
When the check for Digital embed code is successful, you can use the loadForm
API:
KAMPYLE_ONSITE_SDK.loadForm(FormId)
return values: true if the form has been published and can be loaded
false if form cannot be loaded
The FormId
is displayed in the Form page of the Digital Command Center.
In the following JavaScript example, an event listener is added for the neb_OnsiteLoaded
event. The event listener then calls the onsiteLoaded
function to load the form using KAMPYLE_ONSITE_SDK.loadForm
and also determine if the form can be loaded before displaying form ID 281.
loadForm Example:
<script>if (window.KAMPYLE_ONSITE_SDK) {
//Check Digital code has been loaded
onsiteLoaded();
//your custom function
} else {
// On the neb_OnsiteLoaded event call onsiteLoaded function
window.addEventListener('neb_OnsiteLoaded', onsiteLoaded);
}
function onsiteLoaded(){
// load the form and store status (true/false) in neb_status
var neb_status = KAMPYLE_ONSITE_SDK.loadForm(281);
if (neb_status === true){
// if form is loaded then decide what to do
// your code goes here
}}
</script>
Display Form
Once the Digital code is loaded and the form can be loaded (see Check Code Loaded First and Load the Form), call the showForm
API to display the form using the following syntax:
KAMPYLE_ONSITE_SDK.showForm(FormId)
return values: true if form is displayed
false if form cannot be displayed
The FormId
is displayed in the Form page of the Digital Command Center.
In the following HTML and JavaScript example, clicking the "Give Feedback" button calls the KAMPYLE_ONSITE_SDK.showForm
function to display form ID 281. The HTML button has the display
attribute set to none
so it cannot be seen until the check that KAMPYLE_ONSITE_SDK
is loaded succeeds. The onsiteLoaded
function checks if the form with ID 281 can be loaded and stores the result in the neb_status
variable, which is used to set the display
attribute to show the HTML button if FormID
281 is available.
showForm Example:
<button id="mdFormButton" class="display: none;" onclick="KAMPYLE_ONSITE_SDK.showForm(281);">Give Feedback</button>
<script>if (window.KAMPYLE_ONSITE_SDK) {
//If Digital code has been loaded
onsiteLoaded(); //your custom function
} else {
// On the neb_OnsiteLoaded event, call onsiteLoaded function
window.addEventListener('neb_OnsiteLoaded', onsiteLoaded);
}
function onsiteLoaded(){
// load the form and store status (true/false) in neb_status
var neb_status = KAMPYLE_ONSITE_SDK.loadForm(281);
// If form is loaded
if (neb_status === true){
// set CSS attribute display to inherit so button can be seen
document.getElementById("mdFormButton").style.display = "inherit"
}
}
</script>
This is just one example of using the Digital embed code to control how Digital is triggered.
onClick
event to trigger the survey. Using a different technique, such as an href attribute with embedded JavaScript, could result in some browsers not triggering the survey correctly.