When your Matomo site tracks multiple domains, the standard Pages report only shows page paths like /index. To differentiate traffic by domain, you can create a report to capture the domain name for the action URL.

This guide explains how to use the window.location JavaScript function or a regular expression pattern to capture the domain name, which is then passed to Matomo in an Action-scope Custom Dimension. Both approaches can be used with Matomo Tag Manager or the Matomo tracking code.

Use window.location to capture the domain

You can use JavaScript’s window.location function to automatically capture the domain name of each pageview. This method extracts the hostname directly from the page URL and sends it to Matomo as a Custom Dimension.

Note: Ensure the Matomo website settings include the URLs of the domains you want to track.

Create an Action Custom Dimension

  1. In Matomo, go to Administration Settings Cog Icon > Websites > Custom Dimensions.
  2. Under Action Dimensions, click Configure a New Dimension.
  3. Provide a descriptive name e.g., Domain name and set the dimension as Active.
  4. Click Create to save.
  5. Take note of the ID assigned to this Custom Dimension.

Modify the Matomo tracking code

  1. In your site’s tracking code, before the trackPageView call, insert a line that sends the hostname to Matomo using window.location.hostname:
    _paq.push(['setCustomDimension', 1, window.location.hostname.replace(/^www\./, '').toLowerCase()]);
  2. The Matomo tracking code must be placed within the <head> tag of all the pages you are tracking (on all domains).
  3. Replace the ID 1 in setCustomDimension with your dimension ID and your-matomo-domain.example.com with your actual Matomo instance domain.
<script>
  var _paq = window._paq = window._paq || [];
  /* tracker methods like "setCustomDimension" should be called before "trackPageView" */
  _paq.push(['setCustomDimension', 1, window.location.hostname.replace(/^www\./, '').toLowerCase()]);
  _paq.push(['trackPageView']);
  _paq.push(['enableLinkTracking']);
  (function() {
    var u="//your-matomo-domain.example.com/";
    _paq.push(['setTrackerUrl', u+'matomo.php']);
    _paq.push(['setSiteId', '1']);
    var d=document, g=d.createElement('script'), s=d.getElementsByTagName('script')[0];
    g.async=true; g.src=u+'matomo.js'; s.parentNode.insertBefore(g,s);
  })();
</script>

Matomo Tag Manager

If you are using Matomo Tag Manager instead of the tracking code, you can also capture the domain name directly by:

  • adding the Tag Manager built-in variable {{PageHostname}} to return the hostname of the current page, or
  • creating a Custom JavaScript variable to extract and transform or standardise the hostname of the current page each time the tracking tag fires.

Add a Custom JavaScript variable

  1. In Matomo Tag Manager, go to Variables.
  2. Click Create New Variable and choose the Custom JavaScript type.
  3. Provide a descriptive name and add the JavaScript function that returns the domain name from the page URL.
    function () {
    return window.location.hostname.replace(/^www\./, '').toLowerCase();
    }
  4. Click Create New Variable to save.
    add javascript function variable

Configure the Matomo Analytics tracking tag

  1. In Matomo Tag Manager, go to Tags.
  2. Edit your existing Matomo Analytics tracking tag or add a new one by clicking Create New Tag > Matomo Analytics tag and define the settings as required.
  3. Scroll down to Custom Dimensions and add the dimension ID of the Action dimension.
  4. In the Value field, either select the built-in variable {{PageHostname}} or the Custom JavaScript variable created earlier.
    add custom dimension to Matomo tag
  5. Use a Pageview trigger or for SPAs, you can use a History Change trigger.
  6. Save the changes.
  7. Test the Tag Manager configuration using Preview / Debug mode to ensure the trigger and tag fires and the variable captures the domain name.
  8. Perform test actions on the different domain pages.
  9. Go to Matomo > Behaviour and select the Action Dimension you created e.g, Domain Name. The report will show action URLs grouped by domain name.

Use regular expression (regex pattern)

Alternatively, use a regular expression in the Action Custom Dimension that extracts the domain name from the page URL. Note: Ensure the Matomo website settings include the URLs of the domains you want to track.

Create an Action Custom Dimension

  1. In Matomo, go to Administration Settings Cog Icon > Websites > Custom Dimensions.
  2. Under Action Dimensions, click Configure a New Dimension.
  3. Provide a descriptive name e.g., Domain name and set the dimension as Active.
  4. For the Extract value options, choose Page URL and enter the following regex pattern:
    ^(?:http(?:s?)://(?:www.)?)?([A-Za-z0-9_:.-]+)/?
  5. Click Create to save.
  6. Take note of the ID assigned to this Custom Dimension.
    use regex for action dimension

Set the Custom Dimension in the tracking code or tag

  1. In your site’s tracking code, before the trackPageView call, insert the line _paq.push(['setCustomDimension', 1, 'dimensionValue']);
  2. Replace the ID 1 in setCustomDimension with your dimension ID.
  3. For domain A pages, specify the dimensionValue with the domain name and on domain B pages, specify the dimensionValue with the different domain name.
  4. The Matomo tracking code must be placed within the <head> tag of all the pages you are tracking (on all domains).
  5. If you want to use this method with Matomo Tag Manager, add the custom dimension to your Matomo tracking tag.
  6. Perform test actions on the different domain pages.
  7. Go to Matomo > Behaviour and select the Action Dimension you created e.g, Domain Name. The report will show action URLs grouped by domain name.

Both methods, using window.location or a regex pattern will achieve the same goal of capturing the domain name in Matomo. The window.location approach may be easier to maintain while the regex method can be useful if you need to extract specific parts of a URL or apply more complex formatting. Choose the option that best fits your tracking setup and reporting needs.

Previous FAQ: How do I measure and give users access to reports for page sections / categories / mini-sites / languages?