Skip to content

Commit dce0eac

Browse files
authored
feat: custom library options and setter (#443)
* Create option and setter for custom library version * add docs * change naming * Add tests * setter nit * better assertion for library name
1 parent ae53596 commit dce0eac

File tree

3 files changed

+53
-6
lines changed

3 files changed

+53
-6
lines changed

src/amplitude-client.js

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ import UAParser from '@amplitude/ua-parser-js'; // Identifying device and browse
1414
import utils from './utils';
1515
import UUID from './uuid';
1616
import base64Id from './base64Id';
17-
import { version } from '../package.json';
1817
import DEFAULT_OPTIONS from './options';
1918
import getHost from './get-host';
2019
import baseCookie from './base-cookie';
@@ -1294,10 +1293,7 @@ AmplitudeClient.prototype._logEvent = function _logEvent(
12941293
event_properties: utils.truncate(utils.validateProperties(eventProperties)),
12951294
user_properties: utils.truncate(utils.validateProperties(userProperties)),
12961295
uuid: UUID(),
1297-
library: {
1298-
name: 'amplitude-js',
1299-
version: version,
1300-
},
1296+
library: this.options.library,
13011297
sequence_number: sequenceNumber, // for ordering events and identifys
13021298
groups: utils.truncate(utils.validateGroups(groups)),
13031299
group_properties: utils.truncate(utils.validateProperties(groupProperties)),
@@ -1841,7 +1837,19 @@ if (BUILD_COMPAT_2_0) {
18411837
* @returns {number} version number
18421838
* @example var amplitudeVersion = amplitude.__VERSION__;
18431839
*/
1844-
AmplitudeClient.prototype.__VERSION__ = version;
1840+
AmplitudeClient.prototype.__VERSION__ = function getVersion() {
1841+
return this.options.library.version;
1842+
};
1843+
1844+
/**
1845+
* 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
1846+
* @public
1847+
* @param {string} name - Custom library name
1848+
* @param {string} version - Custom library version
1849+
*/
1850+
AmplitudeClient.prototype.setLibrary = function setLibrary(name, version) {
1851+
this.options.library = { name: name, version: version };
1852+
};
18451853

18461854
/**
18471855
* Determines whether or not to push call to this._q or invoke it

src/options.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import Constants from './constants';
22
import language from './language';
33
import { AmplitudeServerZone } from './server-zone';
4+
import { version as libraryVersion } from '../package.json';
45

56
/**
67
* Options used when initializing Amplitude
@@ -24,6 +25,7 @@ import { AmplitudeServerZone } from './server-zone';
2425
* @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.
2526
* @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.
2627
* @property {string} [language=The language determined by the browser] - Custom language to set.
28+
* @property {Object} [library=`{ name: 'amplitude-js', version: packageJsonVersion }`] - Values for the library version
2729
* @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.
2830
* @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`.
2931
* @property {boolean} [optOut=`false`] - Whether or not to disable tracking for the current user.
@@ -70,6 +72,10 @@ export default {
7072
includeReferrer: false,
7173
includeUtm: false,
7274
language: language.getLanguage(),
75+
library: {
76+
name: 'amplitude-js',
77+
version: libraryVersion,
78+
},
7379
logLevel: 'WARN',
7480
logAttributionCapturedEvent: false,
7581
optOut: false,

test/amplitude-client.js

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4116,4 +4116,37 @@ describe('AmplitudeClient', function () {
41164116
assert.equal(amplitude.options.apiEndpoint, constants.EVENT_LOG_EU_URL);
41174117
});
41184118
});
4119+
4120+
describe('custom library options', function () {
4121+
beforeEach(function () {
4122+
reset();
4123+
});
4124+
it('should use default library options', function () {
4125+
amplitude.init(apiKey);
4126+
amplitude.logEvent('Event Type 1');
4127+
const { name, version } = JSON.parse(queryString.parse(server.requests[0].requestBody).e)[0].library;
4128+
assert.equal(name, 'amplitude-js');
4129+
assert.equal(version, amplitude.options.library.version);
4130+
});
4131+
4132+
it('should change library when passed in options', function () {
4133+
const customLibrary = { name: 'test-library', version: '1.0-test' };
4134+
amplitude.init(apiKey, null, { library: customLibrary });
4135+
amplitude.logEvent('Event Type 1');
4136+
const { name, version } = JSON.parse(queryString.parse(server.requests[0].requestBody).e)[0].library;
4137+
assert.equal(name, 'test-library');
4138+
assert.equal(version, '1.0-test');
4139+
});
4140+
4141+
it('should change library when set with method', function () {
4142+
amplitude.init(apiKey);
4143+
amplitude.setLibrary('test-library', '1.0-test');
4144+
amplitude.logEvent('Event Type 2');
4145+
4146+
const { name, version } = JSON.parse(queryString.parse(server.requests[0].requestBody).e)[0].library;
4147+
4148+
assert.equal(name, 'test-library');
4149+
assert.equal(version, '1.0-test');
4150+
});
4151+
});
41194152
});

0 commit comments

Comments
 (0)