2016-07-14 52 views
0

我不明白爲什麼這個工程工作;谷歌通用Analytics(分析) - 事件跟蹤 - 的setTimeout不使用fieldsObject方法

ga('send', 'pageview'); 
setTimeout("ga('send','event','Engagement','SessionPing','30s')", 30000); 

這不起作用

ga('send', 'pageview'); 
setTimeout("ga('send',{ 
    hitType: 'event', 
    eventCategory: 'Engagement', 
    eventAction: 'SessionPing', 
    eventLabel: '30s' 
})", 30000); 

任何人都可以解釋爲什麼一個會的工作,而不是其他?第二種方法似乎可以防止GA完全觸發,並且我在幾天內沒有記錄任何數據 - 顯示的錯誤消息Google的Chrome Tag Assistant是該JavaScript文件尚未加載。

我想知道,因爲我想更好地瞭解GA和JavaScript的 - 這是沒有意義的,我不能看到第二個方法任何語法或格式錯誤。

+0

工作正常,我。在GA –

+0

實時停止工作,這是在標籤助理錯誤 http://d.pr/i/iDR0+ – ericTbear

回答

0

JavaScript語法錯誤。基本上,你使用一個字符串作爲setTimeout的第一個參數,當你把它分成多行時,你做得不夠好。 setTimeout的第一個參數是一個函數,可以用多種方式引用,如here

如果你希望它是一個字符串,那麼它應該要麼是:

​​

,或者如果你想分割爲多行,(注意雙引號)

setTimeout("ga('spaTracker.send', {" + 
      "hitType: 'event', " + 
      "eventCategory: 'Engagement', " + 
      "eventAction: 'SessionPing', " + 
      "eventLabel: '30s'" + 
      "})", 30000); 

你也可以這樣拆分:

setTimeout("ga('spaTracker.send', { \ 
      hitType: 'event', \ 
      eventCategory: 'Engagement', \ 
      eventAction: 'SessionPing', \ 
      eventLabel: '30s' \ 
     })", 5000); 

如果你想保持它作爲一個功能,那麼它應該要麼是:

setTimeout(ga('send','event','Engagement','SessionPing','30s'), 30000); 

或:

setTimeout(ga('spaTracker.send', { 
      hitType: 'event', 
      eventCategory: 'Engagement', 
      eventAction: 'SessionPing', 
      eventLabel: '30s' 
     }), 30000); 
+0

「基本上,你正在使用一個字符串作爲setTimeout的第一個參數」 謝謝你,我知道如果我在這裏發佈,我會學到一些東西。 – ericTbear

+0

很高興我能幫到你。 Plz將答案標記爲已接受。 –

相關問題