Archive for the ‘JavaScript’ Category

Google Analytics tracking PDF and Flash files on your Site

Tuesday, September 29th, 2009


Setting up Google Analytics is pretty easy work thanks to google :)

If you dont have a Analytics account yet  you can get one here: Google Analytics

To start tracking just add the code below to the page(mostly before the end of <body> tag) that you wish to track using Google Analytics


&lt;script type="text/javascript"&gt;

var gaJsHost = (("https:" == document.location.protocol) ? "<a href="https://ssl.&quot;">https://ssl."</a> : "<a href="http://www.&quot;);">http://www.");</a>
document.write(unescape("%3Cscript src='" + gaJsHost + "google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E"));
&lt;/script&gt;
&lt;script type="text/javascript"&gt;
try {
var pageTracker = _gat._getTracker("XX-XXXXXX-XX");
pageTracker._trackPageview();
} catch(err) {}&lt;/script&gt;

Note: Make sure that you replace the ‘X” with your tracking id.

A bit tricky part is tracking downloads for PDF and Flash files.

But since google rocks they have made this simple as well.

To track a file download add the following code to the anchor tag’s onclick event


pageTracker._trackPageview('/downloads/myfile.pdf');

The complete anchor tag would look like something below:


&lt;a href="/myfile.pdf" onclick="pageTracker._trackPageview('/downloads/myfile.pdf')"&gt;My Pdf&lt;/a&gt;

When the user clicks on the above link , you will see the ‘/downloads/myfile.pdf’ url in your analytics report.

This technique can be used to track other file types as well (Flash, WMV, AVI, MOV, etc..)

Post to Twitter

Cancel Event-Bubbling in Dojo

Monday, September 28th, 2009

If you have ever worked with a set of nested div’s which needed to listen for onclick event you must surely have encountered this issue.

If child div is clicked the parent’s click event will also be triggered because of Event-Bubbling.

The Internet Explorer 4 or later and Netscape 6 event model mechanism that propagates events from the target element upward through the HTML Page.

Me and my team mate Anoop were struggling with the problem for a long time and today he finally found a solution to it.

Use the code below to cancel the Event-Bubbling.

dojo.connect(dojo.byId("myDiv"),"onmouseover",

function(evt){

//Do Something
//..
//..

evt.stopPropagation(); //Stop Event Bubbling

});

Here passing the evt paramenter is very crucial otherwise the Event Bubbling wont stop! ;-)

Post to Twitter