|
| 1 | +import { defineNuxtModule, addImports, addComponent, addPlugin, createResolver, addTypeTemplate } from '@nuxt/kit'; |
| 2 | +import type { PostHogConfig } from 'posthog-js'; |
| 3 | +import { defu } from 'defu'; |
| 4 | + |
| 5 | +export interface ModuleOptions { |
| 6 | + /** |
| 7 | + * The PostHog API key |
| 8 | + * @default process.env.POSTHOG_API_KEY |
| 9 | + * @example 'phc_1234567890abcdef1234567890abcdef1234567890a' |
| 10 | + * @type string |
| 11 | + * @docs https://posthog.com/docs/api |
| 12 | + */ |
| 13 | + publicKey: string; |
| 14 | + |
| 15 | + /** |
| 16 | + * The PostHog API host |
| 17 | + * @default process.env.POSTHOG_API_HOST |
| 18 | + * @example 'https://app.posthog.com' |
| 19 | + * @type string |
| 20 | + * @docs https://posthog.com/docs/api |
| 21 | + */ |
| 22 | + host: string; |
| 23 | + |
| 24 | + /** |
| 25 | + * If set to true, the module will capture page views automatically |
| 26 | + * @default true |
| 27 | + * @type boolean |
| 28 | + * @docs https://posthog.com/docs/product-analytics/capture-events#single-page-apps-and-pageviews |
| 29 | + */ |
| 30 | + capturePageViews?: boolean; |
| 31 | + |
| 32 | + /** |
| 33 | + * PostHog Client options |
| 34 | + * @default { |
| 35 | + * api_host: process.env.POSTHOG_API_HOST, |
| 36 | + * loaded: () => <enable debug mode if in development> |
| 37 | + * } |
| 38 | + * @type object |
| 39 | + * @docs https://posthog.com/docs/libraries/js#config |
| 40 | + */ |
| 41 | + clientOptions?: Partial<PostHogConfig>; |
| 42 | + |
| 43 | + /** |
| 44 | + * If set to true, the module will be disabled (no events will be sent to PostHog). |
| 45 | + * This is useful for development environments. Directives and components will still be available for you to use. |
| 46 | + * @default false |
| 47 | + * @type boolean |
| 48 | + */ |
| 49 | + disabled?: boolean; |
| 50 | +} |
| 51 | + |
| 52 | +export default defineNuxtModule<ModuleOptions>({ |
| 53 | + meta: { |
| 54 | + name: 'nuxt-posthog', |
| 55 | + configKey: 'posthog', |
| 56 | + }, |
| 57 | + defaults: { |
| 58 | + publicKey: process.env.POSTHOG_API_KEY as string, |
| 59 | + host: process.env.POSTHOG_API_HOST as string, |
| 60 | + capturePageViews: true, |
| 61 | + disabled: false, |
| 62 | + }, |
| 63 | + setup(options, nuxt) { |
| 64 | + const { resolve } = createResolver(import.meta.url); |
| 65 | + |
| 66 | + // Public runtimeConfig |
| 67 | + nuxt.options.runtimeConfig.public.posthog = defu<ModuleOptions, ModuleOptions[]>( |
| 68 | + nuxt.options.runtimeConfig.public.posthog, |
| 69 | + { |
| 70 | + publicKey: options.publicKey, |
| 71 | + host: options.host, |
| 72 | + capturePageViews: options.capturePageViews, |
| 73 | + clientOptions: options.clientOptions, |
| 74 | + disabled: options.disabled, |
| 75 | + }, |
| 76 | + ); |
| 77 | + |
| 78 | + // Make sure url and key are set |
| 79 | + if (!nuxt.options.runtimeConfig.public.posthog.publicKey) { |
| 80 | + // eslint-disable-next-line no-console |
| 81 | + console.warn('Missing PostHog API public key, set it either in `nuxt.config.ts` or via env variable'); |
| 82 | + } |
| 83 | + |
| 84 | + if (!nuxt.options.runtimeConfig.public.posthog.host) { |
| 85 | + // eslint-disable-next-line no-console |
| 86 | + console.warn('Missing PostHog API host, set it either in `nuxt.config.ts` or via env variable'); |
| 87 | + } |
| 88 | + |
| 89 | + addPlugin(resolve('./runtime/plugins/directives')); |
| 90 | + addPlugin(resolve('./runtime/plugins/posthog.client')); |
| 91 | + addPlugin(resolve('./runtime/plugins/posthog.server')); |
| 92 | + |
| 93 | + addImports({ |
| 94 | + from: resolve('./runtime/composables/usePostHogFeatureFlag'), |
| 95 | + name: 'usePostHogFeatureFlag', |
| 96 | + }); |
| 97 | + |
| 98 | + addComponent({ |
| 99 | + filePath: resolve('./runtime/components/PostHogFeatureFlag.vue'), |
| 100 | + name: 'PostHogFeatureFlag', |
| 101 | + }); |
| 102 | + |
| 103 | + addTypeTemplate({ |
| 104 | + filename: 'types/posthog-directives.d.ts', |
| 105 | + src: resolve('./runtime/types/directives.d.ts'), |
| 106 | + }); |
| 107 | + }, |
| 108 | +}); |
0 commit comments