diff --git a/src/amplitude-client.js b/src/amplitude-client.js index 1f42a3f2..0f43917d 100644 --- a/src/amplitude-client.js +++ b/src/amplitude-client.js @@ -1269,6 +1269,7 @@ AmplitudeClient.prototype._logEvent = function _logEvent( eventProperties = eventProperties || {}; groups = groups || {}; groupProperties = groupProperties || {}; + var event = { device_id: this.options.deviceId, user_id: this.options.userId, @@ -1297,6 +1298,14 @@ AmplitudeClient.prototype._logEvent = function _logEvent( user_agent: this._userAgent, }; + 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); @@ -1317,6 +1326,10 @@ AmplitudeClient.prototype._logEvent = function _logEvent( } }; +const _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..cffa1112 100644 --- a/src/options.js +++ b/src/options.js @@ -28,6 +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] 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.* @@ -67,6 +71,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 () {