Skip to content

fix(analytics): fold deco flag names into experiment keys the collector keeps - #841

Open
thaisrocine wants to merge 2 commits into
mainfrom
thaisrocine/deco-adapter-fold-flag-names
Open

thaisrocine wants to merge 2 commits into
mainfrom
thaisrocine/deco-adapter-fold-flag-names

Conversation

@thaisrocine

@thaisrocine thaisrocine commented Nov 23, 2026 •

Copy link
Copy Markdown
Contributor

Why

A deco_segment flag name is the matcher's block name in the CMS (TestePLPIA), and the deco adapter passed it to setExperiment raw. The collector keeps only experiment keys matching [a-z0-9][a-z0-9_-]*, so every capitalised matcher lost its arm on every event. Monte Carlo runs all of its PLP tests under TestePLPIA.

What changes

src/v8/analytics/runtime/modules/deco.ts: the adapter folds a flag name into a valid key: accents off, lower case, anything else to _. Valid keys pass through unchanged.

This mirrors decocms/analytics#69. Until decocms/analytics builds the tag from this package (task 3.7), its own runtime/ is what production serves, so both copies have to change together. node ops/build-tag.mjs --check --src <this>/packages/blocks/src/v8/analytics passes against this branch: both copies build to the same ten bundles.

Test

The behaviour is asserted end to end in decocms/analytics (tests/browser.spec.ts). Here: vitest run packages/blocks/src/v8/analytics (48 passed) and biome check are clean.

🤖 Generated with Claude Code


Summary by cubic

Fixes deco experiment tracking so matcher flag names like TestePLPIA no longer get dropped by the collector.

The collector keeps only experiment keys matching [a-z0-9][a-z0-9_-]*, so every capitalised matcher lost its arm on every event. Names are now folded into valid keys — accents stripped, lower-cased, everything else to _ — while already-valid keys pass through unchanged. The fold runs in core setExperiment/variant and the pre-boot x, so a page's own arm and the deco module's arm land on the same key. Nested tests (plp/b) now travel as plp_b instead of being dropped, and the deco module keeps the first of two names that fold to one key.

Migration

  • Mirrors the change in decocms/analytics; both copies must land together because production serves decocms/analytics' runtime until it builds from this package.
  • build-tag --check passes against this branch, so both copies build to the same bundles.

Written for commit 9adb904. Summary will update on new commits.

View guided diff Turn on auto-fix

…or keeps

A deco_segment flag name is the matcher's block name in the CMS, such as
TestePLPIA, and the adapter passed it to setExperiment raw. The collector
only keeps keys matching [a-z0-9][a-z0-9_-]*, so every capitalised
matcher lost its arm on every event. The adapter now strips accents,
lower-cases and maps anything else to "_"; valid keys are unchanged.

Mirrors decocms/analytics, whose runtime/ is what production serves until
it builds from this package. build-tag --check --src passes against this
copy, so both build to the same bytes.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
@thaisrocine
thaisrocine requested a review from a team November 23, 2026 13:03

@hugo-ccabral hugo-ccabral left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Verdict: COMMENT - the fold is correct for the cookie path, but it now disagrees with the other setExperiment caller, which breaks the "page arm wins" rule in client.ts.

Blocking

  • runtime/modules/deco.ts:51: only the v7 cookie path is folded. client.ts:114/:150 still pass v8 experiments names raw, and client.ts:148-150 depends on both paths writing the same key so the arm the page rendered overwrites the stale v7 one. They no longer match. Trigger: a v8 site calls startAnalytics({ modules: ["deco"], experiments: { TestePLPIA: false } }) for a visitor who still has a v7 deco_segment with active: ["TestePLPIA"]. cfg.x ends up as { testeplpia: "on", TestePLPIA: "off" }. The Worker drops TestePLPIA (EXP_OK, shared/contract.ts:410) and stores testeplpia=on, so the event is attributed to treatment while the page showed control. Before this PR that event had no arm. Now it has the wrong one. → fix here: move key into shared/contract.ts and apply it in one place every caller goes through: core setExperiment/variant (runtime/src/index.ts:349-352), or at least armsOf as well.

Worth fixing

  • runtime/modules/deco.ts:41: different names can fold to the same key without any warning. TestePLPIA/testeplpia collide, and so do Promo Natal/promo_natal. inactiveDrawn is written after active, so one experiment's off can overwrite another's on. Before, the Worker at least raised exp.charset. Now nothing does, which goes against the contract's own rule that normalising should not be silent. → file an issue
  • Pre-existing: nested v8 tests (plp-optimizer/b, the documented test-within-a-test) contain /, so EXP_OK rejects them and their arm is never stored. Folding in core setExperiment (above) fixes this too. → file an issue

Checked, fine

The expKeyLen check runs on the folded key. Names that fold to empty (all non-Latin, all punctuation) are skipped. Keys that were already valid pass through unchanged, so existing data keeps its keys. Before this PR, invalid names were dropped entirely, so no history is split. The cookie parse is still inside try. CI is green.

const set = (name: string, variant: string) => {
// Experiment keys allow hyphens; anything else the Worker normalises and flags.
if (name && name.length <= LIMITS.expKeyLen) setExperiment?.(name, variant);
const k = key(name);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fix here. This is the only caller that folds. client.ts:150 writes the v8 page's arms raw after this module loads, and that overwrite is how "the arm the page rendered wins". Once the keys differ, the overwrite no longer happens.

Trigger: startAnalytics({ modules: ["deco"], experiments: { TestePLPIA: false } }) for a visitor with a leftover v7 deco_segment of active: ["TestePLPIA"] gives cfg.x = { testeplpia: "on", TestePLPIA: "off" }. The Worker drops the raw key (EXP_OK) and records testeplpia=on, so the visitor is counted as treatment after being shown control. Before this PR that event had no arm. Now it has the wrong one.

Suggest: move this fold into shared/contract.ts and apply it in core setExperiment/variant (runtime/src/index.ts:349-352). Every caller goes through those (this module, armsOf, a site's own __dq.setExperiment), so one place covers them all. It would also stop the Worker dropping nested plp-optimizer/b keys, which contain /.

.normalize("NFD")
.replace(/[\u0300-\u036f]/g, "")
.toLowerCase()
.replace(/[^a-z0-9_-]+/g, "_")

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

file an issue. This fold maps many names to one key, and nothing reports it: TestePLPIA/testeplpia and Promo Natal/promo_natal each end up the same. set runs over active first and then inactiveDrawn, so experiment B's off silently overwrites experiment A's on on every event. The Worker used to flag these as exp.charset, and the contract itself says normalising should not be silent. Cheap guard: if a folded key is already in cfg.x from a different raw name, skip it (or flag it) instead of overwriting.

… in the deco module

Review of #841: only the v7 cookie path was folded, so a v8 page's own arm
(`TestePLPIA: false`) and the deco module's arm (`testeplpia: on`) landed on
different keys. The page's arm could no longer overwrite the stale v7 one, and
the event was attributed to treatment while the page showed control.

- `expKey` moves to `shared/contract.ts` and runs in core's
  `setExperiment`/`variant`. `armsOf` keys the pre-boot `x` the same way.
  Nested tests (`plp/b`) now travel as `plp_b` instead of being dropped by the
  collector.
- The deco module keeps the first of two names that fold to one key.
- The storefront side is written before the segment, ahead of the collector's
  8-entry `exp` budget.

Same source as decocms/analytics#69: `build-tag.mjs --check --src` matches
its lock for all ten bundles.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants