From 90bed162e98e45417b1dcc954cf407f1150486c6 Mon Sep 17 00:00:00 2001 From: John Tran Date: Tue, 9 Nov 2021 17:27:12 -0700 Subject: [PATCH 1/6] Create option and setter for custom library version --- src/amplitude-client.js | 14 ++++++++------ src/options.js | 5 +++++ 2 files changed, 13 insertions(+), 6 deletions(-) diff --git a/src/amplitude-client.js b/src/amplitude-client.js index 701d2934..58d162c2 100644 --- a/src/amplitude-client.js +++ b/src/amplitude-client.js @@ -14,7 +14,7 @@ import UAParser from '@amplitude/ua-parser-js'; // Identifying device and browse import utils from './utils'; import UUID from './uuid'; import base64Id from './base64Id'; -import { version } from '../package.json'; + import DEFAULT_OPTIONS from './options'; import getHost from './get-host'; import baseCookie from './base-cookie'; @@ -1294,10 +1294,7 @@ AmplitudeClient.prototype._logEvent = function _logEvent( event_properties: utils.truncate(utils.validateProperties(eventProperties)), user_properties: utils.truncate(utils.validateProperties(userProperties)), uuid: UUID(), - library: { - name: 'amplitude-js', - version: version, - }, + library: this.options.library, sequence_number: sequenceNumber, // for ordering events and identifys groups: utils.truncate(utils.validateGroups(groups)), group_properties: utils.truncate(utils.validateProperties(groupProperties)), @@ -1841,7 +1838,12 @@ if (BUILD_COMPAT_2_0) { * @returns {number} version number * @example var amplitudeVersion = amplitude.__VERSION__; */ -AmplitudeClient.prototype.__VERSION__ = version; +AmplitudeClient.prototype.__VERSION__ = function getVersion() { + return this.options.library.version; +}; +AmplitudeClient.prototype.setVersion = function setVersion(name, version) { + return (this.options.library = { name, version }); +}; /** * Determines whether or not to push call to this._q or invoke it diff --git a/src/options.js b/src/options.js index 3d0f6140..1748b8fe 100644 --- a/src/options.js +++ b/src/options.js @@ -1,6 +1,7 @@ import Constants from './constants'; import language from './language'; import { AmplitudeServerZone } from './server-zone'; +import { version as libraryVersion } from '../package.json'; /** * Options used when initializing Amplitude @@ -70,6 +71,10 @@ export default { includeReferrer: false, includeUtm: false, language: language.getLanguage(), + library: { + name: 'amplitude-js', + version: libraryVersion, + }, logLevel: 'WARN', logAttributionCapturedEvent: false, optOut: false, From f4b5eb95f0a7c6d8ecbf5515dea3afdf2ed84467 Mon Sep 17 00:00:00 2001 From: John Tran Date: Tue, 9 Nov 2021 17:47:34 -0700 Subject: [PATCH 2/6] add docs --- src/amplitude-client.js | 7 +++++++ src/options.js | 1 + 2 files changed, 8 insertions(+) diff --git a/src/amplitude-client.js b/src/amplitude-client.js index 58d162c2..574ccaa8 100644 --- a/src/amplitude-client.js +++ b/src/amplitude-client.js @@ -1841,6 +1841,13 @@ if (BUILD_COMPAT_2_0) { AmplitudeClient.prototype.__VERSION__ = function getVersion() { return this.options.library.version; }; + +/** + * Sets the library name and version. Default is `amplitude-js` and the version defined in package.json. Used if you're building another library on top of amplitude-js and want a custom data source value + * @public + * @param {string} name - Custom library name + * @param {string} version - Custom library version + */ AmplitudeClient.prototype.setVersion = function setVersion(name, version) { return (this.options.library = { name, version }); }; diff --git a/src/options.js b/src/options.js index 1748b8fe..91ad3ef2 100644 --- a/src/options.js +++ b/src/options.js @@ -25,6 +25,7 @@ import { version as libraryVersion } from '../package.json'; * @property {boolean} [includeReferrer=`false`] - If `true`, captures the referrer and referring_domain for each session, as well as the user's initial_referrer and initial_referring_domain via a setOnce operation. * @property {boolean} [includeUtm=`false`] - If `true`, finds UTM parameters in the query string or the _utmz cookie, parses, and includes them as user properties on all events uploaded. This also captures initial UTM parameters for each session via a setOnce operation. * @property {string} [language=The language determined by the browser] - Custom language to set. + * @property {Object} [library=`{ name: 'amplitude-js', version: packageJsonVersion }`] - Values for the library version * @property {string} [logLevel=`WARN`] - Level of logs to be printed in the developer console. Valid values are 'DISABLE', 'ERROR', 'WARN', 'INFO'. To learn more about the different options, see below. * @property {boolean} [logAttributionCapturedEvent=`false`] - If `true`, the SDK will log an Amplitude event anytime new attribution values are captured from the user. **Note: These events count towards your event volume.** Event name being logged: [Amplitude] Attribution Captured. Event Properties that can be logged: `utm_source`, `utm_medium`, `utm_campaign`, `utm_term`, `utm_content`, `referrer`, `referring_domain`, `gclid`, `fbclid`. For UTM properties to be logged, `includeUtm` must be set to `true`. For the `referrer` and `referring_domain` properties to be logged, `includeReferrer` must be set to `true`. For the `gclid` property to be logged, `includeGclid` must be set to `true`. For the `fbclid` property to be logged, `includeFbclid` must be set to `true`. * @property {boolean} [optOut=`false`] - Whether or not to disable tracking for the current user. From 5447f40962574c9f402e61592f8673542e9ccc03 Mon Sep 17 00:00:00 2001 From: John Tran Date: Wed, 10 Nov 2021 11:37:29 -0700 Subject: [PATCH 3/6] change naming --- 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 574ccaa8..311e800d 100644 --- a/src/amplitude-client.js +++ b/src/amplitude-client.js @@ -1848,7 +1848,7 @@ AmplitudeClient.prototype.__VERSION__ = function getVersion() { * @param {string} name - Custom library name * @param {string} version - Custom library version */ -AmplitudeClient.prototype.setVersion = function setVersion(name, version) { +AmplitudeClient.prototype.setLibrary = function setLibrary(name, version) { return (this.options.library = { name, version }); }; From fa0c44fb1ea3313461c85f17df5ba500e4091fd0 Mon Sep 17 00:00:00 2001 From: John Tran Date: Wed, 10 Nov 2021 13:21:29 -0700 Subject: [PATCH 4/6] Add tests --- test/amplitude-client.js | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/test/amplitude-client.js b/test/amplitude-client.js index 0742a72b..284f7b59 100644 --- a/test/amplitude-client.js +++ b/test/amplitude-client.js @@ -4116,4 +4116,37 @@ describe('AmplitudeClient', function () { assert.equal(amplitude.options.apiEndpoint, constants.EVENT_LOG_EU_URL); }); }); + + describe('custom library options', function () { + beforeEach(function () { + reset(); + }); + it('should use default library options', function () { + amplitude.init(apiKey); + amplitude.logEvent('Event Type 1'); + const { name, version } = JSON.parse(queryString.parse(server.requests[0].requestBody).e)[0].library; + assert.equal(name, amplitude.options.library.name); + assert.equal(version, amplitude.options.library.version); + }); + + it('should change library when passed in options', function () { + const customLibrary = { name: 'test-library', version: '1.0-test' }; + amplitude.init(apiKey, null, { library: customLibrary }); + amplitude.logEvent('Event Type 1'); + const { name, version } = JSON.parse(queryString.parse(server.requests[0].requestBody).e)[0].library; + assert.equal(name, 'test-library'); + assert.equal(version, '1.0-test'); + }); + + it('should change library when set with method', function () { + amplitude.init(apiKey); + amplitude.setLibrary('test-library', '1.0-test'); + amplitude.logEvent('Event Type 2'); + + const { name, version } = JSON.parse(queryString.parse(server.requests[0].requestBody).e)[0].library; + + assert.equal(name, 'test-library'); + assert.equal(version, '1.0-test'); + }); + }); }); From 3706153fdaf1dc00cf2228928d318fea29f03460 Mon Sep 17 00:00:00 2001 From: John Tran Date: Wed, 10 Nov 2021 13:25:39 -0700 Subject: [PATCH 5/6] setter nit --- src/amplitude-client.js | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/amplitude-client.js b/src/amplitude-client.js index 311e800d..987bd751 100644 --- a/src/amplitude-client.js +++ b/src/amplitude-client.js @@ -14,7 +14,6 @@ import UAParser from '@amplitude/ua-parser-js'; // Identifying device and browse import utils from './utils'; import UUID from './uuid'; import base64Id from './base64Id'; - import DEFAULT_OPTIONS from './options'; import getHost from './get-host'; import baseCookie from './base-cookie'; @@ -1849,7 +1848,7 @@ AmplitudeClient.prototype.__VERSION__ = function getVersion() { * @param {string} version - Custom library version */ AmplitudeClient.prototype.setLibrary = function setLibrary(name, version) { - return (this.options.library = { name, version }); + this.options.library = { name: name, version: version }; }; /** From e5d52a7270ac1731116964dc433f9ca920adf11e Mon Sep 17 00:00:00 2001 From: John Tran Date: Fri, 12 Nov 2021 14:32:56 -0800 Subject: [PATCH 6/6] better assertion for library name --- test/amplitude-client.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/amplitude-client.js b/test/amplitude-client.js index 284f7b59..f60aea56 100644 --- a/test/amplitude-client.js +++ b/test/amplitude-client.js @@ -4125,7 +4125,7 @@ describe('AmplitudeClient', function () { amplitude.init(apiKey); amplitude.logEvent('Event Type 1'); const { name, version } = JSON.parse(queryString.parse(server.requests[0].requestBody).e)[0].library; - assert.equal(name, amplitude.options.library.name); + assert.equal(name, 'amplitude-js'); assert.equal(version, amplitude.options.library.version); });