# A web funnel by traffic source, rebuilt from a public GA4 export

Where does each traffic source lose buyers, how much of that is real and how much is tracking, and how far do sessions by
source forecast next month's orders? This study rebuilds sessions, channels, funnels and attribution from the raw events of
the public obfuscated GA4 sample of the Google Merchandise Store. The page is `index.html`.

## Data

`bigquery-public-data.ga4_obfuscated_sample_ecommerce`, 92 daily tables from 2020-11-01 to 2021-01-31, published by Google
through the BigQuery Public Datasets program (https://developers.google.com/analytics/bigquery/web-ecommerce-demo-dataset).
The publisher states the data is obfuscated, holds placeholder values, may have limited internal consistency, and cannot be
compared with the store's demo account. Raw rows are not copied here; `results/` holds aggregates only.
`data/fingerprint.json` holds each day's row count and timestamp sum, so a fresh read can be compared with the one used here.

## Reproduce

Python 3.12 with `duckdb`, `numpy`, `pyarrow` and `google-auth`. Reading the tables needs a Google Cloud identity (no billing).

From this directory:

```
python3 -B tools/extract.py /path/to/new/dir 3000
python3 -B tools/fingerprint.py /path/to/new/dir data/fingerprint.json check
python3 -B tests/test_ga4.py
python3 -B tools/structure.py /path/to/new/dir
export STUDY_RESULTS=results/corrected
python3 -B tools/run_study.py /path/to/new/dir /path/to/temp/dir
python3 -B tools/added_later.py /path/to/new/dir /path/to/temp/dir
python3 -B tools/after_review.py /path/to/new/dir /path/to/temp/dir
python3 -B tools/change_2.py /path/to/new/dir /path/to/temp/dir
python3 -B tools/change_3.py /path/to/new/dir /path/to/temp/dir
python3 -I -B tools/summarize.py
unset STUDY_RESULTS
python3 -I -B tools/headline_split.py
python3 -I -B tests/test_headline_split.py
python3 -I -B site/render.py
```

The page reads `results/corrected/`, which takes a session's source and medium from one event, as declared. `results/` keeps
a first run that took each value from its own earliest event (see `protocol-r1-change-2.json`). To reproduce it, run
the same steps with `STUDY_SOURCE_RULE=first_run` and `STUDY_RESULTS` set to a new directory, then compare the files byte for
byte; `tools/results_dir.py` refuses to write either directory under the other rule.

The tests and the forecast need no access to the export: `tools/forecast.py` replays from `daily_by_channel.csv`.
The fault copies use DuckDB's row hash, so the exact rows
selected depend on the DuckDB version (2.0 here); the rates are declared.

## Files

- `DESIGN-r1.md`, `protocol-r1.json`, `predictions-r1.json`, `assumptions.json`: the design, committed before any scored run.
- `tools/extract.py`: reads the daily tables to parquet.
- `tools/ga4lib.py`: the flat event table, sessions, channels, every check, both funnel reads, attribution, the faults.
- `tools/forecast.py`: the forecasts and the miss bridge, standard library only.
- `tools/added_later.py`: readings added after the first results and labelled so on the page (tag change days, the stable
  window, the store's own hosts as a source).
- `tools/structure.py`: also added after the first results, as a description: every parameter key, which value slot holds
  it, and where the purchase value and transaction id sit (`results/structure.json`).
- `protocol-r1-change-1.json`: a change to the protocol, written down after the first results and committed before its code
  ran. It adds three descriptions and scores nothing: store-host sessions merged into the visit they continue, steps that
  follow the same sessions inside the stable window with an order check per step, and the money at three shares of budget.
- `tools/after_review.py`: runs the first two of those on the local copy (`results/after_review.json`); the money sweep is
  computed in `site/render.py`.
- `protocol-r1-change-2.json`: a change, written down after the first results and committed before its code ran. It
  sets the session source rule to the declared one (both values from one event), runs every part that reads a
  session's source into `results/corrected/`, scores the predictions under both source rules, and adds four descriptions: referral split
  into outside sites and the store's own hosts, last non-direct with store-host sessions treated like direct, the funnel with
  each store-host session credited to the same user's earlier channel, and counts of store-host sessions that follow an
  earlier session.
- `tools/change_2.py`: runs those four on the local copy (`results/corrected/change_2.json`).
- `protocol-r1-change-3.json`: a change, written down after the first results and committed before its code ran. It
  adds two descriptions and one expectation, apart from the 15 predictions: whether checkout sessions with no add to cart
  follow an add to cart by the same user in an earlier session, and the split of sessions with no usable source into no
  value at all, a placeholder, and one value missing.
- `tools/change_3.py`: runs those two on the local copy (`results/corrected/change_3.json`).
- `protocol-r1-change-4.json`: a change, written down after the first results and committed before the page used
  it. It runs nothing new and changes no result file: it adds three figures derived from stored counts,
  all computed and asserted in `site/render.py`.
- `tools/headline_split.py`: derived after the first results from stored results only, for the top of the page: referral split
  into the store's own hosts and outside sites with outside sites' interval, the sessions with no usable source cut by what
  the event still carries, the weekly range of checkouts with no cart from 29 November on, and the two faults that move order
  credit most (`results/later/headline_split.json`, pinned by `tests/test_headline_split.py`). It reads no export and
  changes no stored file; it writes outside `results/corrected/`, whose file list `protocol-r1-change-4.json` pins.
- `tools/results_dir.py`: keeps `results/` (the first run) and `results/corrected/` (the declared rule) apart.
- `tools/run_study.py`, `tools/summarize.py`: run each part and score the predictions as declared.
- `tests/test_ga4.py`: a planted fault for every check on a small made-up export, known answers, and the replay.
- `assumptions.json`: the money and rollout assumptions behind the page's "What it is worth" section.
- `site/render.py`, `site/page.css`: the page. Every number comes from `results/corrected/`, `results/structure.json` or
  `assumptions.json`, with the first run's values from `results/` where the page compares the two runs, and the claims the
  text makes are asserted in `render.py`.
- `vendor/viz-design-system/`: the chart kit (version 2.3.0) with its licences.

## Licence of the data

The sample belongs to its publisher. No licence text specific to the rows was found on the publisher's page, so this study
credits the source, publishes aggregates and method, and redistributes no rows.

Some files are packed in web-funnel-by-source-data-1.zip; unzip it here to restore the full export, which the manifest lists file by file.
