@funnelsgrove/runtime 0.1.33 → 0.1.35

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,23 +1,48 @@
1
1
  import type { FunnelManifestExperiment } from './funnel.manifest.types.js';
2
- export type FunnelExperimentDefinition = {
2
+ export type FunnelExperimentStatus = 'draft' | 'running' | 'paused' | 'stopped';
3
+ export type FunnelRouteExperimentType = 'paywall' | 'step';
4
+ type FunnelExperimentBase = {
3
5
  id: string;
4
6
  name: string;
5
- type: 'paywall' | 'step';
6
- status?: 'running' | 'paused' | 'stopped';
7
+ status?: FunnelExperimentStatus;
7
8
  launchDate: string;
9
+ };
10
+ export type FunnelRouteExperimentDefinition = FunnelExperimentBase & {
11
+ type: FunnelRouteExperimentType;
12
+ stepId?: string;
8
13
  control: {
14
+ variantKey?: string;
9
15
  stepId: string;
10
16
  label?: string;
11
17
  trafficPercent: number;
12
18
  };
13
19
  variant: {
20
+ variantKey?: string;
14
21
  stepId: string;
15
22
  label?: string;
16
23
  trafficPercent: number;
17
24
  };
18
25
  };
19
- export type PaywallExperimentDefinition = FunnelExperimentDefinition & {
26
+ export type FunnelPricingExperimentDefinition = FunnelExperimentBase & {
27
+ type: 'pricing';
28
+ paywallStepId: string;
29
+ control: {
30
+ variantKey?: string;
31
+ offerSetKey: string;
32
+ label?: string;
33
+ trafficPercent: number;
34
+ };
35
+ variant: {
36
+ variantKey?: string;
37
+ offerSetKey: string;
38
+ label?: string;
39
+ trafficPercent: number;
40
+ };
41
+ };
42
+ export type FunnelExperimentDefinition = FunnelRouteExperimentDefinition | FunnelPricingExperimentDefinition;
43
+ export type PaywallExperimentDefinition = FunnelRouteExperimentDefinition & {
20
44
  type: 'paywall';
21
45
  };
22
46
  export declare const defineFunnelExperiments: <const T extends readonly FunnelExperimentDefinition[]>(experiments: T) => T;
23
47
  export declare const toManifestExperiments: (experiments: readonly FunnelExperimentDefinition[]) => FunnelManifestExperiment[];
48
+ export {};
@@ -11,9 +11,15 @@ export const toManifestExperiments = (experiments) => experiments
11
11
  .filter((experiment) => { var _a; return ((_a = experiment.status) !== null && _a !== void 0 ? _a : 'running') === 'running'; })
12
12
  .map((experiment) => ({
13
13
  experimentId: experiment.id,
14
- stepId: experiment.control.stepId,
14
+ stepId: experiment.type === 'pricing'
15
+ ? experiment.paywallStepId
16
+ : experiment.stepId || experiment.control.stepId,
15
17
  variants: [
16
- Object.assign(Object.assign({ variantKey: 'control' }, (experiment.control.label ? { label: experiment.control.label } : {})), { trafficPercent: experiment.control.trafficPercent, routeToStepId: experiment.control.stepId }),
17
- Object.assign(Object.assign({ variantKey: 'variant_b' }, (experiment.variant.label ? { label: experiment.variant.label } : {})), { trafficPercent: experiment.variant.trafficPercent, routeToStepId: experiment.variant.stepId }),
18
+ Object.assign(Object.assign({ variantKey: experiment.control.variantKey || 'control' }, (experiment.control.label ? { label: experiment.control.label } : {})), { trafficPercent: experiment.control.trafficPercent, routeToStepId: experiment.type === 'pricing'
19
+ ? experiment.paywallStepId
20
+ : experiment.control.stepId }),
21
+ Object.assign(Object.assign({ variantKey: experiment.variant.variantKey || 'variant_b' }, (experiment.variant.label ? { label: experiment.variant.label } : {})), { trafficPercent: experiment.variant.trafficPercent, routeToStepId: experiment.type === 'pricing'
22
+ ? experiment.paywallStepId
23
+ : experiment.variant.stepId }),
18
24
  ],
19
25
  }));
@@ -2,6 +2,7 @@ import posthog from 'posthog-js';
2
2
  let initialized = false;
3
3
  let readyPromise = null;
4
4
  export const POSTHOG_FEATURE_FLAG_READY_TIMEOUT_MS = 2500;
5
+ const STORED_PERSON_PROPERTIES_KEY = '$stored_person_properties';
5
6
  const buildFlagScopeProperties = (config) => {
6
7
  var _a, _b;
7
8
  const properties = {};
@@ -15,6 +16,42 @@ const buildFlagScopeProperties = (config) => {
15
16
  }
16
17
  return properties;
17
18
  };
19
+ const parseStorageObject = (value) => {
20
+ if (!value) {
21
+ return {};
22
+ }
23
+ try {
24
+ const parsed = JSON.parse(value);
25
+ return parsed && typeof parsed === 'object' && !Array.isArray(parsed)
26
+ ? parsed
27
+ : {};
28
+ }
29
+ catch (_a) {
30
+ return {};
31
+ }
32
+ };
33
+ const toPostHogPersistenceKey = (apiKey) => {
34
+ const token = apiKey.replace(/\+/g, 'PL').replace(/\//g, 'SL').replace(/=/g, 'EQ');
35
+ return `ph_${token}_posthog`;
36
+ };
37
+ const primePostHogFlagScopePersistence = (apiKey, scopeProperties) => {
38
+ var _a;
39
+ if (Object.keys(scopeProperties).length === 0 || typeof window === 'undefined') {
40
+ return;
41
+ }
42
+ try {
43
+ const storage = window.localStorage;
44
+ const persistenceKey = toPostHogPersistenceKey(apiKey);
45
+ const stored = parseStorageObject(storage.getItem(persistenceKey));
46
+ const currentPersonProperties = parseStorageObject(typeof stored[STORED_PERSON_PROPERTIES_KEY] === 'string'
47
+ ? stored[STORED_PERSON_PROPERTIES_KEY]
48
+ : JSON.stringify((_a = stored[STORED_PERSON_PROPERTIES_KEY]) !== null && _a !== void 0 ? _a : {}));
49
+ storage.setItem(persistenceKey, JSON.stringify(Object.assign(Object.assign({}, stored), { [STORED_PERSON_PROPERTIES_KEY]: Object.assign(Object.assign({}, currentPersonProperties), scopeProperties) })));
50
+ }
51
+ catch (_b) {
52
+ // PostHog can still initialize without persistence priming when storage is unavailable.
53
+ }
54
+ };
18
55
  export const bootstrapPostHog = (config) => {
19
56
  if (readyPromise) {
20
57
  return readyPromise;
@@ -36,6 +73,8 @@ export const bootstrapPostHog = (config) => {
36
73
  resolve();
37
74
  };
38
75
  readyTimeoutId = setTimeout(markReady, POSTHOG_FEATURE_FLAG_READY_TIMEOUT_MS);
76
+ const scopeProperties = buildFlagScopeProperties(config);
77
+ primePostHogFlagScopePersistence(config.apiKey, scopeProperties);
39
78
  posthog.init(config.apiKey, {
40
79
  api_host: config.apiHost,
41
80
  persistence: 'localStorage+cookie',
@@ -43,15 +82,15 @@ export const bootstrapPostHog = (config) => {
43
82
  advanced_disable_feature_flags_on_first_load: true,
44
83
  bootstrap: { distinctID: config.distinctId },
45
84
  loaded: () => {
46
- const scopeProperties = buildFlagScopeProperties(config);
47
- if (Object.keys(scopeProperties).length > 0) {
48
- posthog.setPersonPropertiesForFlags(scopeProperties, false);
49
- }
50
85
  let unsubscribe = null;
51
86
  unsubscribe = posthog.onFeatureFlags(() => {
52
87
  unsubscribe === null || unsubscribe === void 0 ? void 0 : unsubscribe();
53
88
  markReady();
54
89
  });
90
+ if (Object.keys(scopeProperties).length > 0) {
91
+ posthog.setPersonPropertiesForFlags(scopeProperties, true);
92
+ return;
93
+ }
55
94
  posthog.reloadFeatureFlags();
56
95
  },
57
96
  });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@funnelsgrove/runtime",
3
- "version": "0.1.33",
3
+ "version": "0.1.35",
4
4
  "type": "module",
5
5
  "private": false,
6
6
  "main": "./dist/index.js",