From f7e045cb013c755106d1a207b9994090a23f4164 Mon Sep 17 00:00:00 2001 From: shrouti Date: Thu, 16 Sep 2021 18:47:41 +0530 Subject: [PATCH] review comment addressed for initial commit --- integrations/Sentry/browser.js | 34 +++++++++++---------------------- integrations/Sentry/utils.js | 35 ++++++++++++++++++++++++++++++++++ 2 files changed, 46 insertions(+), 23 deletions(-) create mode 100644 integrations/Sentry/utils.js diff --git a/integrations/Sentry/browser.js b/integrations/Sentry/browser.js index a7f94b3343..6fe4ae5183 100644 --- a/integrations/Sentry/browser.js +++ b/integrations/Sentry/browser.js @@ -1,7 +1,8 @@ /* eslint-disable no-unused-expressions */ import logger from "../../utils/logUtil"; -import ScriptLoader from "../ScriptLoader"; +import {SentryScriptLoader, identifierPayloadBuilder} from "./utils"; +import {getDefinedTraits} from "../../utils/utils"; class Sentry { @@ -25,12 +26,10 @@ class Sentry { logger.debug("DSN is a mandatory field"); return; } - ScriptLoader( + SentryScriptLoader( "Sentry", `https://browser.sentry-cdn.com/6.12.0/bundle.min.js` ); - integrity="sha384-S3qfdh3AsT1UN84WIYNuOX9vVOoFg3nB17Jp5/pTFGDBGBt+dtz7MGAV845efkZr" - crossorigin="anonymous" window.Sentry = { dsn: this.dsn, @@ -49,38 +48,27 @@ class Sentry { // eslint-disable-next-line class-methods-use-this isLoaded() { logger.debug("===in Sentry isLoaded==="); - return !!(window.Sentry && window.Sentry.push !== Array.prototype.push); + return !!(window.Sentry && window.Sentry.setUser !== Array.prototype.push); } // eslint-disable-next-line class-methods-use-this isReady() { logger.debug("===in Sentry isReady==="); - return !!(window.criteo_q && window.Sentry.push !== Array.prototype.push); + return !!(window.Sentry && window.Sentry.setUser !== Array.prototype.push); } - Identify(rudderElement) { + identify(rudderElement) { const { traits } = rudderElement.message; - const { userId, email, name } = getDefinedTraits(message); // userId sent as id and username sent as name + const { userId, email, name } = getDefinedTraits(rudderElement.message); // userId sent as id and username sent as name const ip_address = get (message,"traits.ip_address") || get (message,"context.traits.ip_address"); - const userIdentificationProperty = ["userId", "email" , "name", "ip_address"] + if( ! userId && ! email && ! name && ! ip_address ) { // if no user identification property is present the event will be dropped + logger.debug("Any one of userId, email, name and ip_address is mandatory"); return; } - const userIdentifierPayload = {}; - if (userId) { - userIdentifierPayload.id = userId; - } - if(email) { - userIdentifierPayload.email = email; - } - if(name) { - userIdentifierPayload.username = name; - } - if(ip_address) { - userIdentifierPayload.ip_address = ip_address; - } - + const userIdentifierPayload = identifierPayloadBuilder (userId, email, name, ip_address); + const finalPayload = { ... userIdentifierPayload, ...traits diff --git a/integrations/Sentry/utils.js b/integrations/Sentry/utils.js new file mode 100644 index 0000000000..579adcde42 --- /dev/null +++ b/integrations/Sentry/utils.js @@ -0,0 +1,35 @@ +import logger from "../../utils/logUtil"; + +const SentryScriptLoader = (id, src) => { + logger.debug(`in script loader=== ${id}`); + const js = document.createElement("script"); + js.src = src; + js.integrity = "sha384-S3qfdh3AsT1UN84WIYNuOX9vVOoFg3nB17Jp5/pTFGDBGBt+dtz7MGAV845efkZr"; + js.crossorigin = "anonymous" + js.async = true; + js.type = "text/javascript"; + js.id = id; + const e = document.getElementsByTagName("script")[0]; + logger.debug("==parent script==", e); + logger.debug("==adding script==", js); + e.parentNode.insertBefore(js, e); +}; + +const identifierPayloadBuilder = (userId, email, name, ip_address) => { + payload = {}; + if (userId) { + userIdentifierPayload.id = userId; + } + if(email) { + userIdentifierPayload.email = email; + } + if(name) { + userIdentifierPayload.username = name; + } + if(ip_address) { + userIdentifierPayload.ip_address = ip_address; + } + return payload; +}; + +export {SentryScriptLoader, identifierPayloadBuilder};