How to capture the domain name as a Custom Dimension in Matomo
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
- In Matomo, go to Administration
> Websites > Custom Dimensions.
- Under Action Dimensions, click Configure a New Dimension.
- Provide a descriptive name e.g., Domain name and set the dimension as Active.
- Click Create to save.
- Take note of the ID assigned to this Custom Dimension.
Modify the Matomo tracking code
- In your site’s tracking code, before the
trackPageView
call, insert a line that sends the hostname to Matomo usingwindow.location.hostname
:
_paq.push(['setCustomDimension', 1, window.location.hostname.replace(/^www\./, '').toLowerCase()]);
- The Matomo tracking code must be placed within the
<head>
tag of all the pages you are tracking (on all domains). - Replace the ID
1
insetCustomDimension
with your dimension ID andyour-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
- In Matomo Tag Manager, go to Variables.
- Click Create New Variable and choose the Custom JavaScript type.
- 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();
} - Click Create New Variable to save.
Configure the Matomo Analytics tracking tag
- In Matomo Tag Manager, go to Tags.
- 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.
- Scroll down to Custom Dimensions and add the dimension ID of the Action dimension.
- In the Value field, either select the built-in variable
{{PageHostname}}
or the Custom JavaScript variable created earlier.
- Use a Pageview trigger or for SPAs, you can use a History Change trigger.
- Save the changes.
- Test the Tag Manager configuration using Preview / Debug mode to ensure the trigger and tag fires and the variable captures the domain name.
- Perform test actions on the different domain pages.
- 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
- In Matomo, go to Administration
> Websites > Custom Dimensions.
- Under Action Dimensions, click Configure a New Dimension.
- Provide a descriptive name e.g., Domain name and set the dimension as Active.
- For the Extract value options, choose Page URL and enter the following regex pattern:
^(?:http(?:s?)://(?:www.)?)?([A-Za-z0-9_:.-]+)/?
- Click Create to save.
- Take note of the ID assigned to this Custom Dimension.
Set the Custom Dimension in the tracking code or tag
- In your site’s tracking code, before the
trackPageView
call, insert the line_paq.push(['setCustomDimension', 1, 'dimensionValue']);
- Replace the ID
1
insetCustomDimension
with your dimension ID. - For domain A pages, specify the
dimensionValue
with the domain name and on domain B pages, specify thedimensionValue
with the different domain name. - The Matomo tracking code must be placed within the
<head>
tag of all the pages you are tracking (on all domains). - If you want to use this method with Matomo Tag Manager, add the custom dimension to your Matomo tracking tag.
- Perform test actions on the different domain pages.
- 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.