Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/416.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
javascript变量引用_Javascript_Google Analytics - Fatal编程技术网

javascript变量引用

javascript变量引用,javascript,google-analytics,Javascript,Google Analytics,因此,我试图为通用分析设置一个指标。下面是我正在使用的JavaScript: function setCustomMetric(index, value){ if(typeof index === "number" && index > 0 && typeof value === "number"){ var metricIndex = 'metric' + index; ga('set', {metricIndex: value});

因此,我试图为通用分析设置一个指标。下面是我正在使用的JavaScript:

function setCustomMetric(index, value){
  if(typeof index === "number" && index > 0 && typeof value === "number"){
    var metricIndex = 'metric' + index;
    ga('set', {metricIndex: value});
  }
}
当我发送函数1和5作为各自的参数时,我得到这个返回 我的问题是它正在发送ga(“set”,{metricIndex:5});而不是ga(“集合”{metric1:5});我可以对20个可能的索引使用switch语句,但我不希望对所有可能的索引都进行硬编码。有什么想法吗?

试试这个:

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

myObject[metricIndex]=value;
ga('set', myObject);
试试这个:

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

myObject[metricIndex]=value;
ga('set', myObject);

令人惊叹的!感谢并原谅我对JS知识的缺乏!感谢并原谅我缺乏JS方面的知识