Skip to content

Instantly share code, notes, and snippets.

@hmnd
Created April 16, 2020 21:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hmnd/61a81853b0d927fbd10d4078a5fedaca to your computer and use it in GitHub Desktop.
Save hmnd/61a81853b0d927fbd10d4078a5fedaca to your computer and use it in GitHub Desktop.
// Javascript to set date & time a user accesses any page
$(document).on('knack-scene-render.any', function (event, view) {
var userId = Knack.getUserAttributes().id;
var apiScene = 'SCENE_KEY';
var apiView = 'VIEW_KEY';
var dateField = 'FIELD_KEY';
var apiUrl = `https://api.knack.com/v1/scenes/${apiScene}/views/${apiView}/records/${userId}`;
var dateStr = new Date().toLocaleString();
var resultArray = dateStr.split(':');
var result =
dateStr.replace(':' + resultArray[2], ' ') + resultArray[2].split(' ')[1];
var data = { [dateField]: result };
$.ajax({
url: apiUrl,
type: 'PUT',
headers: {
'X-Knack-Application-Id': Knack.application_id,
'X-Knack-REST-API-Key': 'knack',
Authorization: Knack.getUserToken(),
},
data: data,
error: function (xhr) {
console.error('Error saving user date. Status: ' + xhr.status + ' ' + xhr.statusText);
},
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment