Web Niraj
  • Facebook
  • Flickr
  • Github
  • Linkedin
  • Twitter
  • YouTube
Online portfolio, code examples and developer blog
  • About
  • Contact
  • Portfolio
  • WordPress
Search the site...
  • Home
  • Blog
  • Facebook API: Tracking Shares using the JavaScript SDK

Facebook API: Tracking Shares using the JavaScript SDK

39

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 to create the Share popup and then use a callback function to log the result. Use this tutorial to learn how to trigger a callback function to save the result of Shares. In my applications, I use a PHP script (Code Igniter) to save the result to a database.

Important Update:

This tutorial no longer works due to changes with Facebook. In particular, the Share Dialog no longer returns the post_id in the response, which means shares can no longer be tracked.

The example on JSBin still works, but you will notice that no response is received from Facebook when a post has been shared.

JavaScript Code:

Working Demo:

You can see a working example of the above code on jsbin. If you keep the Console tab open, you will see that the callback function will return the ID of the post, if successfully published on Facebook. This ID can be stored for tracking purposes, and you can make a Facebook API call to the ID (e.g. https://graph.facebook.com/{post_id}) to get more details about what was posted (you will need read_stream permission to do so).

The Response:

If the post is successfully shared on Facebook, the callback function will return a response like the following:

You may notice that the post_id contains the user’s Facebook ID (the 60506094 in the post_id is my Facebook User ID), which means you know who shared your post. You can make an API call using the user_id to find public information about the user.

If you have the read_stream permission for the user, making an API call with the post_id returned in the callback function will return something like the following:

Ajax Script (PHP):

For reference, the below is an example of the PHP script I’ve used in my Facebook applications to log any shares to a database.

The Result:

The share to the user’s timeline will look like any other. Here is what will appear on your timeline if you use the above code:

Facebook Share Tracking
Facebook, Facebook Graph API, JavaScript, jQuery, Tracking, Tutorial

39 comments on “Facebook API: Tracking Shares using the JavaScript SDK”

  1. Rahul Saha says:
    March 24, 2014 at 2:15 PM

    Hi Niraj,

    Its a great tutorial. 🙂

    I have 1 more requirement. Hope you can help me. I am able to share a photo from FB wall, if the photo already uploaded in internet and I have a link for this.

    I am using android phonegap 2.9.0 and need to upload/share Images from Android SD card to Facebook wall through Phonegap/JS. Do you have any working example for that?

    If yes then please help me.

    Thanks,

    Reply
    • Rahul Saha says:
      March 27, 2014 at 8:07 AM

      Hi Niraj,

      Do you have any idea about my query ?

      Thanks,

      Reply
      • Niraj Shah says:
        March 27, 2014 at 9:47 AM

        Hi Rahul. I’m afraid I don’t have any sample code for this. I’m unfamiliar with PhoneGap, but will see what I can find.

  2. Gilbert says:
    March 25, 2014 at 10:10 PM

    The example is giving me an error.

    Reply
    • Niraj Shah says:
      March 25, 2014 at 11:04 PM

      It would really help me if you can indicate which part is giving an error, and what the error is.

      Please note that the code isn’t designed to work completely. While the JavaScript code will work, the PHP code is only part complete – it needs to be adapted for your application.

      Reply
      • Gilbert says:
        March 26, 2014 at 12:47 PM

        I meant your demo was producing an error. It is working fine now.

  3. Neha Sharma says:
    April 17, 2014 at 1:54 PM

    it is not working for me

    Reply
  4. Alex Gallace says:
    June 17, 2014 at 4:34 PM

    It is not working for me, console:
    Uncaught SyntaxError: Unexpected token <
    in http://getbootstrap.com/2.3.2/

    Reply
    • Niraj Shah says:
      June 17, 2014 at 4:40 PM

      It means you have an error in your code. Fix it first and then see if it works.

      Reply
  5. Helen AndPeter Burns says:
    June 26, 2014 at 8:53 AM

    Hi,

    This exactly what I need.. but your demo is giving me the error ‘Sorry this feature isn’t available right now, an error occured while processing this request, please try again later’

    Any ideas?

    Thanks so much

    Reply
    • Niraj Shah says:
      June 26, 2014 at 10:42 AM

      The demo does work, but only if you are not in the “Edit” mode.

      Reply
  6. Helen AndPeter Burns says:
    June 26, 2014 at 9:06 AM

    Don’t worry! I tried it on my server and it worked PERFECTLY! Thank you so much! You’re a genius!

    Reply
  7. Ali says:
    July 18, 2014 at 7:16 AM

    please tell me how i can achieve this by facebook share dialog instead of feed dialog.
    i’m working on a project my client requirements only use open graph. i have tried a lot of article but it did not work please help me

    Reply
    • Niraj Shah says:
      July 18, 2014 at 9:50 AM

      This can only be achieved using the Feed Dialog offered by the JS SDK. You can look as hard as you want, but its not possible to do using using just OpenGraph tags on a page. If you are using Like buttons on the client’s site, you are already using the JavaScript SDK in some form.

      Reply
  8. My My says:
    August 20, 2014 at 6:25 AM

    Nirai Shah, it is not working for me too, mabe it’s very old.
    Now, it is not facebook user id. I need facebook user id when they press share button. I got post_id: 1465280260425136_1465300913756404, but the first parameter is not facebook id

    Reply
    • Niraj Shah says:
      August 20, 2014 at 2:40 PM

      If your app was created after April 30th 2014, the first part of the post_id is the User ID specific to your application, and not the global Facebook ID. If the post_id is 10100439951266536_10100439952059946, then 10100439951266536 is the App Scoped User ID. You won’t be able to get the user’s real ID anymore.

      Reply
  9. Satyajeet says:
    November 2, 2014 at 4:34 PM

    Hi Niraj,

    This doesnt work for me. I’m embedding one of my FB posts from my fanpage on my website and then i want a button on the page that lets people share my fan page post, and as an incentive i give them a gift if they have shared.

    I wanted to build a content locker based on your code. When i execute the code this is the error i get

    ‘API Error Code: 191
    API Error Description: The specified URL is not owned by the application
    Error Message: redirect_uri is not owned by the application.”

    Arent you allowed to use facebook url’s?

    Please advise

    Thanks

    Reply
    • Niraj Shah says:
      November 3, 2014 at 10:25 AM

      The error is to do with your Facebook App Settings. You need to make sure the URLs are setup correctly.

      Reply
      • Satyajeet says:
        November 3, 2014 at 11:12 AM

        Could you elaborate as to what settings you are referring to, so i can set that up correctly.

        Are you referring to this?

        http://screencast.com/t/WJbDShCwG

        Thanks

      • Niraj Shah says:
        November 3, 2014 at 11:39 AM

        Yes, you need to set the App Domains and add Website as a platform to the settings, with the URL to your website.

      • Satyajeet says:
        November 3, 2014 at 1:14 PM

        Thanks for your reply Niraj, but what I want is for people to share is my facebook fanpage post using a share button on my website. To do this i would need to put facebook.com as the app domain, which facebook wont let me do. i can use the old sharer.php ( which still isnt deprecated), but there is no callback available if i use sharer so I dont know if the person actually shared the post ( im trying to build a content locker, so people can access my premium content as an incentive, after they share my post) Anyway to accomplish that? Thanks

      • Niraj Shah says:
        November 3, 2014 at 1:31 PM

        You don’t understand. The App Domain setting is so Facebook know where you’re running the script from – it’t not the URL you want to share. If you set the App Domain correctly, the JavaScript SDK will let you share anything you want. If anything is unclear, please check the Facebook Documentation or contact us for Paid support.

      • Satyajeet says:
        November 3, 2014 at 3:28 PM

        Got it working…thanks for your help 🙂

  10. eleganttemplates says:
    November 20, 2014 at 10:28 PM

    Hi , i own a Like Exchange website and i’m trying to implement your tutorial with no success , what i’m trying to achieve is getting callbacks for all kind of shares – photo/post/url , but i get successful callback for url’s only. Any idea of how to fix this?
    Regards,
    Majid

    Reply
    • Niraj Shah says:
      November 21, 2014 at 9:00 AM

      The script should work for any type of share, but it won’t distinguish between them. You’ll only need supply a few of the parameters for this to work, thought it seems that the Share Dialog is broken on Facebook’s side.

      Reply
      • eleganttemplates says:
        November 21, 2014 at 1:07 PM

        Could you please help me make this ork? I can pay. Thanks.

      • Niraj Shah says:
        November 21, 2014 at 3:36 PM

        Sure. Please send me an email with your source code and any additional details and I’ll see if I can help you out.

  11. Rajendar says:
    February 23, 2015 at 1:36 PM

    Hi Niraj,

    As per my client requirement, they have a facebook page, and who ever share the posts / events from that page should open a URL on the next tab of user and moved to home page with user details.

    is this possible, I check different things and find a way to show the last post from fb page on the website home page and if any shares from there I could get the details easily but my client want from facebook page when shares open the client website.

    Please help me.

    Reply
    • Niraj Shah says:
      February 23, 2015 at 9:43 PM

      Its unclear exactly what you’re asking, but you cannot track shares from a Facebook page. The code in this tutorial only works on websites or apps.

      Reply
  12. Aishwarya Taneja says:
    February 3, 2016 at 3:36 PM

    How can i get response using this script?

    Reply
    • Niraj Shah says:
      March 3, 2016 at 9:07 AM

      The response is returned in the function defined on line 8-16, and outputted to console using console.log( response );.

      Reply
  13. Aman Kumar says:
    March 19, 2016 at 5:21 AM

    Your Code does not works when it is opened in private window

    Reply
    • Niraj Shah says:
      March 20, 2016 at 6:02 PM

      That would most likely be Facebook’s callback doesn’t work for private windows.

      Reply
  14. Rameez Makhdoomi says:
    April 11, 2016 at 4:58 AM

    Niraj …Rameez here

    Actually everything is working fine but i want that user should be only able to publish the post as public not only friends or only me…

    Is there a way for that since im new to programming.

    With Regards..

    Reply
    • Niraj Shah says:
      May 17, 2016 at 8:30 PM

      You cannot set the privacy of the post using JavaScript. This option is only available to the user at time of posting.

      Reply
  15. Rashmy says:
    November 9, 2017 at 9:10 AM

    Hi nirij, can u please tell me what part of the code goes in which file. sorry but i am new to using fb share. Also, how can i share a post in the above code. How to add a point to the user if he/she shares a post and also reward a point if he/she likes a post.

    Reply
    • Niraj Shah says:
      November 12, 2017 at 9:21 PM

      Sorry, you are asking a complicated set of questions that would take hours to explain and understand. I suggest you try some tutorials and at least attempt some basic Facebook integration. You need a little understanding of how the API works.

      Reply
  16. Ajay says:
    March 7, 2019 at 1:59 PM

    Hello Niraj Shah,

    Could you please help me to get return post_id .
    we are using the same code but get null response .

    we see many articles to get post_id and told that need to set the “publish_actions” but with the new version of api 3.2 we don’t have option for it.

    So can you explain ,how we can get post_Id ?

    Reply
    • Niraj Shah says:
      March 21, 2019 at 2:57 PM

      The `publish_actions` permission has been deprecated so can no longer be used. However, if you’re using the Share Dialog, this permission is not required anyway. Facebook have stopped returning the `post_id` when sharing, so you can no longer track is the share was successful.

      Reply

Leave a Reply to AliCancel reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

StackExchange / StackOverflow

profile for Niraj Shah on Stack Exchange, a network of free, community-driven Q&A sites

Support Me

Buy Me a Coffee

PSN Profile

Tags

ACL Amazon Amazon Web Services Android Android 4.4 KitKat Android 5.0 Lollipop Apache Backup Bug Command Line Cordova cPanel / WHM Facebook Facebook Graph API Facebook PHP SDK 4.0 Facebook Social Plugins Fan Page Firewall Flash Gadget Geolocation Google Nexus 5 Hacking HTML5 iOS JavaScript jQuery Laravel 5 Linux NodeJS Parse PDF PHP Plugin Portfolio PS4 Review Security Server SSH SSL Sysadmin Tutorial WordPress WordPress Plugins
© 2011-2025 Niraj Shah
  • Blog
  • Portfolio
  • WordPress
  • About Me
  • Contact Me
  • Privacy Policy
Privacy & Cookies: This site uses cookies. By continuing to use this website, you agree to their use.
To find out more, including how to control cookies, see here: Privacy Policy