Receiving Information from Drift

If you'd like to send Drift events to an outside platform or customize Drift using specific events, you can use our Widget API.

Here is what we'll be covering:

First, initialize your Drift Widget

The ready event indicates that Drift widget is ready for use. It contains a single argument which is the actual API object that can then be acted upon.

<script>
drift.on('ready',function(api, payload) {
 // your code goes here
})
</script>
<script>
analytics.ready(function(){
    //interact with the api here
})
</script>

Here's a code sample, showing how to track an event using our Widget API

Let's say, you want to track that a user started a conversation & the corresponding conversation ID.
You'd want to use the startConversation event, which returns an object like this:

{
  conversationId, // the ID for the conversation
  inboxId,        // the ID for the inbox for this conversation
}

If you wanted to send this event to a platform like Google Analytics, you would need to add the Google Analytics tracking call. It would look something like the following:

drift.on('ready', function (api) {
  drift.on('startConversation', function (event) {
    // Enter Google Analytics function here
    ga('send', 'event', {
      eventCategory: 'Drift Conversations',
      eventAction: 'Started Conversation',
      eventLabel: event.conversationId,
    });
  })
})

OR integrate with GA with one click here.

Sidebar Events

sidebarOpen is the event that fires when the sidebar opens.
sidebarClose is the event that fires when the sidebar closes.

The event argument contains the same UI flags as the message event.

Event attributes:

event.data.sidebarOpen is true if the sidebar is in the open state. false if it's closed.
event.data.widgetVisible is true if the widget is visible. false if it's hidden.
event.data.isOnline is true if your organization is currently online.

The following shows an example: when the sidebarClose event is triggered, if the widget is visible, we call the hide() method.

<script> 
drift.on('ready',function(api){
  drift.on('sidebarClose',function(e){
    if(e.data.widgetVisible){
      api.widget.hide()
    }
  })
})
</script>
Conversation Sidebar

Conversation Sidebar

Welcome Message Events

welcomeMessage:open is the event that fires when the welcome message is open.
welcomeMessage:close fires when the welcome message is closed.

<script>
drift.on('welcomeMessage:open', function() {
  console.log('Welcome Message is open 🌞')
})
drift.on('welcomeMessage:close', function() {
  console.log('Welcome Message is closed 🌞')
})
</script>
Welcome Message

Welcome Message

Away Message Events

awayMessage:open is the event fired when an away message opens.
awayMessage:close is the event fired when an away message closes.

<script> 
drift.on('awayMessage:open', function() {
  console.log('Away Message is open 🌝')
})
drift.on('awayMessage:close', function() {
  console.log('Away Message is closed 🌝')
})
</script>

Campaign Events

We fire events for the campaign interactions that you see in your Playbook Reports. Your Playbooks are your targeted messages. They differ from your welcome message, which is the catch-all message that you've customized in your settings.

A note on Campaign event listeners

Campaign event listeners fire for single time campaigns only (non bot-based playbooks) such as the slider or email capture messages.

If you are looking to listen for leadbot playbooks starting, you should use the conversation:playbookFired event described on this page.

campaign:open fires when campaign begins.
campaign:dismiss fires when a user closes a campaign message.
campaign:click fires when a call to action (CTA) is clicked.
campaign:submit fires when a user starts a chat or submits an email capture.

window.drift.on("campaign:dismiss", function(data) {
  console.log("User dismissed campaign", JSON.stringify(data));
});

Example Payload

User dismissed campaign 
{"data": 
  {
    "sidebarOpen":false,
    "widgetVisible":true,
    "isOnline":true
  },
 "campaignId":111593
}

Slider Message Events

Slider Message Close- sliderMessage:close

Slider Message Close

the sliderMessage:close event fires when the slider message is closed

window.drift.on("sliderMessage:close", function(data) {
  console.log("User closed slider the slider");
});

Your event handler will receive an object with this shape:

{
  botMessage,                     // if the message closed was a bot message
  userInteractedWithConversation, // if the user has interacted with the conversation the slider message is coming from
}

