开发者

Is there a way to programmatically check if a user shared a link on Facebook?

开发者 https://www.devze.com 2023-02-18 01:23 出处:网络
I\'m currently using jQuery to record each click on a Facebook share link on my site, but I\'m looking for a more accurate solu开发者_如何学Pythontion. Instead of recording the clicks, I\'d like to re

I'm currently using jQuery to record each click on a Facebook share link on my site, but I'm looking for a more accurate solu开发者_如何学Pythontion. Instead of recording the clicks, I'd like to record the actual "shares". Is there a way to get a response message back from Facebook after the user shares a link?


I came across this today: http://graph.facebook.com/http://stackoverflow.com

It returns a total share count for a specified URL. I have unique URLs for each user in my application so I can track their shares easily by using this.

It also works for twitter: http://urls.api.twitter.com/1/urls/count.json?url=http://stackoverflow.com


For as long as it remains active, you can use the Facebook "Feed" dialog rather than the "Share" dialog to ALWAYS get a share confirmation. (This applies to the Javascript Facebook SDK.)

Here's my understanding of the differences between the two:


Share Method

FB.ui({
    method: 'share',
    href: 'http://stackoverflow.com/questions/5363517',
}, function(response){
   console.log(response)
});

If the user is LOGGED IN and authorized your app:

  • If the post was shared, you will get a post id in the response variable: {post_id: "10205041067000110_10205045578512895"}.
  • If the post was not shared, response will be null.

If the user is NOT LOGGED IN to your app (they still need to be logged into Facebook):

  • The response variable will be null regardless of whether the user shared or did not share.

Thus, if you absolutely need the share confirmation, you have to ask the user to login first. This may not be a problem for your app, but in my case this added unnecessary friction to the sharing process.


Feed Method

(Deprecated but still active)

FB.ui({
  method: 'feed',
  link: 'http://stackoverflow.com/questions/5363517',
  caption: 'Test Caption',
}, function(response){
    console.log(response)
});

IRRESPECTIVE of login status (w.r.t. your app; they still need to be logged into Facebook):

  • If a post was actually shared, the response variable returned by Facebook will have the post id: {post_id: "10205041067000110_10205045578512895"}.
  • If the post was not shared, response will be null.

Using this method, you will always be able to tell whether someone posted or not, regardless of their login status.


You could use something like ShareThis instead of your jquery function and even get a bit more insights, analytics etc. However, you will encounter the same problem, created from off-site shares/likes, like posted here: http://forums.sharethis.com/topic.php?id=2947


Here's what I do....Create the share using normal anchor that calls this javascript function (spacing or brackets or something might be off):

FB.ui(
        {

            display:'iframe',
            method: 'stream.publish',
            caption: 'Put something here',
            description: 'put something here',
            name: 'foo',
            link: 'http://www.foo.com',
            picture: 'http://fo.com/img.gif'

        },
        function (response) {
            if (response && response.post_id) {
            //this means the post was completed....response.post_id is the FB post ID

            $.ajax({
                var URL = '/pages/ajax_InsertUserFacebookPost.aspx?';
                URL += 'facebookpostid=' + response.post_id;
                    type: "GET",
                    url: URL,
                    data: "{}",
                    contentType: "application/json; charset=utf-8",
                    dataType: "json",
                    async: true
                });


            } else {
                //alert('Post was not published.');
            }
        }
        );


As of Apr 2016, Twitter has deprecated v1 of their api and don't provide an alternate for the previous method to determine shares for a link. You'll need to use their Search api which only maintains share count for last 7 days and now you need to subscribe to their streaming api to maintain your own long running count.

Alternatively, you could use a free service like http://newsharecounts.com or http://opensharecount.com/ so they can subscribe to the api on your behalf and maintain the count. However, expect that share count could take a minute (or more) to update.

From newsharecounts FAQ -

"No matter how big your website is, or how old your content is, we detect new shares within 1 minute on average"

From opensharecount.com homepage -

"It can take some time for search results to come in, so initially the count that is reported will be zero on all links. Contact us if after one hour it is still zero. "

Re: FB sharing (http://graph.facebook.com/(url)), note that its possible for the user to use the share dialog and choose the 'Only Me' option and it is still counted in the same way as if they had shared with their friends. Know that the sharer can also delete the post shortly afterwards but future calls to the api will not reflect that.

0

精彩评论

暂无评论...
验证码 换一张
取 消