文档
浏览文档

Add Privatus Analytics to Eleventy (11ty)

Add cookieless analytics to an Eleventy (11ty) base layout, with a global data file so the tracker loads in production builds but not in eleventy --serve.

查看 Markdown

In your base layout, e.g. _includes/base.njk:

njk
<head>
  <meta charset="utf-8">
  <title>{{ title }}</title>
  {% if env.production %}
    <script defer src="https://privatusanalytics.com/js/pa.js" data-site="pa_YOURSITEID"></script>
  {% endif %}
</head>

Expose env.production with a global data file:

js
// _data/env.js
export default { production: process.env.ELEVENTY_RUN_MODE === 'build' }

eleventy --serve then leaves the tracker out, and production builds include it.