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