javascript 用于返回Google Analytics的自定义事件跟踪器的功能,如果未针对本地/演示环境设置GA,则将其包装

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了javascript 用于返回Google Analytics的自定义事件跟踪器的功能,如果未针对本地/演示环境设置GA,则将其包装相关的知识,希望对你有一定的参考价值。

// Wrap it so if GA is not initialised it doesn't go 'undefined'!
function analytics_custom_event_tracker(name, event, label)
{
    if (typeof ga === 'function')
    {
        return ga('send', 'event', name, event, label);
    }
}


// Attach it to a standard JavaScript event listener 
var clickMe = document.getElementById('click-me');
clickMe.addEventListener('change', function () {
    // your code goodness
    
    analytics_custom_event_tracker('Dans Event Listener', 'click', clickMe.value);
    // Sending the value of element to Google Analytics on a click event with the name 'Dans Event Listener'
}, false);


// Attach it to a standard jQuery event listener
$('#click-me').on({
    click: function () {
        // the rest of your magic 
        
        analytics_custom_event_tracker('Dans Event Listener', 'click', $(this).val())); 
        // Sending the value of element to Google Analytics on a click event with the name 'Dans Event Listener'
    }
});

以上是关于javascript 用于返回Google Analytics的自定义事件跟踪器的功能,如果未针对本地/演示环境设置GA,则将其包装的主要内容,如果未能解决你的问题,请参考以下文章