How do I set a custom page title using the Matomo javascript title?
Sometimes the page titles are quite long and unclear in the reports which are generated inside Matomo. It can cause difficulties in reading reports because a user is unable to understand and analyze the visitor behavior for a particular page. Therefore, in order to overcome this challenge, Matomo has the capability of setting up a Custom Page Title. The Custom Page Title helps in easily identifying the pages in reports Behaviour > Page titles. We need to set the Page Title that Matomo will use before we track a page view. In our examples below, we include the trackPageview JavaScript function to set the document title before we track a page view. It is not needed to include the extra ‘trackPageView’.
By default Matomo uses the title of the HTML page to track the page title. Therefore, it becomes easy to customise it by using the function setDocumentTitle
:
_paq.push(['setDocumentTitle', document.title]);
_paq.push(['trackPageView']);
We can manually set the page title to anything we would like to. This can be useful if we want to set a custom page title on one page. For example:
_paq.push(['setDocumentTitle', "My Custom Title"]);
_paq.push(['trackPageView']);
If we track multiple subdomains on the same website, we may want our page titles to be prefixed by the sub-domain to make it easy to see the traffic and data for each sub-domain. In order to do so, please add the below changes:
_paq.push(['setDocumentTitle', document.domain + "/" + document.title]);
_paq.push(['trackPageView']);
Advanced users can also dynamically generate the page name using PHP, for example:
_paq.push(['setDocumentTitle',"<?php echo $myPageTitle ?>"]);
_paq.push(['trackPageView']);
A custom URL can similarly be set using the Matomo javascript tracker.