最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

google analytics - Javascript: Increment count by 5 for a variable inside a setInterval() function - Stack Overflow

programmeradmin2浏览0评论

I'm trying to use Google Analytics events to track time spent on site more accurately (without relying on delta time between visits to another page on site). I'm using setInterval() to continuously trigger that GA even every 5 seconds. How do I properly increment seconds elapsed to send along with the GA event data?

  var count = 0;
  setInterval(function(){
    // increment "count" by 5 each time setInterval is run
    ga('send', 'event', 'time', 'tracking', 'seconds', count);
  }, 5000);

I'm trying to use Google Analytics events to track time spent on site more accurately (without relying on delta time between visits to another page on site). I'm using setInterval() to continuously trigger that GA even every 5 seconds. How do I properly increment seconds elapsed to send along with the GA event data?

  var count = 0;
  setInterval(function(){
    // increment "count" by 5 each time setInterval is run
    ga('send', 'event', 'time', 'tracking', 'seconds', count);
  }, 5000);
Share Improve this question asked Feb 21, 2014 at 18:54 SteveSteve 5,19414 gold badges51 silver badges65 bronze badges
Add a ment  | 

4 Answers 4

Reset to default 5
  var count = 0;
  setInterval(function(){
    // increment "count" by 5 each time setInterval is run
    count+=5; //is this what you need?
    ga('send', 'event', 'time', 'tracking', 'seconds', count);
  }, 5000);

Is this what your looking for?

var count = 0;
setInterval(function(){
  count = count + 5;
  // increment "count" by 5 each time setInterval is run
  ga('send', 'event', 'time', 'tracking', 'seconds', count);
}, 5000);

How about this?

var count = 0;
setInterval(function(){
    count+=5;
    // increment "count" by 5 each time setInterval is run
    ga('send', 'event', 'time', 'tracking', 'seconds', count);
}, 5000);

Not sure if I gather this correctly, but we can do this like: var count = 0,interval=5000; setInterval(function(){ // increment "count" by 5 each time setInterval is run ga('send', 'event', 'time', 'tracking', 'seconds', count+interval); }, interval);

In clear interval you can set count = 0 again.

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论