Redirect users on first login using Rules and User Stats

This tutorial will utilize the Rules and the User Stats module in order to redirect the user based on the number of logins. This could be the first login, the tenth, first ten, etc. With a slight variable tweak, this will also allow you to redirect a user based on the number of posts, days since last post, join date, and more…

IMPORTANT: In order for this to work, you must enable the “View statistics” permission for Authenticated users (under user/permissions).

Modules required:

Code used in the video:

<?php return (user_stats_get_stats('login_count', $account->uid) == 1); ?>

User Stats module fix:
in /sites/all/modules/user_stats/



Replace with


Note: If you want a simple redirect after registration, or email confirmation, you can use Logintoboggan ( If you need more advanced integration, follow the steps in the video.

Be Sociable, Share!
Tarik Assagai
Tarik (@tarik916) is a web developer and owner of ACS Digital Media, a Sacramento based web development agency specializing in open source CMS systems. If you are looking for support for your current or upcoming projects, visit


  1. Rachel /

    Thank you so much for this very helpful video, it helped me tremendously! I few things to point out that may help others.

    1. I changed the code to:
    uid) == 1) { echo 1;} else { echo 0;} ?>
    it just would not work with the shorthand if statement (it could have been my mistake).

    2. In order to print out some of the stats values I needed to give permissions to the authenticated user.
    Thanks again!

    • tarik916 /

      I’m glad the video was helpful. Sorry you were having trouble with the shorthand. I believe I copied it directly from my working example, but I will double-check. You are right about the permissions. I forgot to mention this in my video. Thank you!

  2. Thanks so much for this post. I was pulling my hair out trying to get this to work and finally found this post. Thanks for the great write-up.

  3. Huma Sayyed /

    Hey I have a problem that everytime I logout and login it goes to the same page. I’ve written the syntax as below in the condition statement.
    uid) == 1) ? ‘1’ : ”; ?>

    • tarik916 /

      If your code looks exactly like that, you will need to replace all of the apostrophes with the unformatted version.
      This uid) == 1) ? ’1′ : ”; should look like this: uid) == 1) ? ‘1’ : ”;

  4. Thanks for the post! This is exactly what I was looking for — LoginTobbogan was too heavy for me (and did all kinds of stuff that I didn’t want it to do).

    One note — I couldn’t get the condition on the rule to work as you had it. I’m using Drupal 7 and all of the latest versions of the modules. This is what ended up working for me:

    return (user_stats_get_stats(‘login_count’, $account->uid) == 1);

    • tarik916 /

      Great! The return value works in all versions, so I have updated my code. Thanks!

  5. Hello Tarik916,

    Thank you for this video it is very helpful,

    But i have only one problem. It will works only when the user has the uid 1.
    how do I make multiple users?


    • tarik916 /

      I’m sorry you are having issues. The user ID should not affect the code, but it would if permissions are not set for the user to access stats. Check user permissions and make sure the “View statistics” option is checked for authenticated users.

  6. thank you for your reply.

    now it works

    it was the permissions of the user stats

  7. Evelyn /


    great tutorial, I was wondering, is it possible to check if it’s the first time a user has logged in with a particular role?

    Thanks again,


    • tarik916 /

      Yes, this can be done using Rules. In the same area where you added a condition to check the login count, you can add a condition to check the user group.

  8. Evelyn /


    thank you for your quick response. I tried using the condition of role but unfortunately the rule still judges on whether it’s the first time the user has logged in rather than the first time the user has logged in with a specific role.

    Any ideas?

    Thanks again,


    • tarik916 /

      Yes, you would need to combine the conditions into an “And” group. Click the arrow next to one of the conditions, to form and or group. Edit the group and change “Or” to “And.” Add the second condition to the group.

  9. Evelyn /


    I decided to take an alternative approach but thank you very much for your help, it’s been appreciated.


  10. Hi, thanks for the video. I’m building a site that reward points to the user based on their logins. I have two questions:

    1. I want to check if a user is logged in for the first time on the same day.
    2. I want to also check if a user has logged in on 5 consecutive days.

    Any ideas?

    • tarik916 /

      Since User Stats can track “days since last login,” you can create a separate rule using this variable. That should make it pretty easy to track whether or not they have logged in within x number of days.

  11. Thank you so much for this video tutorial! It really worked me out, although I had to do a different workaround with Drupal 7.

    Thanks again!

    – Cesar

  12. Helene /

    I couldn’t get the not-first-time condition (to the code in the example) working so I wrote a new condition using php code that does something equivalent in a different way:

    If ([account:user-last-login-raw]) {
    return TRUE;
    else {
    return FALSE;

Leave a Reply