Commit 3e2b6e790f7 for woocommerce
commit 3e2b6e790f7a785cb3c097cd48f4bc195e2a1bfa
Author: Cem Ünalan <raicem@users.noreply.github.com>
Date: Fri Nov 20 11:30:31 2026 +0300
Fix untagged control group in the Marketplace product preview test (#71103)
* WOOMKT-830: Tag the control group in the Marketplace product preview test
ExPlat returns its default variation as null, the same as a store outside the experiment, so control stores never sent preview_variation or utm_term. Read the variation name from the assignments field of the same response instead.
Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
* Add changelog entries for the product preview control group fix
Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
* Simplify the default variation name fetch and note the shared cache
Set the variation name on the parsed response instead of copying it twice. Add a comment that assignments are cached per experiment name across clients.
Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
* Explain when the default variation name fetch is needed
Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
* Test both no-variation assignment shapes and clarify the docblock
ExPlat sends variation: null for a store the traffic ramp leaves out.
The null entry case is a safety check for the shape the old wording allowed.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
---------
Co-authored-by: Claude Fable 5.2 <noreply@anthropic.com>
diff --git a/packages/js/explat/changelog/woomkt-830-default-variation-name b/packages/js/explat/changelog/woomkt-830-default-variation-name
new file mode 100644
index 00000000000..e5f68241091
--- /dev/null
+++ b/packages/js/explat/changelog/woomkt-830-default-variation-name
@@ -0,0 +1,4 @@
+Significance: minor
+Type: add
+
+Add useExperimentWithDefaultVariationName, which returns the default variation by name.
diff --git a/packages/js/explat/src/assignment.ts b/packages/js/explat/src/assignment.ts
index 523cd352324..734a82fa72f 100644
--- a/packages/js/explat/src/assignment.ts
+++ b/packages/js/explat/src/assignment.ts
@@ -107,6 +107,34 @@ export const fetchExperimentAssignment = async ( {
return await response.json();
};
+/**
+ * Same request as fetchExperimentAssignment, with the default variation returned by name.
+ *
+ * Use it when an experiment runs on only part of the stores, for example at a 50% traffic ramp. ExPlat sends the default variation as null in `variations`, the same value a store outside the experiment gets, so the two cannot be told apart there. `assignments` has the variation name, with `variation: null` for a store the traffic ramp leaves out.
+ */
+export const fetchExperimentAssignmentWithDefaultVariationName = async ( {
+ experimentName,
+ anonId,
+}: {
+ experimentName: string;
+ anonId: string | null;
+} ): Promise< unknown > => {
+ const response = ( await fetchExperimentAssignment( {
+ experimentName,
+ anonId,
+ } ) ) as {
+ variations?: Record< string, unknown >;
+ assignments?: Record< string, { variation?: unknown } | null >;
+ } | null;
+ const variationName = response?.assignments?.[ experimentName ]?.variation;
+
+ if ( response?.variations && typeof variationName === 'string' ) {
+ response.variations[ experimentName ] = variationName;
+ }
+
+ return response;
+};
+
export const fetchExperimentAssignmentWithAuth = async ( {
experimentName,
anonId,
diff --git a/packages/js/explat/src/index.ts b/packages/js/explat/src/index.ts
index 0d3e884f0cb..4bc339cd6cd 100644
--- a/packages/js/explat/src/index.ts
+++ b/packages/js/explat/src/index.ts
@@ -12,6 +12,7 @@ import { logError } from './error';
import {
fetchExperimentAssignment,
fetchExperimentAssignmentWithAuth,
+ fetchExperimentAssignmentWithDefaultVariationName,
canTrack,
} from './assignment';
import { getAnonId, initializeAnonId } from './anon';
@@ -48,6 +49,19 @@ export const { loadExperimentAssignment, dangerouslyGetExperimentAssignment } =
export const { useExperiment, Experiment, ProvideExperimentData } =
createExPlatClientReactHelpers( exPlatClient );
+// A client that returns the default variation by name, so it can be told apart from not being in the experiment.
+// Assignments are cached per experiment name across clients, so read an experiment through one hook only.
+export const { useExperiment: useExperimentWithDefaultVariationName } =
+ createExPlatClientReactHelpers(
+ createExPlatClient( {
+ fetchExperimentAssignment:
+ fetchExperimentAssignmentWithDefaultVariationName,
+ getAnonId,
+ logError,
+ isDevelopmentMode,
+ } )
+ );
+
// Create another auth client that send request to wpcom as auth user.
const exPlatClientWithAuth = createExPlatClient( {
fetchExperimentAssignment: fetchExperimentAssignmentWithAuth,
diff --git a/packages/js/explat/src/test/assignment-test.js b/packages/js/explat/src/test/assignment-test.js
index f70ebe606ea..ef932f21aac 100644
--- a/packages/js/explat/src/test/assignment-test.js
+++ b/packages/js/explat/src/test/assignment-test.js
@@ -13,6 +13,7 @@ import { addFilter } from '@wordpress/hooks';
import {
fetchExperimentAssignment,
fetchExperimentAssignmentWithAuth,
+ fetchExperimentAssignmentWithDefaultVariationName,
} from '../assignment';
global.fetch = jest.fn().mockImplementation( () =>
Promise.resolve( {
@@ -114,6 +115,53 @@ describe( 'fetchExperimentAssignment', () => {
} );
} );
+describe( 'fetchExperimentAssignmentWithDefaultVariationName', () => {
+ it.each( [
+ [
+ 'returns the default variation by name',
+ { woocommerce_test: null },
+ { woocommerce_test: { variation: 'control' } },
+ { woocommerce_test: 'control' },
+ ],
+ [
+ 'keeps a non-default variation',
+ { woocommerce_test: 'treatment' },
+ { woocommerce_test: { variation: 'treatment' } },
+ { woocommerce_test: 'treatment' },
+ ],
+ [
+ 'keeps null when the traffic ramp leaves the store out',
+ { woocommerce_test: null },
+ { woocommerce_test: { variation: null } },
+ { woocommerce_test: null },
+ ],
+ [
+ 'keeps null when the assignment entry is null',
+ { woocommerce_test: null },
+ { woocommerce_test: null },
+ { woocommerce_test: null },
+ ],
+ [ 'keeps the response when the experiment is not running', {}, {}, {} ],
+ ] )( '%s', async ( _case, variations, assignments, expected ) => {
+ window.fetch.mockImplementation( () =>
+ Promise.resolve( {
+ json: () =>
+ Promise.resolve( { variations, assignments, ttl: 60 } ),
+ ok: true,
+ status: 200,
+ } )
+ );
+
+ const response =
+ await fetchExperimentAssignmentWithDefaultVariationName( {
+ experimentName: 'woocommerce_test',
+ anonId: '1234',
+ } );
+
+ expect( response.variations ).toEqual( expected );
+ } );
+} );
+
describe( 'fetchExperimentAssignmentWithAuth', () => {
it( 'applies woocommerce_explat_request_args before constructing the full URL', async () => {
fetchMock.mockClear();
diff --git a/plugins/woocommerce/changelog/WOOMKT-830-control-variation-name b/plugins/woocommerce/changelog/WOOMKT-830-control-variation-name
new file mode 100644
index 00000000000..e7d3994f8cd
--- /dev/null
+++ b/plugins/woocommerce/changelog/WOOMKT-830-control-variation-name
@@ -0,0 +1,3 @@
+Significance: patch
+Type: dev
+Comment: Tag control arm clicks and links in the in-app Marketplace product preview test.
diff --git a/plugins/woocommerce/client/admin/client/marketplace/contexts/marketplace-context.tsx b/plugins/woocommerce/client/admin/client/marketplace/contexts/marketplace-context.tsx
index 7ce82a992d2..9d9c1731e39 100644
--- a/plugins/woocommerce/client/admin/client/marketplace/contexts/marketplace-context.tsx
+++ b/plugins/woocommerce/client/admin/client/marketplace/contexts/marketplace-context.tsx
@@ -7,7 +7,7 @@ import {
useCallback,
createContext,
} from '@wordpress/element';
-import { useExperiment } from '@woocommerce/explat';
+import { useExperimentWithDefaultVariationName } from '@woocommerce/explat';
/**
* Internal dependencies
@@ -51,10 +51,12 @@ export function MarketplaceContextProvider( props: {
const [ selectedTab, setSelectedTab ] = useState( '' );
const [ iamSettings, setIamSettings ] = useState( {} );
// Loaded up front so product cards already know whether to open the preview modal when clicked.
- const [ , productPreviewAssignment ] = useExperiment(
- PRODUCT_PREVIEW_EXPERIMENT_NAME
- );
- // useExperiment keeps returning a cached assignment after tracking is turned off, so drop it here.
+ // The control arm is ExPlat's default variation, and its clicks and links are tagged too, so it needs its name.
+ const [ , productPreviewAssignment ] =
+ useExperimentWithDefaultVariationName(
+ PRODUCT_PREVIEW_EXPERIMENT_NAME
+ );
+ // The hook keeps returning a cached assignment after tracking is turned off, so drop it here.
const productPreviewVariation = window.wcTracks?.isEnabled
? ( productPreviewAssignment?.variationName ?? null )
: null;
diff --git a/plugins/woocommerce/client/admin/client/marketplace/contexts/test/marketplace-context.test.tsx b/plugins/woocommerce/client/admin/client/marketplace/contexts/test/marketplace-context.test.tsx
index 3203b1071aa..5880a3f00da 100644
--- a/plugins/woocommerce/client/admin/client/marketplace/contexts/test/marketplace-context.test.tsx
+++ b/plugins/woocommerce/client/admin/client/marketplace/contexts/test/marketplace-context.test.tsx
@@ -3,10 +3,10 @@
*/
import { render } from '@testing-library/react';
import { useContext } from '@wordpress/element';
-import { useExperiment } from '@woocommerce/explat';
+import { useExperimentWithDefaultVariationName } from '@woocommerce/explat';
jest.mock( '@woocommerce/explat', () => ( {
- useExperiment: jest.fn(),
+ useExperimentWithDefaultVariationName: jest.fn(),
} ) );
/**
@@ -26,10 +26,9 @@ describe( 'MarketplaceContextProvider', () => {
const originalTracks = window.wcTracks;
beforeEach( () => {
- ( useExperiment as jest.Mock ).mockReturnValue( [
- false,
- { variationName: 'treatment' },
- ] );
+ ( useExperimentWithDefaultVariationName as jest.Mock ).mockReturnValue(
+ [ false, { variationName: 'treatment' } ]
+ );
global.fetch = jest.fn( () => new Promise( () => {} ) ) as jest.Mock;
} );