Skip to content

Instantly share code, notes, and snippets.

@epintos
Last active January 22, 2023 10:39
Show Gist options
  • Star 24 You must be signed in to star a gist
  • Fork 5 You must be signed in to fork a gist
  • Save epintos/4bea8d70c9f52a47170f8cd66ddecfa1 to your computer and use it in GitHub Desktop.
Save epintos/4bea8d70c9f52a47170f8cd66ddecfa1 to your computer and use it in GitHub Desktop.
Export Medium Stats Stores to CSV
// Run Inspector Console in chrome and copy and paste the following code in the /stats/stories view
function download(filename, text) {
var pom = document.createElement('a');
pom.setAttribute('href', 'data:text/plain;charset=utf-8,' + encodeURIComponent(text));
pom.setAttribute('download', filename);
if (document.createEvent) {
var event = document.createEvent('MouseEvents');
event.initEvent('click', true, true);
pom.dispatchEvent(event);
}
else {
pom.click();
}
}
content = "";
rows = document.querySelectorAll(".sortableTable-row.js-statsTableRow")
rows.forEach(function(row) {
title = row.querySelectorAll(".sortableTable-title > a")[0].innerText
values = row.querySelectorAll(".sortableTable-value")
views = values[1].innerText
reads = values[2].innerText
readRatio = values[3].innerText
recommends = values[4].innerText
content += title + ";" + views + ";" + reads + ";" + readRatio + ";" + recommends + "\n"
});
download("medium-metrics-" + new Date().toISOString().slice(0, 10) + ".csv", content)
@k3sava
Copy link

k3sava commented Apr 6, 2018

Thank you sharing this. It works perfectly!

PS. Is there any way to export the day each story was posted, and add in referral traffic for views?

@jenni95
Copy link

jenni95 commented Sep 25, 2018

This doesn't seem to be working anymore, but it looks like Medium has made updates to their stats recently. Is there an update available?
Thanks,

@kristofferso
Copy link

Yes, Medium changed some of the structure on their stats page. If you remove "h2" from line 21 in the code, it still works though

@polleyg
Copy link

polleyg commented May 22, 2019

Unfortunately, it doesn't seem to be working even when you remove the "h2":

VM85:19 Uncaught TypeError: Cannot read property 'innerText' of undefined
    at <anonymous>:19:57
    at NodeList.forEach (<anonymous>)
    at <anonymous>:18:6

@mbianchihr
Copy link

You just need to change the row 21 to following:
title = row.querySelectorAll(".sortableTable-title > a")[0].innerText

Enjoy!

@JoshCS
Copy link

JoshCS commented Jul 12, 2019

Thanks, works great! This is really appreciated by me and the folks I work with.

One note: If a story title includes a comma, it will split the content before the comma into a separate cell from the rest of the title.

@mbianchihr
Copy link

The script should not affect any commas (,) but rather semicolon (;) make sure when importing the CSV you use a semicolon (;) as a delimiter and have no semicolons (;) in your titles

@jdgemm
Copy link

jdgemm commented Jan 10, 2020

Many thanks for providing. Is there any way to grab additional views (smaller numbers)?

@ogyalcin
Copy link

Hey Marko,

Great work! This script is exactly what I am looking for, but I keep downloading empty CSV files. I believe Medium updated the class names. Any chance you might tell me how I can update the script?

Cheers,
Orhan

@epintos
Copy link
Author

epintos commented Oct 27, 2020

Hi @ogyalcin. I'm sorry but I don't have access to the account I was using anymore. I am not sure if the HTML and CSS changed, but if you use the Chrome Dev Inspector you might be able to fix it with a few modifications :)

@mbianchihr
Copy link

Howdy,

Same with me I don't use Medium anymore so can't really assist, apologies.

Take care!

@ogyalcin
Copy link

Thanks for the quick reply guys. I really appreciate it.

I am gonna ask a follow-up, hoping you remember the document structure:)

I see that you extract the data based on the class names with querySelectorAll(), and then, parse them. But, I could not find the div where the list of daily counts is stored. Is my assumption correct? Was it stored under a single div within the main document object?

@epintos
Copy link
Author

epintos commented Oct 27, 2020

Hey @ogyalcin, I am sorry but I can't remember the HTML structure. It looks that I am doing exactly that in the script.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment