You add the script to your site and a small endpoint to your server. Every pageview and every event you track goes to that endpoint first, not to a third party.
The endpoint ignores bots, drops email addresses and similar personal data from URLs and event properties, and keeps no IP address. Then it hands the event to the storage you configured.