Last active
June 27, 2025 20:55
-
-
Save davidfpease/cb89b4cc6f1031773bf0288e604fb943 to your computer and use it in GitHub Desktop.
Salesforce Community Page Identify Site Visitor
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> | |
| "use strict"; | |
| // Install Drift normally: Insert your Drift installation code here | |
| ! 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('your_drift_embed_id'); // Replace with your Drift widget ID | |
| // End of Drift installtion code | |
| // Now that Drift is installed normally, attempt to make the identity request to the Visualforce Apex script | |
| const vfpName = "DriftCommunityUserBotInfo" | |
| // Replace with the name of your VisualForce Page script | |
| let requestUrl = `/apex/${vfpName}` | |
| async function setDriftUser() { | |
| try { | |
| const response = await fetch(requestUrl); | |
| if (!response.ok) { | |
| console.log("Error retrieving user details from Salesforce: "); | |
| throw new Error(`HTTP error! status: ${response.status}`); | |
| } | |
| const user = await response.json(); | |
| console.log("User details fetched from Salesforce: ", user); | |
| drift.on('ready', function () { | |
| //add below | |
| window.drift.on("conversation:firstInteraction", function(data) { | |
| convo_id = data.conversationId; | |
| drift.api.setUserAttributes({ | |
| drift_conversation_id: data.conversationId //add this line | |
| }) | |
| }); | |
| //end | |
| console.log("User data sent to Drift"); | |
| const queryString = new URLSearchParams(window.location.search).get('iterationName') || 'NoIteration'; | |
| console.log("Iteration from URL: ", queryString); | |
| drift.api.setUserAttributes({ | |
| email: user.email, | |
| first_name: user.firstName, | |
| iteration_name: queryString, | |
| }); | |
| }); | |
| } catch (error) { | |
| console.error('Error fetching user data:', error); | |
| } | |
| } | |
| setDriftUser(); | |
| </script> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment