1

So I am trying to set a metric for Universal Analytics. Here is the JavaScript that I am using:

function setCustomMetric(index, value){
  if(typeof index === "number" && index > 0 && typeof value === "number"){
    var metricIndex = 'metric' + index;
    ga('set', {metricIndex: value});
  }
}

When I send the function 1 and 5 as the respective parameters I get this return My problem is that it is sending ga("set", {metricIndex: 5}); rather than ga("set", {metric1: 5}); I could use a switch statement for the 20 possible indexes but I would rather not have to hard code all of the possibilities. Any ideas?

0

1 Answer 1

4

Try this:

var metricIndex = 'metric' + index;
var myObject={};

myObject[metricIndex]=value;
ga('set', myObject);
Sign up to request clarification or add additional context in comments.

1 Comment

Awesome! Thanks and excuse my lack of knowledge in JS

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.