DEV Community

Discussion on: The easy way to prevent self-reporting with Plausible Analytics

Collapse
 
bajtos profile image
Miroslav Bajtoš

Thank you for a great tip! When implementing this approach in my website, I found that onload event fires after the Plausible script has already attempted to read data-domain attribute.

I fixed the issue by programatically creating the script tag from JavaScript:

<script>
  const site = window.location.host;
  const tag = document.createElement('script');
  tag.defer = true;
  tag.src = '/s/main.js'
  tag.setAttribute('data-api', '/s/event');
  tag.setAttribute('data-domain', site);
  document.head.appendChild(tag);
</script>
Enter fullscreen mode Exit fullscreen mode