Search code examples
facebooktwittercountersocial-media

Is it possible to make a social link share counter


Hey there:) Is it possible to make a counter for how many who shared your website on facebook and twitter?

I'm thinking to structure the code like this. They only see "facebook" and "twitter" as links, + the counter of how many have shared already.

<!-- social media share links -->
<a href="javascript:myPopup('https://www.facebook.com/sharer/sharer.php?u=http://url.dk')" `   class="facebook-share-wrap">Facebook</a>`
<a href="https://twitter.com/home?status=Share%20awesome%20link">Twitter</a>
<!-- Add Facebook + Twitter shares -->
<p>0 shares</p>

Hope it makes sense :)


Solution

  • There is a public API endpoint for Twitter:

    https://cdn.api.twitter.com/1/urls/count.json?url=
    

    E.g.

    https://cdn.api.twitter.com/1/urls/count.json?url=https://code.google.com/p/pts-mini-gpl/wiki/StaticPython
    

    Will return

    {"count":2,"url":"https:\/\/code.google.com\/p\/pts-mini-gpl\/wiki\/StaticPython\/"}
    

    That URL has been shared 2 times.