ReedyBear's Blog

Sort Bear Analytics by Toast Count

Bear Analytics are sorted by the number of page views within the selected timeframe, with no sorting options available.

So I wrote a little script that sorts by toast count. I wanted to make this a plugin you install via your BearBlog Dashboard, but plugins do not load on the analytics page. SO, to use this script, you'll make a bookmark in your browser. (see below)

Default Sorting
List of post titles with their toast count & read count, screenshot from the analytics page, in order by read count

Sorted by Toast
List of post titles with their toast count & read count, screenshot from the analytics page, in order by toast count

How To

To use this plugin:

  1. Copy the code below
  2. Create a new bookmark in your browser
  3. In the 'URL' field, PASTE the code

Now, visit your analytics page, and when you click that bookmark, it will run this little script and sort your analytics. When you refresh the page, it'll go back to normal.

The Code:

javascript:  

/* make sure we're on the analytics page */  
if (window.location.href.includes("/analytics/")){  

    /* search through <h3> nodes to find 'Pages' header. */  
    h3s = document.querySelectorAll('h3');  
    ul = null;  
    for (h3 of h3s){  
        if (h3.innerText.trim() == 'Pages'){  
            /* Get the post list's container node */  
            ul = h3.parentNode.querySelector('ul');  
            break;  
        }  
    }  

    /* Loop over every analytics entry.  
       Get the toast count of each entry.  
       Create an array which we will use for sorting. */  
    lis = ul.querySelectorAll('li');  
    sortables = [];  
    for (li of lis){  
        small = li.querySelector('small');  
        if (small == null){  
            /* there is no toast count for this entry */  
            sortables.push([-1, li]);  
        } else {  
            /* extract the NUMBER from the toast count text */  
            num = small.innerText.match(/[0-9]+/)[0];  
            sortables.push([num, li]);  
        }  
        /* Remove the list item, because we will re-add it after sorting */  
        ul.removeChild(li);  
    }  

    /* Sort the list items by their toast counts */  
    sorted = sortables.sort(  
        function(a, b){  
            return b[0]-a[0];  
        }  
    );  

    /* re-add all list items in the new order */  
    for (item of sorted){  
        ul.appendChild(item[1]);  
    }  
}  

Bearblog Tips/Tools

#bearblog-featured