Turbo and other external JS libraries colliding for HTTP 406 errors

I have a relatively obscure but highly difficult bug to fix. I have two external JS libraries (Heap and Inspectlet), both used for analytics purposes, that are colliding with Turbo and causing errors in our Rails app.

When hard refreshing a page and then submitting a form — which is supposed to be submitted by Turbo as a TURBO_STREAM — it is submitted as HTML and, as a result, responded with a 406 (as we don’t have a render for html format).

This appears to happen because these other JS libraries are also hijacking the form submit event in an effort to send analytics. And once they hijack that event, Turbo no longer hijacks it and submits the form correctly as a TURBO_STREAM.

I’ve confirmed that removing these two libraries does indeed fix the 406 error. I’ve also been able to make it work by ensuring uBlock (or another ad blocker) is active, which doesn’t load the analytics JS libraries. Interestingly, I’ve been unable to reproduce this in a development environment on localhost.

Does anyone have thoughts on how this could be fixed? Thanks!