How to Track Downloads in Google Analytics for Sitecore


    <script type="text/javascript">
        if (typeof jQuery != 'undefined') {

            jQuery(document).ready(function($) {
                var filetypes = /\.(zip|pdf|doc*|xls*|ppt*|jpg|ashx)$/i;
                var baseHref = '';
                if (jQuery('base').attr('href') != undefined) baseHref = jQuery('base').attr('href');
                jQuery('a').each(function() {
                    var href = jQuery(this).attr('href');
                    if (href) {

                        if (href.indexOf('?') != '-1') {
                            href = href.substring(0, href.indexOf('?'));
                        }

                        if (href.match(filetypes)) {
                            jQuery(this).click(function() {
                                var extension = String((/[.]/.exec(href)) ? /[^.]+$/.exec(href) : undefined);
                                var filePath = String(href);

                                _gaq.push(['_trackEvent', 'Download', extension, filePath]);
                                if (jQuery(this).attr('target') != undefined && jQuery(this).attr('target').toLowerCase() != '_blank') {
                                    setTimeout(function() {
                                        location.href = baseHref + href;
                                    }, 200);
                                    return false;
                                }
                            });
                        }
                    }
                });
            });
        }
</script>
Advertisements

One thought on “How to Track Downloads in Google Analytics for Sitecore

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s