Firing asynchronous quantcast pixels

If you are building an AJAX-driven web application (like a swipe-driven mobile website) and want to register quantcast pageviews at times other than window.onload, here is a small change to the code snippet quantcast provides that will get you there.

Note that this does not remove or replace any existing functionality, you will still need to initialize it with their snippet:

<script type="text/javascript">var _qevents = _qevents || [];

(function() {
	var elem = document.createElement('script');

	elem.src = (document.location.protocol == "https:" ? "https://secure" : "http://edge") + ".quantserve.com/quant.js";
	elem.async = true;
	elem.type = "text/javascript";
	elem.onload = function(){
               // attach the asynchronous pixel handler
		__qc.async = function(){
			__qc.qpixelsent = [];
			_qevents.push({ 
			        qacct: __qc.qopts.qacct
			});
			__qc.firepixels();
		}
	}
	var scpt = document.getElementsByTagName('script')[0];
	scpt.parentNode.insertBefore(elem, scpt);  
})();
</script>
<script type="text/javascript">
_qevents.push( { qacct:"p-test123"} );
</script>
<noscript>
<div style="display: none;"><img src="http://pixel.quantserve.com/pixel/p-test123.gif" height="1" width="1" alt="Quantcast"/></div>
</noscript>

Just drop that snippet in your page and you will be ready to track asynchronous events, here is an example of usage:


$.ajax({
	method: 'GET',
	url:'/next-page-content/',
	success: function(response){
		// track the asynchronous page view
		__qc.async();
	}
})

Happy Coding!

Leave a Reply