Run a Script when a vision client loses connection to the gateway

I have a client event script on shutdown and it runs when the app is closed or when the user logs out. The script runs a names query and update a database to track how long a user is using the app. However sometimes the PC that the client is running on loses connection to the gateway. So the script on shutdown doesn’t run. Is there away to run a script when the connection is lost? I was thinking about a Gateway Event Scripts on tag change maybe. Any help would be greatly appreciated, Thank you.

Do your telemetry the other way around. Run a periodic task on the gateway, that collects all clients it ‘knows’ about (via system.util.getSessionInfo) - and then check for ‘missing’ sessions compared to the last run to know when to ‘close’ a session in your monitoring.