Classic Stats Redirect script v2.1.0 released!

v2.1.0 of my classic stats redirect script has been released! As I had mentioned in my previous release post, v2.1 of this script features support for redirecting when there is no blog domain in the URL. This is the case for /stats, /stats/insights, /stats/day, and the others stats types. Unfortunately, due to the nature of the solution, the page must load first before you are redirected. Fortunately, though, this probably isn’t a very common case.

The script was updated earlier today, so you may have already received the update if you have auto-updating enabled for the script. Otherwise, you can install the new version manually from Greasy Fork. Leave a comment if you experience any issues or have any questions.

Technical details follow.

I knew right away when I made v2 of the script that in order to support redirecting without a blog domain, I’d have to scrape the page for the link because getting the user’s default blog requires an authenticated API call.

After some observations, I determined that /stats/day and the other time-based stats types has a link to the stats of the default blog first in the list of blogs. The header of the card (the while box) contained the link, so all I had to do was find a selector that would refer to that element. I came up with /stats was also covered because it shows the same page as /stats/day.

/stats/insights was a little trickier because there was no link. Fortunately, the blog domain was also listed in the list of sites, so I figured the best approach was to scrape the domain and use it to construct an insights URL, treat that as the page’s URL, and do the rest of the redirect as usual. The selector I found was .stats-tab a.

To avoid code duplication, I decided to move the main sequence to a function that takes in a URI on which the redirect will be based. That way, I can pass the URL I get from scraping to the function and get it parsed to perform the redirect. This avoids code duplication because I can pass the page’s URL to that function at the beginning instead of relying on global variables. The code snippet below shows the result of the refactor:

function doRedirect(uri) {
  var parsedUri = uri.match(/stats(?:\/(insights|day|week|month|year))?(?:\/([^\/]*))?/);
  var statsType = parsedUri[1];
  var blogDomain = parsedUri[2];

  if (blogDomain) {
    // Redirect to post URL based on API results
    // API docs:
    fetchJSONFile("" + blogDomain,
      // attempt to redirect using API
      function(data) {
        redirectToClassicStats(data.URL, statsType);

      // fallback: attempt to use the blog domain
      function() {
        // use http instead of https in case the server doesn't support https
        // (e.g. for Jetpack sites)
        redirectToClassicStats('http://' + blogDomain, statsType);
  } else if (statsType != "insights") {
    window.onload = function() {
      // the first blog listed is the user's default blog
      var defaultBlogStatsUrl = document.querySelector("").href;
  } else {
    window.onload = function() {
      // insights page; get the domain and construct an insights URI
      blogDomain = document.querySelector(".stats-tab a").href
      doRedirect("/stats/insights/" + blogDomain);


With that, I think I’ve covered most of the cases. I think there’s the case where there’s a range associated with the time-based stats type, but you would only be able to get to that through the new interface itself, so I don’t think it’s an issue.

Again, please leave a comment if you have any questions or experience any issues.

  1. Dennis said:

    This time I got the 2.1 update automatically via Greasemonkey. Not sure why I had to do it manually with 2.0 before. Can’t add anything else for now, works for me and I am already got used to the classic-classic-page :) Thanks!

    • You’re welcome! It’s good to hear it works.

      For the record, I just remembered that you can force an update by going to your list of scripts and clicking “Find Updates” in the script’s right-click menu. I couldn’t tell you why you had to do it manually for v2.0, especially since my post went up more than a day since I pushed the code, but whatever, as long as it works now.

  2. I got 2.1 working with just a click or two (pure dumb luck because I’ve forgotten what little I once knew about TamperMonkey. ‘Fraid I’ve no idea what the above is about, but I’m happy with what I have. Thanks!

    And best wishes for a great new year.

    • You’re welcome, and best wishes for the new year as well!

      As you’ve experienced, using user scripts is kind of like driving a car: as long as you know how to use it, you don’t need to know it works internally. It’s there, though, for anyone who wants to learn from it.

`$name' says...

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: