@keak/sdk 1.0.5 → 1.0.7
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.cjs.js +63 -37
- package/dist/index.cjs.js.map +1 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +63 -37
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
- package/src/plugins/next.cjs +184 -76
- package/src/plugins/webpack-loader-babel/index.js +199 -0
package/dist/index.js
CHANGED
|
@@ -2901,6 +2901,12 @@ const Experiment = ({ name, children, active = true }) => {
|
|
|
2901
2901
|
contentLength: variantContent.length
|
|
2902
2902
|
};
|
|
2903
2903
|
});
|
|
2904
|
+
// Determine if we're in development/preview mode (Keak Code desktop app)
|
|
2905
|
+
// Calculate this before registration effect so it's available there
|
|
2906
|
+
const isDevMode = typeof window !== 'undefined' && (window.location.hostname === 'localhost' ||
|
|
2907
|
+
window.location.hostname === '127.0.0.1' ||
|
|
2908
|
+
window.location.port !== '' ||
|
|
2909
|
+
keakInstance?.config.debug);
|
|
2904
2910
|
// Register experiment with SDK for live management
|
|
2905
2911
|
useEffect(() => {
|
|
2906
2912
|
if (keakInstance && variantElements.length > 0) {
|
|
@@ -2924,11 +2930,22 @@ const Experiment = ({ name, children, active = true }) => {
|
|
|
2924
2930
|
keakInstance.registerLiveExperiment(experimentData);
|
|
2925
2931
|
// Only track impressions if experiment is active
|
|
2926
2932
|
if (active) {
|
|
2927
|
-
// Log split testing assignment and track impression immediately
|
|
2928
2933
|
const variantNames = variantMetadata.map(v => v.name);
|
|
2929
|
-
|
|
2930
|
-
|
|
2931
|
-
|
|
2934
|
+
// In dev mode, don't call getVariant (which stores in localStorage)
|
|
2935
|
+
// Instead, use "treatment" for tracking purposes
|
|
2936
|
+
let assignedVariant;
|
|
2937
|
+
if (isDevMode) {
|
|
2938
|
+
assignedVariant = variantNames.includes('treatment') ? 'treatment' : variantNames[0];
|
|
2939
|
+
if (keakInstance.config.debug) {
|
|
2940
|
+
console.log(`🎯 [Keak SDK] Experiment "${name}" in dev mode, using "${assignedVariant}" for tracking (not stored)`);
|
|
2941
|
+
}
|
|
2942
|
+
}
|
|
2943
|
+
else {
|
|
2944
|
+
// Production mode: use normal assignment (stores in localStorage)
|
|
2945
|
+
assignedVariant = keakInstance.getVariant(name, variantNames);
|
|
2946
|
+
if (keakInstance.config.debug) {
|
|
2947
|
+
console.log(`🎯 [Keak SDK] Split test assignment for "${name}": → Variant "${assignedVariant}"`);
|
|
2948
|
+
}
|
|
2932
2949
|
}
|
|
2933
2950
|
// Track impression immediately when variant is determined
|
|
2934
2951
|
if (assignedVariant) {
|
|
@@ -2941,46 +2958,55 @@ const Experiment = ({ name, children, active = true }) => {
|
|
|
2941
2958
|
}
|
|
2942
2959
|
}
|
|
2943
2960
|
}
|
|
2944
|
-
}, [name, variantElements.length, variantMetadata, active]);
|
|
2961
|
+
}, [name, variantElements.length, variantMetadata, active, isDevMode]);
|
|
2945
2962
|
// Get variant assignment from SDK (with proper split testing)
|
|
2946
2963
|
const variantNames = variantMetadata.map(v => v.name);
|
|
2947
|
-
// Determine if we're in development/preview mode (Keak Code desktop app)
|
|
2948
|
-
const isDevMode = typeof window !== 'undefined' && (window.location.hostname === 'localhost' ||
|
|
2949
|
-
window.location.hostname === '127.0.0.1' ||
|
|
2950
|
-
window.location.port !== '' ||
|
|
2951
|
-
keakInstance?.config.debug);
|
|
2952
2964
|
let selectedIndex;
|
|
2953
|
-
|
|
2954
|
-
|
|
2955
|
-
|
|
2956
|
-
|
|
2957
|
-
|
|
2958
|
-
|
|
2959
|
-
|
|
2960
|
-
|
|
2965
|
+
// Priority 1: Check for forced variant from setExperimentVariant (highest priority)
|
|
2966
|
+
const forcedVariant = keakInstance?.assignments[name] || assignments[name];
|
|
2967
|
+
if (forcedVariant) {
|
|
2968
|
+
const forcedIndex = variantElements.findIndex((variant) => variant.props.name === forcedVariant);
|
|
2969
|
+
if (forcedIndex !== -1) {
|
|
2970
|
+
selectedIndex = forcedIndex;
|
|
2971
|
+
if (keakInstance?.config.debug) {
|
|
2972
|
+
console.log(`🎯 [Keak SDK] Experiment "${name}" using forced variant: "${forcedVariant}"`);
|
|
2973
|
+
}
|
|
2961
2974
|
}
|
|
2962
2975
|
}
|
|
2963
|
-
|
|
2964
|
-
|
|
2965
|
-
|
|
2966
|
-
|
|
2967
|
-
|
|
2968
|
-
selectedIndex
|
|
2969
|
-
|
|
2970
|
-
|
|
2971
|
-
|
|
2976
|
+
// If no forced variant, use normal logic
|
|
2977
|
+
if (selectedIndex === undefined) {
|
|
2978
|
+
if (!active) {
|
|
2979
|
+
// Inactive: Always show control variant (original)
|
|
2980
|
+
selectedIndex = variantElements.findIndex((variant) => variant.props.name === 'control');
|
|
2981
|
+
if (selectedIndex === -1) {
|
|
2982
|
+
selectedIndex = 0; // Fallback to first variant if no "control" named variant
|
|
2983
|
+
}
|
|
2984
|
+
if (keakInstance?.config.debug) {
|
|
2985
|
+
console.log(`🔇 [Keak SDK] Experiment "${name}" is inactive, showing control (original)`);
|
|
2986
|
+
}
|
|
2972
2987
|
}
|
|
2973
|
-
|
|
2974
|
-
|
|
2975
|
-
|
|
2976
|
-
|
|
2977
|
-
|
|
2978
|
-
|
|
2979
|
-
|
|
2980
|
-
if (selectedIndex === -1 && variantElements.length > 0) {
|
|
2981
|
-
selectedIndex = 0;
|
|
2988
|
+
else if (isDevMode) {
|
|
2989
|
+
// Active in dev mode: Show treatment variant (for preview purposes)
|
|
2990
|
+
selectedIndex = variantElements.findIndex((variant) => variant.props.name === 'treatment');
|
|
2991
|
+
if (selectedIndex === -1) {
|
|
2992
|
+
// Fallback: show the last variant (usually treatment) or first if only one
|
|
2993
|
+
selectedIndex = variantElements.length > 1 ? 1 : 0;
|
|
2994
|
+
}
|
|
2982
2995
|
if (keakInstance?.config.debug) {
|
|
2983
|
-
console.
|
|
2996
|
+
console.log(`🎯 [Keak SDK] Experiment "${name}" is active (dev mode), showing treatment (variant)`);
|
|
2997
|
+
}
|
|
2998
|
+
}
|
|
2999
|
+
else {
|
|
3000
|
+
// Active in production: Normal A/B split testing
|
|
3001
|
+
const assignedVariantName = keakInstance?.getVariant(name, variantNames) || variantNames[0];
|
|
3002
|
+
// Find the index of the assigned variant
|
|
3003
|
+
selectedIndex = variantElements.findIndex((variant) => variant.props.name === assignedVariantName);
|
|
3004
|
+
// Safety fallback - if assigned variant not found, use first variant
|
|
3005
|
+
if (selectedIndex === -1 && variantElements.length > 0) {
|
|
3006
|
+
selectedIndex = 0;
|
|
3007
|
+
if (keakInstance?.config.debug) {
|
|
3008
|
+
console.warn(`⚠️ [Keak SDK] Assigned variant "${assignedVariantName}" not found, using first variant`);
|
|
3009
|
+
}
|
|
2984
3010
|
}
|
|
2985
3011
|
}
|
|
2986
3012
|
}
|