Forum Discussion

PerBonomi's avatar
11 years ago

In the spirit of "gamification"

Thinking back to my history in gaming, I came up with a script that lets you track how many consecutive days a user has logged in.

 

Basically this means that you can reward users (with a badge, rank?) for logging in every day.

 

Maybe someone finds it useful.

 

I use three variables, date,a,b. String example: 2014-07-10,1,5.

 

Where date is last date the user logged in.

a Is the running total of consecutive logins. If a user skips a day of logging, this resets to zero.

b Is the max number of consecutive days the user has managed to reach so far.

 

Here's what the script does:

Read string from a custom profile field, field1.

Check last login date (set today if this is the first login (since adding the script, or ever).

If user logged in yesterday as well, increase a by 1.

Check if a is greater than b. If so, increase b by 1.

 

<!-- Consecutive logins -->
<#if user.registered>
<#assign UserDateLast = restadmin("/users/id/${user.id}/profiles/name/field1").value />
<script type="text/javascript">
var vToday = new Date();var dd = vToday.getDate();var mm = vToday.getMonth()+1;var yyyy = vToday.getFullYear();

if(dd<10){dd='0'+dd} 
if(mm<10){mm='0'+mm} 
vToday = yyyy+'-'+mm+'-'+dd;
var vLastLoginDate = '${UserDateLast}';
var arLastLoginDate = new Array();

if (vLastLoginDate == '') {
	arLastLoginDate[0] = vToday;
	arLastLoginDate[1] = 0;
	arLastLoginDate[2] = 1;
	} else {
	arLastLoginDate = vLastLoginDate.split(",");
	}
var firstDate = new Date(arLastLoginDate[0]);
var secondDate = new Date(vToday);
var diffDays = (secondDate - firstDate) / (24 * 60 * 60 * 1000);


if (diffDays > 1) {
	arLastLoginDate[1] = 0;
}

if (diffDays == 1) {
	arLastLoginDate[1] = Number(arLastLoginDate[1]) + 1;
}

if (Number(arLastLoginDate[1]) > Number(arLastLoginDate[2])) {
	arLastLoginDate[2] = Number(arLastLoginDate[2]) + 1;
}

arLastLoginDate[0] = vToday;

$.post('/restapi/vc/users/id/${user.id}/profiles/name/field1/set', {value: '' + arLastLoginDate});
</script>
</#if>

 

  • Awesome idea, but on the topic of games, I'm also reminded of achievement hunters that would try to game the game, so to speak. If you went further and wrapped that date statement in an Ajax call back to the server, I believe that would result in a more reliable time stamp since it doesn't rely on the client side time setting to self-report date and time.

     

    Would result in more server requests though, so that's still not ideal.. but I love the idea of using gamification to encourage users to come back! :)


    • jkwok wrote:

      Awesome idea, but on the topic of games, I'm also reminded of achievement hunters that would try to game the game, so to speak. If you went further and wrapped that date statement in an Ajax call back to the server, I believe that would result in a more reliable time stamp since it doesn't rely on the client side time setting to self-report date and time.

       

      Would result in more server requests though, so that's still not ideal.. but I love the idea of using gamification to encourage users to come back! :)


      On that note :)

      http://community.lithium.com/t5/Developers-Discussion/Badge-for-consecutive-daily-logins/td-p/207172

      • jkwok's avatar
        jkwok
        Adept
        Heh sorry to dig up such an old thread, but thanks for sharing the latest and greatest! :)