Conversation Events

Conversation Started- startConversation
Message Sent - message:sent
Message Received - message
Email Captured- emailCapture
Phone Captured- phoneCapture
Meeting Request Sent- scheduling:requestMeeting
Meeting Booked- scheduling:meetingBooked
Playbook Fired- conversation:playbookFired
Button Clicked- conversation:buttonClicked
First Interaction- conversation:firstInteraction

Conversation Started

the startConversation event fires when the user starts a new chat.

startConversation is only intended for non-playbook conversations

This event tracks when a site visitor starts a conversation, not the bot.

We recommend and provide the First Interaction client event for tracking first messages in all conversations (included the ones initiated by a bot playbook).

window.drift.on("startConversation", function(data) {
  console.log("User started a new conversation " + data.conversationId);
});

Your event handler will receive an object with this shape:

{
  conversationId, // the ID for the conversation
  inboxId		      // the ID for this conversation's inbox
}

User Sent a Message

The message:sent event fires when the user replies to a conversation

window.drift.on("message:sent", function(data) {
  console.log("User replied to conversation " + data.conversationId);
});

Your event handler will receive an object with this shape:

{
  conversationId, // the ID for the conversation
  inboxId,        // the ID for this conversation's inbox
}

User Received a Message

the message event fires when the user receives a message from a team member.

window.drift.on("message", function(data) {
  console.log("User received a message from ' + data.teamMember.name + ' in conversation " + data.conversationId);
});

Your event handler will receive an object with this shape:

{
  conversationId, // the ID for the conversation
  inboxId,        // the ID for this conversation's inbox
  teamMember: {   // the team member that messaged the user
    id,           // the team member's Drift ID
    name,         // the team member's name
  }
}

Email Captured

The emailCapture event fires when the user identifies through an Email Capture Campaign, completes an email capture form inside the Widget, or if they simply type their email into a message.

window.drift.on("emailCapture", function(e) {
  console.log("user identified as: " + e);
});


/*
ex:
user identified as:
{"data":{"email":"q@q.com","conversationId":583998469,"interactionId":XXX, "campaignId": XXX, "playbookId": XXX}}
*/

Note that if you used startInteraction to start the bot, only the interactionId will be provided - which an id value specific for DriftLinks. Otherwise for normal playbook targeting, we'll include as many of the present id values as possible - interactionId, playbookId, and campaignId.

In general,

{
  data: {
    email: (string),
    conversationId: (int),
    playbookId: (optional int),
    interactionId: (optional int),
    campaignId: (optional int)
  }
}

Phone Captured

The phoneCapture event fires whenever a user provides a phone number in chat. Works for US numbers only currently. You'll receive a payload like the following:

window.drift.on("phoneCapture", function(data) {
     console.log("User provided a phone number: " + JSON.stringify(data))
 })

/*
ex:

User types: "Hi, my number is 555-444-5555" anywhere in chat.

User provided a phone number: {"conversationId":3215047,"messageId":3003184162,"createdAt":1552946646658,
"authorId":3000192098,"phone":"555-444-5555"}
*/

HowTo: Setting a phone number from anywhere

Note the phone capture event won't automatically set the property on the end user, but with a simple extension this is possible:

Using the phone capture event in combination with setUserAttributes, you'll be able set the number from anywhere in a conversation thread on the end user object. Once the user provides an email the phone number should be present on the contact record!

window.drift.on("phoneCapture", function(data) {
     drift.api.setUserAttributes({phone: data.phone})
})

Meeting Request Sent

The scheduling:requestMeeting event fires when the Schedule Meeting card is pushed to a conversation. This can happen when you call the api.scheduleMeeting method, when the user tries to book a meeting through a Drift Profile, or when a member of your team shares a calendar to a conversation.

window.drift.on("scheduling:requestMeeting", function(data) {
  console.log("user wants to schedule a meeting with " + data.teamMember.name);
});

Your function will receive an object with the following shape:

