schematic-vue

schematic-vue is a client-side Vue library for Schematic which provides composables to track events, check flags, and more. schematic-vue provides the same capabilities as schematic-js, for Vue apps.

Install

$npm install @schematichq/schematic-vue
># or
>yarn add @schematichq/schematic-vue
># or
>pnpm add @schematichq/schematic-vue

Usage

SchematicPlugin

You can use the SchematicPlugin to make Schematic available throughout your Vue application:

1import { createApp } from "vue";
2import { SchematicPlugin } from "@schematichq/schematic-vue";
3import App from "./App.vue";
4
5const app = createApp(App);
6app.use(SchematicPlugin, { publishableKey: "your-publishable-key" });
7app.mount("#app");

Setting context

To set the user context for events and flag checks, you can use the identify function provided by the useSchematicEvents composable:

1<script setup lang="ts">
2import { onMounted } from "vue";
3import { useSchematicEvents } from "@schematichq/schematic-vue";
4
5const { identify } = useSchematicEvents();
6
7onMounted(() => {
8 identify({
9 keys: { id: "my-user-id" },
10 company: {
11 keys: { id: "my-company-id" },
12 traits: { location: "Atlanta, GA" },
13 },
14 });
15});
16</script>

To learn more about identifying companies with the keys map, see key management in Schematic public docs.

Tracking usage

Once you’ve set the context with identify, you can track events:

1<script setup lang="ts">
2import { useSchematicEvents } from "@schematichq/schematic-vue";
3
4const { track } = useSchematicEvents();
5
6function handleQuery() {
7 track({ event: "query" });
8}
9</script>
10
11<template>
12 <button @click="handleQuery">Run Query</button>
13</template>

If you want to record large numbers of the same event at once, or perhaps measure usage in terms of a unit like tokens or memory, you can optionally specify a quantity for your event:

1track({ event: "query", quantity: 10 });

Checking flags

To check a flag, you can use the useSchematicFlag composable:

1<script setup lang="ts">
2import { useSchematicFlag } from "@schematichq/schematic-vue";
3
4const isFeatureEnabled = useSchematicFlag("my-flag-key");
5</script>
6
7<template>
8 <div v-if="isFeatureEnabled">
9 <Feature />
10 </div>
11 <div v-else>
12 <Fallback />
13 </div>
14</template>

Checking entitlements

You can check entitlements (i.e., company access to a feature) using a flag check as well, and using the useSchematicEntitlement composable you can get additional data to render various feature states:

1<script setup lang="ts">
2import {
3 useSchematicEntitlement,
4 useSchematicIsPending,
5} from "@schematichq/schematic-vue";
6
7const schematicIsPending = useSchematicIsPending();
8const {
9 featureAllocation,
10 featureUsage,
11 featureUsageExceeded,
12 value: isFeatureEnabled,
13} = useSchematicEntitlement("my-flag-key");
14</script>
15
16<template>
17 <!-- Loading state -->
18 <Loader v-if="schematicIsPending" />
19
20 <!-- Usage exceeded state -->
21 <div v-else-if="featureUsageExceeded">
22 You have used all of your usage ({{ featureUsage }} / {{ featureAllocation }})
23 </div>
24
25 <!-- Either feature state or "no access" state -->
26 <Feature v-else-if="isFeatureEnabled" />
27 <NoAccess v-else />
28</template>

Note: useSchematicIsPending is checking if entitlement data has been loaded, typically via identify. It should, therefore, be used to wrap flag and entitlement checks, but never the initial call to identify.

Options API Support

While the primary API uses the Composition API, you can still use these composables in the Options API:

1<script>
2import { useSchematicFlag, useSchematicEvents } from "@schematichq/schematic-vue";
3
4export default {
5 setup() {
6 const isFeatureEnabled = useSchematicFlag("my-flag-key");
7 const { track } = useSchematicEvents();
8
9 return {
10 isFeatureEnabled,
11 track,
12 };
13 },
14 methods: {
15 handleAction() {
16 this.track({ event: "action" });
17 },
18 },
19};
20</script>

Troubleshooting

For debugging and development, Schematic supports two special modes:

Debug Mode

Enables console logging of all Schematic operations:

1// Enable at initialization
2import { createApp } from "vue";
3import { SchematicPlugin } from "@schematichq/schematic-vue";
4
5const app = createApp(App);
6app.use(SchematicPlugin, {
7 publishableKey: "your-publishable-key",
8 debug: true,
9});
10
11// Or via URL parameter
12// https://yoursite.com/?schematic_debug=true

Offline Mode

Prevents network requests and returns fallback values for all flag checks:

1// Enable at initialization
2import { createApp } from "vue";
3import { SchematicPlugin } from "@schematichq/schematic-vue";
4
5const app = createApp(App);
6app.use(SchematicPlugin, {
7 publishableKey: "your-publishable-key",
8 offline: true,
9});
10
11// Or via URL parameter
12// https://yoursite.com/?schematic_offline=true

Offline mode automatically enables debug mode to help with troubleshooting.

Advanced Usage

Using a Pre-configured Client

If you need more control over the Schematic client initialization, you can create a client instance and pass it to the plugin:

1import { createApp } from "vue";
2import { Schematic } from "@schematichq/schematic-js";
3import { SchematicPlugin } from "@schematichq/schematic-vue";
4import App from "./App.vue";
5
6const client = new Schematic("your-publishable-key", {
7 useWebSocket: true,
8 debug: true,
9});
10
11const app = createApp(App);
12app.use(SchematicPlugin, { client });
13app.mount("#app");

Per-Component Client Override

You can override the client for a specific component by passing a client option to any composable:

1import { useSchematicFlag } from "@schematichq/schematic-vue";
2import { Schematic } from "@schematichq/schematic-js";
3
4const customClient = new Schematic("different-api-key");
5const isFeatureEnabled = useSchematicFlag("my-flag-key", {
6 client: customClient,
7});

Server-Side Rendering (SSR)

All composables are SSR-compatible and work seamlessly with Nuxt and other Vue SSR frameworks:

  • Initial flag/entitlement values are retrieved synchronously for server-side rendering
  • Real-time subscriptions are deferred to client-side hydration
  • No special configuration needed - it just works!
1// plugins/schematic.ts
2import { SchematicPlugin } from '@schematichq/schematic-vue'
3
4export default defineNuxtPlugin((nuxtApp) => {
5 const config = useRuntimeConfig()
6 nuxtApp.vueApp.use(SchematicPlugin, { publishableKey: config.public.schematicPublishableKey })
7})
1<!-- Works in Nuxt/SSR -->
2<script setup lang="ts">
3import { useSchematicFlag } from "@schematichq/schematic-vue";
4
5// Initial value available on server, updates subscribed on client
6const isFeatureEnabled = useSchematicFlag("my-feature");
7</script>

License

MIT

Support

Need help? Please open a GitHub issue or reach out to support@schematichq.com and we’ll be happy to assist.