@lowdefy/plugin-posthog 0.0.0-experimental-20260925135632
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/LICENSE +201 -0
- package/README.md +81 -0
- package/dist/actions/PostHogAlias.js +31 -0
- package/dist/actions/PostHogAlias.schema.js +31 -0
- package/dist/actions/PostHogCapture.js +48 -0
- package/dist/actions/PostHogCapture.schema.js +42 -0
- package/dist/actions/PostHogCapturePageLeave.js +33 -0
- package/dist/actions/PostHogCapturePageLeave.schema.js +27 -0
- package/dist/actions/PostHogFeatureFlag.js +51 -0
- package/dist/actions/PostHogFeatureFlag.schema.js +42 -0
- package/dist/actions/PostHogGroup.js +38 -0
- package/dist/actions/PostHogGroup.schema.js +41 -0
- package/dist/actions/PostHogIdentify.js +61 -0
- package/dist/actions/PostHogIdentify.schema.js +38 -0
- package/dist/actions/PostHogInit.js +66 -0
- package/dist/actions/PostHogInit.schema.js +70 -0
- package/dist/actions/PostHogOptIn.js +28 -0
- package/dist/actions/PostHogOptIn.schema.js +17 -0
- package/dist/actions/PostHogOptOut.js +27 -0
- package/dist/actions/PostHogOptOut.schema.js +17 -0
- package/dist/actions/PostHogPageview.js +35 -0
- package/dist/actions/PostHogPageview.schema.js +27 -0
- package/dist/actions/PostHogReloadFeatureFlags.js +61 -0
- package/dist/actions/PostHogReloadFeatureFlags.schema.js +28 -0
- package/dist/actions/PostHogReset.js +31 -0
- package/dist/actions/PostHogReset.schema.js +27 -0
- package/dist/actions/PostHogSetPersonProperties.js +39 -0
- package/dist/actions/PostHogSetPersonProperties.schema.js +43 -0
- package/dist/actions.js +27 -0
- package/dist/lib/cleanProperties.js +22 -0
- package/dist/lib/getPostHog.js +35 -0
- package/dist/lib/initPostHog.js +45 -0
- package/dist/lib/loadPostHog.js +37 -0
- package/dist/lib/postHogState.js +32 -0
- package/dist/lib/warnUninitialized.js +25 -0
- package/dist/schemas.js +27 -0
- package/dist/test/resetPostHogState.js +25 -0
- package/dist/types.js +31 -0
- package/package.json +60 -0
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
/*
|
|
2
|
+
Copyright 2020-2026 Lowdefy, Inc
|
|
3
|
+
|
|
4
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
5
|
+
you may not use this file except in compliance with the License.
|
|
6
|
+
You may obtain a copy of the License at
|
|
7
|
+
|
|
8
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
9
|
+
|
|
10
|
+
Unless required by applicable law or agreed to in writing, software
|
|
11
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
12
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
13
|
+
See the License for the specific language governing permissions and
|
|
14
|
+
limitations under the License.
|
|
15
|
+
*/ export default {
|
|
16
|
+
type: 'object',
|
|
17
|
+
params: {
|
|
18
|
+
type: 'object',
|
|
19
|
+
required: [
|
|
20
|
+
'type',
|
|
21
|
+
'key'
|
|
22
|
+
],
|
|
23
|
+
properties: {
|
|
24
|
+
type: {
|
|
25
|
+
type: 'string',
|
|
26
|
+
minLength: 1,
|
|
27
|
+
description: 'The group type, for example organization. Group types have to be enabled on the PostHog project.'
|
|
28
|
+
},
|
|
29
|
+
key: {
|
|
30
|
+
type: 'string',
|
|
31
|
+
minLength: 1,
|
|
32
|
+
description: 'The id of the group instance.'
|
|
33
|
+
},
|
|
34
|
+
properties: {
|
|
35
|
+
type: 'object',
|
|
36
|
+
description: 'Group properties.'
|
|
37
|
+
}
|
|
38
|
+
},
|
|
39
|
+
additionalProperties: false
|
|
40
|
+
}
|
|
41
|
+
};
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
/*
|
|
2
|
+
Copyright 2020-2026 Lowdefy, Inc
|
|
3
|
+
|
|
4
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
5
|
+
you may not use this file except in compliance with the License.
|
|
6
|
+
You may obtain a copy of the License at
|
|
7
|
+
|
|
8
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
9
|
+
|
|
10
|
+
Unless required by applicable law or agreed to in writing, software
|
|
11
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
12
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
13
|
+
See the License for the specific language governing permissions and
|
|
14
|
+
limitations under the License.
|
|
15
|
+
*/ import { type } from '@lowdefy/helpers';
|
|
16
|
+
import cleanProperties from '../lib/cleanProperties.js';
|
|
17
|
+
import getPostHog from '../lib/getPostHog.js';
|
|
18
|
+
// Tie the current browser session to a person. Run it once the user is known,
|
|
19
|
+
// usually from the page onInit event after PostHogInit, or after a successful
|
|
20
|
+
// login. It is safe to run on every page.
|
|
21
|
+
//
|
|
22
|
+
// Never pass personally identifiable information as a property.
|
|
23
|
+
async function PostHogIdentify({ params }) {
|
|
24
|
+
const { id, properties, propertiesOnce } = params ?? {};
|
|
25
|
+
if (!type.isNone(id) && !type.isString(id)) {
|
|
26
|
+
throw new Error(`PostHogIdentify "id" must be a string. Received ${JSON.stringify(id)}.`);
|
|
27
|
+
}
|
|
28
|
+
if (!type.isNone(properties) && !type.isObject(properties)) {
|
|
29
|
+
throw new Error(`PostHogIdentify "properties" must be an object. Received ${JSON.stringify(properties)}.`);
|
|
30
|
+
}
|
|
31
|
+
if (!type.isNone(propertiesOnce) && !type.isObject(propertiesOnce)) {
|
|
32
|
+
throw new Error(`PostHogIdentify "propertiesOnce" must be an object. Received ${JSON.stringify(propertiesOnce)}.`);
|
|
33
|
+
}
|
|
34
|
+
const posthog = await getPostHog({
|
|
35
|
+
action: 'PostHogIdentify'
|
|
36
|
+
});
|
|
37
|
+
if (type.isNone(posthog)) return null;
|
|
38
|
+
// Anonymous visitor on a public page. Leave the anonymous session alone:
|
|
39
|
+
// it is what a later identify() merges into the person.
|
|
40
|
+
if (type.isNone(id) || id.trim() === '') return null;
|
|
41
|
+
const set = cleanProperties(properties);
|
|
42
|
+
const setOnce = cleanProperties(propertiesOnce);
|
|
43
|
+
if (posthog.get_distinct_id() === id) {
|
|
44
|
+
// posthog-js skips a setPersonProperties call identical to the last one,
|
|
45
|
+
// so running this on every page load does not resend unchanged properties.
|
|
46
|
+
if (Object.keys(set).length > 0 || Object.keys(setOnce).length > 0) {
|
|
47
|
+
posthog.setPersonProperties(set, setOnce);
|
|
48
|
+
}
|
|
49
|
+
return null;
|
|
50
|
+
}
|
|
51
|
+
// PostHog refuses to move an identified distinct_id onto a different person,
|
|
52
|
+
// so a person swap on a shared browser needs a fresh session first. An
|
|
53
|
+
// anonymous session is not reset: identify() merges it into the person,
|
|
54
|
+
// which is what makes signup funnels work.
|
|
55
|
+
if (posthog.get_property('$user_state') === 'identified') {
|
|
56
|
+
posthog.reset();
|
|
57
|
+
}
|
|
58
|
+
posthog.identify(id, set, setOnce);
|
|
59
|
+
return null;
|
|
60
|
+
}
|
|
61
|
+
export default PostHogIdentify;
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
/*
|
|
2
|
+
Copyright 2020-2026 Lowdefy, Inc
|
|
3
|
+
|
|
4
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
5
|
+
you may not use this file except in compliance with the License.
|
|
6
|
+
You may obtain a copy of the License at
|
|
7
|
+
|
|
8
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
9
|
+
|
|
10
|
+
Unless required by applicable law or agreed to in writing, software
|
|
11
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
12
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
13
|
+
See the License for the specific language governing permissions and
|
|
14
|
+
limitations under the License.
|
|
15
|
+
*/ export default {
|
|
16
|
+
type: 'object',
|
|
17
|
+
params: {
|
|
18
|
+
type: 'object',
|
|
19
|
+
properties: {
|
|
20
|
+
id: {
|
|
21
|
+
type: [
|
|
22
|
+
'string',
|
|
23
|
+
'null'
|
|
24
|
+
],
|
|
25
|
+
description: "The person's id. A missing, null or empty id does nothing, so the anonymous session on a public page is left alone."
|
|
26
|
+
},
|
|
27
|
+
properties: {
|
|
28
|
+
type: 'object',
|
|
29
|
+
description: 'Person properties to set. Null and undefined values are dropped, so an absent value never overwrites one PostHog already has.'
|
|
30
|
+
},
|
|
31
|
+
propertiesOnce: {
|
|
32
|
+
type: 'object',
|
|
33
|
+
description: 'Person properties that are only set the first time, for example a signup date.'
|
|
34
|
+
}
|
|
35
|
+
},
|
|
36
|
+
additionalProperties: false
|
|
37
|
+
}
|
|
38
|
+
};
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
/*
|
|
2
|
+
Copyright 2020-2026 Lowdefy, Inc
|
|
3
|
+
|
|
4
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
5
|
+
you may not use this file except in compliance with the License.
|
|
6
|
+
You may obtain a copy of the License at
|
|
7
|
+
|
|
8
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
9
|
+
|
|
10
|
+
Unless required by applicable law or agreed to in writing, software
|
|
11
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
12
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
13
|
+
See the License for the specific language governing permissions and
|
|
14
|
+
limitations under the License.
|
|
15
|
+
*/ import { type } from '@lowdefy/helpers';
|
|
16
|
+
import initPostHog from '../lib/initPostHog.js';
|
|
17
|
+
const defaultApiHost = 'https://us.i.posthog.com';
|
|
18
|
+
// Initialise posthog-js. Every other action in this package does nothing until
|
|
19
|
+
// it has run, so run it from the onInit event of every page, shared with _ref.
|
|
20
|
+
// Calling it again with the same apiKey does nothing, which is what makes that
|
|
21
|
+
// safe. The deployment environment (config.environments / LOWDEFY_ENVIRONMENT)
|
|
22
|
+
// is registered as the `environment` super property, so every event carries it.
|
|
23
|
+
async function PostHogInit({ params, lowdefyApp }) {
|
|
24
|
+
if (!type.isObject(params)) {
|
|
25
|
+
throw new Error(`PostHogInit params must be an object. Received ${JSON.stringify(params)}.`);
|
|
26
|
+
}
|
|
27
|
+
const { apiKey, apiHost, debug, enabled, options } = params;
|
|
28
|
+
if (!type.isNone(enabled) && !type.isBoolean(enabled)) {
|
|
29
|
+
throw new Error(`PostHogInit "enabled" must be a boolean. Received ${JSON.stringify(enabled)}.`);
|
|
30
|
+
}
|
|
31
|
+
if (!type.isNone(options) && !type.isObject(options)) {
|
|
32
|
+
throw new Error(`PostHogInit "options" must be an object. Received ${JSON.stringify(options)}.`);
|
|
33
|
+
}
|
|
34
|
+
if (!type.isNone(apiHost) && (!type.isString(apiHost) || apiHost.trim() === '')) {
|
|
35
|
+
throw new Error(`PostHogInit "apiHost" must be a non-empty string. Received ${JSON.stringify(apiHost)}.`);
|
|
36
|
+
}
|
|
37
|
+
if (!type.isNone(debug) && !type.isBoolean(debug)) {
|
|
38
|
+
throw new Error(`PostHogInit "debug" must be a boolean. Received ${JSON.stringify(debug)}.`);
|
|
39
|
+
}
|
|
40
|
+
// A disabled deployment never talks to PostHog, so it does not need a key.
|
|
41
|
+
// The current environment can switch PostHog off (config.environments.<env>.posthog.enabled:
|
|
42
|
+
// false). That wins over the enabled param, and a switched-off deployment needs no key.
|
|
43
|
+
const environmentOff = (lowdefyApp?.disabled ?? []).includes('posthog');
|
|
44
|
+
if (enabled !== false && !environmentOff && (!type.isString(apiKey) || apiKey.trim() === '')) {
|
|
45
|
+
throw new Error(`PostHogInit "apiKey" must be a non-empty string. Received ${JSON.stringify(apiKey)}.`);
|
|
46
|
+
}
|
|
47
|
+
const initOptions = options ?? {};
|
|
48
|
+
const config = {
|
|
49
|
+
...initOptions,
|
|
50
|
+
api_host: apiHost ?? initOptions.api_host ?? defaultApiHost
|
|
51
|
+
};
|
|
52
|
+
if (type.isBoolean(debug)) {
|
|
53
|
+
config.debug = debug;
|
|
54
|
+
}
|
|
55
|
+
const superProperties = type.isNone(lowdefyApp?.environment) ? {} : {
|
|
56
|
+
environment: lowdefyApp.environment
|
|
57
|
+
};
|
|
58
|
+
await initPostHog({
|
|
59
|
+
apiKey: apiKey ?? null,
|
|
60
|
+
config,
|
|
61
|
+
enabled: environmentOff ? false : enabled,
|
|
62
|
+
superProperties
|
|
63
|
+
});
|
|
64
|
+
return null;
|
|
65
|
+
}
|
|
66
|
+
export default PostHogInit;
|
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
/*
|
|
2
|
+
Copyright 2020-2026 Lowdefy, Inc
|
|
3
|
+
|
|
4
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
5
|
+
you may not use this file except in compliance with the License.
|
|
6
|
+
You may obtain a copy of the License at
|
|
7
|
+
|
|
8
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
9
|
+
|
|
10
|
+
Unless required by applicable law or agreed to in writing, software
|
|
11
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
12
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
13
|
+
See the License for the specific language governing permissions and
|
|
14
|
+
limitations under the License.
|
|
15
|
+
*/ export default {
|
|
16
|
+
type: 'object',
|
|
17
|
+
params: {
|
|
18
|
+
type: 'object',
|
|
19
|
+
properties: {
|
|
20
|
+
apiKey: {
|
|
21
|
+
type: [
|
|
22
|
+
'string',
|
|
23
|
+
'null'
|
|
24
|
+
],
|
|
25
|
+
description: 'The PostHog project API key. A public, write only key that is meant to ship in the browser bundle - never a personal API key. Required unless enabled is false.'
|
|
26
|
+
},
|
|
27
|
+
apiHost: {
|
|
28
|
+
type: 'string',
|
|
29
|
+
minLength: 1,
|
|
30
|
+
description: 'The PostHog ingestion host. Defaults to https://us.i.posthog.com. Use https://eu.i.posthog.com for the EU cloud, or the host of a self hosted instance.'
|
|
31
|
+
},
|
|
32
|
+
options: {
|
|
33
|
+
type: 'object',
|
|
34
|
+
description: 'Passed through to posthog.init as its config object, for example capture_pageview, person_profiles, autocapture, persistence or opt_out_capturing_by_default.'
|
|
35
|
+
},
|
|
36
|
+
debug: {
|
|
37
|
+
type: 'boolean',
|
|
38
|
+
description: 'Log everything PostHog does to the browser console.'
|
|
39
|
+
},
|
|
40
|
+
enabled: {
|
|
41
|
+
type: 'boolean',
|
|
42
|
+
description: 'Set to false to skip loading posthog-js. Every other PostHog action then does nothing. Defaults to true.'
|
|
43
|
+
}
|
|
44
|
+
},
|
|
45
|
+
if: {
|
|
46
|
+
not: {
|
|
47
|
+
properties: {
|
|
48
|
+
enabled: {
|
|
49
|
+
const: false
|
|
50
|
+
}
|
|
51
|
+
},
|
|
52
|
+
required: [
|
|
53
|
+
'enabled'
|
|
54
|
+
]
|
|
55
|
+
}
|
|
56
|
+
},
|
|
57
|
+
then: {
|
|
58
|
+
required: [
|
|
59
|
+
'apiKey'
|
|
60
|
+
],
|
|
61
|
+
properties: {
|
|
62
|
+
apiKey: {
|
|
63
|
+
type: 'string',
|
|
64
|
+
minLength: 1
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
},
|
|
68
|
+
additionalProperties: false
|
|
69
|
+
}
|
|
70
|
+
};
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
/*
|
|
2
|
+
Copyright 2020-2026 Lowdefy, Inc
|
|
3
|
+
|
|
4
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
5
|
+
you may not use this file except in compliance with the License.
|
|
6
|
+
You may obtain a copy of the License at
|
|
7
|
+
|
|
8
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
9
|
+
|
|
10
|
+
Unless required by applicable law or agreed to in writing, software
|
|
11
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
12
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
13
|
+
See the License for the specific language governing permissions and
|
|
14
|
+
limitations under the License.
|
|
15
|
+
*/ import { type } from '@lowdefy/helpers';
|
|
16
|
+
import getPostHog from '../lib/getPostHog.js';
|
|
17
|
+
// Start capturing for this person. Pair it with PostHogOptOut and a PostHogInit
|
|
18
|
+
// that sets options.opt_out_capturing_by_default to true when consent is
|
|
19
|
+
// required before any data is sent.
|
|
20
|
+
async function PostHogOptIn() {
|
|
21
|
+
const posthog = await getPostHog({
|
|
22
|
+
action: 'PostHogOptIn'
|
|
23
|
+
});
|
|
24
|
+
if (type.isNone(posthog)) return null;
|
|
25
|
+
posthog.opt_in_capturing();
|
|
26
|
+
return null;
|
|
27
|
+
}
|
|
28
|
+
export default PostHogOptIn;
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
/*
|
|
2
|
+
Copyright 2020-2026 Lowdefy, Inc
|
|
3
|
+
|
|
4
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
5
|
+
you may not use this file except in compliance with the License.
|
|
6
|
+
You may obtain a copy of the License at
|
|
7
|
+
|
|
8
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
9
|
+
|
|
10
|
+
Unless required by applicable law or agreed to in writing, software
|
|
11
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
12
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
13
|
+
See the License for the specific language governing permissions and
|
|
14
|
+
limitations under the License.
|
|
15
|
+
*/ export default {
|
|
16
|
+
type: 'object'
|
|
17
|
+
};
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
/*
|
|
2
|
+
Copyright 2020-2026 Lowdefy, Inc
|
|
3
|
+
|
|
4
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
5
|
+
you may not use this file except in compliance with the License.
|
|
6
|
+
You may obtain a copy of the License at
|
|
7
|
+
|
|
8
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
9
|
+
|
|
10
|
+
Unless required by applicable law or agreed to in writing, software
|
|
11
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
12
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
13
|
+
See the License for the specific language governing permissions and
|
|
14
|
+
limitations under the License.
|
|
15
|
+
*/ import { type } from '@lowdefy/helpers';
|
|
16
|
+
import getPostHog from '../lib/getPostHog.js';
|
|
17
|
+
// Stop capturing for this person. The choice is remembered in the persistence
|
|
18
|
+
// store configured on PostHogInit.
|
|
19
|
+
async function PostHogOptOut() {
|
|
20
|
+
const posthog = await getPostHog({
|
|
21
|
+
action: 'PostHogOptOut'
|
|
22
|
+
});
|
|
23
|
+
if (type.isNone(posthog)) return null;
|
|
24
|
+
posthog.opt_out_capturing();
|
|
25
|
+
return null;
|
|
26
|
+
}
|
|
27
|
+
export default PostHogOptOut;
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
/*
|
|
2
|
+
Copyright 2020-2026 Lowdefy, Inc
|
|
3
|
+
|
|
4
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
5
|
+
you may not use this file except in compliance with the License.
|
|
6
|
+
You may obtain a copy of the License at
|
|
7
|
+
|
|
8
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
9
|
+
|
|
10
|
+
Unless required by applicable law or agreed to in writing, software
|
|
11
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
12
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
13
|
+
See the License for the specific language governing permissions and
|
|
14
|
+
limitations under the License.
|
|
15
|
+
*/ export default {
|
|
16
|
+
type: 'object'
|
|
17
|
+
};
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
/*
|
|
2
|
+
Copyright 2020-2026 Lowdefy, Inc
|
|
3
|
+
|
|
4
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
5
|
+
you may not use this file except in compliance with the License.
|
|
6
|
+
You may obtain a copy of the License at
|
|
7
|
+
|
|
8
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
9
|
+
|
|
10
|
+
Unless required by applicable law or agreed to in writing, software
|
|
11
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
12
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
13
|
+
See the License for the specific language governing permissions and
|
|
14
|
+
limitations under the License.
|
|
15
|
+
*/ import { type } from '@lowdefy/helpers';
|
|
16
|
+
import getPostHog from '../lib/getPostHog.js';
|
|
17
|
+
// Capture a pageview by hand. Lowdefy routes on the client without a page
|
|
18
|
+
// load, so the posthog-js default (capture_pageview: true) only records the
|
|
19
|
+
// first page. Prefer PostHogInit options.capture_pageview: 'history_change',
|
|
20
|
+
// which records every client side navigation. Use this action instead when
|
|
21
|
+
// pageviews need extra properties or tighter control: set capture_pageview to
|
|
22
|
+
// false and run it from each page's onMount event.
|
|
23
|
+
async function PostHogPageview({ params }) {
|
|
24
|
+
const { properties } = params ?? {};
|
|
25
|
+
if (!type.isNone(properties) && !type.isObject(properties)) {
|
|
26
|
+
throw new Error(`PostHogPageview "properties" must be an object. Received ${JSON.stringify(properties)}.`);
|
|
27
|
+
}
|
|
28
|
+
const posthog = await getPostHog({
|
|
29
|
+
action: 'PostHogPageview'
|
|
30
|
+
});
|
|
31
|
+
if (type.isNone(posthog)) return null;
|
|
32
|
+
posthog.capture('$pageview', properties ?? undefined);
|
|
33
|
+
return null;
|
|
34
|
+
}
|
|
35
|
+
export default PostHogPageview;
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
/*
|
|
2
|
+
Copyright 2020-2026 Lowdefy, Inc
|
|
3
|
+
|
|
4
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
5
|
+
you may not use this file except in compliance with the License.
|
|
6
|
+
You may obtain a copy of the License at
|
|
7
|
+
|
|
8
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
9
|
+
|
|
10
|
+
Unless required by applicable law or agreed to in writing, software
|
|
11
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
12
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
13
|
+
See the License for the specific language governing permissions and
|
|
14
|
+
limitations under the License.
|
|
15
|
+
*/ export default {
|
|
16
|
+
type: 'object',
|
|
17
|
+
params: {
|
|
18
|
+
type: 'object',
|
|
19
|
+
properties: {
|
|
20
|
+
properties: {
|
|
21
|
+
type: 'object',
|
|
22
|
+
description: 'Properties to add to the $pageview event.'
|
|
23
|
+
}
|
|
24
|
+
},
|
|
25
|
+
additionalProperties: false
|
|
26
|
+
}
|
|
27
|
+
};
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
/*
|
|
2
|
+
Copyright 2020-2026 Lowdefy, Inc
|
|
3
|
+
|
|
4
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
5
|
+
you may not use this file except in compliance with the License.
|
|
6
|
+
You may obtain a copy of the License at
|
|
7
|
+
|
|
8
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
9
|
+
|
|
10
|
+
Unless required by applicable law or agreed to in writing, software
|
|
11
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
12
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
13
|
+
See the License for the specific language governing permissions and
|
|
14
|
+
limitations under the License.
|
|
15
|
+
*/ import { type } from '@lowdefy/helpers';
|
|
16
|
+
import getPostHog from '../lib/getPostHog.js';
|
|
17
|
+
const defaultTimeout = 5000;
|
|
18
|
+
// Ask PostHog to evaluate this person's feature flags again, and resolve once
|
|
19
|
+
// the new flags have arrived. Run it after PostHogIdentify or PostHogGroup,
|
|
20
|
+
// then read the flags with PostHogFeatureFlag.
|
|
21
|
+
//
|
|
22
|
+
// Resolves with { flags, variants } - flags is the list of enabled flag keys,
|
|
23
|
+
// variants maps every flag key to its value. Resolves with empty results if
|
|
24
|
+
// PostHog does not answer within "timeout" milliseconds, so a flaky network
|
|
25
|
+
// can never stall the action chain.
|
|
26
|
+
async function PostHogReloadFeatureFlags({ params }) {
|
|
27
|
+
const { timeout } = params ?? {};
|
|
28
|
+
if (!type.isNone(timeout) && (!type.isInt(timeout) || timeout < 0)) {
|
|
29
|
+
throw new Error(`PostHogReloadFeatureFlags "timeout" must be a non-negative integer. Received ${JSON.stringify(timeout)}.`);
|
|
30
|
+
}
|
|
31
|
+
const posthog = await getPostHog({
|
|
32
|
+
action: 'PostHogReloadFeatureFlags'
|
|
33
|
+
});
|
|
34
|
+
if (type.isNone(posthog)) return {
|
|
35
|
+
flags: [],
|
|
36
|
+
variants: {}
|
|
37
|
+
};
|
|
38
|
+
return new Promise((resolve)=>{
|
|
39
|
+
let listening = false;
|
|
40
|
+
let timer;
|
|
41
|
+
let unsubscribe;
|
|
42
|
+
function settle(flags, variants) {
|
|
43
|
+
clearTimeout(timer);
|
|
44
|
+
unsubscribe();
|
|
45
|
+
resolve({
|
|
46
|
+
flags: flags ?? [],
|
|
47
|
+
variants: variants ?? {}
|
|
48
|
+
});
|
|
49
|
+
}
|
|
50
|
+
// onFeatureFlags calls back straight away with the flags that are already
|
|
51
|
+
// loaded. Those are the stale flags this action exists to replace, so only
|
|
52
|
+
// a call that arrives after it returns counts.
|
|
53
|
+
unsubscribe = posthog.onFeatureFlags((flags, variants)=>{
|
|
54
|
+
if (listening) settle(flags, variants);
|
|
55
|
+
});
|
|
56
|
+
listening = true;
|
|
57
|
+
timer = setTimeout(()=>settle(), timeout ?? defaultTimeout);
|
|
58
|
+
posthog.reloadFeatureFlags();
|
|
59
|
+
});
|
|
60
|
+
}
|
|
61
|
+
export default PostHogReloadFeatureFlags;
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
/*
|
|
2
|
+
Copyright 2020-2026 Lowdefy, Inc
|
|
3
|
+
|
|
4
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
5
|
+
you may not use this file except in compliance with the License.
|
|
6
|
+
You may obtain a copy of the License at
|
|
7
|
+
|
|
8
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
9
|
+
|
|
10
|
+
Unless required by applicable law or agreed to in writing, software
|
|
11
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
12
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
13
|
+
See the License for the specific language governing permissions and
|
|
14
|
+
limitations under the License.
|
|
15
|
+
*/ export default {
|
|
16
|
+
type: 'object',
|
|
17
|
+
params: {
|
|
18
|
+
type: 'object',
|
|
19
|
+
properties: {
|
|
20
|
+
timeout: {
|
|
21
|
+
type: 'integer',
|
|
22
|
+
minimum: 0,
|
|
23
|
+
description: 'Milliseconds to wait for PostHog to answer before resolving with empty results. Defaults to 5000.'
|
|
24
|
+
}
|
|
25
|
+
},
|
|
26
|
+
additionalProperties: false
|
|
27
|
+
}
|
|
28
|
+
};
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
/*
|
|
2
|
+
Copyright 2020-2026 Lowdefy, Inc
|
|
3
|
+
|
|
4
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
5
|
+
you may not use this file except in compliance with the License.
|
|
6
|
+
You may obtain a copy of the License at
|
|
7
|
+
|
|
8
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
9
|
+
|
|
10
|
+
Unless required by applicable law or agreed to in writing, software
|
|
11
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
12
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
13
|
+
See the License for the specific language governing permissions and
|
|
14
|
+
limitations under the License.
|
|
15
|
+
*/ import { type } from '@lowdefy/helpers';
|
|
16
|
+
import getPostHog from '../lib/getPostHog.js';
|
|
17
|
+
// Forget the current person and start a fresh anonymous session. Run it on
|
|
18
|
+
// sign out so the next person on a shared browser is not merged into this one.
|
|
19
|
+
async function PostHogReset({ params }) {
|
|
20
|
+
const { resetDeviceId } = params ?? {};
|
|
21
|
+
if (!type.isNone(resetDeviceId) && !type.isBoolean(resetDeviceId)) {
|
|
22
|
+
throw new Error(`PostHogReset "resetDeviceId" must be a boolean. Received ${JSON.stringify(resetDeviceId)}.`);
|
|
23
|
+
}
|
|
24
|
+
const posthog = await getPostHog({
|
|
25
|
+
action: 'PostHogReset'
|
|
26
|
+
});
|
|
27
|
+
if (type.isNone(posthog)) return null;
|
|
28
|
+
posthog.reset(resetDeviceId === true);
|
|
29
|
+
return null;
|
|
30
|
+
}
|
|
31
|
+
export default PostHogReset;
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
/*
|
|
2
|
+
Copyright 2020-2026 Lowdefy, Inc
|
|
3
|
+
|
|
4
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
5
|
+
you may not use this file except in compliance with the License.
|
|
6
|
+
You may obtain a copy of the License at
|
|
7
|
+
|
|
8
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
9
|
+
|
|
10
|
+
Unless required by applicable law or agreed to in writing, software
|
|
11
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
12
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
13
|
+
See the License for the specific language governing permissions and
|
|
14
|
+
limitations under the License.
|
|
15
|
+
*/ export default {
|
|
16
|
+
type: 'object',
|
|
17
|
+
params: {
|
|
18
|
+
type: 'object',
|
|
19
|
+
properties: {
|
|
20
|
+
resetDeviceId: {
|
|
21
|
+
type: 'boolean',
|
|
22
|
+
description: 'Also generate a new device id, so the browser is not recognised as the same device. Defaults to false.'
|
|
23
|
+
}
|
|
24
|
+
},
|
|
25
|
+
additionalProperties: false
|
|
26
|
+
}
|
|
27
|
+
};
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
/*
|
|
2
|
+
Copyright 2020-2026 Lowdefy, Inc
|
|
3
|
+
|
|
4
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
5
|
+
you may not use this file except in compliance with the License.
|
|
6
|
+
You may obtain a copy of the License at
|
|
7
|
+
|
|
8
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
9
|
+
|
|
10
|
+
Unless required by applicable law or agreed to in writing, software
|
|
11
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
12
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
13
|
+
See the License for the specific language governing permissions and
|
|
14
|
+
limitations under the License.
|
|
15
|
+
*/ import { type } from '@lowdefy/helpers';
|
|
16
|
+
import getPostHog from '../lib/getPostHog.js';
|
|
17
|
+
// Update the properties of the person PostHogIdentify identified. Each call is
|
|
18
|
+
// a billable event; posthog-js skips a call identical to the previous one.
|
|
19
|
+
//
|
|
20
|
+
// Never send personally identifiable information.
|
|
21
|
+
async function PostHogSetPersonProperties({ params }) {
|
|
22
|
+
const { set, setOnce } = params ?? {};
|
|
23
|
+
if (!type.isNone(set) && !type.isObject(set)) {
|
|
24
|
+
throw new Error(`PostHogSetPersonProperties "set" must be an object. Received ${JSON.stringify(set)}.`);
|
|
25
|
+
}
|
|
26
|
+
if (!type.isNone(setOnce) && !type.isObject(setOnce)) {
|
|
27
|
+
throw new Error(`PostHogSetPersonProperties "setOnce" must be an object. Received ${JSON.stringify(setOnce)}.`);
|
|
28
|
+
}
|
|
29
|
+
if (type.isNone(set) && type.isNone(setOnce)) {
|
|
30
|
+
throw new Error('PostHogSetPersonProperties requires a "set" or "setOnce" object.');
|
|
31
|
+
}
|
|
32
|
+
const posthog = await getPostHog({
|
|
33
|
+
action: 'PostHogSetPersonProperties'
|
|
34
|
+
});
|
|
35
|
+
if (type.isNone(posthog)) return null;
|
|
36
|
+
posthog.setPersonProperties(set ?? undefined, setOnce ?? undefined);
|
|
37
|
+
return null;
|
|
38
|
+
}
|
|
39
|
+
export default PostHogSetPersonProperties;
|