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.
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.
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.