Archive | Web development RSS feed for this section

Tracking download of non-html (like pdf) downloads with jQuery and Google Analytics

10 Oct

Hi folks, it’s been quite calm at Developer IT’s this summer since we were all involved in other projects, but we are slowly comming back.

In this post, we will present a simple way of tracking files download with Google Analytics with the help of jQuery. We work for a client that offers a lot of pdf files to download on their web site and wanted to know which one are the most popular. They use Google Analytics for a long time now and we did not want to have a second interface in order to present those stats to our client. So usign IIS logs was not a idea to consider.

Since Google already offers us a splendid web interface and a powerful API, we deceided to hook up simple javascript code into the jQuery click event to notify Analytics that a pdf has been requested.

(function ($) {
    function trackLink(e) {
        var url = $(this).attr('href');
        //alert(url); // for debug purpose
        // old page tracker code
        pageTracker._trackPageview(url);
        // you can use the new one too
        _gaq.push(["_trackPageview",url]);
       //always return true, in order for the browser to continue its job
        return true;
    }

   // When DOM ready
    $(function () {
        // hook up the click event
        $('.pdf-links a').click(trackLink);
    });
})(jQuery);

You can be more presice or even be sure not to miss one click by changing the selector which hooks up the click event. I have been usign this code to track AJAX requests and it works flawlessly.

Advertisement

How to get full query string parameters not UrlDecoded

17 Mar

Introduction

While developing Developer IT’s website, we came across a problem when the user search keywords containing special character like the plus ‘+’ char. We found it while looking for C++ in our search engine. The request parameter output in ASP.NET was “c “. I found it strange that it removed the ‘++’ and replaced it with a space…

Analysis

After a bit of Googling and Reflection, it turns out that ASP.NET calls UrlDecode on each parameters retreived by the Request(“item”) method. The Request.Params property is affected by this two since it mashes all QueryString, Forms and other collections into a single one.

Workaround

Finally, I solve the puzzle usign the Request.RawUrl property and parsing it with the same RegEx I use in my url re-writter. The RawUrl not affected by anything. As its name say it, it’s raw.

Published on http://www.developerit.com/