{
  teamMember: {  // the team member the user wants to schedule a meeting with
    id,          // your team member's Drift ID
    name,        // your team member's name
  }
}

Meeting Booked

the scheduling:meetingBooked event fires when the user books a meeting with a member of your team.

window.drift.on("scheduling:meetingBooked", function(data) {
  console.log("user booked a meeting with " + data.teamMember.name);
});

Your function will receive an object with the following shape:

{
  teamMember: {  // the team member the user booked a meeting with
    id,          // your team member's Drift ID
    name,        // your team member's name
  },
  meeting: {
    time,        // the meeting's start time as a <a href="http://www.unixtimestamp.com/" target="_blank">UNIX timestamp</a>
    duration,    // the meeting duration in minutes
    timeZone,    // the timezone for the meeting's start time
  },
  conversationId: (int),
  playbookId: (optional int),
  interactionId: (optional int),
  campaignId: (optional int)
}

Playbook Fired

The conversation:playbookFired event will fire whenever a leadbot playbook fires for the first time in a new conversation session with a site visitor.

window.drift.on("conversation:playbookFired", function(data) {
    console.log("Playbook fired: " + JSON.stringify(data))
})


/*
ex: 
Playbook fired: {"conversationId":583998469,"messageId":1430463200,"createdAt":1553021092581,
"authorId":2487803352,"interactionId":XXX, "playbookId": XXX, "campaignId": XXX}
*/

Note that if you used startInteraction to start the bot, only the interactionId will be provided - which an id value specific for DriftLinks. Otherwise for normal playbook targeting, we'll include as many of the present id values as possible - interactionId, playbookId, and campaignId.

In general the payload will look like this for each new "playbookFired" event:

{    
  messageId: (long), // id of initial playbook message
  createdAt: (long), // time of initial playbook message
  authorId: (long), // typically the anonymous end user id.
  conversationId: (int),
  playbookId: (optional int),
  interactionId: (optional int),
  campaignId: (optional int)
}

A note on testing the "Playbook Fired" event

Subsequent launches or opens of the same playbook without a site visitor message will not retrigger the conversation:playbookFired event.

Button Clicked

the conversation:buttonClicked event fires when the user clicks on a button as a response to a question. Will capture events like those shown in the following gif: https://cl.ly/cf1f04581909.

As an example, you could use this for custom event tracking in an external system or triggering a particular event on your website in response to a button press with certain text during a conversation.

window.drift.on("conversation:buttonClicked", function(data) {
  console.log("user clicked a button with text: " + data.buttonBody);
});

Your function callback will receive an data object with the following shape:

{
  conversationId, // conversation id this button was clicked in
  messageId,      // the message id this button click created
  createdAt,      // the time this button was clicked (timestamp)
  authorId,       // the authorId (end user or contact id) of the user clicking the button
  questionId,     // id of the question in the playbook (internal value)
  buttonBody      // the text the button displayed
}

First Interaction

the conversation:firstInteraction event fires for the first site visitor message in each unique conversation thread, or each distinct conversation id, associated with a bot playbook.

window.drift.on("conversation:firstInteraction", function(data) {
    console.log("First interaction: " + JSON.stringify(data)
})
    
/*
ex: 
First interaction: {"messageId":1441152633,"conversationId":587543841,
"createdAt":1553118597159,"authorId":2488443669,
"playbookId":XXX,"interactionId":XXX,"campaignId":XXX}
*/

Note that if you used startInteraction to start the bot, only the interactionId will be provided - which an id value specific for DriftLinks. Otherwise for normal playbook targeting, we'll include as many of the present id values as possible - interactionId, playbookId, and campaignId.

In general the payload will look like this for each new "firstInteraction" event:

{    
  messageId: (long), // id of initial playbook message
  createdAt: (long), // time of initial playbook message
  authorId: (long), // typically the anonymous end user id.
  conversationId: (int),
  playbookId: (optional int),
  interactionId: (optional int),
  campaignId: (optional int)
}