1

I search a solution for the problem highlighted in this question.

Unfortunately, the accepted solution (which dates back to 21/11/2012) doesn't work anymore, as you can this in this demo.

Does someone know why?

Body

<div id="fb-root"></div>
<script src="http://connect.facebook.net/en_US/all.js"></script>
<script>
  FB.init({
    appId  : '179378788777832',
    status : true, 
    cookie : true, 
    xfbml  : true  
  });
</script>

<div id="login">
    You are not logged in to FB, Please click<a href="#"> here </a> to login.
</div>

<div id="container_notlike">
YOU DONT LIKE
</div>

<div id="container_like">
YOU LIKE
</div>

JS

var hideLogin = function(){
   $("#login").hide();
}

var showLogin = function(){
   $("#login").show();
}

var doLogin = function(){
    FB.login(function(response) {
      if (response.session) {
           hideLogin();
           checkLike(response.session.uid)
      } else {
        // user is not logged in
      }
    });
}

var checkLike = function(user_id){
    var page_id = "40796308305"; //coca cola
    var fql_query = "SELECT uid FROM page_fan WHERE page_id = "+page_id+"and uid="+user_id;
    var the_query = FB.Data.query(fql_query);

          the_query.wait(function(rows) {

              if (rows.length == 1 && rows[0].uid == user_id) {
                  $("#container_like").show();

                  //here you could also do some ajax and get the content for a "liker" instead of simply showing a hidden div in the page.

              } else {
                  $("#container_notlike").show();
                  //and here you could get the content for a non liker in ajax...
              }
          });        
}

$(document).ready(function(){
    FB.getLoginStatus(function(response) {
      if (response.status === 'connected') {
        hideLogin();
        checkLike(response.authResponse.userID)
      } else {
        showLogin();
      }
     });

    $("#login a").click(doLogin);
});

CSS

body {
width:520px;
margin:0; padding:0; border:0;
font-family: verdana;
background:url(repeat.png) repeat;
margin-bottom:10px;
}
p, h1 {width:450px; margin-left:50px; color:#FFF;}
p {font-size:11px;}

#container_notlike, #container_like, #login {
    display:none
}

I search solution for hours but I didn't find anything what works.

Thank you for help.

stefanobaghino
  • 11,253
  • 4
  • 35
  • 63

3 Answers3

2

Like Gating is not allowed anymore, that´s why it is not possible. The only reliable way to get that information is by authorizing a user with the user_likes permission and using /me/likes/[page-id]. But you will not get that permission approved for like gating in the Login Review.

People need to like something because they really want to, not because they get something for it:

Only incentivize a person to log into your app, enter a promotion on your app’s Page, or check-in at a place. Don’t incentivize other actions

Source: https://developers.facebook.com/policy/

Btw, you can also subscribe to the edge.create event to find out if a user just clicked your like button, but you can´t find out if the user liked it before: https://developers.facebook.com/docs/reference/javascript/FB.Event.subscribe/

andyrandy
  • 72,880
  • 8
  • 113
  • 130
0

The problem on that code is that FQL is deprecated . You can't do what you want to, and thats why changes are necessary . Your code would work if your app is old, created before changes that turns like gating not allowed, but anyway, you cannot use that for show content. You can use that kind of implementation for creating an interactive experience, where you can for example changing the content, saying "Thanks for liking".. Or "Connect with us, liking our page.." ..

You can also think about interfaces, where you show up the page plugin, and just after user likes, you say Thank you ... and hide the page plugin ... But user must always be able to close without liking .

For checking if user likes a page, you need use :

FB.api get on '/me/likes', and with the response ...

if (response.data[likes].name == "Coca-Cola") 

or... better

if (response.data[likes].id == "40796308305") { 

}

I repeat, One thing has nothing to do with another .. You can check if user likes a page, but you cannot restrict content, based on this kind of resource .

There are other ways to check it, for exaple :

