Had an ask today to start reporting on how users are using our app. If the user is creating data, this is pretty easy as long as we’ve added and are populating belongs-to user and created_at fields on our models which we have been. But what if the user is only viewing data and not adding anything new? We could update the app to set a “last_logged_in” time on the user whenever they hit the main view, but this would only capture data moving forward.
I checked the data model and found this on each user indicating every device they’ve enrolled in the app:
But these aren’t actually fields on our user model so I’m not sure how to access them. Is there a way to get at this information from a user object in the app? Specifically we’d want to iterate across the device data for each user.
Hey Fletcher
If you are just interested in knowing which users have been “active” in the app, across all their devices, then you can make use of the “Sessions” API, docs here
This will give you programmatic access to the same information as per your screenshot.
If you want to get this information in the app for the current device then you can look at the journey.diagnostics
information available from your App JS
, docs here
Other than that, there are also more comprehensive ways of tracking user activity in your app workflows, but it involves more coding. For example you can include Google Analytics in your views which allows you to track user activity a lot more granularly.
1 Like
Thanks Tielman. I created a simple cloud code task for retrieving the user data via API:
export async function run() {
let users = await downloadUserData(this.backend);
return users;
}
async function downloadUserData(backend) {
let response = await fetch(backend.url + '/users', {
headers: {
Authorization: backend.authorization
}
});
if(!response.ok) {
throw new Error(response.statusText);
}
let data = await response.json();
return data.objects;
}
And then in the app:
var user_datas = CloudCode.callTask("get_users_data");
if (user_datas) {
for (var i=0; i<user_datas.length; i++) {
var user_data = user_datas[i];
var user = DB.user.first(user_data.id);
// creates a "user_usage" object and adds to an array in the view for displaying in object-table
var usage = getOrAddUsage(user);
if (user_data.enrolled) {
usage.last_login = new Date(user_data.last_connected_at);
}
}
}
Also made use of this guide for exporting the table to CSV. Customer is going to be very happy
1 Like
Minor gotcha: the /users endpoint is paginated and will only return the first 50 users by default. There’s an optional parameter called “limit” to increase this. I want to always get all users, so I’m using:
let response = await fetch(backend.url + "/users?limit=99999", ...
which now works
Note: The limit is capped at 1000. It’s a silent cap - no error returned, it just doesn’t have an effect to specify a bigger limit.