我正在尝试为Google Analytics创建一个测试,但看起来事件'click'不会被触发。
这是我的代码
trackEvent: function() {
$eventID = $('[data-trigger="click"]');
$eventID.on('click', function() {
var element = $(this);
var eventCategory = element.data('category') ? element.data('category') : '',
eventAction = element.data('action') ? element.data('action') : '',
eventLabel = element.data('label') ? element.data('label') : '',
eventSocialNetwork = element.data('socialNetwork') ? element.data('socialNetwork') : '',
eventSocialAction = element.data('socialAction') ? element.data('socialAction') : '',
eventSocialTarget = element.data('socialTarget') ? element.data('socialTarget') : '';
if(eventCategory !== '' && eventAction !== '' && eventLabel !== '') {
ga('send', {
'hitType': 'event',
'eventCategory': eventCategory,
'eventAction': eventAction,
'eventLabel': eventLabel
});
}
if(eventSocialNetwork !== '' && eventSocialAction !== '' && eventSocialTarget !== '') {
ga('send', {
'hitType': 'social',
'socialNetwork': eventSocialNetwork,
'socialAction': eventSocialAction,
'socialTarget': eventSocialTarget
});
}
});
},
这是我的测试
它('应该解雇社交互动事件',函数(){
var theDiv = sandbox({
'id': 'social-facebook',
'data-trigger': 'click',
'data-socialNetwork': 'facebook',
'data-socialAction': 'share',
'data-socialTarget': 'url'
});
$(document.body).append(theDiv);
eventTracker.trackEvent();
var event = jQuery.Event('click');
event.target = jQuery('#social-facebook')[0];
jQuery('#social-facebook').trigger('click');
expect(window.ga).toHaveBeenCalledWith('send', {
'hitType': 'social',
'socialNetwork': 'facebook',
'socialAction': 'share',
'socialTarget': 'url'
});
});