how to keep track of users of your application, and how to remove them properly when they remove it
there are two things we are going to discuss in this article. both of these are very important issues for successful marketing of your app, to generate precise traffic report. and if you want to sell your app or generate revenue, traffic report is very important. so we are going to show you how to maintain a precise list of your active users and how to precisely remove them when they remove your app.
keeping track of your users
this one is fairly easy. but beside storing their user id you should keep track of their session ids. why? for extended permissions and for viral marketing. lets create a mysql table for storing these data, along with the active/inactive
+------------+---------------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +------------+---------------------+------+-----+---------+-------+ | uid | bigint(20) unsigned | NO | PRI | | | | sessionkey | varchar(128) | YES | | NULL | | | active | char(1) | YES | MUL | 1 | | +------------+---------------------+------+-----+---------+-------+
here is the SQL
CREATE TABLE `users` ( `uid` bigint(20) unsigned NOT NULL, `sessionkey` varchar(128) default NULL, `active` char(1) default '1', PRIMARY KEY (`uid`), KEY `activity` (`active`) )
now can you remember that there is a field named “Post authorize redirect url” in the “Authentication” tab of your application settings page? set the data in that field before proceeding further. have a look at the following screenshot. you can see it’s enlarged view by clicking on it
now lets see how to store the user ids and their corresponding session key. in the following code block there are two functions for storing user ids in your database and for updating their session key.
<?php
/**
* insert a new user and corresponding session key in the users table
*
* @param int $uid user id
* @param string $sk session key
* @param resource $db mysql connection resource
* @return bool
*/
function insertUser($uid,$sk,$db)
{
$data= mysql_query("select uid from users where uid='{$uid}'",$db);
if(mysql_num_rows($data)==0)
{
$sql = "INSERT INTO users (uid,sessionkey, active) VALUES('{$uid}','{$sk}','1')";
mysql_query($sql,$db);
return true;
}
return false;
}
/**
* update an user's corresponding session key in users table
*
* @param int $uid user id
* @param string $sk session key
* @param resource $db mysql connection resource
* @return bool
*/
function updateSessionKey($uid,$sk,$db)
{
return mysql_query("update users set sk = '{$sk}' where uid='{$uid}",$db);
}
?>
now in your install.php all you have to do is place the following code.
include_once("config.php");
$sk = $facebook->api_client->session_key;
$db = mysql_connect('host','user','db');
mysql_select_db("db",$db);
insertUser($uid,$sk, $db);
echo "<fb:redirect url='YourApplicationUrl' />";
thats it. now you can periodically update your user’s session key to stay uptodate using updateSessionKey() function.
how to remove your user
remember in the previous screenshot there was a field named “Post-Remove Callback URL” in the application setup page? when any of your application users remove this application from their account, facebook sends some information to this callback url so that you, the application developer, can successfully take necessary steps to remove this user from your database or set the inactive flag “ON”. lets see how to do this. here is the code of “remove.php”
<?php
include_once("config.php");
$facebook = new Facebook($apikey, $secret);
$user = $facebook->get_loggedin_user();
if ($user != NULL &amp;&amp; $facebook->fb_params['uninstall'] == 1)
{
makeUserInactive($uid,$db); //$db is your mysql link indentifier
}
/**
* set an user as inactive, indicating that user has removed your application
*
* @param int $uid user id
* @param resource $db mysql connection resource
* @return bool
*/
function makeUserInactive($uid,$db)
{
return mysql_query("update users set active = '0' where uid='{$uid}",$db);
}
?>
thats it! for reference you can check out facebook wiki entry.
3 Responses to “how to keep track of users of your application, and how to remove them properly when they remove it”
Leave a Reply




























bijon on February 6th, 2009
The articles content is very easy to understand.Nice articles.
However to remove the user why we don’t delete from the table rather then inactive status ?
Hasin Hayder on February 6th, 2009
just to keep track for future reference. and this is helpful for some specific cases, for example, if you want to give the exact status back to returning user (who has removed your application and added it again) like points, stories and other app related data then you must keep track of all of yoru users. no matter if they have removed or not, ur data should persist
and keeping track of inactive users are also good for generating different types of traffic reports
bAsTeR on May 26th, 2010
One question:
If one user uninstall your application then the status of that user is set to active=0, but if the user install again the application, how can you deal with that issue?