Search code examples
phpfacebook-graph-apicakephpoauthfacebook-sharer

Facebook API: How to get number of shares of a URL by a specific user?


I am building a web campaign app where the user oauths then has a selection of 20 different sharable images. I need to keep track of how many times the user shares total and potentially which ones they share.

The second and easier option is to keep track of the count when they click the share button on the page. Though this wont give an accurate count as the user could click the share button on the site, wait for the .sharer window to pop up, close it and ultimately game the site. I'd like to avoid that.

Is there another way to keep track of what he user is sharing?

I am building this in Cakephp.


Solution

  • If you use the JavaScript SDK, you can track what the user shares, and only when it's successfully shared. The basic code to achieve this below. You can send unique identifiers for each image so you know which one was shared. Of course, this can still be gamed by the user - they can share an image and then delete it from their timeline.

    function fb_share() {
        FB.ui( {
            method: 'feed',
            name: "Facebook API: Tracking Shares using the JavaScript SDK",
            link: "https://www.webniraj.com/2013/05/11/facebook-api-tracking-shares-using-the-javascript-sdk/",
            picture: "https://stackexchange.com/users/flair/557969.png",
            caption: "Tracking Facebook Shares on your website or application is a useful way of seeing how popular your articles are with your readers. In order to tracking Shares, you must used the Facebook JavaScript SDK."
        }, function( response ) {
            if ( response !== null && typeof response.post_id !== 'undefined' ) {
              // ajax call to track share
            }
        } );
    
    }
    

    You can find the complete code and working example here.