Skip to content

Instantly share code, notes, and snippets.

@peternixey
Created June 28, 2012 11:24
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save peternixey/3010759 to your computer and use it in GitHub Desktop.
Save peternixey/3010759 to your computer and use it in GitHub Desktop.
Google Analytics bookmark to show just the last X days of data
javascript: function analyticsURL(){ var properties = { profileID: 'EDIT__YOUR_ANALYTICS_REPORT_ID', daysToShow: EDIT__NUMBER_OF_DAYS_TO_SHOW,includeToday: EDIT__INCLUDE_TODAY_TRUE_OR_FALSE, report_url: 'EDIT__URL_OF_REPORT_YOU_WANT_TO_SHOW' }; function stringify(date){ return date.getFullYear().toString() + pad(date.getMonth() + 1) + pad(date.getDate()); }; function pad(numeral){ return numeral<10 ? '0' + numeral.toString() : numeral.toString(); }; void(properties.report_url = properties.report_url.replace(/([^/])$/, '$1/')); var endDate = new Date(); var startDate = new Date(); properties.includeToday ? false : endDate.setDate( endDate.getDate()-1 ); startDate.setDate( endDate.getDate() - properties.daysToShow + 1 ); return properties.report_url + properties.profileID + '/%3F_.date00%3D' + stringify(startDate) + '%26_.date01%3D' + stringify(endDate) + '/'; }; window.location = analyticsURL();
// INSTRUCTIONS
// 1. In the URL above, change the following four variables at the start
// of the URL to the ones appropriate for you:
//
// EDIT__YOUR_ANALYTICS_REPORT_ID
// EDIT__NUMBER_OF_DAYS_TO_SHOW
// EDIT__INCLUDE_TODAY_TRUE_OR_FALSE
// EDIT__URL_OF_REPORT_YOU_WANT_TO_SHOW
//
// What do each of these actually refer to?
//
// EDIT__YOUR_ANALYTICS_REPORT_ID (string)
// This is the ID of your analytics account and
// will look something like a845538w4920063p60424739.
// Note that this is different from your GA ID and can be found
// by looking at the URL of your analytics reports
// e.g. for
// https://www.google.com/analytics/web/#report/visitors-browser/a845538w4920063p60424739/%3F_.date00%3D20120620%26_.date01%3D20120627/
// the appropriate ID would be a845538w4920063p60424739
// EDIT__NUMBER_OF_DAYS_TO_SHOW (integer)
// How many days back you want the report to go (e.g. 7 for a week)
// EDIT__INCLUDE_TODAY_TRUE_OR_FALSE (bool)
// Do you want the report to end with yesterday or today? Enter true for
// tody and false for yesterday
// EDIT__URL_OF_REPORT_YOU_WANT_TO_SHOW (string)
// The URL of the GA report you want to see. You can create different
// bookmarklets for different reports.
// e.g. https://www.google.com/analytics/web/#report/visitors-browser/
// or https://www.google.com/analytics/web/#dashboard/default/
//
// A completed bookmarklet should look like the one below:
// javascript: function analyticsURL(){ var properties = { profileID: 'a845538w4920063p60424739', daysToShow: 7,includeToday: false,report_url: 'https://www.google.com/analytics/web/#report/visitors-browser' }; function stringify(date){ return date.getFullYear().toString() + pad(date.getMonth() + 1) + pad(date.getDate()); }; function pad(numeral){ return numeral<10 ? '0' + numeral.toString() : numeral.toString(); }; void(properties.report_url = properties.report_url.replace(/([^/])$/, '$1/')); var endDate = new Date(); var startDate = new Date(); properties.includeToday ? false : endDate.setDate( endDate.getDate()-1 ); startDate.setDate( endDate.getDate() - properties.daysToShow + 1 ); return properties.report_url + properties.profileID + '/%3F_.date00%3D' + stringify(startDate) + '%26_.date01%3D' + stringify(endDate) + '/'; }; window.location = analyticsURL();
//
// 2. Open your bookmarks manager and add a new bookmark
// 3. Copy across the bookmarklet you've made and make sure that it starts
// with 'javascript: '. Some browsers (e.g. Chrome) strip this for security
// reasons but make sure it's there or it'll stop the bookmarklet working
// --------------------------------------
// RAW CODE
// (you don't need to see this but it's here if you want to play with it)
javascript: function analyticsURL(){
var properties = {
profileID: 'a845538w4920063p60424739',
daysToShow: 7,
includeToday: false,
report_url: 'https://www.google.com/analytics/web/#report/visitors-browser'
};
function stringify(date){
return date.getFullYear().toString() + pad(date.getMonth() + 1) + pad(date.getDate());
};
function pad(numeral){
return numeral<10 ? '0' + numeral.toString() : numeral.toString();
};
void(properties.report_url = properties.report_url.replace(/([^/])$/, '$1/'));
var endDate = new Date();
var startDate = new Date();
properties.includeToday ? false : endDate.setDate( endDate.getDate()-1 );
startDate.setDate( endDate.getDate() - properties.daysToShow + 1 );
return properties.report_url + properties.profileID + '/%3F_.date00%3D' + stringify(startDate) + '%26_.date01%3D' + stringify(endDate) + '/';
}; window.location = analyticsURL();
// --------------------------------------
@udondan
Copy link

udondan commented Jun 24, 2015

Awesome! Thanks for sharing 👍

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