Friday, April 20, 2012

Distilled bookmarklet modified to support GA Custom URLs

Tom Critchlow from Distilled released a bookmarklet this week that lets you instantly access the traffic stats in GA for whatever page on which you are navigating (assuming you have permission in GA of course).  It works perfectly on any site with GA so long as it does not use custom URL in the _trackPageview command (well over 90% of sites).

However, for sites like ours, we were out of luck because we always use custom URLs to give us finer tuned reporting and allows us to leave URLs optimized (or at least semi-optimized with what our CMS will allow) while having detailed and optimized data for reporting and analysis.

I have modified the script to work with sites that use custom URLs for tracking in GA as well as for standard implementations.

javascript:location.href="https://www.google.com/analytics/web/#report/content-pages/INSERT-CODE-HERE/%3F_r.drilldown%3Danalytics.pagePath"+encodeURIComponent(":"+ (typeof _gat.N[0].F == "undefined" ? location.pathname : _gat.N[0].F))+"%26explorer-table.secSegmentId%3Danalytics.source/";

The key change is to substitute location.pathname from the original script and replace it with a block that checks for any custom URLs stored in GA for that page

(typeof _gat.N[0].F == "undefined" ? location.pathname : _gat.N[0].F)

The only caveat is that you are still out of luck if your use inbound filters to transform your URLs inside GA. Unfortunately, there is no easy solution other then creating a regex yourself to apply the same transformation to the URL.

Instructions for making this work on your site:
1) Go to Tom's post
2) Enter your code from GA as he mentions it in his post
3) Drag the bookmarklet to your toolbar
4) Right click the bookmarklet and edit the script by changing:
location.pathname
to
(typeof _gat.N[0].F == "undefined" ? location.pathname : _gat.N[0].F)

Change the part that says location.pathname to (typeof _gat.N[0].F == "undefined" ? location.pathname : _gat.N[0].F)
5) Click OK and you are good to go

No comments:

Post a Comment