Shoora
10/8/2018 - 4:46 PM

Html code (plain javascript) that allows tracking of phone number clicks within Google Analytics

Html code (plain javascript) that allows tracking of phone number clicks within Google Analytics

<!-- Track phone number link clicks -->
<script type="text/javascript">
/* <![CDATA[ */
    (function(){
        var DOMReady = function( a, b, c ) {
            b = document;
            c = 'addEventListener';
            b[c] ? b[c]( 'DOMContentLoaded',a ) : window.attachEvent( 'onload', a );
        };
        var addDOMListener = function( o, e, f, c ) {
            if(o.addEventListener) {
                return o.addEventListener( e, f, c );
            }
            else if( o.attachEvent ) {
                return o.attachEvent( "on" + e, f );
            }
            /* log error to console */
            console.log('failed to attach event:');
            console.log([o,e,f,c]);
            return o;
        };
        var addTracker = function() {
            var links = document.querySelectorAll('a[href^="tel:"]');
            if( links && links.length ) {
                for( var i = 0; i < links.length; i++ ) {
                    addDOMListener( links[i], 'click', function() {
                        ga && ga( 'send', 'event', 'Links', 'Click', ( ( this.href ) ? this.href : '' ) );
                    });
                }
            }
        };
        DOMReady( addTracker );
    })();
/* ]]> */
</script>
<!-- END Track phone number link clicks -->