WebDesires

Google Adsense Tracking With JavaScript/jQuery

If you have seen the tracking code for Google Adsense, it requires the including of various scripts and variables, its good if you need to track a landing page, or success page. But it is not made clear how you can track user actions on a page where you can only do this through the JavaScript that is being called, such cases as when you have a contact form that is handled using AJAX and the user never leaves the page. If you need to fire your Google Adsense tracking via Javascript you can do this very easily by abusing the noscript implementation of tracking, for instance you may have the following tracking code from google:

<!-- Google Code for Form Fill Conversion Page -->
<script type="text/javascript">
/*<![CDATA[*/
var google_conversion_id = 1086878749;
var google_conversion_language = "en";
var google_conversion_format = "3";
var google_conversion_color = "ffffff";
var google_conversion_label = "fr4kCNCjlGQQnYe27gM";
var google_remarketing_only = false;
/*]]>*/
</script>
<script type="text/javascript" src="//www.googleadservices.com/pagead/conversion.js">
</script>
<noscript>
<div style="display:inline;">
<img height="1" width="1" style="border-style:none;" alt="" src="//www.googleadservices.com/pagead/conversion/1086878749/?label=fr4kCNCjlGQQnYe27gM&guid=ON&script=0"/>
</div>
</noscript>

If you notice, there is a section at the bottom with <noscript> tags wrapped around it, specifically this section:

<noscript>
<div style="display:inline;">
<img height="1" width="1" style="border-style:none;" alt="" src="//www.googleadservices.com/pagead/conversion/1086878749/?label=fr4kCNCjlGQQnYe27gM&guid=ON&script=0"/>
</div>
</noscript>

You need to take the SRC attribute for use in the below code, so in this example you need: //www.googleadservices.com/pagead/conversion/1086878749/?label=fr4kCNCjlGQQnYe27gM&amp;amp;guid=ON&amp;amp;script=0 The code below can be placed in ANY Javascript/jQuery function, can be called upon AJAX response or whatever you choose, the important thing is the tracking will only fire when this code is fired on the page.

var imagex = new Image(1, 1);
imagex.src = "//www.googleadservices.com/pagead/conversion/1086878749/?label=fr4kCNCjlGQQnYe27gM&guid=ON&script=0";

And thats it! As simple as that you can now track any Javascript events with Google Adsense.    

Author: Dean Williams

Professional PHP Web Developer with expertise in OpenCart Web Development, WordPress Web Development, Bespoke Systems - also a seasoned Linux Server Administrator.


Leave a Reply

Your email address will not be published. Required fields are marked *