Load Drift after opt-in through HubSpot Cookie Consent Banner

Customers using HubSpot may want to defer loading Drift’s async JavaScript until their native HubSpot cookie-compliance banner receives an opt-in event for the visitor to be tracked. Since loading Drift's JavaScript will always cause tracking cookies to be placed and create a durable server-side record of the visitor’s IP address according to Drift's Contact Model, it is recommended to load Drift as a function on the acceptance event from HubSpot.

<!--Start of Async Drift Code loadDriftWidget() function added-->
<script>
"use strict";
function loadDriftWidget() {
  var t = window.driftt = window.drift = window.driftt || [];
  if (!t.init) {
    if (t.invoked) return void (window.console && console.error && console.error("Drift snippet included twice."));
    t.invoked = !0, t.methods = [ "identify", "config", "track", "reset", "debug", "show", "ping", "page", "hide", "off", "on" ],
    t.factory = function(e) {
      return function() {
        var n = Array.prototype.slice.call(arguments);
        return n.unshift(e), t.push(n), t;
      };
    }, t.methods.forEach(function(e) {
      t[e] = t.factory(e);
    }), t.load = function(t) {
      var e = 3e5, n = Math.ceil(new Date() / e) * e, o = document.createElement("script");
      o.type = "text/javascript", o.async = !0, o.crossorigin = "anonymous", o.src = "https://js.driftt.com/include/" + n + "/" + t + ".js";
      var i = document.getElementsByTagName("script")[0];
      i.parentNode.insertBefore(o, i);
    };
  }
  drift.SNIPPET_VERSION = '0.3.1';
  drift.load('yvkgi5rsby2k');
};
</script>
<!--End of Async Drift Code-->
<!--Start of Async Drift Code loadDriftWidget() function added-->
<script>
"use strict";
function loadDriftWidget() {
  var t = window.driftt = window.drift = window.driftt || [];
  if (!t.init) {
    if (t.invoked) return void (window.console && console.error && console.error("Drift snippet included twice."));
    t.invoked = !0, t.methods = [ "identify", "config", "track", "reset", "debug", "show", "ping", "page", "hide", "off", "on" ],
    t.factory = function(e) {
      return function() {
        var n = Array.prototype.slice.call(arguments);
        return n.unshift(e), t.push(n), t;
      };
    }, t.methods.forEach(function(e) {
      t[e] = t.factory(e);
    }), t.load = function(t) {
      var e = 3e5, n = Math.ceil(new Date() / e) * e, o = document.createElement("script");
      o.type = "text/javascript", o.async = !0, o.crossorigin = "anonymous", o.src = "https://js.driftt.com/include/" + n + "/" + t + ".js";
      var i = document.getElementsByTagName("script")[0];
      i.parentNode.insertBefore(o, i);
    };
  }
  drift.SNIPPET_VERSION = '0.3.1';
  drift.load('yvkgi5rsby2k');
};
</script>
<!--End of Async Drift Code-->
<!--Start of Async Drift Code loadDriftWidget() function added-->
<script>
"use strict";
function loadDriftWidget() {
  var t = window.driftt = window.drift = window.driftt || [];
  if (!t.init) {
    if (t.invoked) return void (window.console && console.error && console.error("Drift snippet included twice."));
    t.invoked = !0, t.methods = [ "identify", "config", "track", "reset", "debug", "show", "ping", "page", "hide", "off", "on" ],
    t.factory = function(e) {
      return function() {
        var n = Array.prototype.slice.call(arguments);
        return n.unshift(e), t.push(n), t;
      };
    }, t.methods.forEach(function(e) {
      t[e] = t.factory(e);
    }), t.load = function(t) {
      var e = 3e5, n = Math.ceil(new Date() / e) * e, o = document.createElement("script");
      o.type = "text/javascript", o.async = !0, o.crossorigin = "anonymous", o.src = "https://js.driftt.com/include/" + n + "/" + t + ".js";
      var i = document.getElementsByTagName("script")[0];
      i.parentNode.insertBefore(o, i);
    };
  }
  drift.SNIPPET_VERSION = '0.3.1';
  drift.load('****Put_Your_embedId_HERE****'); //add your Drift embedId from your default widget install found within our product
};
</script>
<!--End of Async Drift Code-->

<!--Event listener to load Drift async script on consent opt-in-->
<script>
var _hsp = (window._hsp = window._hsp || []);
_hsp.push(["addPrivacyConsentListener", function (consent) {
    if (consent.allowed) {
        console.log('consent received, loading Drift'),
        loadDriftWidget()
    }
}])
</script>
<!-- End of Drift Script Loader-->

What's Next?

Once Drift has loaded in a compliant manner, you can launch a chat using our native playbook targeting, or decide which experience visitors see by programmatically selecting a playbook by its interactionId.

Did this page help you?