@unproducts/nuxt-posthog 2.0.3 → 2.0.6
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/module.d.mts +11 -8
- package/dist/module.json +1 -1
- package/dist/module.mjs +25 -25
- package/dist/runtime/plugins/directives.d.ts +1 -1
- package/dist/runtime/plugins/nitro.js +1 -2
- package/dist/runtime/plugins/posthog.client.d.ts +2 -2
- package/dist/runtime/plugins/posthog.client.js +8 -5
- package/dist/runtime/plugins/posthog.server.d.ts +2 -2
- package/dist/runtime/plugins/posthog.server.js +9 -5
- package/dist/runtime/types/index.d.ts +10 -3
- package/dist/runtime/utils/nitro.d.ts +1 -1
- package/dist/runtime/utils/nitro.js +11 -4
- package/package.json +3 -2
package/dist/module.d.mts
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import * as _nuxt_schema from '@nuxt/schema';
|
|
2
2
|
import { PostHogConfig } from 'posthog-js';
|
|
3
|
+
import { PostHogOptions } from 'posthog-node';
|
|
3
4
|
|
|
4
5
|
interface ModuleOptions {
|
|
5
6
|
/**
|
|
@@ -9,7 +10,7 @@ interface ModuleOptions {
|
|
|
9
10
|
* @type string
|
|
10
11
|
* @docs https://posthog.com/docs/api
|
|
11
12
|
*/
|
|
12
|
-
|
|
13
|
+
key: string;
|
|
13
14
|
/**
|
|
14
15
|
* The PostHog API host
|
|
15
16
|
* @default process.env.POSTHOG_API_HOST
|
|
@@ -42,21 +43,23 @@ interface ModuleOptions {
|
|
|
42
43
|
*/
|
|
43
44
|
clientOptions?: Partial<PostHogConfig>;
|
|
44
45
|
/**
|
|
45
|
-
*
|
|
46
|
-
*
|
|
47
|
-
*
|
|
48
|
-
*
|
|
46
|
+
* PostHog Server options
|
|
47
|
+
* @default {
|
|
48
|
+
* api_host: process.env.POSTHOG_API_HOST,
|
|
49
|
+
* }
|
|
50
|
+
* @type object
|
|
51
|
+
* @docs https://posthog.com/docs/libraries/node#config
|
|
49
52
|
*/
|
|
50
|
-
|
|
53
|
+
serverOptions?: Partial<PostHogOptions>;
|
|
51
54
|
/**
|
|
52
55
|
* If set to true, the module will be enabled on the client side.
|
|
53
|
-
* @default
|
|
56
|
+
* @default false
|
|
54
57
|
* @type boolean
|
|
55
58
|
*/
|
|
56
59
|
client?: boolean;
|
|
57
60
|
/**
|
|
58
61
|
* If set to true, the module will be enabled on the server side.
|
|
59
|
-
* @default
|
|
62
|
+
* @default false
|
|
60
63
|
* @type boolean
|
|
61
64
|
*/
|
|
62
65
|
server?: boolean;
|
package/dist/module.json
CHANGED
package/dist/module.mjs
CHANGED
|
@@ -7,39 +7,39 @@ const module$1 = defineNuxtModule({
|
|
|
7
7
|
configKey: "posthog"
|
|
8
8
|
},
|
|
9
9
|
defaults: {
|
|
10
|
-
|
|
10
|
+
key: process.env.POSTHOG_API_KEY,
|
|
11
11
|
host: process.env.POSTHOG_API_HOST,
|
|
12
12
|
capturePageViews: true,
|
|
13
13
|
capturePageLeaves: true,
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
server: true,
|
|
14
|
+
client: false,
|
|
15
|
+
server: false,
|
|
17
16
|
proxy: false
|
|
18
17
|
},
|
|
19
18
|
setup(options, nuxt) {
|
|
20
19
|
const { resolve } = createResolver(import.meta.url);
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
{
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
20
|
+
const mergedKeyHost = defu(nuxt.options.runtimeConfig.public.posthog, { key: options.key, host: options.host });
|
|
21
|
+
if (options.server) {
|
|
22
|
+
nuxt.options.runtimeConfig.posthog = {
|
|
23
|
+
...nuxt.options.runtimeConfig.posthog,
|
|
24
|
+
server: true,
|
|
25
|
+
...options.serverOptions && { serverOptions: options.serverOptions }
|
|
26
|
+
};
|
|
27
|
+
}
|
|
28
|
+
if (options.client) {
|
|
29
|
+
nuxt.options.runtimeConfig.public.posthog = {
|
|
30
|
+
...nuxt.options.runtimeConfig.public.posthog,
|
|
31
|
+
key: mergedKeyHost.key,
|
|
32
|
+
host: mergedKeyHost.host,
|
|
33
|
+
capturePageViews: options.capturePageViews,
|
|
34
|
+
capturePageLeaves: options.capturePageLeaves,
|
|
35
|
+
clientOptions: options.clientOptions,
|
|
36
|
+
proxy: options.proxy,
|
|
37
|
+
client: true
|
|
38
|
+
};
|
|
39
39
|
}
|
|
40
|
-
const
|
|
41
|
-
if (
|
|
42
|
-
const url = new URL(
|
|
40
|
+
const publicPosthog = nuxt.options.runtimeConfig.public.posthog;
|
|
41
|
+
if (publicPosthog?.proxy && publicPosthog?.host) {
|
|
42
|
+
const url = new URL(publicPosthog.host);
|
|
43
43
|
const region = url.hostname.split(".")[0];
|
|
44
44
|
if (!region) {
|
|
45
45
|
throw new Error("Invalid PostHog API host when setting proxy");
|
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
declare const _default: import("
|
|
1
|
+
declare const _default: import("#app").Plugin<Record<string, unknown>> & import("#app").ObjectPlugin<Record<string, unknown>>;
|
|
2
2
|
export default _default;
|
|
@@ -4,8 +4,7 @@ import { getCookie } from "h3";
|
|
|
4
4
|
export default defineNitroPlugin((nitroApp) => {
|
|
5
5
|
nitroApp.hooks.hook("request", async (event) => {
|
|
6
6
|
const config = useRuntimeConfig().public.posthog;
|
|
7
|
-
|
|
8
|
-
if (!config || !runtimeConfig.server) return;
|
|
7
|
+
if (!config?.client) return;
|
|
9
8
|
const distinctId = getCookie(event, "ph-identify");
|
|
10
9
|
event.context.posthogId = distinctId;
|
|
11
10
|
});
|
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
import type { PostHog } from 'posthog-js';
|
|
2
|
-
declare const _default: import("
|
|
2
|
+
declare const _default: import("#app").Plugin<{
|
|
3
3
|
posthog: "Deprecated: use $clientPosthog instead.";
|
|
4
4
|
clientPosthog: PostHog | null;
|
|
5
|
-
}> & import("
|
|
5
|
+
}> & import("#app").ObjectPlugin<{
|
|
6
6
|
posthog: "Deprecated: use $clientPosthog instead.";
|
|
7
7
|
clientPosthog: PostHog | null;
|
|
8
8
|
}>;
|
|
@@ -2,18 +2,21 @@ import { defineNuxtPlugin, useCookie, useRouter, useRuntimeConfig, useState } fr
|
|
|
2
2
|
import { defu } from "defu";
|
|
3
3
|
export default defineNuxtPlugin({
|
|
4
4
|
name: "posthog",
|
|
5
|
-
enforce: "
|
|
5
|
+
enforce: "post",
|
|
6
6
|
setup: async () => {
|
|
7
|
-
console.log("setup posthog client");
|
|
8
|
-
const runtimeConfig = useRuntimeConfig().posthog;
|
|
9
7
|
const config = useRuntimeConfig().public.posthog;
|
|
10
|
-
if (!config
|
|
8
|
+
if (!config?.client)
|
|
11
9
|
return {
|
|
12
10
|
provide: {
|
|
13
11
|
posthog: "Deprecated: use $clientPosthog instead.",
|
|
14
12
|
clientPosthog: null
|
|
15
13
|
}
|
|
16
14
|
};
|
|
15
|
+
if (!config.key || !config.host) {
|
|
16
|
+
throw new Error(
|
|
17
|
+
"PostHog client is enabled but key or host not found. Set client to false in module options to disable."
|
|
18
|
+
);
|
|
19
|
+
}
|
|
17
20
|
const posthog = (await import("posthog-js")).posthog;
|
|
18
21
|
const posthogFeatureFlags = useState("ph-feature-flags");
|
|
19
22
|
const posthogFeatureFlagPayloads = useState("ph-feature-flag-payloads");
|
|
@@ -32,7 +35,7 @@ export default defineNuxtPlugin({
|
|
|
32
35
|
clientOptions.ui_host = `https://${region}.posthog.com`;
|
|
33
36
|
clientOptions.api_host = `${window.location.origin}/ingest/ph`;
|
|
34
37
|
}
|
|
35
|
-
const posthogClient = posthog.init(config.
|
|
38
|
+
const posthogClient = posthog.init(config.key, clientOptions);
|
|
36
39
|
const identity = useCookie("ph-identify");
|
|
37
40
|
identity.value = posthog.get_distinct_id();
|
|
38
41
|
if (config.capturePageViews) {
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import type { PostHog } from 'posthog-node';
|
|
2
|
-
declare const _default: import("
|
|
2
|
+
declare const _default: import("#app").Plugin<{
|
|
3
3
|
serverPosthog: PostHog | null;
|
|
4
|
-
}> & import("
|
|
4
|
+
}> & import("#app").ObjectPlugin<{
|
|
5
5
|
serverPosthog: PostHog | null;
|
|
6
6
|
}>;
|
|
7
7
|
export default _default;
|
|
@@ -1,18 +1,22 @@
|
|
|
1
1
|
import { defineNuxtPlugin, useCookie, useRuntimeConfig, useState } from "#app";
|
|
2
2
|
export default defineNuxtPlugin({
|
|
3
3
|
name: "posthog-server",
|
|
4
|
-
enforce: "
|
|
4
|
+
enforce: "post",
|
|
5
5
|
setup: async () => {
|
|
6
|
-
const
|
|
7
|
-
|
|
8
|
-
if (!config || !runtimeConfig.server)
|
|
6
|
+
const publicConfig = useRuntimeConfig().public.posthog;
|
|
7
|
+
if (!publicConfig?.client)
|
|
9
8
|
return {
|
|
10
9
|
provide: {
|
|
11
10
|
serverPosthog: null
|
|
12
11
|
}
|
|
13
12
|
};
|
|
13
|
+
if (!publicConfig.key || !publicConfig.host) {
|
|
14
|
+
throw new Error(
|
|
15
|
+
"PostHog client (SSR) is enabled but key or host not found. Set client to false in module options to disable."
|
|
16
|
+
);
|
|
17
|
+
}
|
|
14
18
|
const PostHog = (await import("posthog-node")).PostHog;
|
|
15
|
-
const posthog = new PostHog(
|
|
19
|
+
const posthog = new PostHog(publicConfig.key, { host: publicConfig.host });
|
|
16
20
|
await posthog.reloadFeatureFlags();
|
|
17
21
|
const identity = useCookie("ph-identify", { default: () => "" });
|
|
18
22
|
const { featureFlags, featureFlagPayloads } = await posthog.getAllFlagsAndPayloads(identity.value);
|
|
@@ -1,13 +1,20 @@
|
|
|
1
1
|
import type { ModuleOptions } from '../../module';
|
|
2
2
|
|
|
3
3
|
declare module '@nuxt/schema' {
|
|
4
|
+
/** Server-only config (private). Key/host live in public only; Nitro usePostHog reads serverOptions here. */
|
|
4
5
|
interface RuntimeConfig {
|
|
5
|
-
posthog:
|
|
6
|
+
posthog: {
|
|
7
|
+
server: boolean;
|
|
8
|
+
serverOptions?: ModuleOptions['serverOptions'];
|
|
9
|
+
};
|
|
6
10
|
}
|
|
11
|
+
/** Client config (public). Single source of truth for key/host. Read in client plugins and server plugin. */
|
|
7
12
|
interface PublicRuntimeConfig {
|
|
8
13
|
posthog?: Pick<
|
|
9
14
|
ModuleOptions,
|
|
10
|
-
'
|
|
11
|
-
|
|
15
|
+
'key' | 'host' | 'capturePageViews' | 'capturePageLeaves' | 'clientOptions' | 'proxy'
|
|
16
|
+
> & {
|
|
17
|
+
client: boolean;
|
|
18
|
+
};
|
|
12
19
|
}
|
|
13
20
|
}
|
|
@@ -1,2 +1,2 @@
|
|
|
1
1
|
import type { PostHog } from 'posthog-node';
|
|
2
|
-
export declare const usePostHog: () => Promise<PostHog |
|
|
2
|
+
export declare const usePostHog: () => Promise<PostHog | null>;
|
|
@@ -1,12 +1,19 @@
|
|
|
1
1
|
import { useRuntimeConfig } from "#imports";
|
|
2
|
+
const POSTHOG_SERVER_MISSING = "PostHog server is enabled but key or host not found. Set server to false in module options to disable.";
|
|
2
3
|
let posthog = null;
|
|
3
4
|
export const usePostHog = async () => {
|
|
4
|
-
const
|
|
5
|
-
const
|
|
6
|
-
if (!
|
|
5
|
+
const serverConfig = useRuntimeConfig().posthog;
|
|
6
|
+
const publicConfig = useRuntimeConfig().public.posthog;
|
|
7
|
+
if (!serverConfig?.server) return null;
|
|
8
|
+
if (!publicConfig?.key || !publicConfig?.host) {
|
|
9
|
+
throw new Error(POSTHOG_SERVER_MISSING);
|
|
10
|
+
}
|
|
7
11
|
if (!posthog) {
|
|
8
12
|
const PostHog = (await import("posthog-node")).PostHog;
|
|
9
|
-
posthog = new PostHog(
|
|
13
|
+
posthog = new PostHog(publicConfig.key, {
|
|
14
|
+
host: publicConfig.host,
|
|
15
|
+
...serverConfig.serverOptions
|
|
16
|
+
});
|
|
10
17
|
}
|
|
11
18
|
return posthog;
|
|
12
19
|
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@unproducts/nuxt-posthog",
|
|
3
|
-
"version": "2.0.
|
|
3
|
+
"version": "2.0.6",
|
|
4
4
|
"description": "PostHog module for nuxt",
|
|
5
5
|
"publishConfig": {
|
|
6
6
|
"access": "public"
|
|
@@ -39,7 +39,7 @@
|
|
|
39
39
|
},
|
|
40
40
|
"peerDependencies": {
|
|
41
41
|
"posthog-js": "^1.252.0",
|
|
42
|
-
"posthog-node": "5.14.0"
|
|
42
|
+
"posthog-node": "^5.14.0"
|
|
43
43
|
},
|
|
44
44
|
"devDependencies": {
|
|
45
45
|
"@nuxt/devtools": "^3.1.1",
|
|
@@ -48,6 +48,7 @@
|
|
|
48
48
|
"@nuxt/schema": "^4.2.1",
|
|
49
49
|
"@nuxt/test-utils": "^3.19.1",
|
|
50
50
|
"@types/node": "^24.0.1",
|
|
51
|
+
"baseline-browser-mapping": "^2.9.19",
|
|
51
52
|
"changelogen": "^0.6.1",
|
|
52
53
|
"eslint": "^9.29.0",
|
|
53
54
|
"eslint-config-prettier": "^10.1.5",
|