How do I get comment count in Disqus?

Obay picture Obay · Feb 5, 2012 · Viewed 17.4k times · Source

I'd like to store the Disqus comment count on my own database, so that I can sort my articles by comment count. Basically, every time a page is read on my site, I'd like to ask Disqus how many comments that certain page has, then update the database with that count.

http://docs.disqus.com/help/3/ doesn't seem to be helpful.

Any suggestions?

Answer

Youssef Subehi picture Youssef Subehi · Dec 6, 2013

Get comment counts with disqus API

Here's what you'll need to have done before starting:

Register for a Disqus API key (optional) Have your own site to replace the example data

NOTE: The URL you use must match what's set as the URL in Disqus. See Web Integration docs for information on setting this up reliably.

Example HTML

<!DOCTYPE html>
<html>
    <head>
        <title>Disqus Comment Counts Example</title>
    </head>
    <body>
        <h1>Comment Counts Example</h1>
        <div>
            <a href="http://thenextweb.com/google/2013/05/03/fullscreen-beam-launches-first-youtube-app-for-google-glass-with-public-or-private-sharing/">
                <h2>Fullscreen BEAM: The first YouTube app for Google Glass comes with public or private sharing</h2>
                <div class="count-comments" data-disqus-url="http://thenextweb.com/google/2013/05/03/fullscreen-beam-launches-first-youtube-app-for-google-glass-with-public-or-private-sharing/"></div>
            </a>
        </div>
        <div>
            <a href="http://thenextweb.com/apps/2013/05/04/traktor-dj/">
                <h2>Traktor DJ: Native Instruments remixes its impressive DJ software for iPhone</h2>
                <div class="count-comments" data-disqus-url="http://thenextweb.com/apps/2013/05/04/traktor-dj/"></div>
            </a>
        </div>
        <div>
            <a href="http://thenextweb.com/video/2013/05/04/ninja-innovation-in-the-21st-century-with-gary-shapiro-of-the-consumer-electronics-association-at-tnw2013-video/">
                <h2>Ninja innovation in the 21st Century with the Consumer Electronics Association&#8217;s Gary Shapiro [Video]</h2>
                <div class="count-comments" data-disqus-url="http://thenextweb.com/video/2013/05/04/ninja-innovation-in-the-21st-century-with-gary-shapiro-of-the-consumer-electronics-association-at-tnw2013-video/"></div>
            </a>
        </div>
        <button type="button" id="get-counts-button">Get Comment Counts</button>
    </body>
</html>

Variables:

<script src="http://code.jquery.com/jquery-1.8.3.min.js"></script>
<script type="text/javascript">
$(document).ready(function () {
  var disqusPublicKey = "YOUR_PUBLIC_KEY";
  var disqusShortname = "thenextweb"; // Replace with your own shortname

  var urlArray = [];
  $('.count-comments').each(function () {
    var url = $(this).attr('data-disqus-url');
    urlArray.push('thread:link='+url);
  });
});
</script>

Making the Request API

$('#get-counts-button').click(function () {

    $.ajax({
        type: 'GET',
        url: 'https://disqus.com/api/3.0/threads/set.json?'+urlArray.join('&')+'&forum='+disqusShortname+'&api_key='+disqusPublicKey,
        cache: false,
        dataType: 'json',
        success: function (result) {

          for (var i in result.response) {

            var countText = " comments";
            var count = result.response[i].posts;

            if (count == 1) {
              countText = " comment";
            }

            $('[data-disqus-url="' + result.response[i].link + '"]').html('<h4>' + count + countText + '</h4>');
          }
        }
    });
});