Vue

Identify visitors in Vue 3 or Nuxt with @trace-id/sdk-vue.

10 minutes

This quickstart adds Trace to a Vue 3 app with @trace-id/sdk-vue. You install a plugin once, identify the visitor in a component, and send the eventId to your backend for verification. It works the same in Nuxt.

Before you start

You need a running Trace server and a public key (pk_...) whose allowed origins include your app. The quick start guide covers both.

  1. 1

    Install the SDK

    npm install @trace-id/sdk-vue
  2. 2

    Install the plugin

    TypeScript
    // main.ts
    import { createApp } from 'vue';
    import { createIdentify } from '@trace-id/sdk-vue';
    import App from './App.vue';
    
    createApp(App)
      .use(createIdentify({ apiKey: import.meta.env.VITE_TRACE_PUBLIC_KEY, endpoint: import.meta.env.VITE_TRACE_ENDPOINT }))
      .mount('#app');

    In Nuxt, do the same in a client-only plugin (plugins/trace.client.ts) with nuxtApp.vueApp.use(createIdentify({ ... })).

  3. 3

    Identify on submit

    useVisitorData returns refs: data, error, isLoading, and a getData function.

    Vue
    <!-- SignupForm.vue -->
    <script setup lang="ts">
    import { ref } from 'vue';
    import { useVisitorData } from '@trace-id/sdk-vue';
    
    const email = ref('');
    const { getData, isLoading } = useVisitorData({ immediate: false });
    
    async function submit() {
      const result = await getData({ tags: { action: 'signup' } }); // null if Trace is unavailable
      await fetch('/api/signup', {
        method: 'POST',
        headers: { 'Content-Type': 'application/json' },
        body: JSON.stringify({ email: email.value, eventId: result?.eventId }),
      });
    }
    </script>
    
    <template>
      <form @submit.prevent="submit">
        <input v-model="email" type="email" required />
        <button :disabled="isLoading">Create account</button>
      </form>
    </template>

    getData never throws. On failure it resolves null and sets error to an IdentifyError with a code.

  4. 4

    Verify on your server

Next steps

Suggest an edit

Spotted something wrong or unclear? Tell us on GitHub and we will fix it.