Last active
January 8, 2026 18:44
-
-
Save davidfpease/017490cfa3acead68908577eb2ebbc3f to your computer and use it in GitHub Desktop.
Change Drift Behavior for A/B Testing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| //variant A: Drift will not pop open the full chat window, hook message is automatically dismissed | |
| "use strict"; | |
| !function () { | |
| 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"; | |
| //add config to ensure Drift is non-blocking | |
| window.drift_init_options = { loadType: "ON_INTERACTIVE" } | |
| //set site visitor attribute for playbook targetting | |
| drift.identify("A", {"optimizely_test_version":"A"}) | |
| drift.load("add-your-embed-id-here"); | |
| drift.on('ready', function (api, payload) { | |
| console.log("drift ready: " + (new Date().toLocaleString())); | |
| drift.on('conversation:firstInteraction', function (e) { | |
| console.log("drift first interaction: " + (new Date().toLocaleString())); | |
| //if a site visitor begins a conversation, record which variant as a contact attribute | |
| drift.setUserAttributes({ | |
| 'Optimizely Test': "Variant A" //replace with actual variant value | |
| }); | |
| }); | |
| //log the id of the playbook fired for troubleshooting | |
| drift.on('conversation:playbookFired', function (e) { | |
| console.log(`drift playbook ${e.playbookId} fired: ` + (new Date().toLocaleString())); | |
| }); | |
| //listen for the previewOpen message | |
| //this allows us to control the timing of when hidePreview is called | |
| window.addEventListener("message", function (event) { | |
| if (JSON.stringify(event.data).includes("render-conversation-preview")) { | |
| console.log("drift preview opened: " + (new Date().toLocaleString())); | |
| drift.hidePreview(); | |
| } | |
| }, false); | |
| }); | |
| //end variant A | |
| //variant B, Drift opens in full chat | |
| "use strict"; | |
| !function () { | |
| 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"; | |
| //add config to ensure Drift is non-blocking | |
| window.drift_init_options = { loadType: "ON_INTERACTIVE" } | |
| //set site visitor attribute for playbook targetting | |
| drift.identify("B", {"optimizely_test_version":"B"}) | |
| drift.load("add-your-embed-id-here"); | |
| drift.on('ready', function (api, payload) { | |
| console.log("drift ready: " + (new Date().toLocaleString())); | |
| //always hide the playbook hook message when it opens | |
| window.addEventListener("message", function (event) { | |
| if (JSON.stringify(event.data).includes("render-conversation-preview")) { | |
| console.log("drift preview opened: " + (new Date().toLocaleString())); | |
| drift.hidePreview(); | |
| } | |
| }, false); | |
| drift.on('conversation:firstInteraction', function (e) { | |
| console.log("drift first interaction: " + (new Date().toLocaleString())); | |
| //if a site visitor begins a conversation, record which variant as a contact attribute | |
| drift.setUserAttributes({ | |
| 'Optimizely Test': "Variant B" //replace with actual variant value | |
| }); | |
| }); | |
| //log the id of the playbook fired for troubleshooting | |
| drift.on('conversation:playbookFired', function (e) { | |
| console.log(`drift playbook ${e.playbookId} fired: ` + (new Date().toLocaleString())); | |
| //check if playbook was dismissed in this session before opening chat window | |
| if (sessionStorage.getItem("playbookDismissed") === null) { | |
| drift.openChat(); | |
| } | |
| }); | |
| drift.on('chatClose', function (e) { | |
| console.log(`drift playbook ${e.playbookId} closed: ` + (new Date().toLocaleString())); | |
| //set a session storage item to indicate playbook was dismissed | |
| sessionStorage.setItem("playbookDismissed", "true"); | |
| }); | |
| }); | |
| //end variant B |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment