- 26 Nov 2024
- 1 Minute to read
- DarkLight
Call Back Function
- Updated on 26 Nov 2024
- 1 Minute to read
- DarkLight
Summary
This article provides instructions for implementing the CallBack function. Using a Callback Function enables site owners to create a custom function within their LeadiD campaign javascript snippet, returning the LeadiD to this function rather than the standard field defined by VMS.
Using a CallBack Function
The Create Callback Function allows site owners to write a function and name that function within their campaign javascript snippet.
When the LeadiD is returned, it is delivered to the custom function rather than the LeadiD defined hidden input field "leadid_token". This gives the site owner the ability to control the field into which the LeadiD is placed.
Instructions
The campaign code snippet must be updated to include "?callback=" after the .js within the src. A sample code snippet would look like this:
<script id="LeadiDscript" type="text/javascript">
(function() {
var s = document.createElement('script');
s.id = 'LeadiDscript_campaign';
s.type = 'text/javascript';
s.async = true;
s.src = '//create.lidstatic.com/campaign/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx.js?snippet_version=2&callback=myCallbackFunction';
var LeadiDscript = document.getElementById('LeadiDscript');
LeadiDscript.parentNode.insertBefore(s, LeadiDscript);
})();
</script>
With the above campaign code snippet the LeadiD token will be returned to the function 'myCallbackFunction' where the site owner can then control which field the LeadiD is placed in.
The script tag containing the callback function needs to be placed BEFORE the LeadiD code snippet. This ensures that the callback function referenced in the query string parameter of the code snippet actually exists at the time it's executed.
Example 1: Defining a Callback Function
Sample JavaScript that defines the function 'myCallbackFunction':
<script type="text/javascript">
function myCallbackFunction(token){
alert('This is my custom callback function which can accept the LeadiD token '+ token + ' and do something with it now.');}
</script>
Example 2: Place a Token in a Custom Field
Sample JavaScript that uses the function 'myCallbackFunction' to place the LeadiD token in a field 'myCustomField' (assumes site operator has a form field with ID attribute myCustomField):
<script type="text/javascript">
function myCallbackFunction(token){
var field = document.getElementById('myCustomField');
if (field) {
field.setAttribute('value',token);}
}
</script>
Once implemented, follow the steps in the Campaign Implementation Self Test to verify the implementation is working as expected.