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
Pingback: A Guide to Google Analytics and Useful Tools | Developer's Toolbox | Smashing Magazine
Pingback: A Guide to Google Analytics and Useful Tools « Tech7.Net
Pingback: U.S. Art Studios Inc. » Blog Archive - A Guide to Google Analytics and Useful Tools - U.S. Art Studios Inc. . A full firm advertising agency firm specialized in motion picture, photography, graphic design, printing and web design.
Pingback: A Guide to Google Analytics and Useful Tools | Design Visibility
Pingback: A Guide to Google Analytics and Useful Tools | Desinine
Pingback: A Guide to Google Analytics and Useful Tools | Brainless Web
Pingback: A Guide to Google Analytics and Useful Tools | VNAMEDIA Sharing Center
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”
Pingback: A Guide to Google Analytics and Useful Tools « Vibbit Social Networking and Website Design for Under 89.00
Pingback: A Guide to Google Analytics and Useful Tools by Oshoamy
Pingback: Six Digit Media | A Guide to Google Analytics and Useful Tools | Cincinnati's Premier Web Design and Development Company - We are the Bottom Line in Online Marketing
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;
}