Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 33 additions & 1 deletion pages/analytics/hooks/useVideoLayerData.js
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,17 @@ const TRACKER_AUTO_NAME_RE = new RegExp(
') layer at \\d+(\\.\\d+)?s$',
);

/**
* Exact separator the hotspot tracker puts between the parent layer name
* and the hotspot's own label when it composes `layer_name`. It builds
* `<parentLayerName> — <subLabel>` (space, em dash, space) in
* `hotspotLayerManager.js`; keep this in sync with that string. Matching
* the full separator — rather than just the parent name — is what lets us
* strip the prefix without touching a hotspot label that legitimately
* begins with a dash.
*/
const HOTSPOT_NAME_SEPARATOR = ' — ';

/**
* Whether a server-delivered layer_name is missing, a bare UUID, or a
* tracker-side auto-generated name — i.e. anything we should replace with
Expand Down Expand Up @@ -432,7 +443,28 @@ export function groupRows( rows, layerType, configIndex ) {
// path the parent uses so a raw composite UUID never reaches
// the rail. Falls back to a generic ordinal "<TypeLabel> #N"
// when nothing usable.
const rawSubName = md.product_name || row.layer_name || '';
let rawSubName = md.product_name || row.layer_name || '';
// The hotspot tracker packs the parent layer name, the
// `HOTSPOT_NAME_SEPARATOR`, and "Hotspot N" into layer_name. The
// rail's card title already shows the parent, so that prefix is
// pure repetition and, once the row is truncated, it hides the
// "Hotspot N" that actually names the row. Drop the exact
// "<parent> — " prefix, keeping only the hotspot's own label.
// Matching the full separator (not just the parent name) means:
// - a custom label that merely begins with the parent name
// (parent "Sale", label "Sale special") is left intact, and
// - a label that itself starts with a dash (parent "Summer
// sale", label "-50% off") keeps its leading dash instead of
// having it eaten as part of the separator.
// Woo rows carry a bare product_name and never match this, so
// they are left alone.
if ( ! md.product_name && md.parent_layer_name ) {
const parentPrefix =
md.parent_layer_name + HOTSPOT_NAME_SEPARATOR;
if ( rawSubName.startsWith( parentPrefix ) ) {
rawSubName = rawSubName.slice( parentPrefix.length );
}
}

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

The separator cleanup is greedy across dashes, so it can also eat a dash that belongs to the hotspot's own label.

/^[\s\p{Pd}]+/u consumes every leading whitespace/dash character, not just the one separator. A hotspot named -50% off inside a layer named Summer sale is tracked as Summer sale — -50% off (hotspotLayerManager.js composes ${parentLayerName} — ${subLabel}). After slice() the remainder is " — -50% off", and the character class then swallows space + em dash + space and the label's own hyphen, so the rail row reads 50% off — the opposite meaning. Same for something like —Limited—.

Anchoring the strip to a single separator occurrence keeps the label intact, e.g.:

rawSubName = rawSubName
	.slice( md.parent_layer_name.length )
	.replace( /^\s*\p{Pd}\s*/u, '' );

(or only strip when the composite actually matches parent + SEP). Minor/edge-case — everything else in the change looks right to me: the ! md.product_name guard correctly exempts Woo, startsWith avoids touching unrelated names, parent_layer_name landed in the same commit as the composite name so there's no historical-data gap, and an empty result still falls through to the "<Type> #N" ordinal.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Fixed in b91bdab. Went with your "only strip when the composite actually matches parent + SEP" option, which resolves this greedy-dash case and the Copilot prefix-collision case in one check. The strip now matches the exact <parent> — prefix and slices just that — no character-class replace — so a label that begins with its own dash keeps it: Summer sale — -50% off -> -50% off, not 50% off.

The separator is now a module constant HOTSPOT_NAME_SEPARATOR = ' — ' with a comment pointing at hotspotLayerManager.js:213, since that is the source of truth for the composite. Added two tests (leading-dash and prefix-collision); both fail on the old logic and pass now. 15/15 in the suite.

let subName;
if (
rawSubName &&
Expand Down
114 changes: 114 additions & 0 deletions pages/analytics/hooks/useVideoLayerData.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -165,3 +165,117 @@ describe( 'groupRows — a hotspot reports its own viewed, never the layer\'s',
expect( byName[ 'Added yesterday' ] ).toEqual( [ 1, 1 ] );
} );
} );

