@funnelsgrove/runtime 0.1.46 → 0.1.48

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.
@@ -1,9 +1,30 @@
1
1
  import { resolveExperimentVariant } from './posthog-flags.js';
2
+ const resolveForcedVariant = (input) => {
3
+ var _a, _b;
4
+ if (typeof window === 'undefined') {
5
+ return null;
6
+ }
7
+ try {
8
+ const params = new URLSearchParams(window.location.search);
9
+ if (params.get('mode') !== 'test' || params.get('experiment') !== input.experimentId) {
10
+ return null;
11
+ }
12
+ const variantKey = (_a = params.get('variant')) === null || _a === void 0 ? void 0 : _a.trim();
13
+ return variantKey ? (_b = input.variants.find((variant) => variant.variantKey === variantKey)) !== null && _b !== void 0 ? _b : null : null;
14
+ }
15
+ catch (_c) {
16
+ return null;
17
+ }
18
+ };
2
19
  export const resolveExperimentAssignment = (input) => {
3
20
  var _a;
4
21
  if (input.variants.length === 0) {
5
22
  return null;
6
23
  }
24
+ const forcedVariant = resolveForcedVariant(input);
25
+ if (forcedVariant) {
26
+ return { variantKey: forcedVariant.variantKey, routeToStepId: forcedVariant.routeToStepId };
27
+ }
7
28
  const resolved = resolveExperimentVariant(input.experimentId);
8
29
  const match = resolved ? input.variants.find((variant) => variant.variantKey === resolved) : undefined;
9
30
  if (match) {
@@ -76,6 +76,7 @@ export function useFunnelFlowController({ analytics, initialStepId, initialAttri
76
76
  const firstStepViewedTrackedRef = useRef(false);
77
77
  const stepStartedAtByIdRef = useRef({});
78
78
  const engagedStepIdsRef = useRef(new Set());
79
+ const firstStepEngagementTimerRef = useRef(null);
79
80
  const currentUserIdRef = useRef(user.id);
80
81
  const safeActiveStepId = useMemo(() => { var _a; return (_a = resolveRenderableStepId(activeStepId)) !== null && _a !== void 0 ? _a : safeInitialStepId; }, [activeStepId, resolveRenderableStepId, safeInitialStepId]);
81
82
  const activeStepMeta = stepById[safeActiveStepId];
@@ -415,40 +416,41 @@ export function useFunnelFlowController({ analytics, initialStepId, initialAttri
415
416
  occurredAt: startedAt,
416
417
  featureFlags: postHogFeatureFlagsRef.current,
417
418
  });
419
+ if (!engagedStepIdsRef.current.has(renderedStepId)) {
420
+ if (firstStepEngagementTimerRef.current !== null) {
421
+ window.clearTimeout(firstStepEngagementTimerRef.current);
422
+ }
423
+ const engagedStepId = renderedStepId;
424
+ const engagedStartedAt = startedAt;
425
+ const engagedStepName = stepName;
426
+ const engagedStepType = stepType;
427
+ firstStepEngagementTimerRef.current = window.setTimeout(() => {
428
+ var _a;
429
+ firstStepEngagementTimerRef.current = null;
430
+ if (prevStepIdRef.current !== engagedStepId ||
431
+ engagedStepIdsRef.current.has(engagedStepId)) {
432
+ return;
433
+ }
434
+ engagedStepIdsRef.current.add(engagedStepId);
435
+ const engagedAt = new Date().toISOString();
436
+ (_a = analytics === null || analytics === void 0 ? void 0 : analytics.trackStepEngaged) === null || _a === void 0 ? void 0 : _a.call(analytics, {
437
+ userId: currentUserIdRef.current,
438
+ environment: analyticsRuntimeMode,
439
+ stepId: engagedStepId,
440
+ stepName: engagedStepName,
441
+ stepType: engagedStepType,
442
+ startedAt: engagedStartedAt,
443
+ engagedAt,
444
+ durationMs: FIRST_STEP_ENGAGEMENT_THRESHOLD_MS,
445
+ engagementThresholdMs: FIRST_STEP_ENGAGEMENT_THRESHOLD_MS,
446
+ featureFlags: postHogFeatureFlagsRef.current,
447
+ });
448
+ }, FIRST_STEP_ENGAGEMENT_THRESHOLD_MS);
449
+ }
418
450
  }
419
451
  }
420
452
  attributesAtStepStart.current = Object.assign({}, attributesRef.current);
421
453
  prevStepIdRef.current = renderedStepId;
422
- if (isPreviewRuntime ||
423
- renderedStepId !== defaultStepId ||
424
- engagedStepIdsRef.current.has(renderedStepId)) {
425
- return;
426
- }
427
- const engagedStepId = renderedStepId;
428
- const engagedStartedAt = startedAt;
429
- const engagedStepName = stepName;
430
- const engagedStepType = stepType;
431
- const timer = window.setTimeout(() => {
432
- var _a;
433
- if (prevStepIdRef.current !== engagedStepId || engagedStepIdsRef.current.has(engagedStepId)) {
434
- return;
435
- }
436
- engagedStepIdsRef.current.add(engagedStepId);
437
- const engagedAt = new Date().toISOString();
438
- (_a = analytics === null || analytics === void 0 ? void 0 : analytics.trackStepEngaged) === null || _a === void 0 ? void 0 : _a.call(analytics, {
439
- userId: currentUserIdRef.current,
440
- environment: analyticsRuntimeMode,
441
- stepId: engagedStepId,
442
- stepName: engagedStepName,
443
- stepType: engagedStepType,
444
- startedAt: engagedStartedAt,
445
- engagedAt,
446
- durationMs: FIRST_STEP_ENGAGEMENT_THRESHOLD_MS,
447
- engagementThresholdMs: FIRST_STEP_ENGAGEMENT_THRESHOLD_MS,
448
- featureFlags: postHogFeatureFlagsRef.current,
449
- });
450
- }, FIRST_STEP_ENGAGEMENT_THRESHOLD_MS);
451
- return () => window.clearTimeout(timer);
452
454
  }, [
453
455
  activeStepId,
454
456
  analytics,
@@ -462,6 +464,12 @@ export function useFunnelFlowController({ analytics, initialStepId, initialAttri
462
464
  renderedStepMeta === null || renderedStepMeta === void 0 ? void 0 : renderedStepMeta.type,
463
465
  safeActiveStepId,
464
466
  ]);
467
+ useEffect(() => () => {
468
+ if (firstStepEngagementTimerRef.current !== null) {
469
+ window.clearTimeout(firstStepEngagementTimerRef.current);
470
+ firstStepEngagementTimerRef.current = null;
471
+ }
472
+ }, []);
465
473
  useEffect(() => {
466
474
  setActiveStepId(safeInitialStepId);
467
475
  }, [safeInitialStepId]);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@funnelsgrove/runtime",
3
- "version": "0.1.46",
3
+ "version": "0.1.48",
4
4
  "type": "module",
5
5
  "private": false,
6
6
  "main": "./dist/index.js",