AddThis Academy

An ever-growing library of resources to help you become a better online marketer.

How to Get Counter Values Dynamically

Getting Share Counts using JavaScript

Note: As of October 2015 Twitter is phasing out their share counters, so those are no longer available.

With our 5/7 release we created a method which allows developers to get share counters from third-party services by calling the getShareCounts() method inside the addthis.sharecounter object. This function accepts two parameters. The first is either a string, array, or JSON object.

  • If it’s a string, it will be treated as the service to get the count for the URL set in the addthis_share.url property. Supported services are the same as our individual share counters:
    • Facebook (service code: facebook)
    • Pinterest (service code: pinterest)
    • Reddit (service code: reddit)
    • Delicious (service code: delicious)
    • VKontakte (service code: vk)
    • LinkedIn (service code: linkedin)
    • Ondoklassniki (service code: odnoklassniki_ru)
  • If it’s an array, it will be treated as an array of the above service codes and get the count for
  • If it’s a JSON object, there should be two properties:
    • service – a service code string or array of service code strings
    • countUrl – A complete URL, including protocol, subdomain, and path, to retrieve the count for

The second parameter is a callback function that accepts a service count object. The service count object will contain either be a single object or array of objects with the following properties:

  • service – the service code
  • countUrl – the URL the count corresponds to
  • count – the count (an integer)
  • error – false if no error, a string if an error occurred.

The Facebook service will have another property – share. This property will contain the following properties:

  • url – The same as countUrl
  • normalized_url – The URL stored for the Facebook Open Graph object
  • share_count – The number of times the Facebook Open Graph object has been shared on Facebook
  • like_count – The number of times the Facebook Open Graph object has been liked on Facebook or the website
  • comment_count – The number of comments for the Facebook Open Graph object
  • total_count – The same as the count, total of the other counts
  • commentsbox_count – The number of comments from a comments plugin on this URL. This only includestop level comments, not replies
  • comments_fbid – The object_id associated with comments plugin comments for this URL.
  • click_count – The number of times Facebook users have clicked a link to the page from a share or like

 

A Basic Example – Getting the count for Facebook for this URL:

addthis.sharecounters.getShareCounts('facebook', function(obj) {        
    console.log(obj)
});

obj is set to:

{
    "service": "facebook",
    "countUrl": "http://support.addthis.com/customer/portal/articles/1137944-getting-counter-values-dynamically",
    "count": 0,
    "share": {
        "url": "http://support.addthis.com/customer/portal/articles/1137944-getting-counter-values-dynamically",
        "normalized_url": "http://support.addthis.com/customer/portal/articles/1137944-getting-counter-values-dynamically",
        "share_count": 0,
        "like_count": 0,
        "comment_count": 0,
        "total_count": 0,
        "commentsbox_count": 0,
        "comments_fbid": 443506242410253,
        "click_count": 0
    }
}

Getting more than one service

addthis.sharecounters.getShareCounts(['facebook', 'pinterest'], function(obj) {
    console.log(obj)
});

obj is set to:

[
    {
        "service": "facebook",
        "countUrl": "http://www.addthis.com/",
        "count": 60547,
        "share": {
            "url": "http://www.addthis.com/",
            "normalized_url": "http://www.addthis.com/",
            "share_count": 24414,
            "like_count": 34353,
            "comment_count": 1780,
            "total_count": 60547,
            "commentsbox_count": 0,
            "comments_fbid": 419660544273,
            "click_count": 1034
        }
    },
    {
        "service": "pinterest",
        "countUrl": "http://support.addthis.com/customer/portal/articles/1137944-getting-counter-values-dynamically",
        "count": 0,
        "error": false
    }
]

Specifying a different URL with one service:

addthis.sharecounters.getShareCounts({service: 'facebook', countUrl: 'http://www.addthis.com/'}, function(obj) {
    console.log(obj)
});

obj is set to:

{
    "service": "facebook",
    "countUrl": "http://www.addthis.com/",
    "count": 60547,
    "share": {
        "url": "http://www.addthis.com/",
        "normalized_url": "http://www.addthis.com/",
        "share_count": 24414,
        "like_count": 34353,
        "comment_count": 1780,
        "total_count": 60547,
        "commentsbox_count": 0,
        "comments_fbid": 419660544273,
        "click_count": 1034
    }
}

Specifying a URL with more than one service:

addthis.sharecounters.getShareCounts({service: ['facebook'], countUrl: 'http://www.addthis.com/'}, function(obj) {
    console.log(obj)
});

obj is set to:

[
    {
        "service": "facebook",
        "countUrl": "http://www.addthis.com/",
        "count": 60547,
        "share": {
            "url": "http://www.addthis.com/",
            "normalized_url": "http://www.addthis.com/",
            "share_count": 24414,
            "like_count": 34353,
            "comment_count": 1780,
            "total_count": 60547,
            "commentsbox_count": 0,
            "comments_fbid": 419660544273,
            "click_count": 1034
        }
    }
]

Quick Tips

If you’re running into problems getting this working, try using the addthis.ready event. Example code for this can be found in the source at the end of this document.

This function also pulls the counter values directly from the services, rather than from our share service. There is a 10 second timeout in pulling the values, and any share counter that doesn’t return a value will have an error message rather than having error set to false. If you’re loading multiple services we recommend pulling them in parallel rather than doing one function call to prevent a specific service from slowing things down.

How to Get Counter Values Dynamically
2 (40%) 1 vote

Was this article helpful to you?