Stoyan Stefanov

Personal blog


O’Reilly sales data summing

Hello fellow O'Reilly author, and congrats on finishing your book, I know it wasn't easy! 🙂

(If the above means nothing to you, my dear reader, move on, shut down the computer and maybe go outside)

Believe it or not, the O'Reilly site has a page that shows your sales data. Pretty graphs too. What it doesn't have is the total of how many books you've sold in all of the time.

Worry not my sisters and my brothers.

Pop up the console, type data and see what happens - the data that powers the graphics is there. As many titles you have, as many data!@#$% variables pop up from your browser console's autocomplete. In my case - 4 books, 4 datas.


Exploring the data...


As you see - two arrays: digital and print data. X is timestamp, Y is number of books sold. All you do is sum up all Y values.

Except there are duplicates in the data. So making sure that only one Y is counted for each X timestamp does the trick.

And here's a little something I came up with, feel free to improve:

var map = {}; // to de-dup
data57350[0] // digital sales for React: Up and Running
  .values // it's all there
  .filter(v => v.x in map ? false : map[v.x] = 1) // rm dupes
  .reduce((prev, next) => prev + next.y, 0) // totals

One liner so you can paste....

var map = {}; data57350[0].values.filter(v => v.x in map ? false : map[v.x] = 1).reduce((prev, next) => prev + next.y, 0)

Now change [0] to [1] for the print data. When change the data*XYZ*.

Not the whole story

Keep in mind:

  1. What it says right there on the site: "Please Note: This data is from major retailers including Amazon, Barnes and Noble, and Royalties are not calculated on the basis of these sales."
  2. Also the data goes only 2 years back (see the graphs)

Better way/whole picture?

There must be a better way. I just don't know it. Oh wait, I can ask somebody at O'Reilly 🙂

Take care now, bye-bye then!

(Now go back to working on your next title, you slacker, you!)

Leave a Reply

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

Powered by WordPress, theme by ♣, based on Kubrik and Sancta Simplicitas