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

**URL:** <https://forums.knack.com/t/is-knack-capable-of-saving-the-date-to-see-who-logged-in-how-long-they-were-on-the-site/9236>\
**Category:** Ask Developers\
**Created:** [December 21, 2018, 5:29am UTC](https://forums.knack.com/t/is-knack-capable-of-saving-the-date-to-see-who-logged-in-how-long-they-were-on-the-site/9236 "2018-12-21T05:29:27Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![AlaizaGeen68759](https://sea2.discourse-cdn.com/flex020/user_avatar/forums.knack.com/alaizageen68759/32/260_2.png) [@AlaizaGeen68759](https://forums.knack.com/u/AlaizaGeen68759)\
**Post date:** [December 21, 2018, 5:29am UTC](https://forums.knack.com/t/is-knack-capable-of-saving-the-date-to-see-who-logged-in-how-long-they-were-on-the-site/9236/1 "2018-12-21T05:29:27Z")

</div>

I&nbsp;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!

---

<div class="post-metadata">

**Author:** ![KimberlyNi07205](https://avatars.discourse-cdn.com/v4/letter/k/ccd318/32.png) [@KimberlyNi07205](https://forums.knack.com/u/KimberlyNi07205)\
**Post date:** [May 29, 2020, 4:30pm UTC](https://forums.knack.com/t/is-knack-capable-of-saving-the-date-to-see-who-logged-in-how-long-they-were-on-the-site/9236/2 "2020-05-29T16:30:35Z")

</div>

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!

---

<div class="post-metadata">

**Author:** ![JulianKirkness](https://sea2.discourse-cdn.com/flex020/user_avatar/forums.knack.com/juliankirkness/32/770_2.png) [@JulianKirkness](https://forums.knack.com/u/JulianKirkness)\
**Post date:** [April 16, 2020, 10:26pm UTC](https://forums.knack.com/t/is-knack-capable-of-saving-the-date-to-see-who-logged-in-how-long-they-were-on-the-site/9236/3 "2020-04-16T22:26:36Z")

</div>

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](https://www.onlinedatabase.expert/post/adding-an-audit-trail-to-a-knack-app)

Julian

---

<div class="post-metadata">

**Author:** ![hmnd](https://sea2.discourse-cdn.com/flex020/user_avatar/forums.knack.com/hmnd/32/190_2.png) [@hmnd](https://forums.knack.com/u/hmnd)\
**Post date:** [April 16, 2020, 10:00pm UTC](https://forums.knack.com/t/is-knack-capable-of-saving-the-date-to-see-who-logged-in-how-long-they-were-on-the-site/9236/4 "2020-04-16T22:00:20Z")

</div>

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)[https://gist.github.com/hmnd/61a81853b0d927fbd10d4078a5fedaca](https://gist.github.com/hmnd/61a81853b0d927fbd10d4078a5fedaca)), replacing&nbsp;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](mailto:david@hmnd.io).

---

<div class="post-metadata">

**Author:** ![Vincent94744](https://avatars.discourse-cdn.com/v4/letter/v/df788c/32.png) [@Vincent94744](https://forums.knack.com/u/Vincent94744)\
**Post date:** [April 12, 2020, 9:06pm UTC](https://forums.knack.com/t/is-knack-capable-of-saving-the-date-to-see-who-logged-in-how-long-they-were-on-the-site/9236/5 "2020-04-12T21:06:27Z")

</div>

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

Knack developer docs:&nbsp;   
"We strongly recommend that you do&nbsp;_not_&nbsp;use object-based requests from any client-side code, including&nbsp;[custom JavaScript](https://www.knack.com/developer-documentation/#jquery-amp-javascript)&nbsp;added directly to your Knack apps, as this will expose your&nbsp;[API key](https://www.knack.com/developer-documentation/#find-your-api-key-amp-application-id)."

---

<div class="post-metadata">

**Author:** ![DerekArmsh28993](https://avatars.discourse-cdn.com/v4/letter/d/b5a626/32.png) [@DerekArmsh28993](https://forums.knack.com/u/DerekArmsh28993)\
**Post date:** [November 20, 2019, 10:18am UTC](https://forums.knack.com/t/is-knack-capable-of-saving-the-date-to-see-who-logged-in-how-long-they-were-on-the-site/9236/6 "2019-11-20T10:18:36Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![ConorPower39695](https://avatars.discourse-cdn.com/v4/letter/c/ecccb3/32.png) [@ConorPower39695](https://forums.knack.com/u/ConorPower39695)\
**Post date:** [February 28, 2019, 1:51pm UTC](https://forums.knack.com/t/is-knack-capable-of-saving-the-date-to-see-who-logged-in-how-long-they-were-on-the-site/9236/7 "2019-02-28T13:51:17Z")

</div>

Thanks for the code Sam, works like a charm!

---

<div class="post-metadata">

**Author:** ![SamCarney81930](https://avatars.discourse-cdn.com/v4/letter/s/bbce88/32.png) [@SamCarney81930](https://forums.knack.com/u/SamCarney81930)\
**Post date:** [December 31, 2018, 9:45pm UTC](https://forums.knack.com/t/is-knack-capable-of-saving-the-date-to-see-who-logged-in-how-long-they-were-on-the-site/9236/8 "2018-12-31T21:45:09Z")

</div>

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'.&nbsp;

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...&nbsp;

[http://helpdesk.knackhq.com/support/solutions/articles/5000528001-working-with-the-logged-in-user#remote](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** &nbsp;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);
}
});
});
```

---

<div class="post-metadata">

**Author:** ![Sunny\_Singla](https://sea2.discourse-cdn.com/flex020/user_avatar/forums.knack.com/sunny_singla/32/1007_2.png) [@Sunny\_Singla](https://forums.knack.com/u/Sunny_Singla)\
**Post date:** [December 25, 2018, 2:31pm UTC](https://forums.knack.com/t/is-knack-capable-of-saving-the-date-to-see-who-logged-in-how-long-they-were-on-the-site/9236/9 "2018-12-25T14:31:20Z")

</div>

Hello,

&nbsp;

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

&nbsp;

Regards,

Sunny Singla

[ssingla1985@gmail.com](mailto:ssingla1985@gmail.com)

+919855089359
