# Web analytics: the snippet on your pages

> One script on every page, the websites a key may report from, trying it on your own machine with a test key, named events, goals, and what it does not count.

One script on every page measures it, from any website and any stack. It
writes nothing to the visitor's device: no cookie, no local storage, nothing to
consent to. Paste it once, before the closing `</body>` tag, with one of the
project's publishable keys:

```html title=index.html
<script defer src="https://backendfree.com/static/webanalytics/a.js" data-site="pk_live_..."></script>
<noscript><img src="https://backendfree.com/collect/p.gif?k=pk_live_...&u=/" alt="" width="1" height="1"></noscript>
```

The `<noscript>` line counts a visitor without JavaScript, as a page view of the
address in `u`.

## The websites a key may report from

A live key's hits are recorded only from the hostnames on the project's Tracking
screen, under *Websites*: a publishable key sits in the page source of every site
using it, and that list is what stops somebody else's pages spending your month.
A site on both `salon.example` and `www.salon.example` needs both listed, since
a browser sends whichever one it is on.

A test key reports from any host, your own machine included. Its hits are
counted and listed on the Tracking screen and never become figures, so a site
can be checked before it is live.

## Trying it on your own machine

Put a test key (`pk_test_...`) in `data-site` while you build. The script sends
from `localhost` with a test key and with no other, and the Tracking screen
lists the latest 20 hits for 24 hours, each as it would have been
recorded: the website and the page, whether it counted as a page view or as a
named event, the referring host, the `utm_*` tags that were kept, and the
device, browser and system. A name you have not declared is listed as the page
view it counted as, with the name that was sent beside it.

Nothing a test key sends reaches Traffic. It counts against the month's events
like any other hit, and a browser that asks not to be tracked sends nothing
with a test key either.

## Named events

Something worth counting that is not a page, such as a booking made, is an event:

```js title=book.js
backendfree.event('booking_made');
```

Declare the name on the Tracking screen first. A name that is not declared is
recorded as the page view it also was, so a typo stops counting rather than
failing.

## Goals

An event, or a page, can be named as a goal from the Traffic screen: the thing
you hope a visit ends in. Traffic then says what share of visits reached one,
and how many of each source's visits did. A visit counts once for a goal,
however often it got there, and is credited to where it first came from, so a
visit that left to pay and came back still belongs to the search or the link
that brought it. An event has to be declared before it can be a goal.

## Pages that change without a reload

A single-page app changes the address without loading a page, and the script
counts each change as a page view, once: the same address twice in a row is not
a second view.

## What it does not count

Nothing is sent from a visitor whose browser asks not to be tracked (Global
Privacy Control or Do Not Track). With a live key, nothing is sent either from
a page opened as a file, or from
`localhost`, `127.0.0.1`, `[::1]`, `::1`, or a host ending in `.local`. A site's
first figures would otherwise be its builder, so on your own machine a live key
sends nothing at all, by design. A test key does, as above.

A browser driven by a program, such as headless Chrome under Playwright or
Puppeteer, says so in its User-Agent and is counted as a crawler, never as a
visitor: with a live key it appears in the crawler figures, and with a test key
it is not counted at all. A test checking that your page views arrive should
give its browser an ordinary User-Agent.

## Or from your own server

[`@backendfree/webanalytics`](/docs/webanalytics/) reports the same page
views from your server, which also sees crawlers and visitors without
JavaScript. Use one or the other for a page, not both: both would count the same
view.
