VKI Studios is now Cardinal Path! www.CardinalPath.com
Learn more about Cardinal Path

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>

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>

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.

Comments (Comment Moderation is enabled. Your comment will not appear until approved.)
Have you any feedback on clients that used this? I'm just wondering if the conversion gets counted b/c Google so stringently insists not to touch the format of the code in any way/shape/form.

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
# Posted By Daniel Waisberg | 12/17/08 12:16 AM
Daniel, they certainly won't count as conversions if Javascript is turned off as the call to Google's server will never be made.

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.
# Posted By Steve | 12/17/08 1:48 AM
Thats really a good shortcut, but is this gonna work?
# Posted By Nokia Secret Codes | 12/17/08 8:57 AM
Obviously, javascript code will not work if a user disables javascript. Otherwise, the javascript code will execute and the image will be loaded. So yes, this method works, for the same reason that calling the image in the noscript tag that Google provides works, it's just an image call.

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).
# Posted By Erik Vold | 12/17/08 12:52 PM
Obviously, javascript code will not work if a user disables javascript. Otherwise,

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
# Posted By Marlin | 12/18/08 1:37 PM
Oops, My last post got mangled so I'm reposting:
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
# Posted By Marlin | 12/18/08 1:43 PM
@ Marlin:

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?
# Posted By Erik Vold | 12/18/08 2:55 PM
Aha! I see what you are doing now. Normally, if you have javascript enabled, then Google feeds you a piece of Javascript that, when it runs, changes your GETed string and adds lots of parameters. However, if you have Javascript turned off, Conversion tracking will still work, but there
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
# Posted By Marlin | 12/18/08 3:36 PM
you wouldn't belive me for how long I've been searching for this onclick conversion code (including trial and errors plus countless group searches), thanks!
# Posted By kenan | 1/21/09 1:06 AM
Here you are always using the code that Google has provided as a fallback for those who don't have Javascript enabled (i.e. in the noscript tag). If this was the best method, Google would have just kept that one method and not supplied the large Javascript snippet.

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?
# Posted By encryption software | 1/21/09 2:17 AM
I'm not 100% sure, and would appreciate it if anyone can add to this, but the only difference between the javascript snippet and the noscript snippet (which the onclick function above depends on) is that the javascript snippet will display a link to the customer/user to give them information on the Google Adwords conversion tracking code and cookie. This will also give the customer/user an opportunity to provide feedback about their experience with your site.

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..
# Posted By Erik Vold | 1/28/09 12:26 PM
This looks like the answer to my prayers!!! Only one problem...

...I'm a beginner with Javascript - can anyone demonstrate how to call this with an onclick event inside a normal hyperlink?

thanks!!
# Posted By Oliver | 2/4/09 8:49 AM
Oliver, you would just copy the code from the second block above, paste it somewhere in your cod and then add the following onClick="uniqueConversionFunctionName()" to your anchor tag.

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.
# Posted By A Curtis | 2/16/09 11:32 AM
Cool, thanks!
# Posted By Joomla Ads | 2/16/09 4:32 PM
@ Oliver & A Curtis:
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.
# Posted By Erik Vold | 2/19/09 3:49 PM
Hi folks,

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
# Posted By Phillip | 3/3/09 10:29 AM
There is a official answer from Google:
http://adwords.google.com/support/bin/answer.py?hl...
# Posted By Georg | 3/7/09 11:07 AM
# Posted By Georg | 3/7/09 12:06 PM
Thx, look like great resources. We'll try it out and feeback.

cheers
Phillip
# Posted By Phillip Bock | 3/7/09 12:48 PM
@Georg : the official response you point to is about Google Website Optimizer, no Google AdWords
# Posted By Open Keywords | 6/15/10 1:12 AM
.