jquery - Handling <a> click events as Google Analytics events -
i trying send events ga tag when user clicks tags properties (e.g., 'tel:' link, '.zip' extension), having issues binding/unbinding of anchor.
ga doesn't send event unless intercept click also, explains why i'm doing way. couldn't find surefire example online anywhere.
$('a').on('click', function( event ) { $linkobj = $( ); // element attributes href = $linkobj.attr('href') + ''; // safety check if ( !href || href === undefined || typeof href === 'undefined' ) { href = ''; } // if link telephone number try { if ( href.substring(0, 4) === 'tel:' ) { event.preventdefault ? event.preventdefault() : event.returnvalue = false; sendevent( 'call', 'click', href.split( 'tel:' )[ 1 ] + '' ); $linkobj.unbind( 'click' ).click(); // doesn't seem re-enable <a> clickable settimeout ( function () { $linkobj.bind( 'click' ).click(); }, 100 ); } catch (e) { console.log ('err: ', e ); }); var sendevent = function ( category, action, label, value ) { ga( 'send', 'event', category, action, label, value ); };
since using jquery can use 'attribute ends with' or 'attribute contains' selectors:
jquery("[href$='zip'] ,[href*='tel:']").click(function () { // analytics } this return zip- , tel- links in first place not need custom "safety checks".
Comments
Post a Comment