Skip to content

Instantly share code, notes, and snippets.

@lennardtastic
Last active July 23, 2018 11:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lennardtastic/c2d4d3dc615b56210d4fb391a6382973 to your computer and use it in GitHub Desktop.
Save lennardtastic/c2d4d3dc615b56210d4fb391a6382973 to your computer and use it in GitHub Desktop.
/**The MIT License (MIT)
Copyright (c) 2018 Lennard Schoemaker
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
**/
/**
* Function for updating the Custom Variables of Usabilla for websites
* Function accepts 2 parameters and can be ran multiple times to add multiple Custom Variables
* Requires the an object called "usblCustom" to be present in the window context
*
* @param {String} nameVariable
* @param {Value} valueVariable
* @returns
*/
function usblUpdateCustom(nameVariable, valueVariable) {
// Check if Usabilla is active on the page otherwise return
if (typeof window.usabilla_live === 'function') {
// Object for holding Usabilla Custom Variables Data, check for customObj in case legacy approach is used
if (typeof window.usblCustom === 'object') {
// Check if the given variable name is valid
if (typeof nameVariable === 'string') {
usblCustom[nameVariable] = valueVariable;
} else {
var nameVariableStringed = nameVariable.toString();
usblCustom[nameVariableStringed] = valueVariable;
}
//Send custom variables by assigning the usblCustom to the 'custom' object in Usabilla
window.usabilla_live('data', {
custom: usblCustom
});
} else {
console.log('No Custom Variable object available, please make sure to add the usblCustom object');
}
} else {
console.log('no Usabilla detected, no variables to push');
}
}
// IMPORTANT
// Always make sure that you have load the installation snippet Usabilla and FullStory script before using one the methods down below.
// METHOD 1
// Pushing the FullStory data at any time
// Create a object to hold the Usabilla Custom Variables and check for possible existing objects.
var usblCustom = window.customObj || {};
// Create a variable to hold the values of FullStory and push them to Usabilla
var fullstoryUrl = FS.getCurrentSessionURL();
usblUpdateCustom('fsUrl', fullstoryUrl);
var fullstorySession = FS.getCurrentSession();
usblUpdateCustom('fsSession', fullstorySession);
// METHOD 2
// Pushing the FullStory data on _fs_ready
// Use the code down below if you want want to add it to you Full Story Ready fucntion
var _fs_ready = function () {
var usblCustom = window.customObj || {};
var fullstoryUrl = window.FS.getCurrentSessionURL();
usblUpdateCustom('fsUrl', fullstoryUrl);
var fullstorySession = window.FS.getCurrentSession();
usblUpdateCustom('fsSession', fullstorySession);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment