@unproducts/nuxt-posthog 2.0.2
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 +21 -0
- package/README.md +25 -0
- package/dist/module.d.mts +74 -0
- package/dist/module.json +9 -0
- package/dist/module.mjs +89 -0
- package/dist/runtime/components/PostHogFeatureFlag.d.vue.ts +37 -0
- package/dist/runtime/components/PostHogFeatureFlag.vue +14 -0
- package/dist/runtime/components/PostHogFeatureFlag.vue.d.ts +37 -0
- package/dist/runtime/composables/usePostHogFeatureFlag.d.ts +9 -0
- package/dist/runtime/composables/usePostHogFeatureFlag.js +18 -0
- package/dist/runtime/directives/v-posthog-capture.d.ts +2 -0
- package/dist/runtime/directives/v-posthog-capture.js +17 -0
- package/dist/runtime/plugins/directives.d.ts +2 -0
- package/dist/runtime/plugins/directives.js +5 -0
- package/dist/runtime/plugins/nitro.d.ts +2 -0
- package/dist/runtime/plugins/nitro.js +12 -0
- package/dist/runtime/plugins/posthog.client.d.ts +9 -0
- package/dist/runtime/plugins/posthog.client.js +60 -0
- package/dist/runtime/plugins/posthog.server.d.ts +7 -0
- package/dist/runtime/plugins/posthog.server.js +27 -0
- package/dist/runtime/types/directives.d.ts +27 -0
- package/dist/runtime/types/index.d.ts +13 -0
- package/dist/runtime/types/nitro.d.ts +7 -0
- package/dist/runtime/utils/nitro.d.ts +2 -0
- package/dist/runtime/utils/nitro.js +12 -0
- package/dist/types.d.mts +3 -0
- package/package.json +70 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) Nuxt Community
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
# @unproducts/nuxt-posthog
|
|
2
|
+
|
|
3
|
+
[![npm version][npm-version-src]][npm-version-href]
|
|
4
|
+
[![npm downloads][npm-downloads-src]][npm-downloads-href]
|
|
5
|
+
[![License][license-src]][license-href]
|
|
6
|
+
[![Nuxt][nuxt-src]][nuxt-href]
|
|
7
|
+
|
|
8
|
+
> [PostHog](https://posthog.com/) module for [Nuxt](https://nuxt.com/)
|
|
9
|
+
|
|
10
|
+
#### This is a fork of the original [nuxt-posthog](https://github.com/mitjans/nuxt-posthog) with improved runtime config handling. Also upcoming: sourcemaps upload, and logging support.
|
|
11
|
+
|
|
12
|
+
#### Other Changes
|
|
13
|
+
- Better deps management: posthog clients are peers not underlying dependencies.
|
|
14
|
+
- It is possible to selectively use posthog on either client of server.
|
|
15
|
+
|
|
16
|
+
<!-- Badges -->
|
|
17
|
+
|
|
18
|
+
[npm-version-src]: https://img.shields.io/npm/v/@unproducts/nuxt-posthog/latest.svg?style=flat&colorA=18181B&colorB=28CF8D
|
|
19
|
+
[npm-version-href]: https://npmjs.com/package/@unproducts/nuxt-posthog
|
|
20
|
+
[npm-downloads-src]: https://img.shields.io/npm/dm/@unproducts/nuxt-posthog.svg?style=flat&colorA=18181B&colorB=28CF8D
|
|
21
|
+
[npm-downloads-href]: https://npmjs.com/package/@unproducts/nuxt-posthog
|
|
22
|
+
[license-src]: https://img.shields.io/npm/l/@unproducts/nuxt-posthog.svg?style=flat&colorA=18181B&colorB=28CF8D
|
|
23
|
+
[license-href]: https://npmjs.com/package/@unproducts/nuxt-posthog
|
|
24
|
+
[nuxt-src]: https://img.shields.io/badge/Nuxt-18181B?logo=nuxt.js
|
|
25
|
+
[nuxt-href]: https://nuxt.com
|
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
import * as _nuxt_schema from '@nuxt/schema';
|
|
2
|
+
import { PostHogConfig } from 'posthog-js';
|
|
3
|
+
|
|
4
|
+
interface ModuleOptions {
|
|
5
|
+
/**
|
|
6
|
+
* The PostHog API key
|
|
7
|
+
* @default process.env.POSTHOG_API_KEY
|
|
8
|
+
* @example 'phc_1234567890abcdef1234567890abcdef1234567890a'
|
|
9
|
+
* @type string
|
|
10
|
+
* @docs https://posthog.com/docs/api
|
|
11
|
+
*/
|
|
12
|
+
publicKey: string;
|
|
13
|
+
/**
|
|
14
|
+
* The PostHog API host
|
|
15
|
+
* @default process.env.POSTHOG_API_HOST
|
|
16
|
+
* @example 'https://app.posthog.com'
|
|
17
|
+
* @type string
|
|
18
|
+
* @docs https://posthog.com/docs/api
|
|
19
|
+
*/
|
|
20
|
+
host: string;
|
|
21
|
+
/**
|
|
22
|
+
* If set to true, the module will capture page views automatically
|
|
23
|
+
* @default true
|
|
24
|
+
* @type boolean
|
|
25
|
+
* @docs https://posthog.com/docs/product-analytics/capture-events#single-page-apps-and-pageviews
|
|
26
|
+
*/
|
|
27
|
+
capturePageViews?: boolean;
|
|
28
|
+
/**
|
|
29
|
+
* If set to true, the module will capture page leaves automatically
|
|
30
|
+
* @default true
|
|
31
|
+
* @type boolean
|
|
32
|
+
* @docs https://posthog.com/docs/product-analytics/capture-events#single-page-apps-and-pageviews
|
|
33
|
+
*/
|
|
34
|
+
capturePageLeaves?: boolean;
|
|
35
|
+
/**
|
|
36
|
+
* PostHog Client options
|
|
37
|
+
* @default {
|
|
38
|
+
* api_host: process.env.POSTHOG_API_HOST,
|
|
39
|
+
* }
|
|
40
|
+
* @type object
|
|
41
|
+
* @docs https://posthog.com/docs/libraries/js#config
|
|
42
|
+
*/
|
|
43
|
+
clientOptions?: Partial<PostHogConfig>;
|
|
44
|
+
/**
|
|
45
|
+
* If set to true, the module will be disabled (no events will be sent to PostHog).
|
|
46
|
+
* This is useful for development environments. Directives and components will still be available for you to use.
|
|
47
|
+
* @default false
|
|
48
|
+
* @type boolean
|
|
49
|
+
*/
|
|
50
|
+
disabled?: boolean;
|
|
51
|
+
/**
|
|
52
|
+
* If set to true, the module will be enabled on the client side.
|
|
53
|
+
* @default true
|
|
54
|
+
* @type boolean
|
|
55
|
+
*/
|
|
56
|
+
client?: boolean;
|
|
57
|
+
/**
|
|
58
|
+
* If set to true, the module will be enabled on the server side.
|
|
59
|
+
* @default true
|
|
60
|
+
* @type boolean
|
|
61
|
+
*/
|
|
62
|
+
server?: boolean;
|
|
63
|
+
/**
|
|
64
|
+
* If set to true, PostHog will be proxied through the Nuxt server.
|
|
65
|
+
* @default false
|
|
66
|
+
* @type boolean
|
|
67
|
+
* @docs https://posthog.com/docs/advanced/proxy/nuxt
|
|
68
|
+
*/
|
|
69
|
+
proxy?: boolean;
|
|
70
|
+
}
|
|
71
|
+
declare const _default: _nuxt_schema.NuxtModule<ModuleOptions, ModuleOptions, false>;
|
|
72
|
+
|
|
73
|
+
export { _default as default };
|
|
74
|
+
export type { ModuleOptions };
|
package/dist/module.json
ADDED
package/dist/module.mjs
ADDED
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
import { defineNuxtModule, createResolver, addPlugin, addComponent, addTypeTemplate, addServerPlugin, addServerImports } from '@nuxt/kit';
|
|
2
|
+
import { defu } from 'defu';
|
|
3
|
+
|
|
4
|
+
const module$1 = defineNuxtModule({
|
|
5
|
+
meta: {
|
|
6
|
+
name: "nuxt-posthog",
|
|
7
|
+
configKey: "posthog"
|
|
8
|
+
},
|
|
9
|
+
defaults: {
|
|
10
|
+
publicKey: process.env.POSTHOG_API_KEY,
|
|
11
|
+
host: process.env.POSTHOG_API_HOST,
|
|
12
|
+
capturePageViews: true,
|
|
13
|
+
capturePageLeaves: true,
|
|
14
|
+
disabled: false,
|
|
15
|
+
client: true,
|
|
16
|
+
server: true,
|
|
17
|
+
proxy: false
|
|
18
|
+
},
|
|
19
|
+
setup(options, nuxt) {
|
|
20
|
+
const { resolve } = createResolver(import.meta.url);
|
|
21
|
+
if (!options.publicKey || !options.host) {
|
|
22
|
+
options.disabled = true;
|
|
23
|
+
} else if (!options.disabled) {
|
|
24
|
+
nuxt.options.runtimeConfig.public.posthog = defu(
|
|
25
|
+
nuxt.options.runtimeConfig.public.posthog,
|
|
26
|
+
{
|
|
27
|
+
publicKey: options.publicKey,
|
|
28
|
+
host: options.host,
|
|
29
|
+
capturePageViews: options.capturePageViews,
|
|
30
|
+
capturePageLeaves: options.capturePageLeaves,
|
|
31
|
+
clientOptions: options.clientOptions,
|
|
32
|
+
proxy: options.proxy
|
|
33
|
+
}
|
|
34
|
+
);
|
|
35
|
+
nuxt.options.runtimeConfig.posthog = defu(nuxt.options.runtimeConfig.posthog, {
|
|
36
|
+
client: !!options.client,
|
|
37
|
+
server: !!options.server
|
|
38
|
+
});
|
|
39
|
+
}
|
|
40
|
+
const config = nuxt.options.runtimeConfig.public.posthog;
|
|
41
|
+
if (config && config.proxy) {
|
|
42
|
+
const url = new URL(config.host);
|
|
43
|
+
const region = url.hostname.split(".")[0];
|
|
44
|
+
if (!region) {
|
|
45
|
+
throw new Error("Invalid PostHog API host when setting proxy");
|
|
46
|
+
}
|
|
47
|
+
if (!["eu", "us"].includes(region)) {
|
|
48
|
+
throw new Error(`Invalid PostHog API host when setting proxy, expected 'us' or 'eu', got '${region}'`);
|
|
49
|
+
}
|
|
50
|
+
nuxt.options.routeRules = nuxt.options.routeRules || {};
|
|
51
|
+
nuxt.options.routeRules["/ingest/ph/static/**"] = {
|
|
52
|
+
proxy: `https://${region}-assets.i.posthog.com/static/**`
|
|
53
|
+
};
|
|
54
|
+
nuxt.options.routeRules["/ingest/ph/**"] = {
|
|
55
|
+
proxy: `https://${region}.i.posthog.com/**`
|
|
56
|
+
};
|
|
57
|
+
}
|
|
58
|
+
nuxt.hook("imports:dirs", (dirs) => {
|
|
59
|
+
dirs.push(resolve("./runtime/composables"));
|
|
60
|
+
});
|
|
61
|
+
addPlugin(resolve("./runtime/plugins/directives"));
|
|
62
|
+
addPlugin(resolve("./runtime/plugins/posthog.client"));
|
|
63
|
+
addPlugin(resolve("./runtime/plugins/posthog.server"));
|
|
64
|
+
addComponent({
|
|
65
|
+
filePath: resolve("./runtime/components/PostHogFeatureFlag.vue"),
|
|
66
|
+
name: "PostHogFeatureFlag"
|
|
67
|
+
});
|
|
68
|
+
addTypeTemplate({
|
|
69
|
+
filename: "types/posthog-directives.d.ts",
|
|
70
|
+
src: resolve("./runtime/types/directives.d.ts")
|
|
71
|
+
});
|
|
72
|
+
addServerPlugin(resolve("./runtime/plugins/nitro"));
|
|
73
|
+
addServerImports([
|
|
74
|
+
{
|
|
75
|
+
from: resolve("./runtime/utils/nitro"),
|
|
76
|
+
name: "usePostHog"
|
|
77
|
+
}
|
|
78
|
+
]);
|
|
79
|
+
addTypeTemplate(
|
|
80
|
+
{
|
|
81
|
+
filename: "types/posthog-nitro.d.ts",
|
|
82
|
+
src: resolve("./runtime/types/nitro.d.ts")
|
|
83
|
+
},
|
|
84
|
+
{ nitro: true }
|
|
85
|
+
);
|
|
86
|
+
}
|
|
87
|
+
});
|
|
88
|
+
|
|
89
|
+
export { module$1 as default };
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
declare var __VLS_1: {
|
|
2
|
+
payload: any;
|
|
3
|
+
};
|
|
4
|
+
type __VLS_Slots = {} & {
|
|
5
|
+
default?: (props: typeof __VLS_1) => any;
|
|
6
|
+
};
|
|
7
|
+
declare const __VLS_base: import("vue").DefineComponent<import("vue").ExtractPropTypes<{
|
|
8
|
+
name: {
|
|
9
|
+
type: StringConstructor;
|
|
10
|
+
required: true;
|
|
11
|
+
};
|
|
12
|
+
match: {
|
|
13
|
+
default: boolean;
|
|
14
|
+
required: false;
|
|
15
|
+
type: (StringConstructor | BooleanConstructor)[];
|
|
16
|
+
};
|
|
17
|
+
}>, {}, {}, {}, {}, import("vue").ComponentOptionsMixin, import("vue").ComponentOptionsMixin, {}, string, import("vue").PublicProps, Readonly<import("vue").ExtractPropTypes<{
|
|
18
|
+
name: {
|
|
19
|
+
type: StringConstructor;
|
|
20
|
+
required: true;
|
|
21
|
+
};
|
|
22
|
+
match: {
|
|
23
|
+
default: boolean;
|
|
24
|
+
required: false;
|
|
25
|
+
type: (StringConstructor | BooleanConstructor)[];
|
|
26
|
+
};
|
|
27
|
+
}>> & Readonly<{}>, {
|
|
28
|
+
match: string | boolean;
|
|
29
|
+
}, {}, {}, {}, string, import("vue").ComponentProvideOptions, true, {}, any>;
|
|
30
|
+
declare const __VLS_export: __VLS_WithSlots<typeof __VLS_base, __VLS_Slots>;
|
|
31
|
+
declare const _default: typeof __VLS_export;
|
|
32
|
+
export default _default;
|
|
33
|
+
type __VLS_WithSlots<T, S> = T & {
|
|
34
|
+
new (): {
|
|
35
|
+
$slots: S;
|
|
36
|
+
};
|
|
37
|
+
};
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
<script setup>
|
|
2
|
+
import { computed } from "vue";
|
|
3
|
+
import usePostHogFeatureFlag from "../composables/usePostHogFeatureFlag";
|
|
4
|
+
const { name, match } = defineProps({
|
|
5
|
+
name: { type: String, required: true },
|
|
6
|
+
match: { default: true, required: false, type: [String, Boolean] }
|
|
7
|
+
});
|
|
8
|
+
const { getFeatureFlag } = usePostHogFeatureFlag();
|
|
9
|
+
const featureFlag = computed(() => getFeatureFlag(name));
|
|
10
|
+
</script>
|
|
11
|
+
|
|
12
|
+
<template>
|
|
13
|
+
<slot v-if="featureFlag?.value === match" :payload="featureFlag.payload" />
|
|
14
|
+
</template>
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
declare var __VLS_1: {
|
|
2
|
+
payload: any;
|
|
3
|
+
};
|
|
4
|
+
type __VLS_Slots = {} & {
|
|
5
|
+
default?: (props: typeof __VLS_1) => any;
|
|
6
|
+
};
|
|
7
|
+
declare const __VLS_base: import("vue").DefineComponent<import("vue").ExtractPropTypes<{
|
|
8
|
+
name: {
|
|
9
|
+
type: StringConstructor;
|
|
10
|
+
required: true;
|
|
11
|
+
};
|
|
12
|
+
match: {
|
|
13
|
+
default: boolean;
|
|
14
|
+
required: false;
|
|
15
|
+
type: (StringConstructor | BooleanConstructor)[];
|
|
16
|
+
};
|
|
17
|
+
}>, {}, {}, {}, {}, import("vue").ComponentOptionsMixin, import("vue").ComponentOptionsMixin, {}, string, import("vue").PublicProps, Readonly<import("vue").ExtractPropTypes<{
|
|
18
|
+
name: {
|
|
19
|
+
type: StringConstructor;
|
|
20
|
+
required: true;
|
|
21
|
+
};
|
|
22
|
+
match: {
|
|
23
|
+
default: boolean;
|
|
24
|
+
required: false;
|
|
25
|
+
type: (StringConstructor | BooleanConstructor)[];
|
|
26
|
+
};
|
|
27
|
+
}>> & Readonly<{}>, {
|
|
28
|
+
match: string | boolean;
|
|
29
|
+
}, {}, {}, {}, string, import("vue").ComponentProvideOptions, true, {}, any>;
|
|
30
|
+
declare const __VLS_export: __VLS_WithSlots<typeof __VLS_base, __VLS_Slots>;
|
|
31
|
+
declare const _default: typeof __VLS_export;
|
|
32
|
+
export default _default;
|
|
33
|
+
type __VLS_WithSlots<T, S> = T & {
|
|
34
|
+
new (): {
|
|
35
|
+
$slots: S;
|
|
36
|
+
};
|
|
37
|
+
};
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { useState } from "#app";
|
|
2
|
+
export default () => {
|
|
3
|
+
const posthogFeatureFlags = useState("ph-feature-flags");
|
|
4
|
+
const posthogFeatureFlagPayloads = useState("ph-feature-flag-payloads");
|
|
5
|
+
const isFeatureEnabled = (feature) => {
|
|
6
|
+
return posthogFeatureFlags.value?.[feature] ?? false;
|
|
7
|
+
};
|
|
8
|
+
const getFeatureFlag = (feature) => {
|
|
9
|
+
return {
|
|
10
|
+
value: posthogFeatureFlags.value?.[feature] ?? false,
|
|
11
|
+
payload: posthogFeatureFlagPayloads.value?.[feature]
|
|
12
|
+
};
|
|
13
|
+
};
|
|
14
|
+
return {
|
|
15
|
+
isFeatureEnabled,
|
|
16
|
+
getFeatureFlag
|
|
17
|
+
};
|
|
18
|
+
};
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { useNuxtApp } from "#app";
|
|
2
|
+
const directive = (el, { value, arg }) => {
|
|
3
|
+
const nuxtApp = useNuxtApp();
|
|
4
|
+
const $clientPosthog = nuxtApp.$clientPosthog;
|
|
5
|
+
if (!$clientPosthog) return;
|
|
6
|
+
if (el.hasAttribute("posthog-listener")) return;
|
|
7
|
+
el.setAttribute("posthog-listener", "true");
|
|
8
|
+
el.addEventListener(arg ?? "click", () => {
|
|
9
|
+
if (!$clientPosthog) return;
|
|
10
|
+
if (typeof value === "string") $clientPosthog.capture(value);
|
|
11
|
+
else $clientPosthog.capture(value.name, value.properties);
|
|
12
|
+
});
|
|
13
|
+
};
|
|
14
|
+
export const vPostHogCapture = {
|
|
15
|
+
mounted: directive,
|
|
16
|
+
updated: directive
|
|
17
|
+
};
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { useRuntimeConfig } from "#imports";
|
|
2
|
+
import { defineNitroPlugin } from "nitropack/runtime";
|
|
3
|
+
import { getCookie } from "h3";
|
|
4
|
+
export default defineNitroPlugin((nitroApp) => {
|
|
5
|
+
nitroApp.hooks.hook("request", async (event) => {
|
|
6
|
+
const config = useRuntimeConfig().public.posthog;
|
|
7
|
+
const runtimeConfig = useRuntimeConfig().posthog;
|
|
8
|
+
if (!config || !runtimeConfig.server) return;
|
|
9
|
+
const distinctId = getCookie(event, "ph-identify");
|
|
10
|
+
event.context.posthogId = distinctId;
|
|
11
|
+
});
|
|
12
|
+
});
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import type { PostHog } from 'posthog-js';
|
|
2
|
+
declare const _default: import("nuxt/app").Plugin<{
|
|
3
|
+
posthog: "Deprecated: use $clientPosthog instead.";
|
|
4
|
+
clientPosthog: PostHog | null;
|
|
5
|
+
}> & import("nuxt/app").ObjectPlugin<{
|
|
6
|
+
posthog: "Deprecated: use $clientPosthog instead.";
|
|
7
|
+
clientPosthog: PostHog | null;
|
|
8
|
+
}>;
|
|
9
|
+
export default _default;
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
import { defineNuxtPlugin, useCookie, useRouter, useRuntimeConfig, useState } from "#app";
|
|
2
|
+
import { defu } from "defu";
|
|
3
|
+
export default defineNuxtPlugin({
|
|
4
|
+
name: "posthog",
|
|
5
|
+
enforce: "pre",
|
|
6
|
+
setup: async () => {
|
|
7
|
+
console.log("setup posthog client");
|
|
8
|
+
const runtimeConfig = useRuntimeConfig().posthog;
|
|
9
|
+
const config = useRuntimeConfig().public.posthog;
|
|
10
|
+
if (!config || !runtimeConfig.client)
|
|
11
|
+
return {
|
|
12
|
+
provide: {
|
|
13
|
+
posthog: "Deprecated: use $clientPosthog instead.",
|
|
14
|
+
clientPosthog: null
|
|
15
|
+
}
|
|
16
|
+
};
|
|
17
|
+
const posthog = (await import("posthog-js")).posthog;
|
|
18
|
+
const posthogFeatureFlags = useState("ph-feature-flags");
|
|
19
|
+
const posthogFeatureFlagPayloads = useState("ph-feature-flag-payloads");
|
|
20
|
+
const clientOptions = defu(config.clientOptions ?? {}, {
|
|
21
|
+
api_host: config.host,
|
|
22
|
+
capture_pageview: false,
|
|
23
|
+
capture_pageleave: !!config.capturePageLeaves,
|
|
24
|
+
bootstrap: {
|
|
25
|
+
featureFlags: posthogFeatureFlags.value,
|
|
26
|
+
featureFlagPayloads: posthogFeatureFlagPayloads.value
|
|
27
|
+
}
|
|
28
|
+
});
|
|
29
|
+
if (config.proxy && config.host) {
|
|
30
|
+
const url = new URL(config.host);
|
|
31
|
+
const region = url.hostname.split(".")[0];
|
|
32
|
+
clientOptions.ui_host = `https://${region}.posthog.com`;
|
|
33
|
+
clientOptions.api_host = `${window.location.origin}/ingest/ph`;
|
|
34
|
+
}
|
|
35
|
+
const posthogClient = posthog.init(config.publicKey, clientOptions);
|
|
36
|
+
const identity = useCookie("ph-identify");
|
|
37
|
+
identity.value = posthog.get_distinct_id();
|
|
38
|
+
if (config.capturePageViews) {
|
|
39
|
+
const router = useRouter();
|
|
40
|
+
router.afterEach((to) => {
|
|
41
|
+
posthog.capture("$pageview", {
|
|
42
|
+
current_url: to.fullPath
|
|
43
|
+
});
|
|
44
|
+
});
|
|
45
|
+
}
|
|
46
|
+
posthog.onFeatureFlags((flags, featureFlags) => {
|
|
47
|
+
posthogFeatureFlags.value = featureFlags;
|
|
48
|
+
posthogFeatureFlagPayloads.value = flags.reduce((acc, flag) => {
|
|
49
|
+
acc[flag] = posthog.getFeatureFlagPayload(flag);
|
|
50
|
+
return acc;
|
|
51
|
+
}, {});
|
|
52
|
+
});
|
|
53
|
+
return {
|
|
54
|
+
provide: {
|
|
55
|
+
clientPosthog: posthogClient ? posthogClient : null,
|
|
56
|
+
posthog: "Deprecated: use $clientPosthog instead."
|
|
57
|
+
}
|
|
58
|
+
};
|
|
59
|
+
}
|
|
60
|
+
});
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import { defineNuxtPlugin, useCookie, useRuntimeConfig, useState } from "#app";
|
|
2
|
+
export default defineNuxtPlugin({
|
|
3
|
+
name: "posthog-server",
|
|
4
|
+
enforce: "pre",
|
|
5
|
+
setup: async () => {
|
|
6
|
+
const config = useRuntimeConfig().public.posthog;
|
|
7
|
+
const runtimeConfig = useRuntimeConfig().posthog;
|
|
8
|
+
if (!config || !runtimeConfig.server)
|
|
9
|
+
return {
|
|
10
|
+
provide: {
|
|
11
|
+
serverPosthog: null
|
|
12
|
+
}
|
|
13
|
+
};
|
|
14
|
+
const PostHog = (await import("posthog-node")).PostHog;
|
|
15
|
+
const posthog = new PostHog(config.publicKey, { host: config.host });
|
|
16
|
+
await posthog.reloadFeatureFlags();
|
|
17
|
+
const identity = useCookie("ph-identify", { default: () => "" });
|
|
18
|
+
const { featureFlags, featureFlagPayloads } = await posthog.getAllFlagsAndPayloads(identity.value);
|
|
19
|
+
useState("ph-feature-flags", () => featureFlags);
|
|
20
|
+
useState("ph-feature-flag-payloads", () => featureFlagPayloads);
|
|
21
|
+
return {
|
|
22
|
+
provide: {
|
|
23
|
+
serverPosthog: posthog
|
|
24
|
+
}
|
|
25
|
+
};
|
|
26
|
+
}
|
|
27
|
+
});
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import type { ObjectDirective } from 'vue';
|
|
2
|
+
import type { Property } from 'posthog-js';
|
|
3
|
+
|
|
4
|
+
declare global {
|
|
5
|
+
export interface PostHogCaptureEvent {
|
|
6
|
+
/**
|
|
7
|
+
* Event name
|
|
8
|
+
*
|
|
9
|
+
* @docs https://posthog.com/docs/product-analytics/capture-events
|
|
10
|
+
*/
|
|
11
|
+
name: string;
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* Event properties
|
|
15
|
+
*
|
|
16
|
+
* @docs https://posthog.com/docs/product-analytics/capture-events#setting-event-properties
|
|
17
|
+
*/
|
|
18
|
+
properties?: Record<string, Property>;
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
declare module 'vue' {
|
|
23
|
+
export interface ComponentCustomProperties {
|
|
24
|
+
vPosthogCapture: ObjectDirective<HTMLElement, PostHogCaptureEvent | string>;
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
export {};
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import type { ModuleOptions } from '../../module';
|
|
2
|
+
|
|
3
|
+
declare module '@nuxt/schema' {
|
|
4
|
+
interface RuntimeConfig {
|
|
5
|
+
posthog: Pick<ModuleOptions, 'client' | 'server'>;
|
|
6
|
+
}
|
|
7
|
+
interface PublicRuntimeConfig {
|
|
8
|
+
posthog?: Pick<
|
|
9
|
+
ModuleOptions,
|
|
10
|
+
'publicKey' | 'host' | 'capturePageViews' | 'capturePageLeaves' | 'clientOptions' | 'proxy'
|
|
11
|
+
>;
|
|
12
|
+
}
|
|
13
|
+
}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { useRuntimeConfig } from "#imports";
|
|
2
|
+
let posthog = null;
|
|
3
|
+
export const usePostHog = async () => {
|
|
4
|
+
const config = useRuntimeConfig().public.posthog;
|
|
5
|
+
const runtimeConfig = useRuntimeConfig().posthog;
|
|
6
|
+
if (!config || !runtimeConfig.server) return;
|
|
7
|
+
if (!posthog) {
|
|
8
|
+
const PostHog = (await import("posthog-node")).PostHog;
|
|
9
|
+
posthog = new PostHog(config.publicKey, { host: config.host });
|
|
10
|
+
}
|
|
11
|
+
return posthog;
|
|
12
|
+
};
|
package/dist/types.d.mts
ADDED
package/package.json
ADDED
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@unproducts/nuxt-posthog",
|
|
3
|
+
"version": "2.0.2",
|
|
4
|
+
"description": "PostHog module for nuxt",
|
|
5
|
+
"publishConfig": {
|
|
6
|
+
"access": "public"
|
|
7
|
+
},
|
|
8
|
+
"repository": {
|
|
9
|
+
"type": "git",
|
|
10
|
+
"url": "git+https://github.com/unproducts/nuxt-posthog-2.git"
|
|
11
|
+
},
|
|
12
|
+
"license": "MIT",
|
|
13
|
+
"type": "module",
|
|
14
|
+
"exports": {
|
|
15
|
+
".": {
|
|
16
|
+
"types": "./dist/types.d.mts",
|
|
17
|
+
"import": "./dist/module.mjs"
|
|
18
|
+
}
|
|
19
|
+
},
|
|
20
|
+
"files": [
|
|
21
|
+
"dist"
|
|
22
|
+
],
|
|
23
|
+
"resolutions": {
|
|
24
|
+
"unimport": "4.1.1"
|
|
25
|
+
},
|
|
26
|
+
"scripts": {
|
|
27
|
+
"prepack": "nuxt-module-build build",
|
|
28
|
+
"postinstall": "nuxt prepare",
|
|
29
|
+
"dev": "nuxi dev playground",
|
|
30
|
+
"dev:build": "nuxi build playground",
|
|
31
|
+
"dev:prepare": "nuxt-module-build build --stub && nuxt-module-build prepare && nuxi prepare playground",
|
|
32
|
+
"release": "npm run lint && npm run test && npm run prepack && changelogen --release && npm publish && git push --follow-tags",
|
|
33
|
+
"lint": "eslint .",
|
|
34
|
+
"test": "vitest run",
|
|
35
|
+
"test:watch": "vitest watch"
|
|
36
|
+
},
|
|
37
|
+
"dependencies": {
|
|
38
|
+
"@nuxt/kit": "^4.2.1",
|
|
39
|
+
"defu": "^6.1.4"
|
|
40
|
+
},
|
|
41
|
+
"peerDependencies": {
|
|
42
|
+
"posthog-js": "^1.252.0",
|
|
43
|
+
"posthog-node": "5.14.0"
|
|
44
|
+
},
|
|
45
|
+
"devDependencies": {
|
|
46
|
+
"@nuxt/devtools": "^3.1.1",
|
|
47
|
+
"@nuxt/eslint-config": "^1.4.1",
|
|
48
|
+
"@nuxt/module-builder": "^1.0.1",
|
|
49
|
+
"@nuxt/schema": "^4.2.1",
|
|
50
|
+
"@nuxt/test-utils": "^3.19.1",
|
|
51
|
+
"@types/node": "^24.0.1",
|
|
52
|
+
"changelogen": "^0.6.1",
|
|
53
|
+
"eslint": "^9.29.0",
|
|
54
|
+
"eslint-config-prettier": "^10.1.5",
|
|
55
|
+
"eslint-plugin-prettier": "^5.4.1",
|
|
56
|
+
"nuxt": "^4.2.1",
|
|
57
|
+
"prettier": "^3.5.3",
|
|
58
|
+
"typescript": "^5.8.3",
|
|
59
|
+
"vitest": "^4.0.14"
|
|
60
|
+
},
|
|
61
|
+
"pnpm": {
|
|
62
|
+
"onlyBuiltDependencies": [
|
|
63
|
+
"better-sqlite3"
|
|
64
|
+
],
|
|
65
|
+
"ignoredBuiltDependencies": [
|
|
66
|
+
"vue-demi"
|
|
67
|
+
]
|
|
68
|
+
},
|
|
69
|
+
"packageManager": "pnpm@10.12.1"
|
|
70
|
+
}
|