# Page title generation and page:changed event

**URL:** <https://meta.discourse.org/t/page-title-generation-and-page-changed-event/158170>\
**Category:** Product Support\
**Created:** [19 juli 2020 om 14:49 UTC](https://meta.discourse.org/t/page-title-generation-and-page-changed-event/158170 "2020-07-19T14:49:46Z")\
**Posts on this page:** 1\
**Showing post:** 1

<div class="post-metadata">

**Author:** ![gar1t](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/gar1t/32/380142_2.png) [@gar1t](https://meta.discourse.org/u/gar1t)\
**Post date:** [19 juli 2020 om 14:49 UTC](https://meta.discourse.org/t/page-title-generation-and-page-changed-event/158170/1 "2020-07-19T14:49:46Z")

</div>

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:

[https://github.com/discourse/discourse/blob/master/app/assets/javascripts/discourse/app/lib/page-tracker.js#L31](https://github.com/discourse/discourse/blob/master/app/assets/javascripts/discourse/app/lib/page-tracker.js#L31)

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):

> <https://github.com/guildai/discourse-matomo-analytics/blob/master/common/head_tag.html#L2-L13>

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.)

---

_[View the full topic](https://meta.discourse.org/t/page-title-generation-and-page-changed-event/158170)._
