Skip to content

Counting Page Views

How to count a site's page views with @plexusone/analytics, without cookies or browser storage. It suits a marketing or documentation site. Product events, such as a report being created, are sent from the server by SystemForge's analytics package instead.

1. Prepare the PostHog project

  1. Use a project in PostHog's EU cloud. The package sends only there.
  2. Turn on cookieless server hash mode in the project's settings. PostHog does not accept the events without it.
  3. Turn off IP capture if the project has it on.
  4. Copy the project's public key (phc_…). It can send events but not read them, so it may be in the page.

Several sites can share a project. Give each its own site.

2. Add it to the site

pnpm add @plexusone/analytics

Call it once for each page load, from the browser. In an Astro layout:

<script>
  import { startAnalytics } from '@plexusone/analytics';

  startAnalytics({
    site: 'example',
    host: 'example.com',
    key: import.meta.env.PUBLIC_POSTHOG_KEY,
  });
</script>

Set the key as a build variable on the host that builds the live site. Local and preview builds send nothing, because host does not match or the key is missing.

A site with a content security policy allows https://eu.i.posthog.com in connect-src.

3. Check it

  1. Load the live page. One $pageview arrives in PostHog with your site and surface: marketing.
  2. In the browser's storage panel, confirm there are no cookies and nothing in local storage, session storage or IndexedDB.
  3. Turn on Do Not Track or Global Privacy Control and reload. Nothing is sent, and PostHog's client is not loaded.

4. Say so

Add it to the site's privacy page: visits are counted in aggregate, without cookies, and browsers that ask not to be tracked are not counted.

Do not add it to pages for children, or to pages whose address can hold a token or personal data: a page view includes the address.