Skip to content

Instantly share code, notes, and snippets.

@jibiel
Created September 28, 2012 08:47
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jibiel/3798708 to your computer and use it in GitHub Desktop.
Save jibiel/3798708 to your computer and use it in GitHub Desktop.
Include today in Google Analytics' default date range (updated for 2012)
// ==UserScript==
// @name Google Analytics - Include Today
// @author Storm Consultancy & jibiel
// @description Include today in Google Analytics' default date range
// @include https://www.google.com/analytics/web/*
// @version 1.1
// ==/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(){
// This regex may have to be changed in the future alongside with Analytics update
var re = /^https:\/\/www\.google\.com\/analytics\/web\/.*#report\/[a-z-]+\/[a-z0-9]{27}\/$/;
// Expand date range only on _report_ pages and only if custom date range hasn't been already set
if (re.test(window.location.href)) {
var dateToday = new Date();
var today = BuildDates(dateToday);
var past = BuildDates(addDays(dateToday, -31));
var newDateRange = '%3F_.date00%3D' + past['year'] + past['month'] + past['day'] +
'%26_.date01%3D' + today['year'] + today['month'] + today['day'];
window.location.href += newDateRange;
}
}
// 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;
}
window.onhashchange = function () {
Redirect();
}
Redirect();
@jibiel
Copy link
Author

jibiel commented Sep 28, 2012

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment