How to combine hostnames for the same site?

I’m converting to Piwik PRO from Google Analytics. In the Piwik PRO reports, I’m seeing URLs for the same page with different hostnames listed as separate line items, for example:

https://foo.com/page.html
https://www.foo.com/page.html

Note that the only difference is the hostname, and both hostnames point to the same site. In GA, these are combined and listed simply as /page.html. I would like Piwik PRO to work the same way, or at least to treat www.foo.com and foo.com as the same thing. Is there a way to achieve this?

Hi! There are a few ways in which this can be addressed:

  • setting up a 301 redirect on the tracked site to make sure that there is only one valid URL for the same page (it could be also beneficial from the SEO perspective)
  • fixing the URL on the tracking level. To do that, you can add a custom tag in the tag manager module that will modify the URL before sending it to the analytics backend. Here is the command that can be used. Current url is always available under location.href.
    You can also remove the domain entirely (e.g. by calling _paq.push(['setCustomUrl', window.location.pathname.substr(1) + location.search]); ).

Thanks for the tips. It would be beneficial if you could do some filtering after the fact not only on hostname, but also for example to filter out spam traffic. Is this something that’s on the roadmap?

For filtering after the fact, you can use segmentation or dimension value grouping. I understand that it’s not that convenient when working with lots of unique values, so I’ll add a feature request regarding this topic.

Hi @WWNL,

Another option would be creating a custom report, using our extraction feature to extract the path of a Page URL
screenshot_1634

Regarding the Spam part, you can enable the “collect data only from known sites” in the site config

Make sure you enter a full URL like https://example.com in the basic info settings. You can add more URLs if you track a few sites with the same tracking code. Separate with the enter or tab key.

screenshot_1635

1 Like

I am currently using Google Tag Manager on my site. Is it possible to do this with GTM? Do you have a sample showing how to set it up? I had no trouble setting up the basic Piwik PRO integration in GTM thanks to your article on the topic, but I am far from being an expert on tags!

Yes. Have you installed the whole suite or just used our Analytics GTM template? Our recommendation is to use this guide. I’m asking, because if you do it using the recommended way, you can do customizations directly within Piwik PRO UI.

Here’s the code you can use to override the page url for a given page (this part can be controlled using triggers). It will work in both Piwik PRO Tag Manager and GTM.

var _paq = _paq || [];
_paq.push(["setCustomUrl", "https://example.com/your-url"]);

Thanks, @kuba! I had originally used the Analytics GTM template, but I followed the guide you referenced and replaced it with the container code. I was then able to add a custom tag in the Piwik PRO Tag Manager to customize the URL. Seems to be working great!

In case anyone else wants to do this, here is the code I put in my custom tag. This modifies the URL reported to analytics by removing any www. at the beginning of the hostname:

<script type="text/javascript">
var _paq = _paq || [];
_paq.push(["setCustomUrl", window.location.protocol + "//" + window.location.host.replace(/^www\./gi, "") + window.location.pathname + window.location.search + window.location.hash]);
</script>
2 Likes

I still would prefer a fixed filtering place just like in GA to use all kind of formulas en regex to do this. But thanks for the script.