I created a script which enables me to set a Klipfolio variable by monitoring the URL query string. My script works, but I'm struggling with getting it to time correctly. If I set the script to run when the DOM has loaded, Klipfolio is still running its own scripts which seem to override my changes. If I delay the script by 5 seconds, it correctly changes the variable I want to change, but of course using a static time as a delay is not a viable option.
Here's my code (it's embed inside a "HTML Placeholder" Klip):
var getUrlParameter = function getUrlParameter(sParam) {
var sPageURL = decodeURIComponent(window.location.search.substring(1)),
sURLVariables = sPageURL.split('&'),
sParameterName,
i;
for (i = 0; i < sURLVariables.length; i++) {
sParameterName = sURLVariables[i].split('=');
if (sParameterName[0] === sParam) {
return sParameterName[1] === undefined ? true : sParameterName[1];
}
}
};
$( document ).ready( function() {
var PROD = getUrlParameter('prod');
var BETA = getUrlParameter('beta');
var changeKlipfolioVariable = function changeKlipfolioVariable () {
if (PROD == true) {
$("select").val('DataWarehousePROD').change();
console.log( "Switched variable to PROD" );
} else if (BETA == true) {
$("select").val('DataWarehouseBETA').change();
console.log( "Switched variable to BETA" );
}
}
changeKlipfolioVariable();
//setTimeout(changeKlipfolioVariable, 5000)
console.log( "Variables - PROD: " + PROD + " BETA: " + BETA );
});
PasteBin URL for better readability: https://pastebin.com/f79pvmxS
My questions:
Is there a help page / tutorial for using JavaScript with Klipfolio?
Is there an event or something like that inside Klipfolio which I could use to time my script correctly?
Are user created scripts ran only once upon opening the dashboard or is there a possibility they run again after data gets refreshed etc.?
Regards,
Carlos Kynäslahti