diff --git a/integrations/LaunchDarkly/browser.js b/integrations/LaunchDarkly/browser.js new file mode 100644 index 0000000000..e03336189e --- /dev/null +++ b/integrations/LaunchDarkly/browser.js @@ -0,0 +1,69 @@ +/* eslint-disable class-methods-use-this */ +import logger from "../../utils/logUtil"; +import ScriptLoader from "../ScriptLoader"; +import createUser from "./utils"; + +class LaunchDarkly { + constructor(config) { + this.name = "LaunchDarkly"; + this.clientSideId = config.clientSideId; + } + + init() { + logger.debug("===in init LaunchDarkly==="); + if (!this.clientSideId) { + logger.error( + `${this.name} :: Unable to initialize destination - clientSideId is missing in config` + ); + return; + } + ScriptLoader(null, "https://unpkg.com/launchdarkly-js-client-sdk@2"); + } + + isLoaded() { + logger.debug("===In isLoaded LaunchDarkly==="); + return !!window.LDClient; + } + + isReady() { + logger.debug("===In isReady LaunchDarkly==="); + return this.isLoaded(); + } + + identify(rudderElement) { + const { message } = rudderElement; + window.user = createUser(message); + + if (window.ldclient) { + window.ldclient.identify(window.user); + } else { + window.ldclient = window.LDClient.initialize( + this.clientSideId, + window.user + ); + } + } + + track(rudderElement) { + const { event, properties } = rudderElement.message; + if (window.ldclient) { + window.ldclient.track(event, properties); + } else + logger.error( + "=== In LaunchDarkly, track is not supported before identify ===" + ); + } + + alias(rudderElement) { + const { message } = rudderElement; + const newUser = { key: message.userId }; + + if (window.ldclient) { + window.ldclient.alias(newUser, window.user); + } else + logger.error( + "=== In LaunchDarkly, alias is not supported before identify ===" + ); + } +} +export default LaunchDarkly; diff --git a/integrations/LaunchDarkly/index.js b/integrations/LaunchDarkly/index.js new file mode 100644 index 0000000000..06432ca565 --- /dev/null +++ b/integrations/LaunchDarkly/index.js @@ -0,0 +1,3 @@ +import LaunchDarkly from "./browser"; + +export default LaunchDarkly; diff --git a/integrations/LaunchDarkly/utils.js b/integrations/LaunchDarkly/utils.js new file mode 100644 index 0000000000..d6bf10c532 --- /dev/null +++ b/integrations/LaunchDarkly/utils.js @@ -0,0 +1,21 @@ +const createUser = (message) => { + const user = {}; + user.key = message.userId || message.anonymousId; + const { traits } = message.context; + if (traits.anonymous !== undefined) { + user.anonymous = traits.anonymous; + } + if (traits.avatar !== undefined) user.avatar = traits.avatar; + if (traits.country !== undefined) user.country = traits.country; + if (traits.custom !== undefined) user.custom = traits.custom; + if (traits.email !== undefined) user.email = traits.email; + if (traits.firstName !== undefined) user.firstName = traits.firstName; + if (traits.ip !== undefined) user.ip = traits.ip; + if (traits.lastName !== undefined) user.lastName = traits.lastName; + if (traits.name !== undefined) user.name = traits.name; + if (traits.privateAttributeNames !== undefined) + user.privateAttributeNames = traits.privateAttributeNames; + if (traits.secondary !== undefined) user.secondary = traits.secondary; + return user; +}; +export default createUser; diff --git a/integrations/client_server_name.js b/integrations/client_server_name.js index e77d441cfd..12013cdc80 100644 --- a/integrations/client_server_name.js +++ b/integrations/client_server_name.js @@ -43,6 +43,7 @@ const clientToServerNames = { SENTRY: "Sentry", GOOGLE_OPTIMIZE: "GoogleOptimize", POST_AFFILIATE_PRO: "PostAffiliatePro", + LAUNCHDARKLY:"LaunchDarkly" }; export { clientToServerNames }; diff --git a/integrations/index.js b/integrations/index.js index 9838d59122..2dbc9ba7ef 100644 --- a/integrations/index.js +++ b/integrations/index.js @@ -42,6 +42,7 @@ import * as TVSquared from "./TVSquared"; import * as VWO from "./VWO"; import * as GoogleOptimize from "./GoogleOptimize"; import * as PostAffiliatePro from "./PostAffiliatePro"; +import * as LaunchDarkly from "./LaunchDarkly"; // the key names should match the destination.name value to keep partity everywhere // (config-plan name, native destination.name , exported integration name(this one below)) @@ -91,6 +92,7 @@ const integrations = { VWO: VWO.default, GOOGLE_OPTIMIZE: GoogleOptimize.default, POST_AFFILIATE_PRO: PostAffiliatePro.default, + LAUNCHDARKLY: LaunchDarkly.default }; export { integrations }; diff --git a/integrations/integration_cname.js b/integrations/integration_cname.js index 68547607a9..2c24b3c1a2 100644 --- a/integrations/integration_cname.js +++ b/integrations/integration_cname.js @@ -135,6 +135,11 @@ const commonNames = { postaffiliatepro: "POST_AFFILIATE_PRO", POSTAFFILIATEPRO: "POST_AFFILIATE_PRO", POST_AFFILIATE_PRO: "POST_AFFILIATE_PRO", + LaunchDarkly:"LAUNCHDARKLY", + Launch_Darkly:"LAUNCHDARKLY", + LAUNCHDARKLY:"LAUNCHDARKLY", + "Launch Darkly":"LAUNCHDARKLY", + launchDarkly:"LAUNCHDARKLY" }; export { commonNames };