Capture URL Query Strings as Attributes

All query strings are created equal, but not all are captured natively by Drift (like UTM parameters). Clients who wish to pull query strings from the URL can use the following custom code to grab their desired query strings, and assign them to a custom attribute.

In the below example, we're going to capture the Google Click ID, or GCLID.

drift.on('ready', function(api) {
  drift.api.setUserAttributes({
    gclid: new URLSearchParams(window.location.search).get('gclid')
  });
});

Some of this code may seem familiar- Drift's setUserAttributes method allows you to set a value to any attribute. If the attribute has not been declared yet, it will be created for you.

In this case, we've taken the setUserAttributes code a step further, and indicated that we want to pull a specific parameter and feed it into Drift. The attribute defined is "gclid", and the value is the "gclid" query string parameter captured in the URL. Feel free to capture as many custom query string parameters as you need!

Add this custom JavaScript to your Drift code wherever you'd like to capture the query strings, and assign to an attribute. As a reminder, you'll need to wait for the ready event before the API is ready to be called.