From 66e964893f982b64750ad9d9a56fcd4e65864e16 Mon Sep 17 00:00:00 2001 From: dhawal1248 Date: Mon, 14 Sep 2020 12:22:47 +0530 Subject: [PATCH 1/5] make event retry count configurable --- analytics.js | 15 +++++++++++++-- utils/EventRepository.js | 9 ++++++--- 2 files changed, 19 insertions(+), 5 deletions(-) diff --git a/analytics.js b/analytics.js index f2cd66f1da..ed21cdf2bb 100644 --- a/analytics.js +++ b/analytics.js @@ -50,7 +50,7 @@ const queryDefaults = { */ function enqueue(rudderElement, type) { if (!this.eventRepository) { - this.eventRepository = EventRepository; + this.eventRepository = EventRepository(); } this.eventRepository.enqueue(rudderElement, type); } @@ -78,7 +78,6 @@ class Analytics { this.toBeProcessedArray = []; this.toBeProcessedByIntegrationArray = []; this.storage = Storage; - this.eventRepository = EventRepository; this.sendAdblockPage = false; this.sendAdblockPageOptions = {}; this.clientSuppliedCallbacks = {}; @@ -887,6 +886,18 @@ class Analytics { this.registerCallbacks(true); } + const queueOptions = {}; + if (options) { + if (options.maxRetries) { + queueOptions.maxAttempts = options.maxRetries; + } + if (options.maxQueueSize) { + queueOptions.maxItems = options.maxQueueSize; + } + } + + this.eventRepository = new EventRepository(queueOptions); + this.eventRepository.writeKey = writeKey; if (serverUrl) { this.eventRepository.url = serverUrl; diff --git a/utils/EventRepository.js b/utils/EventRepository.js index 9344753d2e..fdde76f232 100644 --- a/utils/EventRepository.js +++ b/utils/EventRepository.js @@ -41,7 +41,7 @@ class EventRepository { *Creates an instance of EventRepository. * @memberof EventRepository */ - constructor() { + constructor(options) { this.eventsBuffer = []; this.writeKey = ""; this.url = ""; @@ -51,6 +51,10 @@ class EventRepository { // previous implementation // setInterval(this.preaparePayloadAndFlush, FLUSH_INTERVAL_DEFAULT, this); + if (options) { + // TODO: add checks for value - has to be +ve? + Object.assign(queueOptions, options); + } this.payloadQueue = new Queue("rudder", queueOptions, function ( item, done @@ -229,5 +233,4 @@ class EventRepository { }); } } -let eventRepository = new EventRepository(); -export { eventRepository as EventRepository }; +export { EventRepository }; From 64cd4cd9f5f102d5bca3f125efb5477905845bf3 Mon Sep 17 00:00:00 2001 From: sayan-mitra Date: Thu, 1 Oct 2020 10:24:11 +0530 Subject: [PATCH 2/5] add support for queueOptions --- analytics.js | 17 ++++++----------- utils/EventRepository.js | 5 ++++- 2 files changed, 10 insertions(+), 12 deletions(-) diff --git a/analytics.js b/analytics.js index ed21cdf2bb..e85734d734 100644 --- a/analytics.js +++ b/analytics.js @@ -50,7 +50,7 @@ const queryDefaults = { */ function enqueue(rudderElement, type) { if (!this.eventRepository) { - this.eventRepository = EventRepository(); + this.eventRepository = EventRepository; } this.eventRepository.enqueue(rudderElement, type); } @@ -78,6 +78,7 @@ class Analytics { this.toBeProcessedArray = []; this.toBeProcessedByIntegrationArray = []; this.storage = Storage; + this.eventRepository = EventRepository; this.sendAdblockPage = false; this.sendAdblockPageOptions = {}; this.clientSuppliedCallbacks = {}; @@ -886,18 +887,12 @@ class Analytics { this.registerCallbacks(true); } - const queueOptions = {}; - if (options) { - if (options.maxRetries) { - queueOptions.maxAttempts = options.maxRetries; - } - if (options.maxQueueSize) { - queueOptions.maxItems = options.maxQueueSize; - } + if (options.queueOptions && options.queueOptions != null && typeof options.queueOptions == "object") { + this.eventRepository.startQueue(options.queueOptions); + } else { + this.eventRepository.startQueue({}); } - this.eventRepository = new EventRepository(queueOptions); - this.eventRepository.writeKey = writeKey; if (serverUrl) { this.eventRepository.url = serverUrl; diff --git a/utils/EventRepository.js b/utils/EventRepository.js index fdde76f232..de63c29e32 100644 --- a/utils/EventRepository.js +++ b/utils/EventRepository.js @@ -50,7 +50,9 @@ class EventRepository { // previous implementation // setInterval(this.preaparePayloadAndFlush, FLUSH_INTERVAL_DEFAULT, this); + } + startQueue(options) { if (options) { // TODO: add checks for value - has to be +ve? Object.assign(queueOptions, options); @@ -233,4 +235,5 @@ class EventRepository { }); } } -export { EventRepository }; +let eventRepository = new EventRepository(); +export { eventRepository as EventRepository }; From fa0773020d8e650bafe6e1e592e7428f74f1477c Mon Sep 17 00:00:00 2001 From: sayan-mitra Date: Fri, 25 Sep 2020 17:58:38 +0530 Subject: [PATCH 3/5] send campaign parsed info --- analytics.js | 67 +++++++++++++++++++++++++++++++++++----------------- 1 file changed, 45 insertions(+), 22 deletions(-) diff --git a/analytics.js b/analytics.js index e85734d734..7d4cc0e5fa 100644 --- a/analytics.js +++ b/analytics.js @@ -13,6 +13,8 @@ import Emitter from "component-emitter"; import after from "after"; import querystring from "component-querystring"; +import merge from "lodash.merge"; +import utm from "@segment/utm-params" import { getJSONTrimmed, generateUUID, @@ -717,8 +719,23 @@ class Analytics { } } + /** + * add campaign parsed details under context + * @param {*} rudderElement + */ + addCampaignInfo(rudderElement) { + const {search} = getDefaultPageProperties(); + let campaign = utm(search); + if(rudderElement.message.context && typeof rudderElement.message.context === "object") { + rudderElement.message.context.campaign = campaign; + } + } + /** * process options parameter + * Apart from top level keys merge everyting under context + * context.page's default properties are overriden by same keys of + * provided properties in case of page call * * @param {*} rudderElement * @param {*} options @@ -726,6 +743,15 @@ class Analytics { */ processOptionsParam(rudderElement, options) { const { type, properties } = rudderElement.message; + + this.addCampaignInfo(rudderElement); + + // assign page properties to context.page + rudderElement.message.context.page = + type == "page" + ? this.getContextPageProperties(properties) + : this.getContextPageProperties(); + const toplevelElements = [ "integrations", "anonymousId", @@ -734,23 +760,22 @@ class Analytics { for (const key in options) { if (toplevelElements.includes(key)) { rudderElement.message[key] = options[key]; - // special handle for ananymousId as transformation expects anonymousId in traits. - /* if (key === "anonymousId") { - rudderElement.message.context.traits["anonymousId"] = options[key]; - } */ - } else if (key !== "context") - rudderElement.message.context[key] = options[key]; - else { - for (const k in options[key]) { - rudderElement.message.context[k] = options[key][k]; + } else if (key !== "context") { + rudderElement.message.context = merge(rudderElement.message.context, { + [key]: options[key], + }); + } else { + if (typeof options[key] === "object" && options[key] != null) { + rudderElement.message.context = merge(rudderElement.message.context, { + ...options[key], + }); + } else { + logger.error( + "[Analytics: processOptionsParam] context passed in options is not object" + ); } } } - // assign page properties to context.page - rudderElement.message.context.page = - type == "page" - ? this.getContextPageProperties(options, properties) - : this.getContextPageProperties(options); } getPageProperties(properties, options) { @@ -766,16 +791,14 @@ class Analytics { } // Assign page properties to context.page if the same property is not provided under context.page - getContextPageProperties(options, properties) { + getContextPageProperties(properties) { const defaultPageProperties = getDefaultPageProperties(); - const contextPageProperties = options && options.page ? options.page : {}; + let contextPageProperties = {}; for (const key in defaultPageProperties) { - if (contextPageProperties[key] === undefined) { - contextPageProperties[key] = - properties && properties[key] - ? properties[key] - : defaultPageProperties[key]; - } + contextPageProperties[key] = + properties && properties[key] + ? properties[key] + : defaultPageProperties[key]; } return contextPageProperties; } From 709bde08b04a67e05878fc2f734fb2552d235ade Mon Sep 17 00:00:00 2001 From: sayan-mitra Date: Fri, 25 Sep 2020 18:01:19 +0530 Subject: [PATCH 4/5] add deps --- package.json | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/package.json b/package.json index b46904a006..af71ef8cfd 100644 --- a/package.json +++ b/package.json @@ -43,6 +43,7 @@ "@segment/localstorage-retry": "^1.2.2", "@segment/store": "^1.3.20", "@segment/top-domain": "^3.0.0", + "@segment/utm-params": "^2.0.0", "after": "^0.8.2", "auto-changelog": "^1.16.2", "babel-eslint": "^10.1.0", @@ -61,6 +62,7 @@ "gh-release": "^3.5.0", "is": "^3.3.0", "json3": "^3.3.3", + "lodash.merge": "^4.6.2", "md5": "^2.2.1", "obj-case": "^0.2.0", "on-body": "0.0.1", @@ -73,6 +75,7 @@ "devDependencies": { "@babel/core": "^7.6.2", "@babel/plugin-proposal-class-properties": "^7.0.0", + "@babel/plugin-proposal-optional-chaining": "^7.11.0", "@babel/plugin-transform-arrow-functions": "^7.10.4", "@babel/preset-env": "^7.6.2", "@mediaeventservices/jest-environment-jsdom-external-scripts": "^21.2.2", @@ -107,6 +110,7 @@ "rollup-plugin-strip": "^1.2.2", "rollup-plugin-terser": "^5.1.2", "rollup-plugin-uglify": "^6.0.3", + "rollup-plugin-visualizer": "^4.1.1", "size-limit": "^4.4.1" } } From 13f43112f968aad8a7c867ab092202261e3797d4 Mon Sep 17 00:00:00 2001 From: sayan-mitra Date: Tue, 20 Oct 2020 15:20:51 +0530 Subject: [PATCH 5/5] bump version --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index af71ef8cfd..33b82a6079 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "rudder-analytics", - "version": "1.1.6", + "version": "1.1.7", "description": "", "main": "./dist/browser.min.js", "size-limit": [