-
Notifications
You must be signed in to change notification settings - Fork 85
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feature(new integration): LiveChat web Device mode integration onboar…
…ding (#633) * feature[livechat]: initial Commit * feature[livechat]: add track calls * feature[livechatIntegration]: change event names and add functions * feature[livechatIntegration]: remove email error validation and set session variable * feature[livechatIntegration]: add traits in session variable * feature[livechatIntegration]: flatten traits * feature[livechat]: add userId and resolve review comments * feature[livechat]: add function description Co-authored-by: shrouti1507 <[email protected]>
- Loading branch information
1 parent
4e0f2f8
commit b018b87
Showing
7 changed files
with
208 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,111 @@ | ||
/* eslint-disable no-underscore-dangle */ | ||
/* eslint-disable class-methods-use-this */ | ||
import get from "get-value"; | ||
import logger from "../../utils/logUtil"; | ||
|
||
import { recordingLiveChatEvents } from "./util"; | ||
import { isObject } from "../../utils/utils"; | ||
import { flattenJson } from "../utils/commonUtils"; | ||
import { NAME } from "./constants"; | ||
|
||
class LiveChat { | ||
constructor(config) { | ||
this.name = NAME; | ||
this.licenseId = config.licenseId; | ||
this.recordLiveChatEvents = config.recordLiveChatEvents; | ||
this.eventsToStandard = config.eventsToStandard; | ||
this.updateEventNames = config.updateEventNames; | ||
this.eventsList = config.eventsList; | ||
} | ||
|
||
init() { | ||
logger.debug("===in init Livechat==="); | ||
window.__lc = window.__lc || {}; | ||
window.__lc.license = this.licenseId; | ||
(function (n, t, c) { | ||
function i(n) { | ||
return e._h ? e._h.apply(null, n) : e._q.push(n); | ||
} | ||
var e = { | ||
_q: [], | ||
_h: null, | ||
_v: "2.0", | ||
on: function () { | ||
i(["on", c.call(arguments)]); | ||
}, | ||
once: function () { | ||
i(["once", c.call(arguments)]); | ||
}, | ||
off: function () { | ||
i(["off", c.call(arguments)]); | ||
}, | ||
get: function () { | ||
if (!e._h) | ||
throw new Error( | ||
"[LiveChatWidget] You can't use getters before load." | ||
); | ||
return i(["get", c.call(arguments)]); | ||
}, | ||
call: function () { | ||
i(["call", c.call(arguments)]); | ||
}, | ||
init: function () { | ||
var n = t.createElement("script"); | ||
(n.async = !0), | ||
(n.type = "text/javascript"), | ||
(n.src = "https://cdn.livechatinc.com/tracking.js"), | ||
t.head.appendChild(n); | ||
}, | ||
}; | ||
!n.__lc.asyncInit && e.init(), (n.LiveChatWidget = n.LiveChatWidget || e); | ||
})(window, document, [].slice); | ||
} | ||
|
||
isLoaded() { | ||
logger.debug("===In isLoaded LiveChat==="); | ||
return !!(window.LiveChatWidget && isObject(window.LiveChatWidget)); | ||
} | ||
|
||
isReady() { | ||
logger.debug("===In isReady LiveChat==="); | ||
|
||
// Dasboard Other Settings | ||
if (this.recordLiveChatEvents) { | ||
recordingLiveChatEvents( | ||
this.updateEventNames, | ||
this.eventsList, | ||
this.eventsToStandard | ||
); | ||
} | ||
return !!window.LiveChatWidget; | ||
} | ||
|
||
identify(rudderElement) { | ||
logger.debug("===In LiveChat Identify==="); | ||
const { message } = rudderElement; | ||
const { userId } = message; | ||
const { traits } = rudderElement.message.context; | ||
const email = get(message, "context.traits.email"); | ||
|
||
if (email) { | ||
window.LiveChatWidget.call("set_customer_email", email); | ||
} else { | ||
logger.error( | ||
"User parameter (email) ,required for identify call, not found." | ||
); | ||
} | ||
|
||
const name = get(message, "context.traits.name"); | ||
|
||
if (name) { | ||
window.LiveChatWidget.call("set_customer_name", name); | ||
} | ||
if (traits) { | ||
const flattenTraits = flattenJson(traits); | ||
if (userId) flattenTraits.userId = userId; | ||
window.LiveChatWidget.call("set_session_variables", flattenTraits); | ||
} | ||
} | ||
} | ||
|
||
export default LiveChat; |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
const NAME = "LIVECHAT"; | ||
const CNameMapping = { | ||
[NAME]: NAME, | ||
LIVECHAT: NAME, | ||
LiveChat: NAME, | ||
Live_Chat: NAME, | ||
livechat: NAME, | ||
"LIVE CHAT": NAME, | ||
"Live Chat": NAME, | ||
"live chat": NAME, | ||
}; | ||
|
||
export { NAME, CNameMapping }; |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
import LiveChat from "./browser"; | ||
|
||
export default LiveChat; |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
import { getHashFromArray } from "../utils/commonUtils"; | ||
|
||
const integrationContext = { | ||
name: "LiveChat", | ||
version: "1.0.0", | ||
}; | ||
|
||
/** | ||
* This function is used to trigger a callback. | ||
* @param {*} standardEventsMap - mapping of events done by the user | ||
* @param {*} eventName - standard event name | ||
* @param {*} updateEventNames - boolean variable to change eventName. | ||
*/ | ||
const makeACall = (standardEventsMap, eventName, updateEventNames) => { | ||
// Updating the event name with any mapping from the webapp if available else | ||
// storing default event name in the updatedEvent | ||
const updatedEvent = | ||
standardEventsMap[eventName] && updateEventNames | ||
? standardEventsMap[eventName] | ||
: eventName; | ||
|
||
window.rudderanalytics.track( | ||
`${updatedEvent}`, | ||
{}, | ||
{ context: { integration: integrationContext } } | ||
); | ||
}; | ||
|
||
const swapKeyValuePairs = (standardEventsMap) => { | ||
const swappedEventsMap = {}; | ||
Object.keys(standardEventsMap).forEach((key) => { | ||
swappedEventsMap[standardEventsMap[key]] = key; | ||
}); | ||
return swappedEventsMap; | ||
}; | ||
|
||
/** | ||
* This function has event listners for the occuring events and to make a call for the event after | ||
* collecting the data. | ||
* @param {*} updateEventNames - variable to Update event name . | ||
* @param {*} userDefinedEventsList - List of requested events by the user. | ||
* @param {*} userDefinedEventsMapping - Mapping of events in the webapp by the user | ||
*/ | ||
function recordingLiveChatEvents( | ||
updateEventNames, | ||
userDefinedEventsList, | ||
userDefinedEventsMapping | ||
) { | ||
let standardEventsMap = getHashFromArray(userDefinedEventsMapping); | ||
standardEventsMap = swapKeyValuePairs(standardEventsMap); | ||
(function (api) { | ||
[ | ||
"ready", | ||
"new_event", | ||
"form_submitted", | ||
"greeting_hidden", | ||
"rating_submitted", | ||
"visibility_changed", | ||
"greeting_displayed", | ||
"availability_changed", | ||
"customer_status_changed", | ||
"rich_message_button_clicked", | ||
].forEach(function (eventName) { | ||
if (userDefinedEventsList.includes(eventName)) { | ||
api.on(eventName, function (payload) { | ||
makeACall(standardEventsMap, eventName, updateEventNames); | ||
}); | ||
} | ||
}); | ||
})(window.LiveChatWidget); | ||
} | ||
|
||
export { integrationContext, recordingLiveChatEvents }; |
This file contains 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
This file contains 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
This file contains 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