Installation and User Identification

The Javascript SDK is an easy way to install Drift, while also identifying users and tracking events if you’re installing it in your app.

Not yet using Drift?

Sign up here!

Installing

If you’re still creating your account, copy and paste the code snippet inside your webpage’s, or web app’s, <head> block. If you already have account, get the Drift code at the bottom left of this page.

To make life easy for you, this JS snippet will never change… after installing this you’ll never have to touch it again no matter how many new features we release.

Platform
Example Approach

Using Wordpress, Shopify, Segment, or Google Tag Manager?

You can easily install Drift with any of these platforms. Connect Drift to your service in app or see this helpdoc for Google Tag Manager.

Many other Content Management Platforms can also be supported by adding a Javascript content element and pasting the Drift snippet in. We recommend inserting the snippet into the <head> block of your page if possible. If the Drift widget appears and is functional, you're good to go.

Using the Python Django framework?

We have a Python package for Django that can assist with installation:
https://github.com/rhymiz/django-drift-chatwidget.

Using ReactJS?

We have a community provided react-component that can be used: https://github.com/chardmd/react-drift.

This can provide a convenient wrapper around the drift javascript code, which is typically stored in a static <script> block in normal html.

Using Server Side Rendering with React?

Server side rendering can often break client side libraries if not handled carefully, here's an approach for handling this with the Drift widget. See:
https://github.com/erikras/react-redux-universal-hot-example/issues/863

You're all set!

Want Drift in your application or product? Let's identify your users!

Drift uses cookies to identify site visitors who return to your website so we can maintain their identify. This means that if the end user clears their cookies or goes to a different device, we'll lose the connection to the previous history.

But if people are already logged into your application, then you know exactly who they are!

When you know the identity of who is on the website, you can explicitly tell Drift which user this is. This way, no matter which device or browser they're using Drift will treat them the same.

You can do this with a single line JavaScript. All you need to know is the unique user ID that your platform uses toidentify whoever is on your website.

A note on the user id in drift.identify

This ID might be numerical, or an email address, or a random string of characters. Just be sure to use distinct id values for distinct users. A call to drift.identify with a new id value not recognized by drift will register this contact.

We strongly recommend that you not use an incremental or otherwise easily guessable value as an ID, as the widget will use this value to access the associated site visitor's data. In the event that you have to use guessable identifiers, Drift also supports identification via a signed identity hash; reach out to support for implementation guidance!

Here's a code example

Know more information about this person?

You can pass more information as attributes in as the second argument to the drift.identify function, and we can use this information in the Drift platform.

In this example, the userId is USER-142

<!-- html webpage -->
<script>
drift.identify('USER-142', {
  email: 'user142@test.com',
  numberOfLogins: 5,
  phone_number: '222-3333-4444'
});
</script>

Typically the identify method can be used in tandem with a login flow - when you become aware of the current visitor's identity or email, or to merge users identified in Drift with the same identification system that your app uses (by using your unique user id value).

Don't want to create a new user, but want to set properties?

You can set attributes on users that haven't even been created in drift yet by using drift.api.setUserAttributes with an object with key value pairs of the attributes. This is described in more detail in the later section: "Sending Information into Drift"

userId
string/number

The unique user ID in your company’s user table for the user.

attributes
Object
Optional

A dictionary of attributes you know about this user, such as their email, name, company.
Note: the attributes object must be a flat object.
See this doc about different data types you can use

Removing Identification when your users log out

When your users are done using your services and log-out of your application, it's important to clear Drift's memory of this user so the next person using the computer doesn't see any previous history.

In order to do this, just use drift.reset() to completely clear Drift's tracking cookies. To Drift, this is a brand new person now.

Installation and User Identification


Suggested Edits are limited on API Reference Pages

You can only suggest edits to Markdown body content, but not to the API spec.