Javascript in theme component only runs once?

Use the page tracker API to call the necessary function on page change. Here an example:

https://github.com/discourse/discourse-matomo-analytics/blob/master/common/head_tag.html