Options
All
  • Public
  • Public/Protected
  • All
Menu

Class Events

Hierarchy

  • Events

Index

Constructors

Methods

Constructors

constructor

Methods

off

  • off(id: number): void
  • Remove an event listener that was previously attached with Otis.events.on or Otis.events.once. Pass a listener ID (returned from the on/once methods) to this method to remove that listener and prevent it from executing its handler function in the future.

    example
    const messagesDuringEachExpansion = {};
    let currentExpansionTimestamp;
    let messageListenerId;
    
    Otis.events.on('expand', () => {
      console.log('Starting collection for this expansion...');
      currentExpansionTimestamp = new Date().getTime().toString();
      messagesDuringEachExpansion[currentExpansionTimestamp] = [];
      messageListenerId = Otis.events.on('message', (event) => {
        messagesDuringEachExpansion[currentExpansionTimestamp].push(event);
      });
    });
    
    Otis.events.on('contract', () => {
      console.log('Finalizing collection for this expansion...')
      Otis.events.off(messageListenerId);
    });
    
    // After a few iterations of expansion/contraction and messages sent during
    // each, the `messagesSentDuringEachExpansion` object should look like this:
    // {
    //   "1589562996085": [MessageEventData, MessageEventData, MessageEventData],
    //   "1589565019455": [MessageEventData],
    //   "1589569346612": [MessageEventData, MessageEventData],
    // }

    Parameters

    • id: number

      Listener ID to remove (ID is the return value from Otis.events.on or Otis.events.once)

    Returns void

on

  • on<E, C>(eventName: E, handler: EventHandler<E, C>, context?: C): number
  • Attaches an event listener to the Otis application.

    Usage

    example
    // Once the Otis application is initialized and ready to accept commands...
    Otis.events.on('init', function(event) {
      console.log('Ready to go!');
      Otis.appointments.promptUser(); // prompt the user to schedule an appointment
    });
    
    // Whenever the chat window is opened...
    Otis.events.on('expand', function(event) {
      console.log('Chat window expanded');
      console.log('Event name:', event.name); // logs 'expand'
      console.log('value of `this`:', this); // logs `Window {...}`
    });
    
    const foo = {
      bar: 123,
      getBar() { console.log(this.bar) },
    };
    
    // Whenever the chat window is closed...
    Otis.events.on('contract', foo.getBar); // logs `undefined`
    Otis.events.on('contract', foo.getBar, foo); // logs `123`
    
    // Whenever a message is sent...
    Otis.events.on('message', (event) => {
      console.log(`${event.from} said "#{event.text}" at #{new Date(event.timestamp)}`);
    });
    
    // Whenever a quote is generated for the user...
    Otis.events.on('quote', (event) => {
      console.log('First name of user:', event.contactInfo.firstName || '(not given)');
      console.log('Names of services selected:', event.services.join(', '));
      console.log('Tire styles selected:', event.tires.join(', '));
      if (event.instant) {
        console.log('Price quoted (in cents):', event.totalCents);
      } else {
        console.log('Unable to quote instantly for the selected services/tires; user will receive quote by email');
      }
    });
    
    // Whenever an appointment is scheduled by the user...
    Otis.events.on('appointment', (event) => {
      console.log('First name of user:', event.contactInfo.firstName || '(not given)');
      console.log('Scheduled for:', new Date(event.scheduledAt));
      console.log('Names of services selected (if any):', event.services.join(', '));
      console.log('Tire styles selected (if any):', event.tires.join(', '));
      console.log('Notes from the user:', event.notes);
    });
    
    // Whenever the user gives contact info...
    Otis.events.on('contact', (event) => {
      console.log("User's first name:", event.firstName || '(not given)');
      console.log("User's last name:", event.lastName || '(not given)');
      console.log("User's email:", event.email || '(not given)');
      console.log("User's phone:", event.phone || '(not given)');
      console.log('User is a returning customer:', event.returningCustomer);
      console.log('User prefers to be contacted via:', event.requestedContactMethod);
      console.log('Message from the user:', event.message || '(not given)');
    });

    Type parameters

    Parameters

    • eventName: E

      Name of the event to listen to; e.g., "message", "expand", "contract", "quote", "appointment", "contact"

    • handler: EventHandler<E, C>

      Callback function to be executed whenever the given event is emitted; receives the event data as an argument

    • Optional context: C

      The value to use as this when the handler function is executed (optional)

    Returns number

    An ID for this listener; you can remove this listener by passing the returned ID to Otis.events.off

once

  • once<E, C>(eventName: E, handler: EventHandler<E, C>, context?: C): number
  • Exactly like Otis.events.on, but only executes the given handler once.

    Usage

    example
    // The first time the chat window is opened...
    Otis.events.once('expand', function() {
      ga('send', 'event', {
        eventCategory: 'MyExampleSite',
        eventAction: 'click',
        eventLabel: 'Otis plugin conversion',
      });
    });

    Type parameters

    Parameters

    • eventName: E

      Name of the event to listen to; e.g., "message", "expand", "contract", "quote", "appointment", "contact"

    • handler: EventHandler<E, C>

      Callback function to be executed once the given event is emitted; receives the event data as an argument

    • Optional context: C

      The value to use as this when the handler function is executed (optional)

    Returns number

    An ID for this listener; you can remove this listener by passing the returned ID to Otis.events.off