Implementing Simple Activity Feeds

One of the best ways to make your site social is to let users see what their friends have been doing on your site. It creates instant personalization and social context for users who login with identities like Facebook, Twitter, Yahoo, LinkedIn, Google, etc. There are many ways to accomplish this using Gigya but here are two approaches to get you started:

1. Activity Feed Plugin

Our Activity Feed Plugin is one of our most popular plugins because it allows logged-in users to see a real-time stream of friend activities on your site. Think of it as having a complete activity stream without having to implement your own back-end solution.

The plugin is dirt simple to get up and running. Just call the socialize.showFeedUI API method from any page on your site and the Activity Feed renders in HTML. It has a single required parameter called “containerID” which is the DIV container where you want the plugin to show up:

[sourcecode language=”html”]
<h3>Activity Feed Plugin Demo</h3>
<div id=”ActivityFeedContainer”></div>
<script type=”text/javascript”>, {containerID: ‘ActivityFeedContainer’});

A complete working example of the Activity Feed plugin can be found here.

Like all Gigya plugins, there are plenty of ways to customize the plugin to match the functional requirements of your site. Some of the options are:

  • defining the privacy level (public, friends or private)
  • changing the plugin dimensions and other CSS-related properties
  • enabling or disabling providers
  • listening for event handlers like onLoad and onSelectionDone

There’s an additional feature to the plugin that’s worth noting. The “Everyone” tab shows general activity on the site in real time. This tab is viewable by both users and non-logged in users alike. So even if a non-logged in user visits your site, they can still get a broad view of the activity happening among site users.

Publishing to the Feed

So how do you publish activities to your activity feed? The easiest way is to set the “scope” parameter of the socialize.showShareUI (“Share plugin”) and socialize.publishUserAction methods to internal or both. This tells Gigya to publish the feed item inside your site scope so it will appear on the Activity Feed Plugin.

You can also listen for callback events from other plugins and send that data to your activity feed. For example, after the onSendDone is fired from our Reactions Plugin you can publish an item to the activity feed using socialize.publishUserAction. In this scenario, set the scope parameter to “internal” since the reaction was already posted a social network.

For more information, check out the Activity Feed Plugin developer’s guide.

2. Use getFriendsInfo

Many times, you’ll have records in your database that you want filtered by some combination of date and social network friends in order to generate an activity feed (game activity, purchased items, etc). Depending on your backend, this often requires creating a comma-limited list of friend IDs in order to run a database query.

A straightforward way of accomplishing this on the server-side is to use our REST API method socialize.getFriendsInfo. With this method, you can retrieve a user’s social network friends and loop over the IDs to create a comma delimited list. In the example below, we’ll use the PHP SDK to build a simple list of Facebook friend UIDs:

[sourcecode language=”php”]
// include PHP SDK
require_once “GSSDK.php”;

// step 1 – define request
$method = “socialize.getFriendsInfo”;
$request = new GSRequest($apiKey,$secretKey,$method);

// step 2 – add parameters
$request->setParam(“uid”, $gigya_UID);
$request->setParam(“enabledProviders”, “facebook”);
$request->setParam(“siteUsersOnly”, true);

// step 3 – send the request to Gigya
$response = $request->send();

// handle response
if ($response->getErrorCode() != 0){
echo (“Got error: ” . $response->getErrorMessage());

// retrieve response data in JSON format
$user_friends = $response->getResponseText();

// convert to PHP array
$friendsinfo = json_decode($user_friends,true);

// loop over friends to create a comma delimited list
$ls_friends = “”;
for($i = 0; $i < count($friendsinfo[‘friends’]); $i++) {
$ls_friends .= $friendsinfo[‘friends’][$i][‘identities’][0][‘providerUID’] . “,”;
// remove last comma
$ls_friends = substr($ls_friends,”,-1);

// output the list
print “<pre>LIST: ($ls_friends)</pre>”

The example above is simple by design, but demonstrates that the process of fetching a list of friend IDs and compiling them for a database query is straightforward. A couple other quick notes:

  • By setting the “siteUsersOnly” parameter to TRUE, only friends who have also registered with your site are returned. This can significantly reduce the number of friends you need to loop over.
  • You may optionally enable only specific providers in the “enabledProviders” property. For example, if you want only the user’s Facebook and Twitter friends, set enabledProviders=”facebook,twitter”.
  • Our definition of a ‘friend’ on Twitter is a person who is both a ‘follower’ and ‘following’ the user.

As you can see, it doesn’t take a lot of effort to start adding activity feeds to your site. This is a popular topic so look for additional blogs about activity feeds in the near future.

By Kevin White