文件
瀏覽文件

Fix caching plugins breaking analytics

Stop WP Rocket, LiteSpeed Cache, Autoptimize, Cloudflare Rocket Loader and other caching or optimization tools from breaking the analytics tracker script.

以 Markdown 檢視

Page caches and "optimize JavaScript" features can:

  • serve old HTML without the snippet: purge the cache after adding it,
  • combine or minify pa.js into a bundle. The tracker reads its own <script> tag (document.currentScript) for data-site and the other attributes. Inside a bundle there's no such tag and it does nothing,
  • delay JavaScript until user interaction: visitors who don't interact are never counted, and time on page is wrong,
  • rewrite or strip data- attributes.

Fix#

Exclude pa.js (and privatusanalytics.com) from combine, minify, defer/delay and lazy-load features. Common settings:

Tool Where
WP Rocket File Optimization → Excluded JavaScript Files and Delay JavaScript execution → Excluded: add pa.js
LiteSpeed Cache Page Optimization → JS Settings → JS Excludes and JS Delayed Excludes
Autoptimize JS Options → Exclude scripts from Autoptimize: add pa.js
W3 Total Cache Minify → JS → Never minify the following JS files
Cloudflare Rocket Loader Add data-cfasync="false" to the script tag
Perfmatters, NitroPack, SiteGround Optimizer Their "exclude from delay/combine" lists

With Cloudflare Rocket Loader:

html
<script data-cfasync="false" defer src="https://privatusanalytics.com/js/pa.js" data-site="pa_YOURSITEID"></script>

Then purge all caches and check the page source again.