plausible / analytics

Simple, open source, lightweight (< 1 KB) and privacy-friendly web analytics alternative to Google Analytics.
https://plausible.io
GNU Affero General Public License v3.0
19.69k stars 1.05k forks source link

Allow multiple (sub)domains with same script #440

Closed shafy closed 3 years ago

shafy commented 3 years ago

Is your feature request related to a problem? Please describe. I have Webflow site and subdomains for different languages (e.g. de.example.com and en.example.com). However, I can't put different header codes for these subdomains. I tried adding two Plausible tags with both data-domain values, but then it only registers visits for the first one. Currently I'm solving it like this:

<script>
  var plausibleScript = document.createElement("script");
  plausibleScript.src = "https://plausible.io/js/plausible.js"
  plausibleScript.setAttribute("asnyc", "");
  plausibleScript.setAttribute("defer", "");
  plausibleScript.setAttribute("data-domain", window.location.host);
  document.head.appendChild(plausibleScript);
</script>

There are probably also other workaround.

How would you like it to work It would be great if we either could define more than one data-domain in the script tag or that the data-domain attribute is not needed at all (e.g. Simple Analytics doesn't require it). Of course, this could also be solved with the subdomain aggregation (I know that this is in works #160 )

ukutaht commented 3 years ago

I think your workaround is good. With #160 you'll be able to send all analytics to one plausible dashboard and then split the data by subdomain in the dashboard itself.

A simple fix for now would be to default the domain to window.location.host if it's not specified on the script. However, we are planning to move to a numeric site ID in the future and this approach would not work with that.

shafy commented 3 years ago

Ok makes sense to me. Out of curiousity: What's the advantage of using a site ID (or using the data-domain attribute) vs. defaulting to window.location.host?

ukutaht commented 3 years ago

storage cost. The events and sessions tables are massive and they will only keep growing. Currently we're storing all stats against a domain string.

Strings take up much more space than numbers. Since the site ID is static we can assign numbers to them and store it that way to reduce storage requirements quite significantly. The simplest way to do this is to move to <script data-site-id="<numeric-id"> and pass that through to the database.

shafy commented 3 years ago

Smart, thanks for explaining. Closing this issue :-)