javascript
  • google-analytics
  • 2014-10-31 69 views 1 likes 
    1

    因此,我正在嘗試爲Universal Analytics設置一個指標。下面是我使用的JavaScript: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(「設置」,{metricIndex:5 });而不是ga(「set」,{metric1:5});我可以爲20個可能的索引使用switch語句,但我寧願不必硬編碼所有的可能性。有任何想法嗎?

    回答

    4

    試試這個:

    var metricIndex = 'metric' + index; 
    var myObject={}; 
    
    myObject[metricIndex]=value; 
    ga('set', myObject); 
    
    +0

    真棒!感謝和原諒我對JS的知識不足 – 2014-10-31 18:44:44

    相關問題