Search code examples
javascriptfirefoxgoogle-analyticsbookmarks

Bookmark with date in the URL


I use Google Analytics, and the first thing I want to check every day is the current day's statistics. But there's no way to bookmark the current day. The URL for any given day is: https://www.google.com/analytics/reporting/dashboard?id=XXXXXXX&pdr=20110921-20110921

You can see a date range at the end of the URL. 20110921 meaning 9-21, 2011.

How can I write a little javascript bookmarklet for Firefox to change the URL to the current date depending on what day I click on it?


Solution

  • Try this - it uses a Date object to get the date:

    var date = new Date();
    var str = "";
    str += date.getFullYear();
    str += pad2(date.getMonth() + 1);
    str += pad2(date.getDate());
    function pad2(n) {
       var str = String(n);
       if(str.length < 2)
        str = "0" + str;
       return str;
    }
    location.href = "https://www.google.com/analytics/reporting/dashboard?id=XXXXXXX&pdr="+str+"-"+str;
    

    Bookmarklet:

    javascript:(function(){function d(a){a=String(a);a.length<2&&(a="0"+a);return a}var c=new Date,b="";b+=c.getFullYear();b+=d(c.getMonth()+1);b+=d(c.getDate());location.href="https://www.google.com/analytics/reporting/dashboard?id=XXXXXXX&pdr="+b+"-"+b})();