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¶
- Use a project in PostHog's EU cloud. The package sends only there.
- Turn on cookieless server hash mode in the project's settings. PostHog does not accept the events without it.
- Turn off IP capture if the project has it on.
- 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¶
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¶
- Load the live page. One
$pageviewarrives in PostHog with yoursiteandsurface: marketing. - In the browser's storage panel, confirm there are no cookies and nothing in local storage, session storage or IndexedDB.
- 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.