From 12f517b0c73fa66c26bc87564b112f77875aad80 Mon Sep 17 00:00:00 2001 From: Qingzhuo Zhen Date: Thu, 16 Sep 2021 16:41:28 -0700 Subject: [PATCH 1/5] feat: add observe plan options --- src/amplitude-client.js | 12 ++++++++++++ src/options.js | 6 ++++++ test/amplitude-client.js | 12 ++++++++++++ 3 files changed, 30 insertions(+) diff --git a/src/amplitude-client.js b/src/amplitude-client.js index 1f42a3f2..c49eda72 100644 --- a/src/amplitude-client.js +++ b/src/amplitude-client.js @@ -1269,6 +1269,13 @@ AmplitudeClient.prototype._logEvent = function _logEvent( eventProperties = eventProperties || {}; groups = groups || {}; groupProperties = groupProperties || {}; + const plan = _isObservePlanSet(this) + ? { + branch: this.options.plan.branch || null, + source: this.options.plan.source || null, + version: this.options.plan.version || null, + } + : null; var event = { device_id: this.options.deviceId, user_id: this.options.userId, @@ -1295,6 +1302,7 @@ AmplitudeClient.prototype._logEvent = function _logEvent( groups: utils.truncate(utils.validateGroups(groups)), group_properties: utils.truncate(utils.validateProperties(groupProperties)), user_agent: this._userAgent, + plan: plan, }; if (eventType === Constants.IDENTIFY_EVENT || eventType === Constants.GROUP_IDENTIFY_EVENT) { @@ -1317,6 +1325,10 @@ AmplitudeClient.prototype._logEvent = function _logEvent( } }; +var _isObservePlanSet = function _isObservePlanSet(scope) { + return scope.options.plan && (scope.options.plan.source || scope.options.plan.branch || scope.options.plan.version); +}; + var _shouldTrackField = function _shouldTrackField(scope, field) { return !!scope.options.trackingOptions[field]; }; diff --git a/src/options.js b/src/options.js index 375032e1..dea51797 100644 --- a/src/options.js +++ b/src/options.js @@ -28,6 +28,7 @@ import language from './language'; * @property {boolean} [optOut=`false`] - Whether or not to disable tracking for the current user. * @property {function} [onError=`() => {}`] - Function to call on error. * @property {function} [onExitPage=`() => {}`] - Function called when the user exits the browser. Useful logging on page exit. + * @property {Object} [plan = {branch: '', source: '', version: ''}] - Observe plan information. * @property {string} [platform=`Web`] - Platform device is running on. Defaults to `Web` (browser, including mobile browsers). * @property {number} [savedMaxCount=`1000`] - Maximum number of events to save in localStorage. If more events are logged while offline, then old events are removed. * @property {boolean} [saveEvents=`true`] - If `true`, saves events to localStorage and removes them upon successful upload. *Note: Without saving events, events may be lost if the user navigates to another page before the events are uploaded.* @@ -67,6 +68,11 @@ export default { optOut: false, onError: () => {}, onExitPage: () => {}, + plan: { + branch: '', + source: '', + version: '', + }, platform: 'Web', savedMaxCount: 1000, saveEvents: true, diff --git a/test/amplitude-client.js b/test/amplitude-client.js index 1d2dc474..2a11551f 100644 --- a/test/amplitude-client.js +++ b/test/amplitude-client.js @@ -855,6 +855,18 @@ describe('AmplitudeClient', function () { amplitude.cookieStorage.options.restore(); }); + + it('should set observer plan options', function () { + var amplitude = new AmplitudeClient('observer plan'); + var plan = { + branch: 'my-feature-branch', + source: 'web', + version: '1.0.0', + }; + amplitude.init(apiKey, null, { plan: plan }); + + assert.deepEqual(amplitude.options.plan, plan); + }); }); describe('runQueuedFunctions', function () { From 0a58409726b7b4b14e2efda5884f7800626c7f9c Mon Sep 17 00:00:00 2001 From: Qingzhuo Zhen Date: Fri, 17 Sep 2021 14:14:05 -0700 Subject: [PATCH 2/5] update comments --- src/options.js | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/options.js b/src/options.js index dea51797..cffa1112 100644 --- a/src/options.js +++ b/src/options.js @@ -28,7 +28,10 @@ import language from './language'; * @property {boolean} [optOut=`false`] - Whether or not to disable tracking for the current user. * @property {function} [onError=`() => {}`] - Function to call on error. * @property {function} [onExitPage=`() => {}`] - Function called when the user exits the browser. Useful logging on page exit. - * @property {Object} [plan = {branch: '', source: '', version: ''}] - Observe plan information. + * @property {Object} [plan] Tracking plan properties + * @property {string} [plan.branch] The tracking plan branch name e.g. "main" + * @property {string} [plan.source] The tracking plan source e.g. "web" + * @property {string} [plan.version] The tracking plan version e.g. "1", "15" * @property {string} [platform=`Web`] - Platform device is running on. Defaults to `Web` (browser, including mobile browsers). * @property {number} [savedMaxCount=`1000`] - Maximum number of events to save in localStorage. If more events are logged while offline, then old events are removed. * @property {boolean} [saveEvents=`true`] - If `true`, saves events to localStorage and removes them upon successful upload. *Note: Without saving events, events may be lost if the user navigates to another page before the events are uploaded.* From fecefca31d3218d97491c575119bc730b473a2df Mon Sep 17 00:00:00 2001 From: Qingzhuo Zhen Date: Fri, 17 Sep 2021 15:01:27 -0700 Subject: [PATCH 3/5] switch to use undefined instead of null --- src/amplitude-client.js | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/amplitude-client.js b/src/amplitude-client.js index c49eda72..2ca48403 100644 --- a/src/amplitude-client.js +++ b/src/amplitude-client.js @@ -1271,11 +1271,11 @@ AmplitudeClient.prototype._logEvent = function _logEvent( groupProperties = groupProperties || {}; const plan = _isObservePlanSet(this) ? { - branch: this.options.plan.branch || null, - source: this.options.plan.source || null, - version: this.options.plan.version || null, + branch: this.options.plan.branch || undefined, + source: this.options.plan.source || undefined, + version: this.options.plan.version || undefined, } - : null; + : undefined; var event = { device_id: this.options.deviceId, user_id: this.options.userId, From 3b47527fba4d4b352ba4502369da12f6bd692351 Mon Sep 17 00:00:00 2001 From: Qingzhuo Zhen Date: Fri, 17 Sep 2021 16:16:56 -0700 Subject: [PATCH 4/5] fix test --- src/amplitude-client.js | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/src/amplitude-client.js b/src/amplitude-client.js index 2ca48403..fede073f 100644 --- a/src/amplitude-client.js +++ b/src/amplitude-client.js @@ -1269,13 +1269,7 @@ AmplitudeClient.prototype._logEvent = function _logEvent( eventProperties = eventProperties || {}; groups = groups || {}; groupProperties = groupProperties || {}; - const plan = _isObservePlanSet(this) - ? { - branch: this.options.plan.branch || undefined, - source: this.options.plan.source || undefined, - version: this.options.plan.version || undefined, - } - : undefined; + var event = { device_id: this.options.deviceId, user_id: this.options.userId, @@ -1302,9 +1296,16 @@ AmplitudeClient.prototype._logEvent = function _logEvent( groups: utils.truncate(utils.validateGroups(groups)), group_properties: utils.truncate(utils.validateProperties(groupProperties)), user_agent: this._userAgent, - plan: plan, }; + if (_isObservePlanSet(this)) { + event.plan = { + branch: this.options.plan.branch || undefined, + source: this.options.plan.source || undefined, + version: this.options.plan.version || undefined, + }; + } + if (eventType === Constants.IDENTIFY_EVENT || eventType === Constants.GROUP_IDENTIFY_EVENT) { this._unsentIdentifys.push({ event, callback, errorCallback }); this._limitEventsQueued(this._unsentIdentifys); From e3b017891bc73fdcfa4a4558fc82870877744098 Mon Sep 17 00:00:00 2001 From: Qingzhuo Zhen Date: Fri, 17 Sep 2021 16:21:03 -0700 Subject: [PATCH 5/5] update to not use var --- src/amplitude-client.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/amplitude-client.js b/src/amplitude-client.js index fede073f..0f43917d 100644 --- a/src/amplitude-client.js +++ b/src/amplitude-client.js @@ -1326,7 +1326,7 @@ AmplitudeClient.prototype._logEvent = function _logEvent( } }; -var _isObservePlanSet = function _isObservePlanSet(scope) { +const _isObservePlanSet = function _isObservePlanSet(scope) { return scope.options.plan && (scope.options.plan.source || scope.options.plan.branch || scope.options.plan.version); };