Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add ingestion_metadata field #552

Merged
merged 1 commit into from
Sep 7, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions src/amplitude-client.js
Original file line number Diff line number Diff line change
Expand Up @@ -1430,6 +1430,13 @@ AmplitudeClient.prototype._logEvent = function _logEvent(
};
}

if (_isIngestionMetadataSet(this)) {
event.ingestion_metadata = {
source_name: this.options.ingestionMetadata.sourceName || undefined,
source_version: this.options.ingestionMetadata.sourceVersion || undefined,
};
}

if (eventType === Constants.IDENTIFY_EVENT || eventType === Constants.GROUP_IDENTIFY_EVENT) {
this._unsentIdentifys.push({ event, callback, errorCallback });
this._limitEventsQueued(this._unsentIdentifys);
Expand Down Expand Up @@ -1469,6 +1476,13 @@ const _isObservePlanSet = function _isObservePlanSet(scope) {
);
};

const _isIngestionMetadataSet = function _isIngestionMetadataSet(scope) {
return (
scope.options.ingestionMetadata &&
(scope.options.ingestionMetadata.sourceName || scope.options.ingestionMetadata.sourceVersion)
);
};

var _shouldTrackField = function _shouldTrackField(scope, field) {
return !!scope.options.trackingOptions[field];
};
Expand Down
7 changes: 7 additions & 0 deletions src/options.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ import { version as libraryVersion } from '../package.json';
* @property {boolean} [includeGclid=`false`] - If `true`, captures the gclid URL parameter as well as the user's initial_gclid via a setOnce operation.
* @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 {Object} [ingestionMetadata] Ingestion metadata
* @property {string} [ingestionMetadata.sourceName] source name in ingestion metadata, e.g. "ampli"
* @property {string} [ingestionMetadata.sourceVersion] source version in ingestion metadata, e.g. "1.0.0"
* @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.
Expand Down Expand Up @@ -74,6 +77,10 @@ export default {
includeGclid: false,
includeReferrer: false,
includeUtm: false,
ingestionMetadata: {
sourceName: '',
sourceVersion: '',
},
language: language.getLanguage(),
library: {
name: 'amplitude-js',
Expand Down
43 changes: 43 additions & 0 deletions test/amplitude-client.js
Original file line number Diff line number Diff line change
Expand Up @@ -908,6 +908,17 @@ describe('AmplitudeClient', function () {
assert.deepEqual(amplitude.options.plan, plan);
});

it('should set ingestion metadata options', function () {
const amplitude = new AmplitudeClient('ingestion metadata');
const ingestionMetadata = {
sourceName: 'ampli',
sourceVersion: '1.0.0',
};
amplitude.init(apiKey, null, { ingestionMetadata: ingestionMetadata });

assert.deepEqual(amplitude.options.ingestionMetadata, ingestionMetadata);
});

it('should set sessionId from config', () => {
const amplitude = new AmplitudeClient();
amplitude.init(apiKey, null, { sessionId: 123 });
Expand Down Expand Up @@ -4572,4 +4583,36 @@ describe('AmplitudeClient', function () {
assert.equal(amplitude._unsentEvents[0].event.partner_id, null);
});
});

describe('ingestionMetadata Support', function () {
beforeEach(function () {
// reset ingestionMetadata
amplitude.options.ingestionMetadata = {
sourceName: '',
sourceVersion: '',
};
reset();
});

it('should include ingestion_metadata', function () {
const ingestionMetadata = {
sourceName: 'ampli',
sourceVersion: '1.0.0',
};
amplitude.init(apiKey, null, { batchEvents: true, ingestionMetadata: ingestionMetadata });

amplitude.logEvent('testEvent1');
assert.deepEqual(amplitude._unsentEvents[0].event.ingestion_metadata, {
source_name: 'ampli',
source_version: '1.0.0',
});
});

it('should set ingestion_metadata default null', function () {
amplitude.init(apiKey, null, { batchEvents: true });

amplitude.logEvent('testEvent1');
assert.equal(amplitude._unsentEvents[0].event.ingestion_metadata, null);
});
});
});