Rich-text Reply

Trying to pass dynamic value in custom attribute

kirtik 2w ago

Trying to pass dynamic value in custom attribute

I am trying to pass dynamic value in custom attribute. Find the below code of that.

I got the console value correctly  but I can't see that value in under segment. If I pass static value in attribute then it's reflect immediately. Not sure why this might be happening. If someone would be able to shed some light on the situation I would be grateful!

 

window.optimizely.push(['addToSegment', 'GeoLocation', 'Homepage']);

function getCookie(cname) {
var name = cname + "=";
var ca = document.cookie.split(';');
for (var i = 0; i < ca.length; i++) {
var c = ca[i];
while (c.charAt(0) == ' ') {
c = c.substring(1);
}
if (c.indexOf(name) == 0) {
return c.substring(name.length, c.length);
}
}
return "";
}
var userLocation = getCookie("ak_location");

 

var regexLocation = /NL|SE|NO|DK|BE|FI|FR|UK|IT|ES|DE|AU|CH|PT|US|IN/g;

var foundLocation = userLocation.match(regexLocation);

console.log(foundLocation);

window["optimizely"].push({
"type": "user",
"attributes": {
GeoLocation: foundLocation
}
});

Level 2

Michal 2w ago
 

Re: Trying to pass dynamic value in custom attribute

Hi there,

 

I'd understand if the issue is about the 'GeoLocation' variable (i.e. the attribute name) rather than the attribute value specifically. This is because of how the JS interpreter handles this situation - it converts the key to a string so the value you're passing to Optimizely would essentially be: "GeoLocation: <whateverfoundLocation evaluates to>. 

 

You can try the following method to see if that helps:

 

[...]
var attributes = {};
attributes[GeoLocation] = foundLocation;
window["optimizely"].push({
      "type": "user",
      "attributes": attributes
});

Alternatively, you can use the following syntax but please note for the the ES6 support is needed (not all browsers have it):

 

[...]
"attributes": {
  [GeoLocation]: foundLocation
}
[...]

I hope this helps,

Michal

Optimizely
kirtik a week ago
 

Re: Trying to pass dynamic value in custom attribute

Thanks for a reply Michal.

As per your suggestion, I have tried but it's didn't work Smiley Sad  

Level 2
Highlighted
Michal a week ago
 

Re: Trying to pass dynamic value in custom attribute

Hm, can you see any attribute being set on the live page or can you see any errors being thrown in the browser console? Can you confirm that an attribute with an API name matching the (dynamic) key exists in the Optimizely project?

At this point, it will probably be the best if you submit a support ticket with us (for instructions on how to do it, have a look here: https://help.optimizely.com/Account_Settings/File_online_tickets_for_support) so I or one of my colleagues can have a look at your specific project & use case.

Best regards,
Optimizely
kirtik Monday
 

Re: Trying to pass dynamic value in custom attribute

Hey Michal,  I not getting any error in browser console, even I got proper value in console, but problem is when I am passing those value in attribute.  I found some article, I have attached that screen shot as well. Please have a look (https://developers.optimizely.com/x/solutions/javascript/reference/index.html#function_setuser)

 

there is mentioned custom attribute must be a string only. I think we cant pass dynamic value in custom attribute.

 

 

 

 

 

 

 

Capture.JPG
Level 2

Re: Trying to pass dynamic value in custom attribute

Using the code above, I see the value being set in localStorage (highlighted section, near the end)

Capture.PNG

--Jason Dahlin
Analytics and Testing Guru Smiley Happy


Experimentation Hero