Last active
April 18, 2024 14:41
-
-
Save davidfpease/0f9f609879c08e7bd21901251c5800b2 to your computer and use it in GitHub Desktop.
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
| <script> | |
| window.addEventListener('load', async (event) => { | |
| console.log("window loaded"); | |
| loadDrift(); | |
| let continueLooping = true; | |
| while (continueLooping) { | |
| if (embedded_svc) { | |
| embedded_svc.hideHelpButton(); | |
| continueLooping = false; | |
| } | |
| await delay(500); | |
| } | |
| }); | |
| function delay(ms) { | |
| return new Promise(resolve => setTimeout(resolve, ms)); | |
| } | |
| function loadDrift() { | |
| "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'; | |
| drift.load('abcdefgh'); //your embedId here | |
| drift.on('ready', async function (api, payload) { | |
| //immediately set all site visitors to chat not enabled until | |
| //we see the live chat button from salesforce | |
| drift.setUserAttributes({ | |
| chat_enabled: false | |
| }) | |
| drift.on("conversation:buttonClicked", async function (data) { | |
| console.log("user clicked a button with text: " + data.buttonBody); | |
| if (data.buttonBody === 'Live chat') { | |
| await delay(4000); //wait for last message from Drift | |
| drift.unload(); | |
| console.log("Clicking help button"); | |
| document.querySelector('div.embeddedServiceHelpButton button').click(); | |
| await delay(2000); | |
| let keepChecking = true; | |
| while (keepChecking) { | |
| let firstName = document.querySelector('#FirstName')?.value; | |
| let lastName = document.querySelector('#LastName')?.value; | |
| let email = document.querySelector('#Email')?.value; | |
| await delay(800); | |
| if (firstName && lastName && email) { | |
| console.log("StartChat payload: ", { | |
| "Email": email, | |
| "Subject": `Chat started from Drift: https://app.drift.com/conversations/${data.conversationId}`, | |
| "FirstName": firstName, | |
| "LastName": lastName | |
| }) | |
| embedded_svc.liveAgentAPI.startChat({ | |
| "prepopulatedPrechatFields": { | |
| "Email": email, | |
| "Subject": `Chat started from Drift: https://app.drift.com/conversations/${data.conversationId}`, | |
| "FirstName": firstName, | |
| "LastName": lastName | |
| } | |
| }); | |
| keepChecking = false; | |
| } | |
| } | |
| } | |
| if (data.buttonBody === 'Raise a ticket') { | |
| console.log("Site visitor clicked a button ") | |
| let ticketFormUrl = "https://help.salesloft.com/s/contactsupport"; | |
| window.open(ticketFormUrl, '_blank'); | |
| } | |
| }); | |
| let continueChecking = true; | |
| let seconds = 0; | |
| while (continueChecking) { | |
| let salesforceChatButton = document.querySelector('div.embeddedServiceHelpButton button'); | |
| if (salesforceChatButton) { | |
| embedded_svc.hideHelpButton(); | |
| console.log(`After ${seconds} seconds, salesforcechatbutton.innerText: `, salesforceChatButton.innerText); | |
| } | |
| if (salesforceChatButton && salesforceChatButton.innerText === '\n\nLive chat:Chat with Support') { | |
| console.log("Set site visitor chat_enabled to TRUE"); | |
| drift.setUserAttributes({ | |
| chat_enabled: true | |
| }) | |
| continueChecking = false; | |
| } | |
| await delay(1000); | |
| seconds++; | |
| } | |
| }) | |
| } | |
| </script> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment