@woocommerce/explat
Version:
WooCommerce component and utils for A/B testing.
69 lines (68 loc) • 3.67 kB
JavaScript
var _a, _b;
/**
* External dependencies
*/
import { stringify } from 'qs';
import { applyFilters } from '@wordpress/hooks';
import apiFetch from '@wordpress/api-fetch';
export const canTrack = ((_a = window.wcTracks) === null || _a === void 0 ? void 0 : _a.isEnabled) || ((_b = window === null || window === void 0 ? void 0 : window._wca) === null || _b === void 0 ? void 0 : _b.push) !== undefined;
const EXPLAT_VERSION = '0.1.0';
const isValidQueryParams = (queryParams) => {
return (queryParams.hasOwnProperty('experiment_name') &&
queryParams.hasOwnProperty('woo_country_code') &&
queryParams.hasOwnProperty('woo_wcadmin_install_timestamp'));
};
const getRequestQueryParams = ({ experimentName, anonId, }) => {
var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k;
/**
* List of URL query parameters to be sent to the server.
*
* @filter woocommerce_explat_request_args
* @example
* addFilter(
* 'woocommerce_explat_request_args',
* 'woocommerce_explat_request_args',
* ( args ) => {
* args.experimentName = 'my-experiment';
* return args;
* });
*/
const queryParams = applyFilters('woocommerce_explat_request_args', {
experiment_name: experimentName,
anon_id: anonId !== null && anonId !== void 0 ? anonId : undefined,
woo_country_code: ((_c = (_b = (_a = window.wcSettings) === null || _a === void 0 ? void 0 : _a.preloadSettings) === null || _b === void 0 ? void 0 : _b.general) === null || _c === void 0 ? void 0 : _c.woocommerce_default_country) ||
((_g = (_f = (_e = (_d = window.wcSettings) === null || _d === void 0 ? void 0 : _d.admin) === null || _e === void 0 ? void 0 : _e.preloadSettings) === null || _f === void 0 ? void 0 : _f.general) === null || _g === void 0 ? void 0 : _g.woocommerce_default_country),
woo_wcadmin_install_timestamp: (_k = (_j = (_h = window.wcSettings) === null || _h === void 0 ? void 0 : _h.admin) === null || _j === void 0 ? void 0 : _j.preloadOptions) === null || _k === void 0 ? void 0 : _k.woocommerce_admin_install_timestamp,
});
if (!isValidQueryParams(queryParams)) {
throw new Error(`Invalid query Params: ${JSON.stringify(queryParams)}`);
}
// Make sure test name is a valid one.
if (!/^[A-Za-z0-9_]+$/.test(queryParams.experiment_name)) {
throw new Error(`Invalid A/B test name: ${queryParams.experiment_name}`);
}
return queryParams;
};
export const fetchExperimentAssignment = async ({ experimentName, anonId, }) => {
if (!canTrack) {
throw new Error(`Tracking is disabled, can't fetch experimentAssignment`);
}
const queryParams = getRequestQueryParams({ experimentName, anonId });
if (!queryParams.anon_id) {
throw new Error(`Can't fetch experiment assignment without an anonId or auth, please initialize anonId first or use fetchExperimentAssignmentWithAuth instead.`);
}
const response = await window.fetch(`https://public-api.wordpress.com/wpcom/v2/experiments/${EXPLAT_VERSION}/assignments/woocommerce?${stringify(queryParams)}`);
return await response.json();
};
export const fetchExperimentAssignmentWithAuth = async ({ experimentName, anonId, }) => {
if (!canTrack) {
throw new Error(`Tracking is disabled, can't fetch experimentAssignment`);
}
// Use apiFetch to send request with credentials and nonce to our backend api to get the assignment with a user token via Jetpack.
return await apiFetch({
path: `/wc-admin/experiments/assignment?${stringify(getRequestQueryParams({
experimentName,
anonId,
}))}`,
});
};