Skip to content

Commit

Permalink
feat: add observe plan options (#427)
Browse files Browse the repository at this point in the history
  • Loading branch information
qingzhuozhen authored Sep 20, 2021
1 parent e010d3e commit 529ad88
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 0 deletions.
13 changes: 13 additions & 0 deletions src/amplitude-client.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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);
Expand All @@ -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];
};
Expand Down
9 changes: 9 additions & 0 deletions src/options.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.*
Expand Down Expand Up @@ -67,6 +71,11 @@ export default {
optOut: false,
onError: () => {},
onExitPage: () => {},
plan: {
branch: '',
source: '',
version: '',
},
platform: 'Web',
savedMaxCount: 1000,
saveEvents: true,
Expand Down
12 changes: 12 additions & 0 deletions test/amplitude-client.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 () {
Expand Down

0 comments on commit 529ad88

Please sign in to comment.