On my Angular website I'm injecting various third party scripts to collect analytics. These scripts are lazily loaded and will add event listeners (for f.e. click events) to certain elements on the page.
Normally I'd wrap this kind of functionality in a runOutsideAngular
, but since these scripts are external I don't think I have this power. Note that these scripts have nothing to do with Angular at all.
When running a performance recording or JavaScript profiler (both in Chrome) it seems the analytics functionality is somehow running through zone.js
, while I actually want them to be running as natively as possible.
How can I make sure these scripts are not influenced by, or influencing any Angular zones?
The way zone.js
works it that it patches all asynchronous operations in the application. So it is correct, all your code, even external code, runs in a zone.
But that does not mean that it runs in the Angular zone and that it causes change detection cycles. It runs in a different zone than Angular. That's the reason Angular even uses zones - to separate Angular app from other scripts.