@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.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
- const assignedVariant = keakInstance.getVariant(name, variantNames);
2930
- if (keakInstance.config.debug) {
2931
- console.log(`🎯 [Keak SDK] Split test assignment for "${name}": → Variant "${assignedVariant}"`);
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
- if (!active) {
2954
- // Inactive: Always show control variant (original)
2955
- selectedIndex = variantElements.findIndex((variant) => variant.props.name === 'control');
2956
- if (selectedIndex === -1) {
2957
- selectedIndex = 0; // Fallback to first variant if no "control" named variant
2958
- }
2959
- if (keakInstance?.config.debug) {
2960
- console.log(`🔇 [Keak SDK] Experiment "${name}" is inactive, showing control (original)`);
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
- else if (isDevMode) {
2964
- // Active in dev mode: Show treatment variant (for preview purposes)
2965
- selectedIndex = variantElements.findIndex((variant) => variant.props.name === 'treatment');
2966
- if (selectedIndex === -1) {
2967
- // Fallback: show the last variant (usually treatment) or first if only one
2968
- selectedIndex = variantElements.length > 1 ? 1 : 0;
2969
- }
2970
- if (keakInstance?.config.debug) {
2971
- console.log(`🎯 [Keak SDK] Experiment "${name}" is active (dev mode), showing treatment (variant)`);
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
- else {
2975
- // Active in production: Normal A/B split testing
2976
- const assignedVariantName = keakInstance?.getVariant(name, variantNames) || variantNames[0];
2977
- // Find the index of the assigned variant
2978
- selectedIndex = variantElements.findIndex((variant) => variant.props.name === assignedVariantName);
2979
- // Safety fallback - if assigned variant not found, use first variant
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.warn(`⚠️ [Keak SDK] Assigned variant "${assignedVariantName}" not found, using first variant`);
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
  }