How can I apply GA download tracking with Sitecore

2019-06-10 23:41发布

问题:

I'm posting this question to Stackflow b/c after doing much research into an answer to this very question online, I did not come across a straight forward answer and had to do my own sleuthwork to resolve this.

Basically, Sitecore uses a handler file .ASHX for all files uploaded to the Media Library. Since the 3rd party GA tracking tool I was using (entourage.js or gatags.js) does not recognize .ashx as a whitelisted download file, it was not adding the appropriate GA tracking syntax to the GA pixel tracker (__utm.gif).

So the solution turns out to be simple but sadly, not retroactive, meaning all files previously uploaded to the Media Library in the Sitecore content tree will continue to use the ashx extension unless you reupload the image. In your web.config file, search for the "Media.RequestExtension" setting. If you change the value associated with this setting from "ashx" to a blank string, this will force Sitecore to use the originalextension of the file and image in the Sitecore Media Library.

Aside from interfering with GA analytics, this method of turning every downloadable file extension into an ashx file is poor SEO practice. AND, Sitecore will not point you in the right direction of getting around this other than a round-about way (google Sitecore dynamic linking and configuration) because they want you to use their Sitecore OMS download tracking capability. And that's it! Two days of research led me to this conclusion.

回答1:

You could use this script to track download events via Google Analytics.

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;
                                }
                            });
                        }
                    }
                });
            });
        }

Just add in the required file types here at this line -

var filetypes = /.(zip|pdf|doc*|xls*|ppt*|jpg|ashx)$/i;



回答2:

Having done a quick google for gatags.js, I can see that you can add an extension to the whitelist on line 24:

var isDoc = path.match(/\.(?:doc|eps|jpg|png|svg|xls|ppt|pdf|xls|zip|txt|vsd|vxd|js|css|rar|exe|wma|mov|avi|wmv|mp3)($|\&|\?)/);

Change it to:

var isDoc = path.match(/\.(?:ashx|doc|eps|jpg|png|svg|xls|ppt|pdf|xls|zip|txt|vsd|vxd|js|css|rar|exe|wma|mov|avi|wmv|mp3)($|\&|\?)/);

Alternatively, you could attach the Google Analytics _trackEvent yourself with a dom selector and a click event.

Either way, I think OMS can track media library files regardless of extension - removing the default ashx extension doesn't stop the file being handled by Sitecore.



回答3:

So the solution turns out to be simple but sadly, not retroactive, meaning all files previously uploaded to the Media Library in the Sitecore content tree will continue to use the ashx extension unless you reupload the image.

Not sure where you got this information, but it's incorrect. You can blank out the Media.RequestExtension setting and all existing files will use their original extension. In IIS7 Integrated Mode, you should be able to make this change without having to make other server configuration changes.

Edit: More Info

If you analyze Sitecore.Configuration.Settings.Media.RequestExtension (the API equivalent to this settings) in a decompiler, you can see that it's only used by the MediaProvider when constructing the Media URL. Sitecore should remember the original extension of the media and can serve it with its original URL, regardless of what this setting was when it was uploaded. That's my experience, anyway, and it seems to be validated by looking into Sitecore.Kernel.