Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

_googWcmGet callback not working via GTM with dynamic phone number

I'm trying to get Adwords Call Conversion tracking working on my site, using GTM. Everything is set up on the Adwords side (call extensions + call from website conversion) but the replace function still isn't working.

<script type="text/javascript">
// the code from adwords, loads the _googWcmGet function
(function(a,e,c,f,g,b,d){var h={ak:"XXXXXXX",cl:"XXXXXXXX"};a[c]=a[c]||function(){(a[c].q=a[c].q||[]).push(arguments)};a[f]||(a[f]=h.ak);b=e.createElement(g);b.async=1;b.src="//www.gstatic.com/wcm/loader.js";d=e.getElementsByTagName(g)[0];d.parentNode.insertBefore(b,d);a._googWcmGet=function(b,d,e){a[c](2,b,h,d,null,new Date,e)}})(window,document,"_googWcmImpl","_googWcmAk","script");

// my callback to replace the number
var replaceNumber = function (formatted_number, unformatted_number) {
  console.log("replaceNumber() called");
}

var getNumber = function () {
  var links = document.getElementsByTagName("a");
  var oldNumber = null;

  for(var i=0; i<links.length; i++) {
    if( links[i].href.indexOf("tel") > -1 ) {
       var el = links[i];
       var oldNumber = el.innerHTML.split("<")[0];
    }
  }
  // console.log(oldNumber);  -> 123-456-7890
  return oldNumber;

}

window.onload = function() {
  _googWcmGet( replaceNumber, getNumber() );
};

</script>

getNumber() is returning the correct phone number to replace (format: 123-456-7890), but replaceNumber is never being called. This looks pretty much exactly like the example, except that the phone number is retrieved by a function instead of provided statically.

Why isn't replaceNumber being called?

like image 986
mz3 Avatar asked Dec 22 '14 17:12

mz3


1 Answers

As it turns out, the callback only runs if the visitor came from an Adwords ad, meaning you need to generate a paid click in order to test.

From the documentation, call tracking can be used to track:

"calls people make from your website after clicking your ad ..."

It's subtle, but it's there. In order to test, you need to click on your own Adwords ad. Then, save the URL (with the tags and such) so that you can use the same URL later to continue testing without generating more click fees. Possibly you don't even need to click an ad if you know what the URL is.

EDIT: The comment below from @dorian is so helpful I'm including it here for visibility, but please give him an upvote as well.

Probably a bit late, but there's an extremely helpful debugging mode which you can activate by appending #google-wcc-debug to the URL of the page where you have activated call tracking. Unfortunately this debug mode seems to be documented nowhere.

like image 67
mz3 Avatar answered Sep 29 '22 11:09

mz3