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
Great job! Thanks! Just what I needed to fix the only annoying thing in Analytics.
Hi – I just installed GM and this script. It my first use of GM. I have the URI in the control panel for GM for this script but nothing happens when I access a report. I see the usual 30 day default date range. What do I need to double check? Thanks!
This has already been mentioned in Google’s Analytic blog and will be “rolled out soon”
thanks great work :)
hi thanks for this.
made a small fix…
if(window.location.href.indexOf(‘pdr=’) < 0){
var seperator = window.location.href.indexOf('&') < 0 ? '?' : '&';
window.location.href += seperator + 'pdr=' + newDateRange;
}
Hi Adam
This is a great script for something that Google should have fixed a long time ago. Unfortunately it does not work with the new Google Analytics anymore. I love the new Dashboard, but I hate that I have to change the date range each day. Do you have a script that works with the new version too? :-)