You could use the is_page()
function on your footer to check if it is a 404 page, then don't add the js code if it is the 404 page. Added the commenter is right - a 404 is a response, not a page, so you can't test for it. (I blame temporary insanity...) So .... the answer is now....
Or, createupdated Create a 404 page template that doesn't include the js code. That template will be used is a page request causes a '404' response.
Related to your question: I also worry about incomplete tracking because of ad blockers. So developed a server-side tracker that sends GA tracking data. Ad-blockers never see it, so I get full tracking data. (With GDPR notifications, of course.) Perhaps an alternative for you.