Analytics
What Publier adds to your pages when you configure analytics, what stays with your analytics provider, and what you handle yourself.
Publier does one thing for analytics: it adds your provider’s script to your pages. The provider’s script then runs in the reader’s browser, and the provider collects, stores and reports the data. This page covers what Publier adds and where, what config errors look like, and the parts you own: a privacy notice, a Content-Security-Policy and checking that it works. The YAML for each provider is in Integrations.
What Publier adds
Publier reads the analytics: block of publier.config.yaml and supports these keys:
| Key | Fields | What the page loads |
|---|---|---|
ga4 | measurementId | https://www.googletagmanager.com/gtag/js?id=<measurementId>, plus an inline gtag setup that calls gtag('config', …) |
posthog | apiKey, optional host (default https://app.posthog.com) | PostHog’s inline loader, which fetches <host>/static/array.js, then calls posthog.init |
plausible | domain, optional selfHosted | https://plausible.io/js/script.js, or <selfHosted>/js/script.js with any trailing / removed; the tag carries defer and data-domain |
fathom | siteId | https://cdn.usefathom.com/script.js with defer and data-site |
umami | websiteId, src | The script at src, with async and data-website-id |
scripts | a list of custom entries | Each entry as configured; see Custom scripts |
Any combination works. The snippets are emitted in the order above, then your custom head scripts in the order you list them.
How the snippet reaches the page
Publier does not write the provider’s <script> tag into each built HTML file. It registers one inline script with Astro’s injectScript() at the head-inline stage, which Astro documents as “Injected into a script tag in the <head> of every page”. When a page loads, that script turns the snippet HTML into elements and appends them to document.head. This has four consequences:
- Every page gets it. Publier has no per-page, per-collection or per-route switch for analytics. Draft status and
noindexare not consulted. - It needs JavaScript. A reader whose browser does not run scripts sends nothing, because the provider’s tag never gets created.
- No development switch. Publier applies the same registration whether you run
astro devorastro build. If a provider counts your local visits, use a separate property or ID for development, or leaveanalytics:out of your local config. - The tag is inside a string. In the built HTML you will find the provider’s URL as text inside an inline script, not as a
<script src>element. See Check that it works.
Config errors
docsShell() validates analytics: when Astro loads the config. Any error stops astro dev and astro build with Invalid publier.config.yaml: followed by one [code] path: message line per problem.
| Problem | Code | Path |
|---|---|---|
analytics is not a mapping | wrong-type | analytics |
| A provider key is not a mapping | wrong-type | analytics.ga4, analytics.posthog, and so on |
| A required field is missing or not a string | missing-required | analytics.ga4.measurementId, analytics.posthog.apiKey, analytics.plausible.domain, analytics.fathom.siteId, analytics.umami.websiteId, analytics.umami.src |
scripts is not a list | wrong-type | analytics.scripts |
A scripts entry is not a mapping | wrong-type | analytics.scripts[0] |
placement is not head or body | invalid-enum | analytics.scripts[0].placement |
A scripts entry has no non-empty src or content | missing-required | analytics.scripts[0] |
Some mistakes pass validation and do nothing, so a missing tracker is not always an error:
- A misspelt key is ignored.
docsShell()reads only the keys it knows and does not report unknown top-level keys, soanalytic:oranalytics.googleAnalyticsis dropped without a message. The same holds for an unknown field inside a provider. - An empty string skips the provider.
measurementId: ""passes validation, but no snippet is emitted for it. - A wrong-typed optional value is ignored.
async: "true"(a string) leavesasyncoff, and a non-stringhostorselfHostedfalls back to the default.
Where Publier stops
Publier injects the snippet. The provider’s script and servers do everything after that: collecting, storing, reporting, retention and any cookies or identifiers the script sets.
In the current release, Publier ships none of the following, so a Publier site has them only if you add them:
- A dashboard or report. Read your numbers in the provider’s own interface.
- First-party event collection or an events API. Custom events come from the provider’s script, called from your own code.
- A consent banner, a consent-gated loader, or Do-Not-Track handling. Once you configure a provider, its script is added to every page load.
What you do yourself
A privacy notice or consent
Whether you must tell readers about analytics, or ask first, depends on the provider, how you configure it, and the law where your readers live. That is your decision, and this page is not legal advice. Publier gives you nothing to show a notice or to gate the script.
If you need the provider’s script to wait for consent, write that logic yourself. A scripts entry with content runs your code verbatim on every page, so it can hold your consent check and load the provider’s tag only afterwards. In that case leave the provider’s own key out of analytics:, or the script loads once unconditionally and once after consent.
Content-Security-Policy
Publier does not set a Content-Security-Policy. The dist/_headers file it writes holds X-Content-Type-Options, Referrer-Policy, X-Frame-Options, Strict-Transport-Security and Cross-Origin-Opener-Policy, plus cache rules. If your host or CDN adds a policy, two things need to be allowed:
- The inline script. The snippet is added by an inline
<script>in<head>, and Publier injects other inline scripts there too. Ascript-srcwithout'unsafe-inline'or a matching hash blocks them; see MDN’sscript-srcreference. Publier does not add nonces. Ascriptsentry withcontentis another inline script and needs the same allowance. - The provider’s script origin. These come from the snippets:
| Provider | Script origin |
|---|---|
ga4 | https://www.googletagmanager.com |
posthog | Your host, or https://app.posthog.com |
plausible | https://plausible.io, or your selfHosted origin |
fathom | https://cdn.usefathom.com |
umami | The origin of your src |
scripts | The origin of each src |
The provider’s script then sends data somewhere, and a policy can block that too (connect-src, img-src). Those origins are the provider’s behaviour, not something in Publier’s source, so take them from the provider’s documentation. Google lists script-src-elem https://www.googletagmanager.com together with img-src and connect-src entries in its CSP guide for Google tags, and PostHog documents script-src and connect-src needs in its content security policy guide.
Check that it works
-
In the built HTML. After
astro build, searchdist/for the provider’s host. Every page rendered through your layouts carries the inline script, so the second command lists only HTML files without it, such as a redirect page:terminal grep -rl "cdn.usefathom.com" dist --include='*.html' | wc -lgrep -rL "cdn.usefathom.com" dist --include='*.html'Use
googletagmanager.com,plausible.io, yourselfHostedhost, or your Umamisrchost for the other providers. A PostHog snippet contains yourhostand/static/array.js. -
In the browser. Open a deployed page, then look at the Network tab for a request to the provider’s script, and at the Elements panel for the
<script>in<head>. A blocked request there usually means a CSP or an ad blocker. -
At the provider. Visit the page and watch the provider’s real-time view. Hits can take longer to appear in its standard reports. How long is up to the provider.
Custom scripts
The scripts list takes any script, not only analytics.
analytics: scripts: - src: https://my-cdn.example.com/my-tag.js async: true - content: "window.dataLayer = window.dataLayer || [];" placement: head| Field | Rule |
|---|---|
src | External script URL, HTML-escaped into the src attribute. |
content | Inline JavaScript. Publier does not escape or change it. It runs on every page with the page’s privileges, so treat it as your own code. |
async, defer | Add the attribute when true. Only YAML booleans count. |
placement | head (the default) or body. |
An entry needs a non-empty src or content, or the build fails. If you set both, Publier uses src and drops content.
Scripts with placement: head are written into each page’s <head> and scripts with placement: body at the end of <body>, just before </body>. Both keep the order they have in the config and appear in the built HTML of every page, so you can check them with “view source”. publier dev loads head scripts at runtime instead.
Related
- Integrations has the YAML for each provider.
- Search says that Publier records no search terms.
- Accessibility says to test third-party additions such as analytics, and Publier does not guarantee a Lighthouse score; measure your pages after adding a provider.