Depending on what you want to do this could be wildly varied, but to call a function, or do something on every page change by the user you could use the plugin API.
Here you can read through a developers guide to using Discourse theming/components (custom JS etc).
Here you can find withPluginApi information.
This is a simple example of me logging the current URL as well as the page title.
I am doing this in the common/head_tag.html
file. Which can be edited at /admin/customize/themes/9/common/head_tag/edit
<script type="text/discourse-plugin" version="0.8">
api.onPageChange((url, title) => {
console.log(url, title);
});
</script>