Skip to content

feat: core pacakge for seamless experiment integration #457

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

Merged
merged 23 commits into from
Feb 4, 2022
Merged
Changes from 5 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
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -19,3 +19,6 @@ amplitude.umd.min.js
amplitude.native.js
amplitude.nocompat.js
amplitude.nocompat.min.js

# For WebStorm IDE
.idea/
17 changes: 9 additions & 8 deletions package.json
Original file line number Diff line number Diff line change
@@ -18,7 +18,8 @@
"@amplitude/ua-parser-js": "0.7.25",
"@amplitude/utils": "^1.0.5",
"blueimp-md5": "^2.10.0",
"query-string": "5"
"query-string": "5",
"@amplitude/amplitude-core": "0.0.1"
},
"devDependencies": {
"@amplitude/eslint-plugin-amplitude": "^1.0.1",
@@ -55,13 +56,13 @@
"mocha": "^4.0.1",
"prettier": "^2.2.1",
"requirejs": "^2.3.6",
"rollup": "^1.4.1",
"rollup-plugin-babel": "^4.3.2",
"rollup-plugin-commonjs": "^9.2.1",
"rollup-plugin-json": "^4.0.0",
"rollup-plugin-legacy": "^1.0.0",
"rollup-plugin-node-resolve": "^4.0.1",
"rollup-plugin-replace": "^2.1.0",
"rollup": "^2.26.3",
"@rollup/plugin-babel": "^5.2.0",
"@rollup/plugin-commonjs": "^15.0.0",
"@rollup/plugin-json": "^4.1.0",
"@rollup/plugin-node-resolve": "^9.0.0",
"@rollup/plugin-replace": "^2.3.3",
"@rollup/plugin-legacy": "^2.2.0",
"rollup-plugin-uglify": "^6.0.2",
"semantic-release": "^17.1.1",
"sinon": "^7.0.0",
23 changes: 14 additions & 9 deletions rollup.config.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import resolve from 'rollup-plugin-node-resolve';
import commonjs from 'rollup-plugin-commonjs';
import replace from 'rollup-plugin-replace';
import babel from 'rollup-plugin-babel';
import json from 'rollup-plugin-json';
import babel from '@rollup/plugin-babel';
import commonjs from '@rollup/plugin-commonjs';
import json from '@rollup/plugin-json';
import resolve from '@rollup/plugin-node-resolve';
import replace from '@rollup/plugin-replace';

export default {
input: 'src/index.js',
@@ -16,18 +16,23 @@ export default {
},
plugins: [
json(),
babel({
exclude: 'node_modules/**',
plugins: ['@babel/plugin-proposal-object-rest-spread'],
}),
resolve({
browser: true,
}),
replace({
preventAssignment: false,
BUILD_COMPAT_SNIPPET: 'true',
BUILD_COMPAT_LOCAL_STORAGE: 'true',
BUILD_COMPAT_2_0: 'true',
}),
commonjs(),
babel({
babelHelpers: 'runtime',
exclude: 'node_modules/**',
plugins: [
'@babel/plugin-proposal-object-rest-spread',
'@babel/plugin-transform-runtime'
],
}),
],
};
25 changes: 13 additions & 12 deletions rollup.esm.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import commonjs from 'rollup-plugin-commonjs';
import replace from 'rollup-plugin-replace';
import babel from 'rollup-plugin-babel';
import json from 'rollup-plugin-json';
import commonjs from '@rollup/plugin-commonjs';
import replace from '@rollup/plugin-replace';
import babel from '@rollup/plugin-babel';
import json from '@rollup/plugin-json';

export default {
input: 'src/index.js',
@@ -12,21 +12,22 @@ export default {
},
plugins: [
json(),
babel({
exclude: 'node_modules/**',
plugins: [
'@babel/plugin-transform-runtime',
'@babel/plugin-proposal-object-rest-spread'
],
runtimeHelpers: true
}),
replace({
preventAssignment: false,
BUILD_COMPAT_SNIPPET: 'false',
BUILD_COMPAT_LOCAL_STORAGE: 'true',
BUILD_COMPAT_2_0: 'true',
}),
commonjs({
include: "node_modules/**"
}),
babel({
babelHelpers: 'runtime',
exclude: 'node_modules/**',
plugins: [
'@babel/plugin-proposal-object-rest-spread',
'@babel/plugin-transform-runtime'
],
}),
],
};
2 changes: 1 addition & 1 deletion rollup.snippet-tests.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import config from './rollup.config.js';
import legacy from 'rollup-plugin-legacy';
import legacy from '@rollup/plugin-legacy';

config.plugins.push(legacy({
'./amplitude-snippet.min.js': 'amplitude',
23 changes: 14 additions & 9 deletions rollup.umd.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import resolve from 'rollup-plugin-node-resolve';
import commonjs from 'rollup-plugin-commonjs';
import replace from 'rollup-plugin-replace';
import babel from 'rollup-plugin-babel';
import json from 'rollup-plugin-json';
import resolve from '@rollup/plugin-node-resolve';
import commonjs from '@rollup/plugin-commonjs';
import replace from '@rollup/plugin-replace';
import babel from '@rollup/plugin-babel';
import json from '@rollup/plugin-json';

export default {
input: 'src/index.js',
@@ -16,18 +16,23 @@ export default {
},
plugins: [
json(),
babel({
exclude: 'node_modules/**',
plugins: ['@babel/plugin-proposal-object-rest-spread'],
}),
resolve({
browser: true,
}),
replace({
preventAssignment: false,
BUILD_COMPAT_SNIPPET: 'true',
BUILD_COMPAT_LOCAL_STORAGE: 'true',
BUILD_COMPAT_2_0: 'true',
}),
commonjs(),
babel({
babelHelpers: 'runtime',
exclude: 'node_modules/**',
plugins: [
'@babel/plugin-proposal-object-rest-spread',
'@babel/plugin-transform-runtime'
],
}),
],
};
37 changes: 37 additions & 0 deletions src/amplitude-client.js
Original file line number Diff line number Diff line change
@@ -20,6 +20,8 @@ import baseCookie from './base-cookie';
import { AmplitudeServerZone, getEventLogApi } from './server-zone';
import ConfigManager from './config-manager';

import { AmplitudeCore } from '@amplitude/amplitude-core';

/**
* AmplitudeClient SDK API - instance constructor.
* The Amplitude class handles creation of client instances, all you need to do is call amplitude.getInstance()
@@ -54,6 +56,9 @@ var AmplitudeClient = function AmplitudeClient(instanceName) {
this._sessionId = null;
this._isInitialized = false;

// used to integrate with experiment SDK (client-side exposure tracking & real-time user properties)
this._core = AmplitudeCore.getInstance(this._instanceName);

this._userAgent = (navigator && navigator.userAgent) || null;
};

@@ -242,6 +247,21 @@ AmplitudeClient.prototype.init = function init(apiKey, opt_userId, opt_config, o
);
}
}

// Sets an event receiver to receive and forward exposure events from the experiment SDK.
this._core.analyticsConnector.setEventReceiver((event) => {
this._logEvent(event.eventType, event.eventProperties, event.userProperties);
});

// Set the user ID and device ID in the core identity store to enable fetching variants.
const editor = this._core.identityStore.editIdentity();
if (this.options.deviceId) {
editor.setDeviceId(this.options.deviceId);
}
if (this.options.userId) {
editor.setUserId(this.options.userId);
}
editor.commit();
} catch (err) {
utils.log.error(err);
if (type(opt_config.onError) === 'function') {
@@ -886,6 +906,10 @@ AmplitudeClient.prototype.setUserId = function setUserId(userId, startNewSession
}

_saveCookieData(this);

// Update core identity store to propagate new user info
// to experiment SDK and trigger a fetch if the ID has changed.
this._core.identityStore.editIdentity().setUserId(this.options.userId).commit();
} catch (e) {
utils.log.error(e);
}
@@ -1015,6 +1039,10 @@ AmplitudeClient.prototype.setDeviceId = function setDeviceId(deviceId) {
if (!utils.isEmptyString(deviceId)) {
this.options.deviceId = '' + deviceId;
_saveCookieData(this);

// Update core identity store to propagate new user info
// to experiment SDK and trigger a fetch if the ID has changed.
this._core.identityStore.editIdentity().setDeviceId(this.options.deviceId).commit();
}
} catch (e) {
utils.log.error(e);
@@ -1355,6 +1383,15 @@ AmplitudeClient.prototype._logEvent = function _logEvent(

this._sendEventsIfReady();

// In the case of an identify event, update the core user store so the experiment SDK can fetch new variants and
// utilize user properties in real time.
if (eventType === Constants.IDENTIFY_EVENT) {
this._core.identityStore
.editIdentity()
.updateUserProperties(utils.truncate(utils.validateProperties(userProperties)))
.commit();
}

return eventId;
} catch (e) {
utils.log.error(e);
Loading