2011-06-23 45 views
4

下面是我在我的網站上使用Facebook Like按鈕(和Share按鈕)的代碼。去哪裏放置FB.Event.subscribe

它很好用。我點擊了Like按鈕,並且繁榮,我的Facebook帳戶的個人資料頁面中有一個很好的通知。 Facebook甚至會自動生成一個恰好是我網站標識的預覽圖像。精彩。我網站上的Like計數器正確計算。

所以我想記錄點擊Like按鈕。根據this頁面,代碼FB.Event.subscribe('edge.create', function(response) {});可以讓你做到這一點。

在下面的代碼中,我應該把代碼FB.Event.subscribe('edge.create', function(response) {});

<html xmlns="http://www.w3.org/1999/xhtml" xmlns:fb="http://www.facebook.com/2008/fbml"> 

    <?php 

    echo '<div id="fb-root"></div>'; 

    echo "<script type='text/javascript'> 
     window.fbAsyncInit = function() { 
     FB.init({appId: 'my_fb_app_id', status: true, cookie: true, 
       xfbml: true}); 
     }; 


     (function() { 
     var e = document.createElement('script'); e.async = true; 
     e.src = document.location.protocol + 
      '//connect.facebook.net/en_US/all.js'; 
     document.getElementById('fb-root').appendChild(e); 
     }()); 



    }); 

    </script>"; 


    echo '<script src="http://connect.facebook.net/en_US/all.js#xfbml=1"></script><fb:like href="" send="true" layout="button_count" width="450" show_faces="false" font="arial"></fb:like>'; 

?> 

回答

4

我不能完全肯定,如果這是正確的,但根據this stack overflow thread,您將FB.init()後和匿名函數調用之前的事件訂閱代碼,

window.fbAsyncInit = function() { 
    FB.init({ 
     appId : 'sensored-app-id', 
     status : true, // check login status 
     cookie : true, // enable cookies to allow the server to access the session 
     xfbml : true // parse XFBML 
    }); 

    /* All the events registered */ 
    FB.Event.subscribe('comments.add', function (response) { 
     // do something with response 
     alert("comment added"); 
    }); 
}; 

(function() { 
    var e = document.createElement('script'); 
    e.src = document.location.protocol + '//connect.facebook.net/fi_FI/all.js'; 
    e.async = true; 
    document.getElementById('fb-root').appendChild(e); 
}()); 

另外,如果你看到this developer's code example,你會發現他也在Facebook初始化代碼之後放置了事件訂閱代碼。

+0

沒錯:在初始化後立即放置所有事件訂閱通常是最容易的。這樣你就不會錯過任何東西。 – Femi