Populate UDO variables with values from an array created with AudienceStream data

Silver Contributor
Silver Contributor

Hi!

 

I'm trying to populate new UDO data sources from an array I've created and ordered (high to low) from AudienceStream variables. I can verify that the data populated the array and ordered it correctly by dumping it the console...I've created new UDO data sources (FavCat1, FavCat2, FavCat3, FavCat4)...but I cannot get them to show in the utag_data layer no matter what I try. I'm way rusty on Javascript - anyone have any ideas what silly thing I forgot??

 

Thank you!  

 

Monique

 

function sortObject(obj) {
    var FavCat = [];
    for (var prop in obj) {
        if (obj.hasOwnProperty(prop)) {
            FavCat.push({
                'key': prop,
                'value': obj[prop]
            });
        }
    }
    FavCat.sort(function(a, b) { return b.value - a.value; });
    return FavCat; 
}

var list = {"1000": utag_data["va.metric_sets.5155.Riding Apparel & Clothing"], "2000": utag_data["va.metric_sets.5155.Riding Boots and Chaps"], "3000": utag_data["va.metric_sets.5155.Horse Tack"], "4000": utag_data["va.metric_sets.5155.Horse Blankets"], "5000": utag_data["va.metric_sets.5155.Horse Care"], "6000": utag_data["va.metric_sets.5155.Stable Supplies"], "7000": utag_data["va.metric_sets.5155.Gifts"], "8000": utag_data["va.metric_sets.5155.CLOSEOUTS"], "9000": utag_data["va.metric_sets.5155.Sale"]};
var FavCat = sortObject(list);
console.log(FavCat);

b.FavCat1 = FavCat[0];
b.FavCat2 = FavCat[1];
b.FavCat3 = FavCat[2];
b.FavCat4 = FavCat[3];
5 REPLIES 5

Populate UDO variables with values from an array created with AudienceStream data

Silver Contributor
Silver Contributor

Oh! Nevermind, it worked! Don't know if it was cached or something, but I just went to check again, and they're all there!

 

Object {site_region: "usa", site_currency: "usd", page_name: "Home", page_type: "Home", pageType: "other"…}
FavCat1: Object
key: "5000"
value: 78
__proto__: Object
FavCat2: Object
key: "1000"
value: 47
__proto__: Object
FavCat3: Object
key: "3000"
value: 41
__proto__: Object
FavCat4: Object
key: "2000"
value: 19
__proto__: Object
_cbrand: Array[0]

Yay! Thanks for having a forum where questions like this can be asked!  =)

Populate UDO variables with values from an array created with AudienceStream data

Employee Emeritus

No, no thank you @monique_trulson for stopping on by and asking. Let us know if you have any other questions. We're here to help. 

Remember to give me a kudo if you like my post! Accepting my post as a solution is even better! Also remember that search is your friend.

Populate UDO variables with values from an array created with AudienceStream data

Silver Contributor
Silver Contributor

Actually, I may have spoken too soon - I have an issue now where for some reason the values I see in the console assigned to the array are populating the variables incorrectly - but not always!

 

Updated code:

 

function sortObject(obj) {
    var arr = [];
    for (var prop in obj) {
        if (obj.hasOwnProperty(prop)) {
            arr.push({
                'key': prop,
                'value': obj[prop]
            });
        }
    }
    arr.sort(function(a, b) { return b.value - a.value; });
    //arr.sort(function(a, b) { a.value.toLowerCase().localeCompare(b.value.toLowerCase()); }); //use this to sort as strings
    return arr; // returns array
}
var list = {"1000": utag_data["va.metric_sets.5155.Riding Apparel & Clothing"], "2000": utag_data["va.metric_sets.5155.Riding Boots and Chaps"], "3000": utag_data["va.metric_sets.5155.Horse Tack"], "4000": utag_data["va.metric_sets.5155.Horse Blankets"], "5000": utag_data["va.metric_sets.5155.Horse Care"], "6000": utag_data["va.metric_sets.5155.Stable Supplies"], "7000": utag_data["va.metric_sets.5155.Gifts"], "8000": utag_data["va.metric_sets.5155.CLOSEOUTS"], "9000": utag_data["va.metric_sets.5155.Sale"]};
console.log(list);
var arr = sortObject(list);
console.log(arr);

b.FavCat1 = arr[0].key;
b.FavCat2 = arr[1].key;
b.FavCat3 = arr[2].key;
b.FavCat4 = arr[3].key;

I dump the list to the console to check it- looks good:

Object {1000: 51, 2000: 19, 3000: 41, 4000: 10, 5000: 78, 6000: 10, 7000: 2, 8000: 7, 9000: 13}

I dump the array to the console to check it - looks good:

[Object, Object, Object, Object, Object, Object, Object, Object, Object]
0: Object
key: "5000"
value: 78
__proto__: Object
1: Object
key: "1000"
value: 51
__proto__: Object
2: Object
key: "3000"
value: 41
__proto__: Object
3: Object
key: "2000"
value: 19
__proto__: Object
4: Object
5: Object
6: Object
7: Object
8: Object
length: 9

Then I check what got assigned in utag_data to FavCat1, 2, 3, 4 - 

utag_data
Object {site_region: "usa", site_currency: "usd", page_name: "Home", page_type: "Home", pageType: "other"…}
FavCat1: "1000"
FavCat2: "1000"
FavCat3: "1000"
FavCat4: "1000"
_cbrand: Array[0]
_ccat: Array[0]
_ccat2: Array[0]
_ccity: ""....

Any ideas?

 

Thanks!

Monique

 

Populate UDO variables with values from an array created with AudienceStream data

Tealium Employee

Hi @monique_trulson

 

So the issue isn't actually with you extension. That is doing excatly what you want it to do.

It is actually the following extensions that are causing the isuue.

 

For example you have

 

 

if (b.FavCat1.key = "1000")

This is not actually doing a check, it is assigning the value "1000" to b.FavCat1.key. Which is happening to each of the b.FavCat# objects.

 

You will need to change each of the if statments to be:

 

if (b.FavCat1.key == "1000")

 

Adrian

 

Ask me anything Tealium related or TypeScript/JavaScript, or NodeJS.
Please remember to mark solutions as accepted for future searchers.

Populate UDO variables with values from an array created with AudienceStream data

Silver Contributor
Silver Contributor

THANK YOU!!! I knew there was something silly I was completely missing...you guys rock!  :)

Public