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.

PlatformExample Approach
Using Wordpress 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 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 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 just a few lines of code. All you need is a unique user ID that your platform uses to identify whoever is on your website.

If you use unguessable IDs, you can set this up with just a single line of JavaScript. Just make sure that signed identities are not required in your widget security settings.

If your IDs are guessable (for instance, you use incremental IDs or email addresses), you will need to use our Signed Identities feature to ensure your identification process is secure.

📘

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.

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 f2c611e9-ee43-42d4-ab56-66d5c0153ee9. Since this is an unguessable UUID, we don't have to worry about signed identities. For code examples with signed identities, please refer to Securing Drift on your site with signed identities

drift.identify('f2c611e9-ee43-42d4-ab56-66d5c0153ee9', {
  email: '[email protected]',
  numberOfLogins: 5,
  phone_number: '222-3333-4444'
});

❗️

Please ensure that identify is called as soon as possible on the page (generally before drift.load(...)) - this will ensure that the site visitor is updated with the information provided when we authenticate them.

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.