public
Last active

Google Analytics bookmark to show just the last X days of data

  • Download Gist
ga_last_x_days.js
JavaScript
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70
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();
 
 
// --------------------------------------

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.