admin管理员组

文章数量:1327824

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: ;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?

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./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?

Share Improve this question edited Sep 30, 2011 at 16:52 Yahel 37.3k23 gold badges106 silver badges154 bronze badges asked Sep 22, 2011 at 2:48 RobHardgoodRobHardgood 5501 gold badge8 silver badges17 bronze badges
Add a ment  | 

5 Answers 5

Reset to default 4

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./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./analytics/reporting/dashboard?id=XXXXXXX&pdr="+b+"-"+b})();

JavaScript has date methods that can individually give the parts of a date, but .toISOString() might perhaps work for your application most concisely. Very little string manipulation would have to be performed on the result to get the UTC date in the correct format. For example:

javascript:
d = new Date().toISOString().slice(0, 10).replace(/-/g, '');
location = "https://www.google./analytics/reporting/dashboard?id=XXXXXXX&pdr="
           + d + "-" + d;

To build on the answer by @PleaseStand - Firefox even has a non-standard Date.toDateLocale() function. So the whole thing can be simplified even further:

javascript:void(location.href = "https://www.google./analytics/reporting/dashboard?id=XXXXXXX&pdr=" + new Date().toLocaleFormat("%Y%m%d-%Y%m%d"))

  1. Customize this code below with your Google Analytics ID, timezone offset, and other desired parameters.

  2. Use a site like this to convert JavaScript to a bookmarklet.

  3. Rejoice.

    var gaID = YOUR_ID; var hourOffset = 8; var rowCount = 50;

    var utcDate = new Date(new Date().toUTCString()); utcDate.setHours(utcDate.getHours() - hourOffset); var localDate = new Date(utcDate); var todayDate = localDate.toISOString().split("T")[0].replace(/-/g, "");

    location.href = "https://analytics.google./analytics/web/#/report/advertising-adwords-keywords/" + gaID +"/_u.date00=" + todayDate + "&_u.date01=" + todayDate + "&explorer-table.plotKeys=%5B%5D&explorer-table.rowCount=" + rowCount;

I know this is a really old thread, but it is still relevant.

I found that if you set up your report using todays date, then edit the URL and use a future date, it will default to the current date's statistics.

For example, I use this:

https://analytics.google./analytics/web/#/dashboard/xxxxxxxxxxxxxxxx/xxxxxxxxxxxxxxxx/_u.date00=20221130&_u.date01=20221130/

The date codes are both the same date 2 years in the future. This causes analytics to use show the current dates stats.

本文标签: javascriptBookmark with date in the URLStack Overflow