describe( 'groupRows — hotspot sub-name drops the redundant parent prefix', () => {
// The tracker writes each hotspot event's layer_name as the parent layer
// name, a separator dash, then "Hotspot N" (e.g. the parent "Hotspot layer
// at 4.59s" followed by " <dash> Hotspot 2"). The rail already shows the
// parent as its card title, so the row must read just "Hotspot N". SEP is
// built from a code point so no literal em dash lives in this file.
const SEP = ` ${ String.fromCharCode( 0x2014 ) } `;
const PARENT = 'Hotspot layer at 4.59s';
const base = { layer_type: 'hotspot', page_url: 'https://example.com', timestamp: 4.59 };
const parentRow = {
...base,
layer_id: 'h-1',
layer_name: PARENT,
viewed: 2, clicked: 1, hovered: 2,
conversion_rate: 50,
layer_metadata: JSON.stringify( { parent_layer_id: 'h-1', parent_layer_name: PARENT } ),
};
const subRow = ( suffix, label, index ) => ( {
...base,
layer_id: `h-1::${ suffix }`,
layer_name: `${ PARENT }${ SEP }${ label }`,
viewed: 1, clicked: 0, hovered: 0,
conversion_rate: 0,
layer_metadata: JSON.stringify( {
parent_layer_id: 'h-1',
parent_layer_name: PARENT,
hotspot_index: index,
} ),
} );

it( 'strips the parent name and separator, leaving only "Hotspot N"', () => {
const rows = [ parentRow, subRow( 'a', 'Hotspot 1', 0 ), subRow( 'b', 'Hotspot 2', 1 ) ];
const [ parent ] = groupRows( rows, 'hotspot', OPEN_CONFIG );
expect( parent.sub_hotspots.map( ( s ) => s.name ).sort() ).toEqual( [ 'Hotspot 1', 'Hotspot 2' ] );
} );

it( 'leaves a Woo product name untouched (guarded by product_name)', () => {
const wooParent = {
...parentRow,
layer_type: 'woo',
layer_id: 'w-1',
layer_name: 'Woo layer at 9.90s',
layer_metadata: JSON.stringify( { parent_layer_id: 'w-1', parent_layer_name: 'Woo layer at 9.90s' } ),
};
const wooSub = {
...base,
layer_type: 'woo',
layer_id: 'w-1::p1',
layer_name: 'Lamp',
viewed: 0, hovered: 1, added_to_cart: 0, conversion_rate: 0,
layer_metadata: JSON.stringify( { parent_layer_id: 'w-1', product_id: 1, product_name: 'Lamp' } ),
};
const [ parent ] = groupRows( [ wooParent, wooSub ], 'woo', OPEN_CONFIG );
expect( parent.sub_hotspots.map( ( s ) => s.name ) ).toEqual( [ 'Lamp' ] );
} );

it( 'keeps a sub name that does not start with the parent prefix', () => {
const custom = {
...subRow( 'c', 'ignored', 2 ),
layer_name: 'Buy now',
layer_metadata: JSON.stringify( { parent_layer_id: 'h-1', parent_layer_name: PARENT } ),
};
const [ parent ] = groupRows( [ parentRow, custom ], 'hotspot', OPEN_CONFIG );
expect( parent.sub_hotspots.map( ( s ) => s.name ) ).toContain( 'Buy now' );
} );

// A custom label that merely begins with the parent name but is not the
// tracker composite (no separator). Stripping on the bare parent name
// would leave "special"; matching the full "<parent> — " separator keeps
// the label whole.
it( 'leaves a custom label that only shares the parent name as a prefix', () => {
const collidingParent = 'Sale';
const parentSale = {
...base,
layer_id: 'h-2',
layer_name: collidingParent,
viewed: 2, clicked: 1, hovered: 2, conversion_rate: 50,
layer_metadata: JSON.stringify( { parent_layer_id: 'h-2', parent_layer_name: collidingParent } ),
};
const sub = {
...base,
layer_id: 'h-2::a',
layer_name: 'Sale special',
viewed: 1, clicked: 0, hovered: 0, conversion_rate: 0,
layer_metadata: JSON.stringify( { parent_layer_id: 'h-2', parent_layer_name: collidingParent } ),
};
const [ parent ] = groupRows( [ parentSale, sub ], 'hotspot', OPEN_CONFIG );
expect( parent.sub_hotspots.map( ( s ) => s.name ) ).toEqual( [ 'Sale special' ] );
} );

// A hotspot whose own label starts with a dash. Only the single separator
// is the prefix; the label's leading dash must survive so "-50% off" does
// not become "50% off" (the opposite meaning).
it( 'keeps a hotspot label that itself begins with a dash', () => {
const dashParent = 'Summer sale';
const parentSummer = {
...base,
layer_id: 'h-3',
layer_name: dashParent,
viewed: 2, clicked: 1, hovered: 2, conversion_rate: 50,
layer_metadata: JSON.stringify( { parent_layer_id: 'h-3', parent_layer_name: dashParent } ),
};
const sub = {
...base,
layer_id: 'h-3::a',
layer_name: `${ dashParent }${ SEP }-50% off`,
viewed: 1, clicked: 0, hovered: 0, conversion_rate: 0,
layer_metadata: JSON.stringify( { parent_layer_id: 'h-3', parent_layer_name: dashParent } ),
};
const [ parent ] = groupRows( [ parentSummer, sub ], 'hotspot', OPEN_CONFIG );
expect( parent.sub_hotspots.map( ( s ) => s.name ) ).toEqual( [ '-50% off' ] );
} );
} );
Loading