abjerner / Skybrud.Social

Skybrud.Social is a framework in .NET for integration with various social services like Twitter, Facebook and Instagram. The framework will handle all the technical parts and API communication so you don't have to.
http://social.skybrud.dk/
MIT License
96 stars 32 forks source link

How to get page shares vis Skybrud #76

Closed blachawk closed 6 years ago

blachawk commented 7 years ago

I am currently on Umbraco 7.6.6.

Is it possible either through Skybrud.Social.Facebook or Skybrud.Social to have a Facebook share button for my Umbraco pages? If this is possible, is there a guide online that explains how? Can anyone provide a razor or c# demo implementation?

Thanks

FDBenevides commented 7 years ago

Hi @blachawk ,

I hope I'm not saying something stupid but the purpose of this framework is to deal with all the technical parts of the - in this case - Facebook API in a server-side perspective.

What you're asking for is something that should be used in client-side. In that case, you can use something like this in a razor view:

@{ string fullUrl = Umbraco.NiceUrlWithDomain(CurrentPage.Id); } <a href="javascript:shareFacebook()" class="social-icon si-colored-facebook" data-toggle="tooltip" data-placement="bottom" title="Share on facebook"> <i class="fa fa-facebook"></i> <i class="fa fa-facebook"></i> </a> <script> shareFacebook = function () { if (FB != undefined) { console.log('Url to share: ' + '@fullUrl'); FB.ui({ method: 'share', mobile_iframe: true, href: '@fullUrl', }); } } </script>

Don't forget that in this case you'll be using the Facebook's js-sdk so you need to initialize it (for example, in your master page):

<script> $(document).ready(function () { $.ajaxSetup({ cache: true }); $.getScript('//connect.facebook.net/en_US/sdk.js', function () { FB.init({ appId: '[YOUR_FB_APP_ID]', version: 'v2.10' }); }); }); </script>

Hope this helps you

blachawk commented 7 years ago

No not stupid at all! Thank you for clarifying the purpose and reminding that page sharing can easily be done from the client side. I used something similar 3 years ago but totally forgot. Thanks!!!

abjerner commented 7 years ago

Awesome that you found a solution 👍

Including a bit of JavaScript like @FDBenevides suggested lets you easily add a share button. But you should be aware of the downsides of doing so.

By inserting the SDK script as suggested, Facebook will be able to track your visitors across your site. For sites that are concerned about the privacy of their visitors (eg. government sites), a script like this is a no go.

A way to get around the script tag, is to create the button on your own - eg. a link that points to something like:

https://www.facebook.com/sharer.php?u=http://social.skybrud.dk/

where the u parameter is the URL of the page. This however won't include the number of times the page has been shared on Facebook. But perhaps the share count isn't a necessity?

If you still wish to display the amount of shares, but avoid the script tag, you can use Skybrud.Social.Facebook for this, but this might be a bit complex, as you have to call the Facebook API for each of your pages (from your server to the Facebook API, whereas the script tag will create calls from the visitor's browser to the Facebook API). I'll try to dig up some examples on how to do this.

I hope this makes sense ;)

abjerner commented 7 years ago

Hmm - sorry. I thought about something else - it is currently not possible to get the amount of shares for an URL via Skybrud.Social :disappointed:

abjerner commented 7 years ago

It seems that you can make a request to the Facebook Graph API like this:

https://graph.facebook.com/?id=http://www.google.com

With the link above, you'll get a JSON object like this:

{
   "share": {
      "comment_count": 2,
      "share_count": 43612013
   },
   "og_object": {
      "id": "1558697820859150",
      "title": "http://www.google.com.pe/?gfe_rd=cr&dcr=0&ei=p6_TWerwMeTFXqf4k8AL",
      "type": "website",
      "updated_time": "2017-10-03T15:44:37+0000"
   },
   "id": "http://www.google.com"
}
blachawk commented 7 years ago

Thanks a million for digging into this. I'll play around with this and get back to you guys.

Thanks!