JavaScript onClick Google AdWords Conversion Tracking
Original posted on Erik Vold's Blog
This is really just a technique to convert the provided Google AdWords Conversion Tracking Code into something more a little more useful.
Take the conversion code provided, usually something like:
<!-- Google AdWords Code for Conversion Page -->
<script language="JavaScript" type="text/javascript">
var google_conversion_id = 1234567890;
var google_conversion_language = "en_US";
var google_conversion_format = "1";
var google_conversion_color = "666666";
if(1){
var google_conversion_value = 1;
}
var google_conversion_label = "Purchase";
</script>
<script language="JavaScript" src="http://www.googleadservices.com/pagead/conversion.js">
</script>
<noscript>
<img height=1 width=1 border=0 src="http://www.googleadservices.com/pagead/conversion/1234567890/?value=1&label=Purchase&script=0">
</noscript>
<script language="JavaScript" type="text/javascript">
var google_conversion_id = 1234567890;
var google_conversion_language = "en_US";
var google_conversion_format = "1";
var google_conversion_color = "666666";
if(1){
var google_conversion_value = 1;
}
var google_conversion_label = "Purchase";
</script>
<script language="JavaScript" src="http://www.googleadservices.com/pagead/conversion.js">
</script>
<noscript>
<img height=1 width=1 border=0 src="http://www.googleadservices.com/pagead/conversion/1234567890/?value=1&label=Purchase&script=0">
</noscript>
And convert it into this:
<script>
var uniqueConversionFunctionName = function(){
var image = new Image(1,1);
image.src = "http://www.googleadservices.com/pagead/conversion/1234567890/?value=1&label=Purchase&script=0";
return;
}
</script>
var uniqueConversionFunctionName = function(){
var image = new Image(1,1);
image.src = "http://www.googleadservices.com/pagead/conversion/1234567890/?value=1&label=Purchase&script=0";
return;
}
</script>
Notice that in the second block of code above, I've taken the source of the image which was provided by Google in the first block of code's noscript tag.
That's it! with this your Google AdWords conversion no longer has to be a landing page, it can be a button click event, or whatever else you want it to be.


In addition, in the original Adwords code, there is a use of script and noscript. While the function provided here does not have the noscript option. Therefore users with JS disabled will not count as conversions. Is it so?
Thanks a lot
Without a doubt in most cases the easiest approach is to develop a seperate landing page, however unfortunately many CMS products are technically limited and prevent you from being able to do this. This method strikes a nice balance between redevelopment and zero conversion tracking.
Steve brings up one example, where customer's are forced to use a redirect page in order to use the conversion code that Google provides (which means you are forced to use a meta tag redirect or a javascript redirect). There are many reasons why one would not want to do this, personally I always try to avoid using redirect pages (http header redirects are ok, meta tag and javascript redirects are not ok) as a rule of thumb.
To be clear, I'm not saying this conversion code is better than the code Google Adwords provides in general. I am saying there are certain situations where this conversion code will be more useful though. The most obvious being to avoid using a redirect page to track your Google Adwords conversions, which is how I came to try this technique (and yes it worked).
I suspect that this does not work. Using Wireshark, a freely available packet
sniffing tool, I compared the interaction of
the real Google AdWords Code with the VKI version www.googleadservices.com and
I think what I'm seeing are some important differences:
Google AdWords Code will send:
http://www.googleadwords.com/pagead/conversion/123...;
cv=2&fst=1229632907443&num=1&fmt=1&value=1&label=purchase&bg=36557a&hl=en&gl=US&
u_h=1050&u_w=1680&u_ah=1020&u_aw=1680&u_cd=32&u_his=23&u_tz=-360&u_nplug=0&
u_nmime=0&url=http%3A//www.mysite.com/myconversion.html
and then google sets a cookie
Whereas the VKI script sends:
http://www.googleadwords.com/pagead/conversion/123...;
script=0
then sends an GIF89A image and no cookie is set. The Google Adwords Code sends no
image.
Note that I've obscured my real Conversion ID, replacing it with 1234567890
I suspect that this does not work. Using Wireshark, a freely available packet sniffing tool, I compared the interaction of the real Google AdWords Code with the VKI version www.googleadservices.com and I think what I'm seeing are some important differences:
Google AdWords Code will send:
www.googleadwords.com/pagead/conversion/1234567890...?
random=1229632907443&cv=2&fst=1229632907443&num=1&
fmt=1&value=1&label=purchase&bg=36557a&hl=en&gl=US&
u_h=1050&u_w=1680&u_ah=1020&u_aw=1680&u_cd=32&u_his=23&
u_tz=-360&u_nplug=0&u_nmime=0&
url=http%3A//www.mysite.com/myconversion.html
and then google sets a cookie
Whereas the VKI script sends:
www.googleadwords.com/pagead/conversion/1234567890...?
value=1&label=Purchases&script=0
then sends an GIF89A image and no cookie is set. The Google Adwords Code sends no image.
Note that I've obscured my real Conversion ID, replacing it with 1234567890
I believe the google adwords image is cached, and the cookie is already set on your machine, which why your second attempt (using the javascript version I describe in my post) did not work.
Can you please verify that this is not the case?
Also, when you tried the regular Google Adwords conversion code, did you just try the contents of the noscript tag, or were you trying the contents of the script tag?
is no opportunity for any modding of the GET string.
So I find that when running just the noscript code, I get identical results as your javascript code. (Both the noscript and the vki script return a GIF89A file, which is apparently normal when javascript is turned off).
So maybe there's merit to this afterall.
Marlin
I appreciate the code you have provided, but what features or data will we be missing if we use this rather than the entire snippet?
https://adwords.google.com/support/bin/answer.py?a...
As far as the conversation tracking goes, you will not miss any extra data by using this method, except conversions for users with javascript disabled, but sometimes that's a hit you just have to take..
...I'm a beginner with Javascript - can anyone demonstrate how to call this with an onclick event inside a normal hyperlink?
thanks!!
Hey Erik, do you think this works with onSubmit?? I am testing it out now on a PayPal checkout button and thinking it might not...trying some more tests now to find out.
Curtis' example is correct, but rename 'uniqueConversionFunctionName' to something that makes more sense for you (the name I used was only an example, I don't want to see people using 'uniqueConversionFunctionName' on their sites =) ... like 'adWordsCampFunc' or something (in both my example and Curtis').
@ A Curtis: Yep it will work for any javascript event, it will make a request for the conversion image once executed, and then the conversion is complete. So if you make sure that the code is actually executing when you desire it to then your golden, and you can use firebug or javascript alerts to do this during your testing phase.
I am heavily interested in that snippet. One question:
If I add that to a mailto event-link: Any ideas how this will come up as a user experience?
Will the mail client still open and focus?
We use a similar technique in our back office. Clicking a mailto-Event (in order to generate an order to our suppliers i.e.) we run a printout of a frame with some info in it.
Now on some of our computers this results in the print-confirmation-window coming up and focussing, but the email-client just appearing in the bottom block (windows applications, sont know how to explain, I am nonnative, sorry).
However I dont want to disturb my customer right in the middle of his conversion event with "Where is that damned email I am trying to generate"-thoughts.
thx
Phillip
http://adwords.google.com/support/bin/answer.py?hl...
http://groups.google.com/group/adwords-help-advanc...
cheers
Phillip