Michael
Michael

Reputation: 51

Facebook Send-To-Messenger Event Listeners

Facebook launched something really cool, I've been trying to figure out how to get the page to redirect on the user clicking and opt-in. Now, I am seriously not any good with java, php is my thing.

REFERENCE : https://developers.facebook.com/docs/messenger-platform/plugin-reference/send-to-messenger#event

Working Code :

FB.Event.subscribe('send_to_messenger', function(e) {
// callback for events triggered by the plugin
console.log('inside the send_to_messenger');  
window.top.location = 'http://google.com';
});

That will kick out the console.log and redirect, but what I want to do is do something when the user clicks the button and triggers the opt-in with the send_to_messener button.

So I tried

FB.Event.subscribe('clicked', function(e) {
// callback for events triggered by the plugin
console.log('user clicked button');  
window.top.location = 'http://google.com';
});

--- also ---

FB.Event.subscribe('send_to_messenger', function(e) {
FB.Event.subscribe('clicked', function(e) {
// callback for events triggered by the plugin
console.log('user clicked button');  
window.top.location = 'http://google.com';
}});

Again, nothing worked - if someone has a clue that could point in the right direction, I would appreciate it .. Thanks!!

Upvotes: 5

Views: 4010

Answers (2)

Lawrence Chernin
Lawrence Chernin

Reputation: 151

// FULL Working Example:

    window.fbAsyncInit = function() {
      FB.init({
        appId: "app_id",
        xfbml: true,
    status: true, 
    cookie: true,
        version: "v2.6"
      });


      FB.Event.subscribe('send_to_messenger', function(response) {
    if ( response.event == 'clicked' ) {
          // callback for events triggered by the plugin
            window.top.location = 'https://www.messenger.com/t/page_id';
    };
      });

    };

    (function(d, s, id){
       var js, fjs = d.getElementsByTagName(s)[0];
       if (d.getElementById(id)) { return; }
       js = d.createElement(s); js.id = id;
       js.src = "//connect.facebook.net/en_US/sdk.js";
       fjs.parentNode.insertBefore(js, fjs);
    }(document, 'script', 'facebook-jssdk'));

  </script>

<br>
<div class="fb-send-to-messenger"

  messenger_app_id="app_id" 
  page_id="page_id"
  data-ref="send this info to the app"
  color="blue"
  size="xlarge" >
</div>

Upvotes: 0

Michael
Michael

Reputation: 51

After looking at it again, I figured it out.

if (response.is_after_optin == true ){ // do something }

Upvotes: 0

Related Questions