May 14

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

Recommended

Greasemonkey Hacks
Greasemonkey HacksAuthor: Mark PilgrimRating: Rating: 4
Winning Results with Google AdWords
Winning Results with Google AdWordsAuthor: Andrew GoodmanRating: Rating: 4

14 Responses to “Include Today in the Default Google Analytics Date Range”

  1. By A Guide to Google Analytics and Useful Tools | Developer's Toolbox | Smashing Magazine on July 16th, 2009 at 1:29 pm

    [...] Include Today in the Default Google Analytics Date RangeBy default, Google Analytics shows you the statistics for a 30 day period up to the previous complete day. This Greasemonkey script includes the current in all reports. [...]

  2. By A Guide to Google Analytics and Useful Tools « Tech7.Net on July 16th, 2009 at 6:32 pm

    [...] Include Today in the Default Google Analytics Date RangeBy default, Google Analytics shows you the statistics for a 30 day period up to the previous complete day. This Greasemonkey script includes the current in all reports. [...]

  3. By 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. on July 16th, 2009 at 11:05 pm

    [...] Include Today in the Default Google Analytics Date RangeBy default, Google Analytics shows you the statistics for a 30 day period up to the previous complete day. This Greasemonkey script includes the current in all reports. [...]

  4. By A Guide to Google Analytics and Useful Tools | Design Visibility on July 18th, 2009 at 1:03 am

    [...] Include Today in the Default Google Analytics Date RangeBy default, Google Analytics shows you the statistics for a 30 day period up to the previous complete day. This Greasemonkey script includes the current in all reports. [...]

  5. By A Guide to Google Analytics and Useful Tools | Desinine on July 18th, 2009 at 10:09 pm

    [...] Include Today in the Default Google Analytics Date RangeBy default, Google Analytics shows you the statistics for a 30 day period up to the previous complete day. This Greasemonkey script includes the current in all reports. [...]

  6. By A Guide to Google Analytics and Useful Tools | Brainless Web on July 24th, 2009 at 4:48 pm

    [...] Include Today in the Default Google Analytics Date Range By default, Google Analytics shows you the statistics for a 30 day period up to the previous complete day. This Greasemonkey script includes the current in all reports. [...]

  7. By A Guide to Google Analytics and Useful Tools | VNAMEDIA Sharing Center on August 2nd, 2009 at 1:18 pm

    [...] Include Today in the Default Google Analytics Date Range By default, Google Analytics shows you the statistics for a 30 day period up to the previous complete day. This Greasemonkey script includes the current in all reports. [...]

  8. By Ricardo Ribeiro on September 5th, 2009 at 12:41 am

    Great job! Thanks! Just what I needed to fix the only annoying thing in Analytics.

  9. By douglas on September 15th, 2009 at 8:20 pm

    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!

  10. By P11D on September 25th, 2009 at 1:53 am

    This has already been mentioned in Google’s Analytic blog and will be “rolled out soon”

  11. By A Guide to Google Analytics and Useful Tools « Vibbit Social Networking and Website Design for Under 89.00 on November 18th, 2009 at 4:18 am

    [...] Include Today in the Default Google Analytics Date Range By default, Google Analytics shows you the statistics for a 30 day period up to the previous complete day. This Greasemonkey script includes the current in all reports. [...]

  12. By   A Guide to Google Analytics and Useful Tools by Oshoamy on December 3rd, 2009 at 8:38 am

    [...] Include Today in the Default Google Analytics Date Range By default, Google Analytics shows you the statistics for a 30 day period up to the previous complete day. This Greasemonkey script includes the current in all reports. [...]

  13. By 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 on December 3rd, 2009 at 3:22 pm

    [...] Include Today in the Default Google Analytics Date Range By default, Google Analytics shows you the statistics for a 30 day period up to the previous complete day. This Greasemonkey script includes the current in all reports. [...]

  14. By A Guide to Google Analytics and Useful Tools | WebsGeek on February 27th, 2010 at 1:38 am

    [...] Include Today in the Default Google Analytics Date Range By default, Google Analytics shows you the statistics for a 30 day period up to the previous complete day. This Greasemonkey script includes the current in all reports. [...]

Leave a Reply