Get api call to userid/likes/pageid returns page info if user likes the page, and returns nothing if user does not like the page .

You will waste time trying do that for controlling content consumption . Your app must be aprooved for asking user_likes permission, and its better you think about creating another experience for users, instead of submitting something like that .

I also think that content with good open graph for sharing, commenting and optional liking is very much more efetive, because i noticed that many people used to like and dislike the page after getting the content .

If you just... Prompt a FB.UI for sharing after 1 minute, for example, you will have much more results .. Aways positioning the page plugin in strategic places, people will naturally like your page ..

Than you can say change the page plugin element : Thank you for liking, please share with your friends .... Who would also like ...

Or use a callback for triggering the share dialog ..

David Augustus
  • 420
  • 3
  • 9
  • "and hide the page plugin" - that is a VERY bad solution, you should never do that. – andyrandy Feb 29 '16 at 08:25
  • Could you tell me why not? I sayd that Using Like Button Callback, the app could "HIDE" the page plugin after the like event been performed by user, the like button callback exists for that. we were not talking about making the plugin invisible to get likes without user interaction, ok ? – David Augustus Aug 29 '16 at 04:38
  • 1
    users need an option to unlike the page. the like button callback does NOT exist for hiding it. i believe that´s even somewhere in the rules. – andyrandy Aug 29 '16 at 07:23
  • I got your point, but look, i mean, if you display a PAGE PLUGIN for example, asking users to like, and give time to close, and option to cancel, if use the callback, for closing the lightbox, after user likes the page, would work on the first time, closing it, i could save a cookie to avoid it from opening again. But if the user returns, he would see the lightbox, not if have valid cookie. also, and he will have option to unlike page, because it will open again. On production there usually exist other page plugin on the site or app there is aways visible. lightbox technique small size . – David Augustus Dec 01 '17 at 01:44
  • what´s the point for that? if the user removes the cookie, he will definitely see the overlay again - even if he already liked the page. there is no reliable way to detect if a user liked your page, unless you get user_likes approved - and that will not happen just to check if the liked your page. – andyrandy Dec 01 '17 at 07:54
  • The reason is Frictionless User experience, after liking, no need close, it just closes, today i use Lightbox Overlay and Page Plugin, its settimeout 30 seconds to close. Can click to close, also. If user likes, it simple closes. The Callback response is going to be deprecated soon. But i think there is another method to get if user likes the page, i dont know if it changed .. – David Augustus Dec 15 '17 at 07:51
  • Check page tabs, installed, app can get acess token.. usually no need login, API returns the page token. I think one response used to be if user liked. Its nice you can have the page Id, get api, receive response, use name, pic, cover, and others to customize pagetab. Admins drive users into, long time ago, we used checking if user likes using FQL also! Today can not make exclusive content for getting likes. Think about, Changing the app, to ask users like the page, if not liked, and Say Thank you for liking, ... As the callback, used to close lighbox.. One click... Like or close .... – David Augustus Dec 15 '17 at 08:08
  • 1
    you can only get that information if you authorize the user with the user_likes permission. there is no other way anymore. earlier it was possible to detect the like status in a tab, yes. but that was removed a long time ago, because like gating is not allowed anymore. don´t annoy users with popups just to get their like ;) - present valuable content on your page and people will like it anyway. – andyrandy Dec 15 '17 at 08:13
  • One more detail.. I do not use cookies. Usually display page plugin random id, from list. 30 seconds to close, can use button or "X" icon, for logged in users i do not display it, User would see same plugin and page opening many times., Cookies works great for this. Users will need to take many cancel and closing actions, for closing ... Also, think about removing a cookie, Everytime page loads, open same lightbox. Same page, asking users to click or wait.. Cookies. Oh, btw, check my Angular + Localstorage pens at my codepen .. 3642066 its awesome !!!!!! – David Augustus Dec 15 '17 at 08:23
  • 30 seconds autoclose...that´s weird too...what´s the point? users will not stay on your page to look at the popup for 30 seconds, and what if the user decides in second number 29 that he wants to like the page? suddenly the popup disappears. or did i misunderstand that? it does sound like very bad UX, to be honest. – andyrandy Dec 15 '17 at 08:29
  • Hello luschen, in brazil users are a kind of lost, in ux. So, i can guarantee you that many users keeps looking that for 30 seconds, and dont know what to do. 30 seconds, in this experience, is a kind of,,,, Showing the likebox, asking for liking, or CLOSING if click closebutton or the X at top right. But they dont know whats happening, so, we have to hide the lightbox, closing the window for them. Also, i used to usethe callback, for closing the window, if they click on like button, and sometimes, saving a cookie, for avoid displaying that again .. – David Augustus Feb 13 '18 at 07:18
  • And Yes, it does sound like very bad UX, specially nowadays, and i used to display random pages, asking them for like if interests them, always let closing it, lightbox comes and goes, no need to take any actions. Go ahead to the app or site after. got, me.. Also had function that, click 'see another" changes pages, reset 30 seconds, giving time to thing, im saying, that worked for us... Not that this is best UX. If users know liking page makes seen updates on feeds, for what interest them, they would click like button. As they likes posts and comments. BR Users watch it as Television . – David Augustus Feb 13 '18 at 07:34
  • Users would call support, and say, "there is a window saying something about "LIKE PAGE", i cant get into the game!!" "please help me" , *********"i cant close this window"*********, got me,, friends, """whats this?????""" so i h ave to : "HELLO USER, CLICK ON LIKE PAGE, TO HAVE OUR BEST UPDaTES ON YOUR FEED" ................ many of them look and look , think what, newssssss feeedd,,,, updates/ what is he saying about, page, like, button, thinking,,, thinking,, OH it closed, game started, great ! – David Augustus Feb 13 '18 at 07:37
  • Have you never seen Google Ads, lighbox ads banners or videos, closing, or allowing click to go ahead, after 3 seconds, my friends, i made this many years ago, people of that old school used a lot of things. I had saw even "LIKE THIS PAGE or wait TO CONTINUE" ....... Bargaining clicks for playing, but i tested i noticed that some of hem usually unlikes and relikes pages, to playing (brazilians), callback closes soon users liked. In that time, like gating was used all tabs, i made liking, for price discounts, on page's tab mini shop. 10 years ago. They still dont know whats a Fanpage . – David Augustus Feb 13 '18 at 07:49
  • And, i think, if you want say something about my UX, or discuss it, you need to see it first, i can tell the concepts, and you tell what concepts is better for you, because, 10 years after, lithboxing article's, giving us great results, because when users know liking our pages, receive updates, and they are clicking it, its working, and i see no problem, to join you on a dicussion about that, all my points, and showing it, how it works. But the problem about this, is not my implementation, its the wrong using of this features, making the users creates mandatory and arbitrary connections . – David Augustus Feb 13 '18 at 08:09
  • Users cant find 10 different actions at different places of page. They watch it. Yesterday,, i have a customer, that couldnt understand what he need to click, and where to click for using our social login button. Its the easier registration system of all times, giving him 3 options, in this point he didnt understood that its a registration system. Tradicional registration/ No way. I would have users saying, that he cant register because his email is in use .. or that our system doesnt work. And i ask confirmation email,,, its impossible. Here is BRazil. – David Augustus Feb 13 '18 at 08:16
  • Some customers had called suport and saying that, MY EMAIL PASS is not working, what happened, i need enter the site, but my email and password is not working, i will check, no registers, so, belieave, some users tryes to login sites, using their email credentials, on other registration system. And they say, its a problem, that its not working. If it worked, was going to be ok. A miracle . – David Augustus Feb 13 '18 at 08:18
  • "Hello, remember me, i call you yesterday, and that lighbox is displayed again, closing the game, and i dont know what to do" - Just CLick on CLOSE, "close///// what is this, there is no close here" .. "help me" ... SIR, wait, close your eys, count to tree and say, i have the power....." and open your eyes, its closed. – David Augustus Feb 13 '18 at 08:25
  • "liking for price discounts" was never allowed ;) - and if the problem is that users stay on the popup for 30 seconds not knowing what to do, then the only good solution is not to show a popup at all. 30 seconds...i would say, users just go away if they don´t know what to do, in much less than 30 seconds...if those things are a problem for the user, make sure the problem does not even occur. – andyrandy Feb 13 '18 at 08:31
  • It is, once that i give him all options and explain him the situation, its connecting users to pages, when they are on articles, and have suggestion to like our page to receive more articles. It closes after 30 secods, for no need actions. Did you understood my point, maybe i can show you. Maybe i can learn other methods. ANd 30 seconds, for our users is not engough, 45 seconds is better, tooks 10 seconds is only to loading full. And liking for discounts and promotions were used worldwide, liking for getting points, liking for playing, listening.. donloading, its about ux not best practices, . – David Augustus Feb 13 '18 at 08:40
  • And i tell you friend, i am thinking on testing change page plugin for some kind of share, so users wouldbe invited to share on one click after they hit end of the page...... They cant understand that SHARE BUTTON EXISTS, but they maybe can think sharing is a good idea,if suggested for them.. "OH, SHARE< THAT SOUNDSNICE< OKAY," – David Augustus Feb 13 '18 at 08:44
  • if your page needs 10 seconds to load, then you should definitely change something...that is way too much. i know that liking for discounts was used worldwide, that does not mean it was ever allowed ;) – andyrandy Feb 13 '18 at 08:53
  • anyway, stackoverflow is no place for lenghty discussions, i actually just wanted to point out that there is only one solution for the question in this thread, and it´s in my answer in short. – andyrandy Feb 13 '18 at 08:55
  • Ok, but i found best solutions for project's demands, not a cut and paste . – David Augustus Feb 13 '18 at 09:31
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/165037/discussion-between-david-augustus-and-luschn). – David Augustus Feb 13 '18 at 10:56
0

