Is Knack capable of saving the date to see who logged in & how long they were on the site?

I want to create a page where I can check how many users were logged in on a specific day and capture how long they were on the site. Just wondering if this is possible on Knack? Thanks!

Thanks, 9144729187, that's a great solution!

I got it working, except if I use ".all" where you have "knack-scene-render.scene_70.scene_76", it doesn't work. I don't mind listing individual scenes, but it means every time we add a new page, I'll have to remember to add the scene to the list. We're doing a lot of development right now, so I'm concerned I will forget. Any ideas on how to manage that a little better?

Thanks again, your blog post is very well explained and very much appreciated!

Of course David is correct in advising you to avoid exposing your API key!

You may also be interested interested in my article on this subject:

https://www.onlinedatabase.expert/post/adding-an-audit-trail-to-a-knack-app

Julian

Van, that is correct. Sam, Conor, Derek, this and any other code that uses your API key on the frontend (the JS section of Knack) should not be used. The API key provides anyone with access to anything you can do yourself in the builder, including accessing, modifying, and deleting all your records.

Instead, you should create a new, login-protected page and hide it from the menu. Then, add a form to edit the logged-in user's record. In that form, remove all fields except the one that you wish to edit using the code (ie. the date field). With the view still open in the builder, look in your address bar for the scene_key and view_key. Paste those into the apiScene and apiView variables of this code (https://gist.github.com/hmnd/61a81853b0d927fbd10d4078a5fedaca), replacing the FIELD_KEY variable with your date field key, as well. That should work! I haven't had a chance to test my fixed code yet, so let me know if there's an issue with it. The manipulation of the date is really wonky, too, so I'll fix that later if I have a chance.

If you need further assistance with this or anything else dev or Knack related, feel free to reach out to me at david@hmnd.io.

Nice, but doesn't this expose your API key?  

Knack developer docs: 
"We strongly recommend that you do not use object-based requests from any client-side code, including custom JavaScript added directly to your Knack apps, as this will expose your API key."

7535514547 - just used this; works perfectly - very many thanks

Thanks for the code Sam, works like a charm!

I have this working w/o the duration part of your question. Basically, it's javascript to record last login on a user which is recorded on the account object. Then I have another object just to hold a table of dates and important items I'd like to keep record of on that date. I run a task just before midnight that records count of accounts with last login of 'today'. 

I didn't come up with the javascript portion, It was on a community post that's no longer accessible, so I'll put what I have in my app below. It was originally here, but now it asks for a login... 

http://helpdesk.knackhq.com/support/solutions/articles/5000528001-working-with-the-logged-in-user#remote

Things to modify for your own use...

  1. object_2 should be the object of your ACCOUNTS object.
  2. replacewithyourownappid should be your Application ID
  3. replacewithyourownkey should your API Key
  4. 315 should the field ID of the DATE/TIME field in your ACCOUNTs Object
// Javascript to set date & time a user accesses any page
$(document).on("knack-scene-render.any", function(event, view) {
var user_rec_id=Knack.getUserAttributes().id;
var api_url="https://api.knack.com/v1/objects/object_2/records/"+user_rec_id;
var string=new Date().toLocaleString();
var resultArray=string.split(":");
var result=string.replace(":"+resultArray[2]," ")+resultArray[2].split(" ")[1];
var data={field_315: result};

$.ajax({
url: api_url,
type: "PUT",
headers: {"X-Knack-Application-Id":"replacewithyourownappid","X-Knack-REST-API-Key":"replacewithyourownkey"},
data: data,
success: function(data, textStatus, xhr) {
console.log("Date set: "+data.field_315);
},
error: function(xhr) {
console.log("Error ! Status: "+xhr.status+" "+xhr.statusText);
}
});
});

Hello,

 

I don't think knack provide this facility to us but using custom JavaScript code we can achieve this.

 

Regards,

Sunny Singla

ssingla1985@gmail.com

+919855089359