event.targetTouches generates javascript error on some browsers

I’ve built a custom carousel with momentum and all works fine on my own browsers. However, Sentry gives me the following error “Cannot read properties of undefined (reading ‘0’)” for some Windows browsers, referring to my line of code: event.targetTouches[0].pageX.

Since not all browsers support targetTouches, I added a condition if’(TouchEvent’ in window), but still the same error message appears.

Does anybody know a method to receive the mouse/touch position which works for all browsers (or with proper error handling for browsers that don’t support)

My current code to detect mouse-position:

function getSwipeMousePosition(event) {
    if ('TouchEvent' in window) return {
       ‘x’: event.pageX || event.targetTouches[0].pageX,
       'y': event.pageY || event.targetTouches[0].pageY
    };
}

In case it helps, my full code can be found at takeyourbackpack.com (search for getSwipeMousePosition)