By default, Google Analytics shows you the statistics for a 30 day period up to the previous complete day. For example, today is the 14th of May and Analytics is showing me stats for 14th April -> 13th May. Now, I don’t know about you, but I’m a bit of a stats junky. I like to know what’s going on. As a result, the first thing I do when I load up Google Analytics, is to open the date range picker and change it so that today’s stats are included. This is a somewhat tiresome and repetitive task. Currently, Google offers no way to set a custom date range, so I have developed a Greasemonkey script to automatically change the date range to include today.
The script will run when you enter a Google Analytics report and redirect you to the new date range, provided no custom date range has been selected. This is achieved by appending the pdr querystring parameter to the URL. Here’s the code:
// ==UserScript== // @name Google Analytics - Include Today // @author Storm Consultancy // @description Include today in Google Analytic's default date // @include https://www.google.com/analytics/reporting* // ==/UserScript== // Function to add days to a date, use negative number to subtract function addDays(myDate,days) { return new Date(myDate.getTime() + days*24*60*60*1000); } // Redirect the user to the new URL function Redirect(newDateRange){ //Only inject the new param is it's not in the querystring already if(window.location.href.indexOf('&pdr') < 0){ window.location.href += '&pdr=' + newDateRange; } else if(document.referrer.indexOf('google.com/analytics/settings/') >= 0){ // If the referrer is the main page, then it already sets the date // range we dont want. so we need to replace it var url = window.location.href.replace(/pdr=[0-9]{8}\-[0-9]{8}/, 'pdr=' + newDateRange) window.location.href = url; } } // Build an array of the date components, formatted for the querystring function BuildDates(date){ var array = new Array(); array['day'] = (date.getDate() < 10) ? '0' + date.getDate().toString() : date.getDate().toString(); array['month'] = (date.getMonth() < 9) ? '0' + (date.getMonth()+1).toString() : (date.getMonth()+1).toString(); array['year'] = date.getFullYear().toString(); return array; } var dateToday = new Date(); var today = BuildDates(dateToday); var past = BuildDates(addDays(dateToday, -30)); var dateRange = past['year'] + past['month'] + past['day'] + '-' + today['year'] + today['month'] + today['day']; Redirect(dateRange);
You can download the user script from here: google_analytics_date_range.user.js