loading...

Pulling your DEV Organization Stats into a Spreadsheet

andrewbrown profile image Andrew Brown 🇨🇦 ・2 min read

If you want to pull your stats for a personal account for DEV it's easy to do so since DEV has an API and there is already this great tutorial on how to do so, but how do you do the same if you're a DEV Organization?

There is no API at the Organization level but we can still get our data through a simple scraper which I wrote. All you have to do is navigate to your Organization's Dashboard.

Your Dashboard is not the same as your public-facing page, just make sure you're on the page that looks like this:

https://dev.to/dashboard/organization/ORG_ID>
  1. Open Chrome
  2. Go to your Organization's Dashboard
  3. Open up Chrome Developer Tools
  4. Paste in the code below and it will automatically download a CSV for all visible stats.
  5. Enjoy reviewing your data
function articles(){
    const results = []
    const articles = document.querySelectorAll('.single-article')
    let data;
    for(let i = 0; i < articles.length; i++){
        data = article(articles[i])
        if (data) {
            results.push(data)
        }
    }
    return results
}

function article(article){
    // if there is no time selector that means this article 
    // is not published and will have no useful stats
    if (article.querySelector('time')){
        const name = article.querySelector('h2').innerText
        const tags = article.querySelectorAll('.tag')
        const author = article.querySelector('option[selected=selected]').innerText
        const date = article.querySelector('time').innerText
        const page_view_count = article.querySelector('.page-views-count').innerText
        const reactions_count = article.querySelector('.reactions-count').innerText
        const comments_count = article.querySelector('.comments-count').innerText
        const tags_string = []  
        for(let t = 0; t < tags.length; t++){
            tags_string.push(tags[t].innerText)
        }   
        return [
            name,
            tags_string.join(' '),
            author,
            date,
            page_view_count,
            reactions_count,
            comments_count
        ]
    } else {
        return false        
    }
}

function save_data(){
    const results = articles()
    results.unshift(['Name','Tags','Author','Date','Page Views Count','Reactions Count','Comments Count'])

    const csv_string = []
    for(let i = 0; i < results.length; i++){
        csv_string.push(results[i].join(','))
    }

  var blob = new Blob([csv_string.join("\n")], {type: 'text/csv'})
  let e    = document.createEvent('MouseEvents')
  let a    = document.createElement('a')
  const date = new Date().getTime()
  const epoch = Math.round(date / 1000)

  a.download = `export-${epoch}.csv`
  a.href = window.URL.createObjectURL(blob)
  a.dataset.downloadurl =  ['text/csv', a.download, a.href].join(':')
  e.initMouseEvent('click', true, false, window, 0, 0, 0, 0, 0, false, false, false, false, 0, null)
  a.dispatchEvent(e)
}

save_data()

The only downside is if the HTML markup changes it could break this scraper and minor changes will need to be made in order to fix this script.

I could see this script being packaged into a chrome extension which could pull daily, push it to a google spreadsheet and show you growth over time.

Posted on by:

andrewbrown profile

Andrew Brown 🇨🇦

@andrewbrown

🚀 CEO of ExamPro 🚀 free AWS Certification courses on freeCodeCamp youtube 🚀 AWS Community Hero 🚀 DEV Moderator for AWS tag 🚀 Star Trek Obsessed

Discussion

markdown guide
 

If you want this as json instead of csv

function articles(){
    const results = []
    const articles = document.querySelectorAll('.single-article')
    let data;
    for(let i = 0; i < articles.length; i++){
        data = article(articles[i])
        if (data) {
            results.push(data)
        }
    }
    return results
}

function article(article){
    // if there is no time selector that means this article 
    // is not published and will have no useful stats
    if (article.querySelector('time')){
        const name = article.querySelector('h2').innerText
        const tags = article.querySelectorAll('.tag')
        const author = article.querySelector('option[selected=selected]').innerText
        const date = article.querySelector('time').innerText
        const page_view_count = article.querySelector('.page-views-count').innerText
        const reactions_count = article.querySelector('.reactions-count').innerText
        const comments_count = article.querySelector('.comments-count').innerText
        const tags_string = []  
        for(let t = 0; t < tags.length; t++){
            tags_string.push(tags[t].innerText)
        }   
        return {
            name: name,
            tags: tags_string.join(' '),
            author: author,
            date: date,
            page_views_count: page_view_count,
            reactions_count: reactions_count,
            comments_count: comments_count
        }
    } else {
        return false        
    }
}

function save_data(){
  const results = JSON.stringify(articles())
  var blob = new Blob([results], {type: 'text/json'})
  let e    = document.createEvent('MouseEvents')
  let a    = document.createElement('a')
  const date = new Date().getTime()
  const epoch = Math.round(date / 1000)

  a.download = `export-${epoch}.json`
  a.href = window.URL.createObjectURL(blob)
  a.dataset.downloadurl =  ['text/json', a.download, a.href].join(':')
  e.initMouseEvent('click', true, false, window, 0, 0, 0, 0, 0, false, false, false, false, 0, null)
  a.dispatchEvent(e)
}

save_data()