OLD SCHOOL API CALL


The method FB.Event.subscribe() allowed apps to subscribe to a range of events, and define callback functions for when they fire, is deprecated .

Also FQL Query is deprecated .

For checking if user likes a page, you need user.likes permission, so you can try :

FB.api get on '/me/likes', and with the response ...

if (response.data[likes].name == "Coca-Cola") 

or... better

if (response.data[likes].id == "40796308305") { }

There are other ways to check it, for exaple :

Get api call to userid/likes/pageid returns page info if user likes the page, and returns nothing if user does not like the page .

You can check if user likes a page using this call, but you cannot restrict content, based on this kind of resource .


2018 UPDATED SOLUTION


But nowadays, in 2018 the best method for is setting Webhooks .

Webhooks are a subscription based system between Facebook and your server. Your app subscribes to receive updates from Facebook via a specified HTTPS endpoint .

Webhooks at app dashboard

This allows your to app to receive notifications whenever there are updates to a chosen set of topics and their fields, so, you can track changes to most sections of the user's profile, such as About, Photos, Posts, Friends, and Likes.

Webhooks update notifications are sent as POST requests to a callback URL that you supply. Notifications can be lightweight, indicating only that a field has been updated, or can include the newly updated value .

webhooks user reference Full list of user profile fields that you can subscribe to, such as About, Photos, Posts, Friends, and Likes.

webhooks page reference The easiest way to set up your app to receive Webhooks updates is to use the App Dashboard's, check out Facebook Platform documentation for more info .

Webhooks documentation

David Augustus
  • 420
  • 3
  • 9
  • he wants to detect if a specific user (already) likes a page. that´s impossible with webhooks. they are used for completely different tasks. – andyrandy Feb 13 '18 at 10:48