diff options
Diffstat (limited to 'plugins/jetpack/_inc/lib/tracks/tracks-ajax.js')
-rw-r--r-- | plugins/jetpack/_inc/lib/tracks/tracks-ajax.js | 49 |
1 files changed, 49 insertions, 0 deletions
diff --git a/plugins/jetpack/_inc/lib/tracks/tracks-ajax.js b/plugins/jetpack/_inc/lib/tracks/tracks-ajax.js new file mode 100644 index 00000000..316aef44 --- /dev/null +++ b/plugins/jetpack/_inc/lib/tracks/tracks-ajax.js @@ -0,0 +1,49 @@ +/* global jpTracksAJAX, jQuery */ + +(function( $, jpTracksAJAX ) { + + $( document ).ready( function () { + $( 'body' ).on( 'click', '.jptracks a, a.jptracks', function( event ) { + + // We know that the jptracks element is either this, or its ancestor + var $jptracks = $( this ).closest( '.jptracks' ); + + var data = { + tracksNonce: jpTracksAJAX.jpTracksAJAX_nonce, + action: 'jetpack_tracks', + tracksEventType: 'click', + tracksEventName: $jptracks.attr( 'data-jptracks-name' ), + tracksEventProp: $jptracks.attr( 'data-jptracks-prop' ) || false + }; + + // We need an event name at least + if ( undefined === data.tracksEventName ) { + return; + } + + var url = $( this ).attr( 'href' ); + var target = $( this ).get( 0 ).target; + if ( url && target && '_self' !== target ) { + var newTabWindow = window.open( '', target ); + } + + event.preventDefault(); + + $.ajax( { + type: 'POST', + url: jpTracksAJAX.ajaxurl, + data: data + } ).always( function() { + // Continue on to whatever url they were trying to get to. + if ( url ) { + if ( newTabWindow ) { + newTabWindow.location = url; + return; + } + window.location = url; + } + } ); + }); + }); + +})( jQuery, jpTracksAJAX ); |