Last active
October 8, 2018 16:46
-
-
Save sergeliatko/cf74be07f8fd77a696509281de48d874 to your computer and use it in GitHub Desktop.
Html code (plain javascript) that allows tracking of phone number clicks within Google Analytics
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!-- 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 --> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment