Skip to main content

Documentation Index

Fetch the complete documentation index at: https://docs.encorekit.com/llms.txt

Use this file to discover all available pages before exploring further.

Set or update user attributes for offer targeting and personalization. Attributes are merged with existing ones, not replaced.

Signature

function setUserAttributes(attributes: UserAttributes): void

Parameters

attributes
UserAttributes
required
User attributes object. Attributes are merged with existing ones.
interface UserAttributes {
  // Identity
  email?: string
  firstName?: string
  lastName?: string
  phoneNumber?: string
  
  // Location
  postalCode?: string
  city?: string
  state?: string
  countryCode?: string
  latitude?: string
  longitude?: string
  
  // Demographics
  dateOfBirth?: string  // ISO 8601
  gender?: string
  language?: string
  
  // Subscription
  subscriptionTier?: string
  monthsSubscribed?: string
  billingCycle?: string
  lastPaymentAmount?: string
  
  // Engagement
  lastActiveDate?: string  // ISO 8601
  totalSessions?: string
  
  // Custom
  custom?: Record<string, string>
}

Return Value

Type: void

Examples

Basic Attributes

import Encore from '@encorekit/web-sdk';

Encore.setUserAttributes({
  email: 'user@example.com',
  firstName: 'John',
  lastName: 'Doe'
});

Complete Profile

Encore.setUserAttributes({
  // Identity
  email: 'user@example.com',
  firstName: 'John',
  lastName: 'Doe',
  phoneNumber: '+1-555-0123',
  
  // Location
  city: 'San Francisco',
  state: 'CA',
  postalCode: '94102',
  countryCode: 'US',
  
  // Subscription
  subscriptionTier: 'premium',
  monthsSubscribed: '12',
  billingCycle: 'annual',
  
  // Custom
  custom: {
    accountType: 'business',
    industry: 'technology'
  }
});

Incremental Updates

Attributes are merged, not replaced:
// Set initial attributes
Encore.setUserAttributes({
  email: 'user@example.com',
  subscriptionTier: 'free'
});

// Later, add more attributes
Encore.setUserAttributes({
  city: 'New York',
  state: 'NY'
});

// All attributes are now set:
// { email, subscriptionTier, city, state }

Attribute Categories

Identity Attributes

Encore.setUserAttributes({
  email: 'user@example.com',
  firstName: 'John',
  lastName: 'Doe',
  phoneNumber: '+1-555-0123'
});

Location Attributes

Encore.setUserAttributes({
  city: 'San Francisco',
  state: 'CA',
  postalCode: '94102',
  countryCode: 'US',
  latitude: '37.7749',
  longitude: '-122.4194'
});

Demographics

Encore.setUserAttributes({
  dateOfBirth: '1990-01-15',  // ISO 8601
  gender: 'male',
  language: 'en'
});

Subscription Attributes

Encore.setUserAttributes({
  subscriptionTier: 'premium',
  monthsSubscribed: '12',
  billingCycle: 'annual',
  lastPaymentAmount: '99.99'
});

Engagement Attributes

Encore.setUserAttributes({
  lastActiveDate: new Date().toISOString(),
  totalSessions: '150'
});

Custom Attributes

Encore.setUserAttributes({
  custom: {
    accountType: 'business',
    referralSource: 'google',
    lifetimeValue: '599.99',
    betaTester: 'true',
    preferredTheme: 'dark'
  }
});

Common Use Cases

After Profile Update

function handleProfileUpdate(updates) {
  // Update your backend
  await updateUserProfile(updates);
  
  // Update Encore
  Encore.setUserAttributes({
    firstName: updates.firstName,
    lastName: updates.lastName,
    city: updates.city,
    state: updates.state
  });
}

After Subscription Change

function handleSubscriptionUpgrade(plan) {
  Encore.setUserAttributes({
    subscriptionTier: plan.tier,
    billingCycle: plan.cycle,
    monthsSubscribed: '0',  // Just upgraded
    lastPaymentAmount: plan.price
  });
}

Track User Activity

function trackUserSession() {
  const sessions = incrementSessionCount();
  
  Encore.setUserAttributes({
    lastActiveDate: new Date().toISOString(),
    totalSessions: String(sessions)
  });
}

Dynamic Segmentation

function updateUserSegment(user) {
  const churnRisk = calculateChurnRisk(user);
  const ltv = calculateLifetimeValue(user);
  
  Encore.setUserAttributes({
    custom: {
      churnRisk: String(churnRisk),
      lifetimeValue: String(ltv),
      userSegment: determineSegment(churnRisk, ltv)
    }
  });
}

Getting Attributes

getUserAttributes()

Retrieve currently set attributes:
const attributes = Encore.getUserAttributes();
console.log('User attributes:', attributes);

Best Practices

1. Update Incrementally

You don’t need to set all attributes at once:
// Good: Set what you know when you know it
Encore.setUserAttributes({ email: user.email });

// Later
Encore.setUserAttributes({ subscriptionTier: 'premium' });

2. Keep Attributes Updated

Update attributes when they change:
// When user upgrades
function onSubscriptionChange(newTier) {
  Encore.setUserAttributes({
    subscriptionTier: newTier,
    lastPaymentAmount: getPrice(newTier)
  });
}

3. Use Custom Attributes

Leverage custom attributes for app-specific data:
Encore.setUserAttributes({
  custom: {
    onboardingCompleted: 'true',
    featureAUsage: '45',
    preferredLanguage: 'es'
  }
});

4. Use ISO 8601 for Dates

// Good: ISO 8601
Encore.setUserAttributes({
  dateOfBirth: '1990-01-15',
  lastActiveDate: new Date().toISOString()
});

// Avoid: Other formats
Encore.setUserAttributes({
  dateOfBirth: '01/15/1990'  // ❌
});

Complete Example

import Encore from '@encorekit/web-sdk';

class UserProfile {
  updateProfile(updates) {
    // Update Encore attributes
    Encore.setUserAttributes({
      firstName: updates.firstName,
      lastName: updates.lastName,
      email: updates.email,
      phoneNumber: updates.phoneNumber,
      city: updates.city,
      state: updates.state,
      postalCode: updates.postalCode
    });
  }
  
  updateSubscription(subscription) {
    Encore.setUserAttributes({
      subscriptionTier: subscription.tier,
      billingCycle: subscription.cycle,
      monthsSubscribed: String(subscription.monthsActive),
      lastPaymentAmount: String(subscription.lastPayment)
    });
  }
  
  trackActivity() {
    const sessionCount = this.getSessionCount();
    
    Encore.setUserAttributes({
      lastActiveDate: new Date().toISOString(),
      totalSessions: String(sessionCount)
    });
  }
  
  setCustomSegment(userData) {
    const ltv = this.calculateLTV(userData);
    const churnRisk = this.predictChurn(userData);
    
    Encore.setUserAttributes({
      custom: {
        lifetimeValue: String(ltv),
        churnRisk: churnRisk,
        userSegment: this.determineSegment(ltv, churnRisk),
        accountAge: String(this.getAccountAge(userData))
      }
    });
  }
}

Next Steps