2017-05-08 56 views
0

我試圖在單擊特定鏈接時觸發Google Analytics事件。爲了確保事件在重定向到url之前發送,Google建議使用hitCallback函數。使用Google Analytics hitCallback在重定向到url之前發送事件

我嘗試了這種方式,但它不會工作,因爲該鏈接立即打開:

$('#service a').on("click",function(e){ 

    e.preventDefault(); 
    var title = $(this).data('title'); 
    var url = $(this).attr('href'); 

    if (typeof ga === 'function') { 
    //console.log('started'); 
    ga('send', { 
     hitType: 'event', 
     eventCategory: 'header', 
     eventAction: 'nav', 
     eventLabel: 'service', 
     eventValue: title, 
     hitCallback : createFunctionWithTimeout(function() { 
     //console.log('done') 
     document.location = url; 
     }, 100000) 
    }); 
    }else{ 
    document.location = url; 
    } 

}); 

Here is the timeout function,建議由谷歌:

function createFunctionWithTimeout(callback, opt_timeout) { 
    var called = false; 
    function fn() { 
     if (!called) { 
     called = true; 
     callback(); 
     } 
    }setTimeout(fn, 1000); 
    return fn; 
} 

我想知道爲什麼它不工作,或者如果有另一種方法來實現這一點。

回答

0

事件值應該是整數類型。你可以嘗試硬編碼一個整數,看看它是否工作?

相關問題