Skip to content

Instantly share code, notes, and snippets.

@epintos
Created January 20, 2017 20:21
Show Gist options
  • Save epintos/a30876b1ce61bf5fafbd884e024b3361 to your computer and use it in GitHub Desktop.
Save epintos/a30876b1ce61bf5fafbd884e024b3361 to your computer and use it in GitHub Desktop.
Export Medium Overview Stats to CSV
// Max value of the Minutes Read Graph
minutesReadMaxAxis = 810
// Max value of the Views Graph
viewsMaxAxis = 431
// Max value of the Visitors Graph
visitorsMaxAxis = 344
// First date of the graph (US Format)
initialDay = new Date('12/22/2016')
// Days in the graphs
days = 30;
// Replaces Jquery
s=document.createElement('script');s.src='https://ajax.googleapis.com/ajax/libs/jquery/1.8/jquery.js';(document.head||d.documentElement).appendChild(s)
// Returns max value of an array
function max(array) {
return Math.max.apply(Math, array);
}
// Downloads a string with a certain filename
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();
}
}
// Generates CSV format string with dates and values
function generateContent(values, title, element) {
content = title + ";" + $(element).text() + "\n"
for(var i=0; i < days; i++){
content += initialDay.addDays(i).toLocaleDateString("en-US") + ";" + values[i] + "\n"
}
return content
}
// Calculates the maximum height in a graph
function getGraphMaxHeight(graph) {
return max($.map($(graph).find("rect"), (rect)=> +$(rect).attr("height")))
}
Date.prototype.addDays = function(days)
{
var dat = new Date(this.valueOf());
dat.setDate(dat.getDate() + days);
return dat;
}
bars = $(".js-barGraphBars");
minutesReadMaxValue = getGraphMaxHeight(bars[0]);
viewsMaxValue = getGraphMaxHeight(bars[1]);
visitorsMaxValue = getGraphMaxHeight(bars[2]);
minutesReadHeightConvertor = minutesReadMaxAxis / minutesReadMaxValue
viewsHeightConvertor = viewsMaxAxis / viewsMaxValue
visitorsHeightConvertor = visitorsMaxAxis / visitorsMaxValue
minutesReadValues = []
$(bars[0]).find(".barGraph2-bar").each((index, bar) => {
height = $(bar).attr("height");
minutesReadValues.push(height * minutesReadHeightConvertor);
});
viewsValues = []
$(bars[1]).find(".barGraph2-bar").each((index, bar) => {
height = $(bar).attr("height");
viewsValues.push(height * viewsHeightConvertor);
});
visitorsValues = []
$(bars[2]).find(".barGraph2-bar").each((index, bar) => {
height = $(bar).attr("height");
visitorsValues.push(height * visitorsHeightConvertor);
});
content = ""
content += generateContent(minutesReadValues, "MinutesRead", $(".js-readingTimeStatsText h2"));
content += generateContent(viewsValues, "Views", $(".js-viewsStatsText h2"));
content += generateContent(visitorsValues, "Visitors", $(".js-visitorsStatsText h2"));
download("medium-overview-metrics-" + new Date().toISOString().slice(0, 10) + ".csv", content)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment