Create a new row when someone logs in

What I’m trying to do:
Hi! I’m trying to work out how to track app useage. I’d love to be able to create a new row in a datatable each time someone loads up the app.

What I’ve tried and what’s not working:
I’ve read the docs on the users etc and have been using it for years now, but I don’t know where to start when it comes to tracking logging in. I guess I could track each time a form is loaded, but thats not exactly what I am after.

Since (in anvil anyway) rows are a finite resource, but columns and data are not, you should probably store login timestamps in a growing simple object column that somehow references each user, instead of creating a row every time they log in. Either a separate table linked to the users table, or within the users table itself.

If you then want to run analysis on the data, transform it before analyzing it, since time stamps are already ordinal.

Can’t you just call a server function, right after the login function for the user is called? (You can even qualify it with checking if the login was successful first) and from inside the server module, you should also have access to the logged in users user table row.

2 Likes

I just realized you might be asking how to store info for non-logged in users.

In that case I would do the same thing as above, but use a hashed version of the client context data as a unique user key to keep my timestamps.

Then I would definitely be using a separate table from users, but still storing the login timestamps in a simple object column, and a unique key column that would either be the logged in user or this hashed context info to avoid duplicates.

1 Like

There’s no perfect trigger to use for this that I know of. But, I think detecting when a new session has started probably comes close. This has to happen on the server side, and for best results is probably a check added to every server function (could be done as a decorator). Check for a session key, if it isn’t there add it and add the login information to the data tables. On subsequent calls in the same session, the session key will be there.

Edit: this obviously depends a lot on the flow of your app. If someone can use your app before logging in, then the start of a session just signals the app loading. You might not have user information at that point.

If you have a single point in the client where the login happens, you could always add a server call after that to check to see if it’s a new login this session.

1 Like

Not only I do have a single point in the client to manage the login, that single point is unique to all my apps.

I never manage logging in or out on any of my apps. I have a custom component called HeaderWithLogin that I add to the header of every app.

The custom component shows the name of the logged user, uses a custom form to manage the login process, manages the user permissions and has the logged_in and logged_out events.

When the app starts one of the two events is always called. If the app remembers the user being logged in, then logged_in is called, otherwise logged_out is called.

In the logged_in event the app checks the permission and shows whatever can be shown.
In the logged_out event the app decides whether to ask the user to log in or to show a public form.

For me would be very easy to do what you are asking: I could log the users logging in or out either in the app’s event app by app, or globally in the custom component.

3 Likes

Ah guys. Thanks so much for the advice. If you were here I would give you a hug, but you’re not so… erm, I guess I’ll ‘send’ you a hug by typing ‘sending hug’ :slight_smile: LOL

but seriously, I appreciate the time to help.