Skip to main content

Vue / Browser — Alias

Link an alias (e.g. anonymous ID) to the current user. Use this to merge anonymous events (from before login) with the identified user.

alias(alias)

Links an alias to the current user. Sends a $create_alias event.
ParameterTypeRequiredDescription
aliasstringyesAlias to link (e.g. previous anonymous ID)
When to use: After login to merge anonymous events (from before login) with the identified user. This ensures all events are associated with the same user profile.

Vue example

<script setup>
import { onMounted, ref } from 'vue'
import { createSignal } from '@signal-js/browser'

let signal = null
const anonymousId = ref(null)

onMounted(async () => {
  signal = createSignal({ endpoint: '...', apiKey: '...', projectId: '...' })
  await signal.start()
  
  // Store anonymous ID before login
  const stored = localStorage.getItem('anonymous_id')
  if (stored) {
    anonymousId.value = stored
  }
})

function handleLogin(userId, userTraits) {
  // Identify the user
  signal?.identify(userId, userTraits)
  
  // Link anonymous ID if it exists
  if (anonymousId.value) {
    signal?.alias(anonymousId.value)
    localStorage.removeItem('anonymous_id')
  }
}
</script>

<template>
  <button @click="handleLogin('user_123', { email: 'user@example.com' })">
    Log in
  </button>
</template>

Vanilla JS example

const signal = createSignal({ endpoint: '...', apiKey: '...', projectId: '...' });
await signal.start();

// Store anonymous ID before login
let anonymousId = localStorage.getItem('anonymous_id') || null;

function handleLogin(userId, userTraits) {
  // Identify the user
  signal.identify(userId, userTraits);
  
  // Link anonymous ID if it exists
  if (anonymousId) {
    signal.alias(anonymousId);
    localStorage.removeItem('anonymous_id');
  }
}

How it works

  1. Before login: User browses anonymously. Events are tracked with an anonymous ID.
  2. After login: Call identify(userId, traits) to set the user’s identity.
  3. Link alias: Call alias(anonymousId) to link the anonymous ID to the identified user.
  4. Result: All events (both anonymous and identified) are now associated with the same user profile.

See also