I'm trying to create a test for Google Analytics but it looks like the event 'click' doesn't get fired.
Here's my code
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
});
}
});
},
Here's my test
it('should fire social interaction event', function() {
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'
});
});
Aucun commentaire:
Enregistrer un commentaire