From 507fe585090bc53caeead61b4b27b915e2145ead Mon Sep 17 00:00:00 2001 From: ItsSudip Date: Thu, 20 Jul 2023 17:53:35 +0530 Subject: [PATCH 1/5] feat(Amplitude): update amplitude SDK --- src/integrations/Amplitude/browser.js | 69 ++++----- src/integrations/Amplitude/loader.js | 212 +++++++++++++------------- 2 files changed, 137 insertions(+), 144 deletions(-) diff --git a/src/integrations/Amplitude/browser.js b/src/integrations/Amplitude/browser.js index 88d388d0ef..317c88b496 100644 --- a/src/integrations/Amplitude/browser.js +++ b/src/integrations/Amplitude/browser.js @@ -21,27 +21,22 @@ class Amplitude { this.trackAllPages = config.trackAllPages || false; this.trackNamedPages = config.trackNamedPages || false; this.trackCategorizedPages = config.trackCategorizedPages || false; - this.trackUtmProperties = config.trackUtmProperties || false; - this.trackReferrer = config.trackReferrer || false; - this.batchEvents = config.batchEvents || false; - this.eventUploadThreshold = +config.eventUploadThreshold || 30; - this.eventUploadPeriodMillis = +config.eventUploadPeriodMillis || 30000; - this.forceHttps = config.forceHttps || false; - this.trackGclid = config.trackGclid || false; - this.saveParamsReferrerOncePerSession = config.saveParamsReferrerOncePerSession || false; - this.deviceIdFromUrlParam = config.deviceIdFromUrlParam || false; - // this.mapQueryParams = config.mapQueryParams; + this.attribution = config.attribution || false; + this.flushQueueSize = config.flushQueueSize || 30; + this.flushIntervalMillis = +config.flushIntervalMillis || 30000; + this.trackNewCampaigns = config.trackNewCampaigns || false; this.trackRevenuePerProduct = config.trackRevenuePerProduct || false; this.preferAnonymousIdForDeviceId = config.preferAnonymousIdForDeviceId || false; this.traitsToSetOnce = []; this.traitsToIncrement = []; - this.appendFieldsToEventProps = config.appendFieldsToEventProps || false; - this.unsetParamsReferrerOnNewSession = config.unsetParamsReferrerOnNewSession || false; this.trackProductsOnce = config.trackProductsOnce || false; this.versionName = config.versionName; this.areTransformationsConnected = destinationInfo && destinationInfo.areTransformationsConnected; this.destinationId = destinationInfo && destinationInfo.destinationId; + this.areTransformationsConnected = + destinationInfo && destinationInfo.areTransformationsConnected; + this.destinationId = destinationInfo && destinationInfo.destinationId; if (config.traitsToSetOnce && config.traitsToSetOnce.length > 0) { config.traitsToSetOnce.forEach((element) => { @@ -65,23 +60,14 @@ class Amplitude { } const initOptions = { - includeUtm: this.trackUtmProperties, - batchEvents: this.batchEvents, - eventUploadThreshold: this.eventUploadThreshold, - eventUploadPeriodMillis: this.eventUploadPeriodMillis, - forceHttps: this.forceHttps, - includeGclid: this.trackGclid, - includeReferrer: this.trackReferrer, - saveParamsReferrerOncePerSession: this.saveParamsReferrerOncePerSession, - deviceIdFromUrlParam: this.deviceIdFromUrlParam, - unsetParamsReferrerOnNewSession: this.unsetParamsReferrerOnNewSession, - deviceId: - this.preferAnonymousIdForDeviceId && this.analytics && this.analytics.getAnonymousId(), + attribution: { disabled: this.attribution, trackNewCampaigns: !this.trackNewCampaigns }, + flushQueueSize: this.flushQueueSize, + flushIntervalMillis: this.flushIntervalMillis, + appVersion: this.versionName, }; - window.amplitude.getInstance().init(this.apiKey, null, initOptions); - if (this.versionName) { - window.amplitude.getInstance().setVersionName(this.versionName); - } + if (this.preferAnonymousIdForDeviceId && this.analytics) + initOptions.deviceId = this.analytics.getAnonymousId(); + window.amplitude.init(this.apiKey, null, initOptions); } identify(rudderElement) { @@ -94,7 +80,7 @@ class Amplitude { const { userId } = rudderElement.message; if (userId) { - window.amplitude.getInstance().setUserId(userId); + window.amplitude.setUserId(userId); } if (traits) { @@ -102,7 +88,6 @@ class Amplitude { Object.keys(traits).forEach((trait) => { const shouldIncrement = this.traitsToIncrement.includes(trait); const shouldSetOnce = this.traitsToSetOnce.includes(trait); - if (shouldIncrement) { amplitudeIdentify.add(trait, traits[trait]); } @@ -133,7 +118,7 @@ class Amplitude { // For track products once, we will send the products in a single call. if (this.trackProductsOnce) { - if (products && type(products) === 'array') { + if (products && Array.isArray(products)) { // track all the products in a single event. const allProducts = []; @@ -161,7 +146,7 @@ class Amplitude { return; } - if (products && type(products) === 'array') { + if (products && Array.isArray(products)) { // track events iterating over product array individually. // Log the actuall event without products array. We will subsequently track each product with 'Product Purchased' event. @@ -206,7 +191,7 @@ class Amplitude { logEventAndCorrespondingRevenue(rudderMessage, dontTrackRevenue) { const { properties, event } = rudderMessage; - window.amplitude.getInstance().logEvent(event, properties); + window.amplitude.logEvent(event, properties); if (properties.revenue && !dontTrackRevenue) { this.trackRevenue(rudderMessage); } @@ -228,7 +213,7 @@ class Amplitude { // all pages if (this.trackAllPages) { const event = 'Loaded a page'; - window.amplitude.getInstance().logEvent(event, properties); + window.amplitude.logEvent(event, properties); } // categorized pages @@ -236,7 +221,7 @@ class Amplitude { let event; if (!useNewPageEventNameFormat) event = `Viewed page ${category}`; else event = `Viewed ${category} Page`; - window.amplitude.getInstance().logEvent(event, properties); + window.amplitude.logEvent(event, properties); } // named pages @@ -244,7 +229,7 @@ class Amplitude { let event; if (!useNewPageEventNameFormat) event = `Viewed page ${name}`; else event = `Viewed ${name} Page`; - window.amplitude.getInstance().logEvent(event, properties); + window.amplitude.logEvent(event, properties); } } @@ -266,10 +251,10 @@ class Amplitude { } if (groupType && groupValue) { - window.amplitude.getInstance().setGroup(groupTypeTrait, groupValueTrait); + window.amplitude.setGroup(groupTypeTrait, groupValueTrait); } else if (groupId) { // Similar as segment but not sure whether we need it as our cloud mode supports only the above if block - window.amplitude.getInstance().setGroup('[Rudderstack] Group', groupId); + window.amplitude.setGroup('[Rudderstack] Group', groupId); } // https://developers.amplitude.com/docs/setting-user-properties#setting-group-properties @@ -279,7 +264,7 @@ class Amplitude { setDeviceId(rudderElement) { const { anonymousId } = rudderElement.message; if (this.preferAnonymousIdForDeviceId && anonymousId) { - window.amplitude.getInstance().setDeviceId(anonymousId); + window.amplitude.setDeviceId(anonymousId); } } @@ -339,7 +324,7 @@ class Amplitude { delete amplitudeRevenue._properties.productId; delete amplitudeRevenue._properties.quantity; } - window.amplitude.getInstance().logRevenueV2(amplitudeRevenue); + window.amplitude.revenue(amplitudeRevenue); } getProductAttributes(product) { @@ -355,11 +340,11 @@ class Amplitude { isLoaded() { logger.debug('in Amplitude isLoaded'); - return !!(window.amplitude && window.amplitude.getInstance().options); + return !!(window.amplitude && window.amplitude.getDeviceId()); } isReady() { - return !!(window.amplitude && window.amplitude.getInstance().options); + return !!(window.amplitude && window.amplitude.getDeviceId()); } } diff --git a/src/integrations/Amplitude/loader.js b/src/integrations/Amplitude/loader.js index f977915824..fae68b384d 100644 --- a/src/integrations/Amplitude/loader.js +++ b/src/integrations/Amplitude/loader.js @@ -3,111 +3,119 @@ import { LOAD_ORIGIN } from '../../utils/ScriptLoader'; /* eslint-disable */ function loader(e, t) { + 'use strict'; var n = e.amplitude || { _q: [], _iq: {} }; - var r = t.createElement('script'); - r.type = 'text/javascript'; - r.integrity = 'sha384-5fhzC8Xw3m+x5cBag4AMKRdf900vw3AoaLty2vYfcKIX1iEsYRHZF4RLXIsu2o+F'; - r.crossOrigin = 'anonymous'; - r.async = true; - r.setAttribute('data-loader', LOAD_ORIGIN); - r.src = 'https://cdn.amplitude.com/libs/amplitude-8.21.4-min.gz.js'; - r.onload = function () { - if (!e.amplitude.runQueuedFunctions) { - console.log('[Amplitude] Error: could not load SDK'); - } - }; - var s = t.getElementsByTagName('script')[0]; - s.parentNode.insertBefore(r, s); - function i(e, t) { - e.prototype[t] = function () { - this._q.push([t].concat(Array.prototype.slice.call(arguments, 0))); - return this; - }; - } - var o = function () { - this._q = []; - return this; - }; - var a = [ - 'add', - 'append', - 'clearAll', - 'prepend', - 'set', - 'setOnce', - 'unset', - 'preInsert', - 'postInsert', - 'remove', - ]; - for (var c = 0; c < a.length; c++) { - i(o, a[c]); - } - n.Identify = o; - var l = function () { - this._q = []; - return this; - }; - var u = ['setProductId', 'setQuantity', 'setPrice', 'setRevenueType', 'setEventProperties']; - for (var p = 0; p < u.length; p++) { - i(l, u[p]); - } - n.Revenue = l; - var d = [ - 'init', - 'logEvent', - 'logRevenue', - 'setUserId', - 'setUserProperties', - 'setOptOut', - 'setVersionName', - 'setDomain', - 'setDeviceId', - 'enableTracking', - 'setGlobalUserProperties', - 'identify', - 'clearUserProperties', - 'setGroup', - 'logRevenueV2', - 'regenerateDeviceId', - 'groupIdentify', - 'onInit', - 'onNewSessionStart', - 'logEventWithTimestamp', - 'logEventWithGroups', - 'setSessionId', - 'resetSessionId', - 'getDeviceId', - 'getUserId', - 'setMinTimeBetweenSessionsMillis', - 'setEventUploadThreshold', - 'setUseDynamicConfig', - 'setServerZone', - 'setServerUrl', - 'sendEvents', - 'setLibrary', - 'setTransport', - ]; - function v(t) { - function e(e) { - t[e] = function () { - t._q.push([e].concat(Array.prototype.slice.call(arguments, 0))); + if (n.invoked) + e.console && console.error && console.error('Amplitude snippet has been loaded.'); + else { + var r = function (e, t) { + e.prototype[t] = function () { + return ( + this._q.push({ name: t, args: Array.prototype.slice.call(arguments, 0) }), this + ); + }; + }, + s = function (e, t, n) { + return function (r) { + e._q.push({ name: t, args: Array.prototype.slice.call(n, 0), resolve: r }); + }; + }, + o = function (e, t, n) { + e[t] = function () { + if (n) + return { promise: new Promise(s(e, t, Array.prototype.slice.call(arguments))) }; + }; + }, + i = function (e) { + for (var t = 0; t < y.length; t++) o(e, y[t], !1); + for (var n = 0; n < g.length; n++) o(e, g[n], !0); }; - } - for (var n = 0; n < d.length; n++) { - e(d[n]); - } + n.invoked = !0; + var a = t.createElement('script'); + a.setAttribute('data-loader', LOAD_ORIGIN), + (a.type = 'text/javascript'), + (a.integrity = + 'sha384-TPZhteUkZj8CAyBx+GZZytBdkuKnhKsSKcCoVCq0QSteWf/Kw5Kb9oVFUROLE1l3'), + (a.crossOrigin = 'anonymous'), + (a.async = !0), + (a.src = 'https://cdn.amplitude.com/libs/analytics-browser-1.9.1-min.js.gz'), + (a.onload = function () { + e.amplitude.runQueuedFunctions || + console.log('[Amplitude] Error: could not load SDK'); + }); + var c = t.getElementsByTagName('script')[0]; + c.parentNode.insertBefore(a, c); + for ( + var u = function () { + return (this._q = []), this; + }, + l = [ + 'add', + 'append', + 'clearAll', + 'prepend', + 'set', + 'setOnce', + 'unset', + 'preInsert', + 'postInsert', + 'remove', + 'getUserProperties', + ], + p = 0; + p < l.length; + p++ + ) + r(u, l[p]); + n.Identify = u; + for ( + var d = function () { + return (this._q = []), this; + }, + f = [ + 'getEventProperties', + 'setProductId', + 'setQuantity', + 'setPrice', + 'setRevenue', + 'setRevenueType', + 'setEventProperties', + ], + v = 0; + v < f.length; + v++ + ) + r(d, f[v]); + n.Revenue = d; + var y = [ + 'getDeviceId', + 'setDeviceId', + 'getSessionId', + 'setSessionId', + 'getUserId', + 'setUserId', + 'setOptOut', + 'setTransport', + 'reset', + ], + g = [ + 'init', + 'add', + 'remove', + 'track', + 'logEvent', + 'identify', + 'groupIdentify', + 'setGroup', + 'revenue', + 'flush', + ]; + i(n), + (n.createInstance = function (e) { + return (n._iq[e] = { _q: [] }), i(n._iq[e]), n._iq[e]; + }), + (e.amplitude = n); } - v(n); - n.getInstance = function (e) { - e = (!e || e.length === 0 ? '$default_instance' : e).toLowerCase(); - if (!Object.prototype.hasOwnProperty.call(n._iq, e)) { - n._iq[e] = { _q: [] }; - v(n._iq[e]); - } - return n._iq[e]; - }; - e.amplitude = n; } window, document; /* eslint-enable */ From 74a5710d6d4922c33065454d0003b40916323ff0 Mon Sep 17 00:00:00 2001 From: ItsSudip Date: Tue, 1 Aug 2023 13:26:55 +0530 Subject: [PATCH 2/5] feat: update logEvent to track --- src/integrations/Amplitude/browser.js | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/src/integrations/Amplitude/browser.js b/src/integrations/Amplitude/browser.js index 317c88b496..6757f39f75 100644 --- a/src/integrations/Amplitude/browser.js +++ b/src/integrations/Amplitude/browser.js @@ -3,7 +3,6 @@ /* eslint-disable no-underscore-dangle */ /* eslint-disable class-methods-use-this */ import Logger from '../../utils/logger'; -import { type } from '../../utils/utils'; import { NAME } from './constants'; import { loader } from './loader'; @@ -191,7 +190,7 @@ class Amplitude { logEventAndCorrespondingRevenue(rudderMessage, dontTrackRevenue) { const { properties, event } = rudderMessage; - window.amplitude.logEvent(event, properties); + window.amplitude.track(event, properties); if (properties.revenue && !dontTrackRevenue) { this.trackRevenue(rudderMessage); } @@ -213,7 +212,7 @@ class Amplitude { // all pages if (this.trackAllPages) { const event = 'Loaded a page'; - window.amplitude.logEvent(event, properties); + window.amplitude.track(event, properties); } // categorized pages @@ -221,7 +220,7 @@ class Amplitude { let event; if (!useNewPageEventNameFormat) event = `Viewed page ${category}`; else event = `Viewed ${category} Page`; - window.amplitude.logEvent(event, properties); + window.amplitude.track(event, properties); } // named pages @@ -229,7 +228,7 @@ class Amplitude { let event; if (!useNewPageEventNameFormat) event = `Viewed page ${name}`; else event = `Viewed ${name} Page`; - window.amplitude.logEvent(event, properties); + window.amplitude.track(event, properties); } } From f4fe6cb867330f3d536fe286b41716d0966596f1 Mon Sep 17 00:00:00 2001 From: ItsSudip Date: Fri, 11 Aug 2023 19:25:46 +0530 Subject: [PATCH 3/5] update mobile and web sdk common fields --- src/integrations/Amplitude/browser.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/integrations/Amplitude/browser.js b/src/integrations/Amplitude/browser.js index 95fdec96d5..823807ae86 100644 --- a/src/integrations/Amplitude/browser.js +++ b/src/integrations/Amplitude/browser.js @@ -21,8 +21,8 @@ class Amplitude { this.trackNamedPages = config.trackNamedPages || false; this.trackCategorizedPages = config.trackCategorizedPages || false; this.attribution = config.attribution || false; - this.flushQueueSize = config.flushQueueSize || 30; - this.flushIntervalMillis = +config.flushIntervalMillis || 30000; + this.flushQueueSize = config.eventUploadThreshold || 30; + this.flushIntervalMillis = +config.eventUploadPeriodMillis || 30000; this.trackNewCampaigns = config.trackNewCampaigns || false; this.trackRevenuePerProduct = config.trackRevenuePerProduct || false; this.preferAnonymousIdForDeviceId = config.preferAnonymousIdForDeviceId || false; From 92baf751f7637d05f2e1732546de4f0c47e928f1 Mon Sep 17 00:00:00 2001 From: ItsSudip Date: Wed, 16 Aug 2023 13:45:33 +0530 Subject: [PATCH 4/5] update default value of trackNewCampaigns --- src/integrations/Amplitude/browser.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/integrations/Amplitude/browser.js b/src/integrations/Amplitude/browser.js index 823807ae86..816c466b05 100644 --- a/src/integrations/Amplitude/browser.js +++ b/src/integrations/Amplitude/browser.js @@ -23,7 +23,7 @@ class Amplitude { this.attribution = config.attribution || false; this.flushQueueSize = config.eventUploadThreshold || 30; this.flushIntervalMillis = +config.eventUploadPeriodMillis || 30000; - this.trackNewCampaigns = config.trackNewCampaigns || false; + this.trackNewCampaigns = config.trackNewCampaigns || true; this.trackRevenuePerProduct = config.trackRevenuePerProduct || false; this.preferAnonymousIdForDeviceId = config.preferAnonymousIdForDeviceId || false; this.traitsToSetOnce = []; From c08d354bb4857af96b002caf2e84c039d6b68e4b Mon Sep 17 00:00:00 2001 From: ItsSudip Date: Thu, 17 Aug 2023 12:15:37 +0530 Subject: [PATCH 5/5] update default eventUploadPeriodMillis to 1000 --- src/integrations/Amplitude/browser.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/integrations/Amplitude/browser.js b/src/integrations/Amplitude/browser.js index 648cbccf81..b6af5ea9d4 100644 --- a/src/integrations/Amplitude/browser.js +++ b/src/integrations/Amplitude/browser.js @@ -21,7 +21,7 @@ class Amplitude { this.trackCategorizedPages = config.trackCategorizedPages || false; this.attribution = config.attribution || false; this.flushQueueSize = config.eventUploadThreshold || 30; - this.flushIntervalMillis = +config.eventUploadPeriodMillis || 30000; + this.flushIntervalMillis = +config.eventUploadPeriodMillis || 1000; this.trackNewCampaigns = config.trackNewCampaigns || true; this.trackRevenuePerProduct = config.trackRevenuePerProduct || false; this.preferAnonymousIdForDeviceId = config.preferAnonymousIdForDeviceId || false;