Jun
22

Facebook Open Graph Tutorial: Customizing the User Experience.

Tags:  Facebook Open Graph Tutorial How-to Custom Experience 

This tutorial will show you how to customize the user experience on your web pages while they are logged in/out of Facebook on your site.
Recommended Resources
JavaScript Basics
HTML Basics
XFBML Basics

Overview.
This tutorial will show you how to display dynamic content on your pages using JavaScript and the Facebook API to fulling customize the user experience on your web pages. By targeting your content to the user based on their Facebook session status, you will be able to make their overall experience more pleasant.

The example in this tutorial will contain a single <div> placed within the <body>, controlled by 2 JavaScript functions used with some simple Facebook API JavaScript.

This tutorial is broken down into 3 simple code components:
  »  HTML opening tag
  »  JavaScript functions for customized content with Facebook API
  »  The customized content <div>

1. HTML opening tag.
Adding these attributes to your opening <html> will allow the Open Graph Protocol to recognize that you will be using its meta tag attributes for posting back to Facebook.

Place the following two xmlns attributes in the opening <html> tag of your document.
<html xmlns:og="http://opengraphprotocol.org/schema/"
xmlns:fb="http://www.facebook.com/2008/fbml">

2. JavaScript functions for customized content with Facebook API
The following code should be placed directly after the opening <body> tag on your web page.

Here is a breakdown of how the following code blocks function:
  »  FB.init: this function initializes the Facebook API (events, etc.)
  »  FB.Event.subscribe: these 2 functions register the events which trigger the login and logout functions of Facebook API and will run when either a login or logout from Facebook has been triggered.
  »  FB.getLoginStatus: this function gets the initial login status when the page is loaded and sets the customized layout depending on whether the user is logged into Facebook already.
  »  login/logout: these functions are triggered by the above functions as noted in the code below. I have utilized them for proper code reusability.

Note: do not forget to replace "YOUR_APP_ID" with your own Facebook app id.
<div id="fb-root"></div>
<script>
window.fbAsyncInit = function() {
FB.init({
appId : '147777435313',
status : true, // check login status
cookie : true, // enable cookies to allow the server to access the session
xfbml : true // parse XFBML
});

FB.Event.subscribe('auth.login', function(response) {
//if user is logged in
login();
});

FB.Event.subscribe('auth.logout', function(response) {
//if user is logged out
logout();
});

FB.getLoginStatus(function(response) {
if (response.session) {
//if user is logged in
login();
} else {
//if user is logged out
logout();
}
});
};

//if user is logged in - do this
function login() {
FB.api('/me', function(response) {
document.getElementById('fb-info-block').innerHTML =
"Login successful! Welcome, " + response.name + ".<br /><br />" +
"<fb:like href = 'www.whitbreaddesign.com' show_faces = 'false' width = '100' action = 'like' colorscheme = 'light'></fb:like>";
});
}

//if user is logged out - do this
function logout() {
document.getElementById('fb-info-block').innerHTML =
"Please log in using Facebook to see the welcome message!";
}

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

3. The customized content <div>
Place the following div tag anywhere within the body of your web page to display the variable content.

Note: make sure that the "id" attribute matches that of the id contained in the login()/logout() functions from step 2.
<div id = 'fb-info-block'></div>

Conclusion.
And that's how you can display customized content to improve the user experience on your web pages.

Click here to see this example in action or download the source below.

More information on Facebook, XFBML and the Open Graph Protocol can be found at http://developers.facebook.com/
Tutorial Downloads

Comments

Do you know where I would add all this code in my wordpress theme? Thanks for your help.

ed 2 months ago

Code from part 1 and 2 can be added in the file "header.php" of your wordpress theme.

For part 1: take the 2 attributes from the html tag in my code and add them to the html tag in "header.php".

For part 2: take the code and place it directly after the opening body tag as instructed above.

Part 3 can go on any page which is loaded after the opening body tag (header.php) and before the closing body tag (footer.php).

Let me know if you have any questions and thanks for the comment!

Marcus 2 months ago

I have it working but what I'm really looking to do is add the Welcome note just next to the login button...Of course in the header of my site...right now it's adding the welcome not up above my header (top left...looks strange...) Can you get me in the right directions....by the way love the site.

Ed Muntz 2 months ago

Hi Ed, I'm glad you like the site! I'm not sure I follow you exactly. Do you want to email your code snippet to contact@whitbreaddesign.com so I can take a closer look? or maybe a screenshot of the issue would be good.

Marcus 2 months ago

Is it possible to add the facebook login to make comments in wordpress (like your site) I had it set up with Facebook Connect before but they do not work side by side (that is Connect and Open Graph)...

ed 2 months ago

www.wolverinesocceracademy.com - Check out the site....the login works but adds the welcome note up top....I would really love for the welcome "Ed" to go right next to the button....Any suggestions....I will most likely add the connect button in my header but for now its just under my slider..

ed 2 months ago

Marcus - I figured out where to place to div tag....Really would like to get this to work with my comments...Also, trying to get user profile pic to show with welcome note

ed 2 months ago

Integrating Facebook with the login/logout to bypass user input can be a bit advanced... and will require a bit of work to describe.

I am going to write a tutorial on integrating Facebook with a comment form.

But in the mean time I can describe what is happening in the background...

Once the user log's in with Facebook on my site, their information is automatically entered into the comment form fields via JavaScript and then I set the fields to "display: hidden". I then display a welcome message + their name instead.

Once the rest of the info is filled out in the form it gets submitted to my database for processing.

See... a bit complex. But I will work on producing a tutorial for this. :)

Marcus 2 months ago

Thanks so much for your help...I would love to see a tut on adding facebook to comments....

Quick question: What is the best way to grab the users profile pic along with the response.name

ed 2 months ago

Any word on how to generate the users fb profile picture next to the "Login successful! Welcome, " + response.name + ...thanks for your help!

ed 2 months ago

Your tutorials are very useful and informative. It has proved sufficient for me to have knowledge about customization of facebook elements.

data recovery about 17 days ago

Leave a Comment

Name
Email (optional)
Website (optional)
...or login using Facebook to bypass these fields.
Messageusable tags: <b> <i>
Robot?
Loading tweets...
Site designed, developed and coded by Marc Whitbread and Whitbread Design ©2010