Skip to content

Instantly share code, notes, and snippets.

@azdak
Last active August 24, 2017 21:29
Show Gist options
  • Save azdak/b8647536c206ffbb66d6aba444e78711 to your computer and use it in GitHub Desktop.
Save azdak/b8647536c206ffbb66d6aba444e78711 to your computer and use it in GitHub Desktop.
Call tracking callback and call, for port to GTM
//
// this is the callback function
//
var NumToReplacePretty;
var NumToReplace;
var callback = function(formatted_number, mobile_number) {
console.log("Call tracking callback fired!");
console.log("formatted #:"+formatted_number);
console.log("mobile #:"+mobile_number);
var pretty = "("+formatted_number.substr(0, 3) + ') ' + formatted_number.substr(3, 3) + '-' + formatted_number.substr(6,4)
updateMapNumber(pretty); // call setter for the number used in the map infobox
jQuery("a[href^='tel']").each(function(){
var replaceStr = jQuery(this).html();
var prettyString = "("+formatted_number.substr(0, 3) + ') ' + formatted_number.substr(3, 3) + '-' + formatted_number.substr(6,4)
replaceStr = replaceStr.replace(NumToReplacePretty, prettyString);
jQuery(this).html(replaceStr);
jQuery(this).attr("href","tel"+mobile_number);
jQuery(this).click(function(){
dataLayer.push({
'event' : 'endaiGAevent',
'eventCategory' : "Main",
'eventAction' : "click on "+replaceStr
});
});
});
};
//
// This is the actual call to the google # replacement script- we use jQuery to find to number to replace, then pass that number and the callback to google. In GTM it might need a different trigger than window.onload?
//
window.onload = function() {
console.log("numToReplace:"+jQuery("a[href^='tel']").first().attr("href"));
NumToReplacePretty = jQuery("a[href^='tel']").first().attr("href").substring(4);
NumToReplace = NumToReplacePretty.replace(/[^\d\+]/g,"");
_googWcmGet( callback, NumToReplace );
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment