Page title generation and page:changed event

This question is in the weeds of the API.

I’m using a fork of the excellent Matomo theme component. This lets you track page views with Matomo. My fork is an attempt to work-around issues. It’s essentially identical. The behavior described below occurs with the upstream component as well.

The component uses the onPageChange callback to configure a Matomo API/facility.

The issue I’m having is that, at times, the onPageChange callback is made before the page title is generated. This is inconsistent. Sometimes the title is fully formed, sometimes it’s not.

The result is that the Matomo log history has a mix of fully-formed page titles and not.

Looking over the Discourse source code, this is called prior to the page:changed trigger:

This all looks like it should work - the title should be refreshed by the time onPageChange is called. It is not - at least not consistently.

There’s another issue that may be related. onPageChange is sometimes called twice. This is my work-around (and the motivation for the fork):

In the case when onPageChange is called called twice for the same page view, one of the two calls provides a url arg that’s different from the current window location. I check this and exit early.

(Note that I handle the page:changed event via onAppEvent rather than onPageChange but I see the same behavior in either case.)

1 Like

I’ve tested seeing if the title gets set with the following code:

<script type="text/discourse-plugin" version="0.2">
api.onPageChange( (url, title) => {
    console.log('url', url);
    console.log('title', title)
})
</script>

What I’m finding is that if I create a new topic, the topic’s title is not set on the call to api.onPageChange that is made immediately after creating the topic. In that case, the site’s title is used instead. Does this seem to be what’s happening on your site? If you are seeing no title at all, make sure that you have filled in the title and site description site settings. Those settings are found on the “Required” tab of your site settings page.

2 Likes

Thanks for looking into this Simon. That is not what I’m seeing. I put together a screen recording and I’ll get you a link out-of-band along with a reference to the theme component source in play.

The gist though is that titles are not rendered on an initial call to a topic from a loaded app. On an entry page (i.e. the first time the app is loaded) I see the title in onPageChanged and on any page after the first. The recording makes this a lot clearer hopefully :slight_smile: