AdWords is now Google Ads. Our new name reflects the full range of advertising options we offer across Search, Display, YouTube, and more. Learn more

Ads
1.3K members online now
1.3K members online now
Dive into advanced features like Remarketing, Flexible Bid Strategies, AdWords Editor, and AdWords Scripts
Guide Me
star_border
Reply

Implementing Adwords Call Tracking on ASP.net based site

Visitor ✭ ✭ ✭
# 1
Visitor ✭ ✭ ✭

I'm working to implement Adwords Call Tracking on a website that utilizes a very old unsupported CMS based around ASP.net.

 

I've been able to add the call tracking script to the <head> section and I've added the appropriate <span> tag to the phone number I want the script to manipulate, but I'm running into major problems adding the <body onload=""> portion of the implementation to the <body> tag of this webpage.

 

 

If I add the code directly to the <body> tag in my template file, this causes a compilation error with the page. This is also not ideal since the same template is used on many site pages and I need to implement different phone numbers on these pages.

 

I've tried to use some jQuery to insert the onload="" code into the <body> tag but this too has not been successful.

 

Are there any other methods I can use to get this code snippet into the <body> tag? Perhaps something specific to ASP.net?

1 Expert replyverified_user
1 ACCEPTED SOLUTION

Accepted Solutions
Marked as Best Answer.
Solution
Accepted by topic author David S
January 2016

Re: Implementing Adwords Call Tracking on ASP.net based site

Top Contributor
# 2
Top Contributor
Hi David S,

This isn't really an asp problem, it's all Javascript.

First, I'm not sure why you would want different phone numbers to show on other pages. The idea behind the script is to show the Google number if the user had used the click to call, and to show your own phone number to everyone else. This helps you track phone conversions.

Ok, back to the javascript. There a couple ways to add the body onload event. You can do it in the body tag. If there is no other onload function defined, you can add it there. If you're getting a compilation error, it's probably in the quotes. ASP has a funny way of escaping quote marks within a quote. Instead of using a backslash to escape the quote character, it looks for 2 of them. If the whole string containing the body tag is with a response.write statement, check the outer quotes. If they are double and you are also using double quotes for the onload definition without escaping them, that's your compilation error.

This should look something like this:

response.write("<body onload=""_googWcmGet('number', '1-800-123-4567')"">")

The rendered code on the delivered page should look like this:

<body onload="_googWcmGet('number', '1-800-123-4567')">

The other way to define the onload function is just a bit more complicated. First, you'll need to create a Javascript function that calls _googWcmGet(), because we can't pass parameters using this method.

function googleNumber() {
_googWcmGet('number', '1-800-123-4567')
}

Then, in a Javascript block near the end of the page include:
window.onload = googleNumber;

Now, if a function had been previously assigned to the onload event, this will wipe it out. A safer method:
&lt;script type=text/javascript&gt;
function addLoadEvent(func){
var oldonload = window.onload;
if (typeof window.onload != 'function'){
window.onload = func;
}
else {
window.onload = function(){
oldonload();
func();
}
}
}
addLoadEvent(googleNumber)
&lt;/script&gt;

Best of Luck. I had fun with the old ASP stuff.

Pete
petebardo -- Deadhead doing AdWords

View solution in original post

Marked as Best Answer.
Solution
Accepted by topic author David S
January 2016

Re: Implementing Adwords Call Tracking on ASP.net based site

Top Contributor
# 2
Top Contributor
Hi David S,

This isn't really an asp problem, it's all Javascript.

First, I'm not sure why you would want different phone numbers to show on other pages. The idea behind the script is to show the Google number if the user had used the click to call, and to show your own phone number to everyone else. This helps you track phone conversions.

Ok, back to the javascript. There a couple ways to add the body onload event. You can do it in the body tag. If there is no other onload function defined, you can add it there. If you're getting a compilation error, it's probably in the quotes. ASP has a funny way of escaping quote marks within a quote. Instead of using a backslash to escape the quote character, it looks for 2 of them. If the whole string containing the body tag is with a response.write statement, check the outer quotes. If they are double and you are also using double quotes for the onload definition without escaping them, that's your compilation error.

This should look something like this:

response.write("<body onload=""_googWcmGet('number', '1-800-123-4567')"">")

The rendered code on the delivered page should look like this:

<body onload="_googWcmGet('number', '1-800-123-4567')">

The other way to define the onload function is just a bit more complicated. First, you'll need to create a Javascript function that calls _googWcmGet(), because we can't pass parameters using this method.

function googleNumber() {
_googWcmGet('number', '1-800-123-4567')
}

Then, in a Javascript block near the end of the page include:
window.onload = googleNumber;

Now, if a function had been previously assigned to the onload event, this will wipe it out. A safer method:
&lt;script type=text/javascript&gt;
function addLoadEvent(func){
var oldonload = window.onload;
if (typeof window.onload != 'function'){
window.onload = func;
}
else {
window.onload = function(){
oldonload();
func();
}
}
}
addLoadEvent(googleNumber)
&lt;/script&gt;

Best of Luck. I had fun with the old ASP stuff.

Pete
petebardo -- Deadhead doing AdWords

Re: Implementing Adwords Call Tracking on ASP.net based site

Visitor ✭ ✭ ✭
# 3
Visitor ✭ ✭ ✭
Hi Pete!

Thanks for getting back to me. This is very useful as I've browsed many Stack Overflow threads looking for the same kind of information but I think the specifics of those threads didn't quite apply to my case.

Even though there is a literal <body> tag in our template file that already features a class and id, I seemed to have trouble escaping the single quote characters in the _googWcmGet function as you described. I attempted to escape them with \, \\, double single quotes ( '' ), and double quotes (according to various articles I found) but they also resulted in compile errors.

My own unfamiliarity with ASP.net is to blame, but I went ahead and tested the JS function you've provided and it looks like the phone number I'm testing is now being successfully changed by Adwords when I load the page! I'm lucky there were no other onload events to wrestle with in the body tag, but I appreciate the assistance!

The JS I was testing before utilized jQuery's loadPage() function but I assume it was not properly passing parameters.

Thank you for your help! I hope others will find this thread valuable

Re: Implementing Adwords Call Tracking on ASP.net based site

Top Contributor
# 4
Top Contributor
Hi David,

This was probably all in the way ASP escapes and parses strings.

Glad it worked out.

Pete
petebardo -- Deadhead doing AdWords