-
-
Save hmnd/61a81853b0d927fbd10d4078a5fedaca to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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