Skip to content

[v36] Store analytics: record store impressions, clicks and purchase placement - #6447

Open
openfront-backport[bot] wants to merge 1 commit into
v36from
cherry-pick/v36/pr-6443
Open

openfront-backport[bot] wants to merge 1 commit into
v36from
cherry-pick/v36/pr-6443

Conversation

@openfront-backport

Copy link
Copy Markdown

Cherry-pick of #6443 onto v36. It applied cleanly but failed typecheck on v36, and Claude fixed it.

Typecheck, format and lint pass on the result.

Check the fix before merging.

…ent (#6443)

## Description:

Store analytics. The store now records which items players see and open,
and where in the store each purchase came from. The data feeds a
per-item baseline for the store. **Nothing visible changes.**

> **Merge after the API change:** this needs the matching API change
deployed first. The API ignores an unknown `placement` field and a
missing `/store/events` route, so shipping early would not break
purchases, but no data would be recorded.

### What each change does

**`src/client/StoreTracking.ts` (new)**
- `StoreTracker` counts an **impression** when a store card is at least
half on screen for at least 1 second. Each item is counted once per
store open. Scrolling a card in and out again doesn't count it twice.
- It counts a **click** when a player opens an item, also once per item
per store open.
- Events are batched and sent to `POST /store/events`:
- when they are sent: every 30 s, when the store closes, and when the
tab is hidden or closed;
  - batch size: at most 200 events per request;
- transport: `fetch` with `keepalive: true`, so the last batch survives
the tab closing.
- The auth header is read synchronously from the cached token. Sending
never triggers a token refresh or any other network call. Signed-out
players send no `Authorization` header.
- Cards that Lit removes are released, so a long session doesn't keep
them alive. A key longer than 120 characters is dropped rather than
failing the batch. Patterns are counted under their base key, because
one card shows every palette.
- Send errors are swallowed and nothing about the player is logged.

**`src/client/Auth.ts`**
- Adds `peekAuthHeader()`, a synchronous, non-refreshing read of the
cached token. Existing auth calls are unchanged.

**`src/client/Store.ts`**
- Starts a new "page view" on each store open, flushes on close, and
watches every product tile.
- Works out each card's **placement**:
  - the flash sale hero;
  - affiliate mode;
  - Cosmetics with its sub-tab (On sale, Skins, Flags, Crowns);
  - otherwise, the tab itself.
- The store tab type now comes from the shared schema, so a new tab
can't reach the API with a value it rejects.

**Purchases: `Cosmetics.ts`, `Api.ts`, `Payments.ts`,
`InlineCheckout.ts`, `WinModal.ts`**
- Each purchase request carries the placement of the card the player
bought from. That covers `/shop/purchase`, `/shop/purchase/pack` and
`/payments/checkout`.
- Post-game purchases send `post_game`.
- When there is no placement, the field is left out.
- Resolving the placement is wrapped so that it can never stop a
purchase.

**`packages/shared/src/ApiSchemas.ts`**
- `StorePlacementSchema` and `StoreEventsBodySchema`, matching the API's
copies.

**`src/client/components/EffectsGrid.ts`**
- Effect tiles get `data-cosmetic-key` so they're tracked like the other
cards.

### Privacy policy

`privacy-policy.html` now says what store analytics records: items shown
and opened, where they appeared, the platform, and whether the player
was signed in. It also says these events are not linked to an account,
IP address or device id, and that no cookies or local storage are used.
A purchase also records where in the store it was made. Raw events are
kept up to 3 months, and daily totals per item may be kept indefinitely.
The legal basis is legitimate interests, under the existing analytics
row. Last Updated is now 11/16/2026.

### Screenshots

There is no UI change, so instead here is the request the built client
sent.

How it was produced:
- the branch's dev server, signed out;
- the store catalog replaced with **sample data**: 30 test flags;
- in Cosmetics → Flags, one card opened, then scrolled, then the store
closed.

The store sent one `keepalive` POST. It contained 9 impressions for the
flags that had been on screen and one click:

```json
{ "platform": "web",
  "events": [
    { "type": "impression", "item": "flag:test_flag_0", "placement": { "kind": "tab", "tab": "cosmetics", "subTab": "flags" } },
    "… flag:test_flag_1 to flag:test_flag_8 …",
    { "type": "click", "item": "flag:test_flag_1", "placement": { "kind": "tab", "tab": "cosmetics", "subTab": "flags" } }
  ] }
```

### Tests
- New: `tests/client/StoreTracking.test.ts`. It covers:
  - the 1 s and 50% rule;
  - deduplication per store open;
  - batch splitting;
  - pagehide and hidden flushes;
  - keepalive sending, and sending without a token refresh;
  - released cards;
  - over-long keys.
- Updated: `tests/client/StoreModal.test.ts`. It covers:
  - every tile having a key;
  - placements;
  - reopening the store counting again;
  - a failing placement never blocking a purchase.
- Updated: `PaymentsCheckout.test.ts`, `PurchaseCosmeticPack.test.ts`
and `GrantedSubscriptionPurchase.test.ts`. They check that the placement
is in the request body, and that the key is absent when there is no
placement.
- Lint, typecheck and `build-prod` are clean. The homepage chunk doesn't
gain the game client.

## Please complete the following:

- [x] I have added screenshots for all UI updates (no UI change; the
request body above stands in)
- [x] I process any text displayed to the user through translateText()
and I've added it to the en.json file (no new text)
- [x] I have added relevant tests to the test directory

## Please put your Discord username so you can be contacted if a bug or
regression is found:

iamlewis

🤖 Generated with [Claude Code](https://claude.com/claude-code)

---------

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
(cherry picked from commit f7d187a)

Adjusted for v36 by Claude: the clean pick failed typecheck.
@coderabbitai

coderabbitai Bot commented Nov 16, 2026

Copy link
Copy Markdown
Contributor

Important

Review skipped

Bot user detected.

To trigger a single review, invoke the @coderabbitai review command.

Run configuration

  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: Organization UI
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: 7353d4fc-4658-45cf-a2a5-3fc1a562917b

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

Use the checkbox below for a quick retry:

  • Trigger review
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

Share

This branch was successfully deployed

1 active deployment
staging — 70666bb1 Deployed Nov 16, 2026 by openfrontio[bot]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: Triage

Development

Successfully merging this pull request may close these issues.

1 participant