0

Doing some work with Omniture analytics and want to be able to set some properties via JSON.

Omniture calls look something like this:

s.linkTrackVars = 'eVar37';
s.eVar37='foo';
s.tl(true, 'o');

I want to write a generic function to take a JSON object and convert it into those variables. AFAIK Omniture doesn't allow you to simply pass it JSON, so this the only method I can think of.

Here's the example of the JSON structure I want to use:

var omniture = {

    "evars":
    [
        {
            "key": "37",
            "value": "foo"
        },

        {
            "key": "32",
            "value": "bar"
        }
    ]

}

My function to work with this JSON looks like this:

for (i in omniture.evars) {
    var evar = omniture.evars[i];
    window[s.eVar + evar.key] = evar.value;
}
alert(s.eVar37); // alerts "undefined"

If I do this, it works:

for (i in omniture.evars) {
    var evar = omniture.evars[i];
    window['eVar' + evar.key] = evar.value;
}
alert(eVar37); // alerts "foo"

It seems that I can't set a variable variable as a property of an object (eg s.eVar37 as opposed to evar37). Can anybody think of a nice way of setting these variables automatically?

thanks, Matt

1
  • There is no JSON in your example, it's a plain JS object. Commented May 27, 2011 at 11:41

2 Answers 2

3

Sure you can. Just set it on s directly:

s['eVar' + evar.key] = evar.value;
Sign up to request clarification or add additional context in comments.

1 Comment

Oh... doh! As simple as that. Thank you.
1

IIUC you want s['eVar' + evar.key] = evar.value;.

edit: alternative

It would be simpler though to just have

var omniture =
{
    evars:
    {
        "37": "foo",
        "32": "bar"
    }
}

copyFromInto(omniture.evars, 'eVar', s);

function copyFromInto(o1, prefix, o2)
{
    for (var i in o1)
    {
         o2[prefix + i] = o1[i];
    }
}

alert(s.eVar37); // alerts "foo"

But of course it all depends on what you want to do.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.