roshanoracle
2/26/2018 - 3:01 PM

Optimizely X : BlueKai (Sync)

Optimizely X : BlueKai (Sync)

/*
##################################################################
### ORACLE BLUEKAI : OPTIMIZELY X : JSON Return handler : Sync ###
##################################################################

Author : roshan.gonsalkorale@oracle.com

Notes:

- Will format results from BlueKai Site Optimisation Tag (ret=js) and surface in BlueKai javascript object for use in Optimizely Audiences
- For debugging, add 'bk_optimizely_logger=true' as a query-string parameter in the URL
- BlueKai Campaign IDs will be available in 'window.bk_optimizely_integration.data.optimizely_formatted.campaigns'
- BlueKai Category IDs will be available in 'window.bk_optimizely_integration.data.optimizely_formatted.categories'

Requirements:

- BlueKai JSON Return tag must be implemented synchronously before this script loads: ("http://tags.bluekai.com/site/XXXXX?ret=js&limit=1")
- This code can be cut and pasted in "Settings > Javascript" in Optimizely X

*/

// CONFIG : EDIT THIS PART

// Create object to store functions
window.bk_optimizely_integration = {};
window.bk_optimizely_integration.functions = {};
window.bk_optimizely_integration.data = {};
window.bk_optimizely_integration.data.unformatted = {};

/* 
##########################################################################################
DO NOT EDIT BELOW THIS LINE
##########################################################################################
*/

// FUNCTION : Logger
bk_optimizely_integration.functions.logger = function(message,attribute_object) {

    if(document.location.href.indexOf('bk_optimizely_logger=true') > -1){
        
        //session cookie
        document.cookie = "bk_optimizely_logger=" + "true" +
        ";path=/;domain=" + document.domain + ";expires=";
    }

    if (document.cookie.indexOf('bk_optimizely_logger=true') > -1) {
        
        if(typeof attribute_object === "undefined"){
            console.log(message);
        } else {
            for (varName in attribute_object){
                console.log(message + varName + "=" + attribute_object[varName]);
            }
        }
    }

};

// FUNCTION : Parse BlueKai data and send to OPTIMIZELY
bk_optimizely_integration.functions.parseBkResults = function() {


    // Parse BlueKai Campaign Results
    window.bk_optimizely_integration.data.unformatted.bluekai_category_ids = [];
    window.bk_optimizely_integration.data.unformatted.bluekai_campaign_ids = [];

    if (typeof(bk_results) != "undefined") {

        if (typeof(bk_results.campaigns[0]) != "undefined") {
            
            bk_optimizely_integration.functions.logger("BLUEKAI OPTIMIZELY : 'bk_results' object found");

            for (var i = 0; i < bk_results.campaigns.length; i++) {
                
                window.bk_optimizely_integration.data.unformatted.bluekai_campaign_ids.push(bk_results.campaigns[i].campaign);                

                for (var j = 0; j < bk_results.campaigns[i].categories.length; j++) {                    

                    if (typeof(bk_results.campaigns[i].categories[j].categoryID) != "undefined") {
                        
                        window.bk_optimizely_integration.data.unformatted.bluekai_category_ids.push(bk_results.campaigns[i].categories[j].categoryID);

                    }
                }

            }

            bk_optimizely_integration.functions.formatOptimizely(); // Send data to Optimizely            

        } else {
            bk_optimizely_integration.functions.logger("BLUEKAI OPTIMIZELY : No campaigns object");
        }

    } else {
        bk_optimizely_integration.functions.logger("BLUEKAI OPTIMIZELY : No 'bk_results' object");
    }

};

bk_optimizely_integration.functions.formatOptimizely = function() {
        
    // Create Attributes object
    window.bk_optimizely_integration.data.optimizely_formatted = {};
    window.bk_optimizely_integration.data.optimizely_formatted.campaigns = {};
    window.bk_optimizely_integration.data.optimizely_formatted.categories = {};
 
    // Shorten var names
    var bk_campaigns_ids = window.bk_optimizely_integration.data.unformatted.bluekai_campaign_ids;
    var bk_category_ids = window.bk_optimizely_integration.data.unformatted.bluekai_category_ids;
    
    if(bk_campaigns_ids.length > bk_category_ids.length){
 
        var bk_array_length = bk_campaigns_ids.length;
 
    } else {var bk_array_length = bk_category_ids.length;}
    
    for (var i = 0; i < bk_array_length; i++) {
        
        if(bk_campaigns_ids[i]){
            window.bk_optimizely_integration.data.optimizely_formatted.campaigns[bk_campaigns_ids[i]] = "true";
            bk_optimizely_integration.functions.logger("BLUEKAI OPTIMIZELY : Setting Campaign : " + bk_campaigns_ids[i] + " : 'true'");
        }
        if(bk_category_ids[i]){
            window.bk_optimizely_integration.data.optimizely_formatted.categories[bk_category_ids[i]] = "true";
            bk_optimizely_integration.functions.logger("BLUEKAI OPTIMIZELY : Setting Category : " + bk_category_ids[i] + " : 'true'");
        }
    };   
 
    
    bk_optimizely_integration.functions.logger("BLUEKAI OPTIMIZELY : See 'window.bk_optimizely_integration.data.optimizely_formatted.campaigns' for available campaigns");    
    bk_optimizely_integration.functions.logger("BLUEKAI OPTIMIZELY : See 'window.bk_optimizely_integration.data.optimizely_formatted.categories' for available categories");    
 
};

// RUN CODE
bk_optimizely_integration.functions.logger("BLUEKAI OPTIMIZELY : Parsing 'bk_results'");
bk_optimizely_integration.functions.parseBkResults(); // Parse results