Skip to content

Commit cd7bfce

Browse files
authored
Add onInit hook, drop json3, update build dependencies (#178)
* Drop json3 polyfill and update build dependencies * fix uri tests and build * add onInit hook to invoke callbacks after init * Add onInit to snippet * Clear onInit queue
1 parent 6ee5164 commit cd7bfce

20 files changed

+929
-794
lines changed

.babelrc

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
{
22
"presets": [
3-
["env", {
3+
["@babel/env", {
44
"targets": {
5-
"browsers": ["ie >= 9"]
5+
"browsers": ["ie >= 8"]
66
},
77
"modules": false
88
}]

CHANGELOG.md

+13-2
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,20 @@
1-
### 5.0.0 (February 27, 2019)
1+
### 5.0.0 (Unreleased)
22

3-
* Stop committing generated files to the git repository
3+
* Add `onInit` method that accepts a callback that will be invoked after init
4+
* Allow for api endpoints that do not end with a trailing slash
45
* Sync with upstream ua-parser for user agent parsing
6+
* Upgrade rollup/babel dependencies
7+
8+
## Breaking Changes
9+
* Drop JSON polyfill. This will break IE 7 and older. You can install your own JSON polyfill before loading amplitude.
10+
* Stop committing generated files to the master branch in the git repository (do not install amplitude from git).
511
* Drop custom user agent parsing for symbian and blackberry
612

13+
14+
### 4.7.0 (March 12, 2019)
15+
16+
* Cherry-picked from 5.0.0: Add `onInit` method that accepts a callback that will be invoked after init
17+
718
### 4.6.0 (February 25, 2019)
819

920
* Add support for unsetting utm params when a new session is created

amplitude-segment-snippet.min.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ var i=function(){this._q=[];return this}
77
;return this}
88
;var u=["setProductId","setQuantity","setPrice","setRevenueType","setEventProperties"]
99
;for(var c=0;c<u.length;c++){r(a,u[c])}n.Revenue=a
10-
;var p=["init","logEvent","logRevenue","setUserId","setUserProperties","setOptOut","setVersionName","setDomain","setDeviceId","setGlobalUserProperties","identify","clearUserProperties","setGroup","logRevenueV2","regenerateDeviceId","groupIdentify","logEventWithTimestamp","logEventWithGroups","setSessionId","resetSessionId"]
10+
;var p=["init","logEvent","logRevenue","setUserId","setUserProperties","setOptOut","setVersionName","setDomain","setDeviceId","setGlobalUserProperties","identify","clearUserProperties","setGroup","logRevenueV2","regenerateDeviceId","groupIdentify","onInit","logEventWithTimestamp","logEventWithGroups","setSessionId","resetSessionId"]
1111
;function l(e){function t(t){e[t]=function(){
1212
e._q.push([t].concat(Array.prototype.slice.call(arguments,0)))}}
1313
for(var n=0;n<p.length;n++){t(p[n])}}l(n);n.getInstance=function(e){

package.json

+10-12
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,13 @@
1313
"dependencies": {
1414
"@amplitude/ua-parser-js": "0.7.20",
1515
"blueimp-md5": "^2.10.0",
16-
"json3": "^3.3.2",
1716
"query-string": "5"
1817
},
1918
"devDependencies": {
20-
"babel-core": "6",
21-
"babel-plugin-external-helpers": "^6.22.0",
22-
"babel-plugin-transform-object-rest-spread": "^6.26.0",
23-
"babel-polyfill": "^6.26.0",
24-
"babel-preset-env": "1",
19+
"@babel/core": "^7.3.4",
20+
"@babel/plugin-external-helpers": "^7.2.0",
21+
"@babel/plugin-proposal-object-rest-spread": "^7.3.4",
22+
"@babel/preset-env": "^7.3.4",
2523
"chai": "^4.1.2",
2624
"express": "^4.16.2",
2725
"fs-extra": "^4.0.2",
@@ -37,14 +35,14 @@
3735
"karma-sinon": "^1.0.5",
3836
"karma-sourcemap-loader": "^0.3.7",
3937
"mocha": "^4.0.1",
40-
"rollup": "^0.56.0",
41-
"rollup-plugin-babel": "^3.0.2",
42-
"rollup-plugin-commonjs": "8.4.1",
38+
"requirejs": "^2.3.6",
39+
"rollup": "^1.4.1",
40+
"rollup-plugin-babel": "^4.3.2",
41+
"rollup-plugin-commonjs": "^9.2.1",
4342
"rollup-plugin-legacy": "^1.0.0",
44-
"rollup-plugin-node-resolve": "^3.0.0",
43+
"rollup-plugin-node-resolve": "^4.0.1",
4544
"rollup-plugin-replace": "^2.1.0",
46-
"rollup-plugin-uglify": "^2.0.1",
47-
"saucelabs": "^1.5.0",
45+
"rollup-plugin-uglify": "^6.0.2",
4846
"sinon": "^4.1.2",
4947
"uglify-js": "^2.0.0"
5048
},

rollup.config.js

+3-4
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import replace from 'rollup-plugin-replace';
44
import babel from 'rollup-plugin-babel';
55

66
export default {
7-
input: 'src/index.compat.js',
7+
input: 'src/index.js',
88
output: {
99
name: 'amplitude',
1010
file: 'amplitude.js',
@@ -13,8 +13,7 @@ export default {
1313
plugins: [
1414
babel({
1515
exclude: 'node_modules/**',
16-
externalHelpersWhitelist: ['defineProperty', 'extends', 'typeof'],
17-
plugins: ['external-helpers', 'transform-object-rest-spread'],
16+
plugins: ['@babel/plugin-proposal-object-rest-spread'],
1817
}),
1918
resolve({
2019
browser: true,
@@ -26,4 +25,4 @@ export default {
2625
}),
2726
commonjs(),
2827
],
29-
}
28+
};

rollup.min.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import config from './rollup.config.js';
2-
import uglify from 'rollup-plugin-uglify';
2+
import { uglify } from 'rollup-plugin-uglify';
33

44
config.plugins.push(uglify());
55
config.output.file = 'amplitude.min.js';

rollup.nocompat.js

+9-4
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,7 @@ export default {
1313
plugins: [
1414
babel({
1515
exclude: 'node_modules/**',
16-
externalHelpersWhitelist: ['defineProperty', 'extends', 'typeof'],
17-
plugins: ['external-helpers', 'transform-object-rest-spread'],
16+
plugins: ['@babel/plugin-proposal-object-rest-spread'],
1817
}),
1918
resolve({
2019
browser: true,
@@ -24,6 +23,12 @@ export default {
2423
BUILD_COMPAT_2_0: 'false',
2524
BUILD_COMPAT_LOCAL_STORAGE: 'false',
2625
}),
27-
commonjs(),
26+
commonjs({
27+
include: [
28+
'node_modules/query-string/**',
29+
'node_modules/@amplitude/ua-parser-js/**',
30+
'node_modules/blueimp-md5/**',
31+
],
32+
}),
2833
],
29-
}
34+
};

rollup.nocompat.min.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import config from './rollup.nocompat.js';
2-
import uglify from 'rollup-plugin-uglify';
2+
import { uglify } from 'rollup-plugin-uglify';
33

44
config.plugins.push(uglify());
55
config.output.file = 'amplitude.nocompat.min.js';

src/amplitude-client.js

+14
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ var AmplitudeClient = function AmplitudeClient(instanceName) {
3131
this._q = []; // queue for proxied functions before script load
3232
this._sending = false;
3333
this._updateScheduled = false;
34+
this._onInit = [];
3435

3536
// event meta data
3637
this._eventId = 0;
@@ -149,6 +150,11 @@ AmplitudeClient.prototype.init = function init(apiKey, opt_userId, opt_config, o
149150
opt_callback(this);
150151
}
151152
}
153+
154+
for (let i = 0; i < this._onInit.length; i++) {
155+
this._onInit[i]();
156+
}
157+
this._onInit = [];
152158
};
153159

154160
/**
@@ -285,6 +291,14 @@ AmplitudeClient.prototype.isNewSession = function isNewSession() {
285291
return this._newSession;
286292
};
287293

294+
/**
295+
* Store callbacks to call after init
296+
* @private
297+
*/
298+
AmplitudeClient.prototype.onInit = function (callback) {
299+
this._onInit.push(callback);
300+
};
301+
288302
/**
289303
* Returns the id of the current session.
290304
* @public

src/amplitude-snippet.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
var funcs = ['init', 'logEvent', 'logRevenue', 'setUserId', 'setUserProperties',
2424
'setOptOut', 'setVersionName', 'setDomain', 'setDeviceId',
2525
'setGlobalUserProperties', 'identify', 'clearUserProperties',
26-
'setGroup', 'logRevenueV2', 'regenerateDeviceId', 'groupIdentify',
26+
'setGroup', 'logRevenueV2', 'regenerateDeviceId', 'groupIdentify', 'onInit',
2727
'logEventWithTimestamp', 'logEventWithGroups', 'setSessionId', 'resetSessionId'];
2828
function setUpProxy(instance) {
2929
function proxyMain(fn) {

src/index.compat.js

-2
This file was deleted.

src/options.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import language from './language';
22

33
// default options
44
export default {
5-
apiEndpoint: 'api.amplitude.com/',
5+
apiEndpoint: 'api.amplitude.com',
66
batchEvents: false,
77
cookieExpiration: 365 * 10,
88
cookieName: 'amplitude_id',

test/amplitude-client.js

+24-3
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,27 @@ describe('AmplitudeClient', function() {
5454
assert.equal(new AmplitudeClient('$DEFAULT_INSTANCE')._instanceName, '$default_instance');
5555
});
5656

57+
it('should invoke onInit callbacks', function() {
58+
let onInitCalled = false;
59+
let onInit2Called = false;
60+
amplitude.onInit(() => { onInitCalled = true; });
61+
amplitude.onInit(() => { onInit2Called = true; });
62+
63+
amplitude.init(apiKey);
64+
assert.ok(onInitCalled);
65+
assert.ok(onInit2Called);
66+
});
67+
68+
it('should clear the onInitQueue', function() {
69+
let onInitCalled = false;
70+
let onInit2Called = false;
71+
amplitude.onInit(() => { onInitCalled = true; });
72+
amplitude.onInit(() => { onInit2Called = true; });
73+
74+
amplitude.init(apiKey);
75+
assert.lengthOf(amplitude._onInit, 0);
76+
});
77+
5778
it('fails on invalid apiKeys', function() {
5879
amplitude.init(null);
5980
assert.equal(amplitude.options.apiKey, undefined);
@@ -1413,7 +1434,7 @@ describe('setVersionName', function() {
14131434
amplitude.options.forceHttps = false;
14141435
amplitude.logEvent('Event Type 1');
14151436
assert.lengthOf(server.requests, 1);
1416-
assert.equal(server.requests[0].url, 'http://api.amplitude.com/');
1437+
assert.equal(server.requests[0].url, 'http://api.amplitude.com');
14171438
assert.equal(server.requests[0].method, 'POST');
14181439
assert.equal(server.requests[0].async, true);
14191440
});
@@ -1422,7 +1443,7 @@ describe('setVersionName', function() {
14221443
amplitude.options.forceHttps = true;
14231444
amplitude.logEvent('Event Type 1');
14241445
assert.lengthOf(server.requests, 1);
1425-
assert.equal(server.requests[0].url, 'https://api.amplitude.com/');
1446+
assert.equal(server.requests[0].url, 'https://api.amplitude.com');
14261447
assert.equal(server.requests[0].method, 'POST');
14271448
assert.equal(server.requests[0].async, true);
14281449
});
@@ -1431,7 +1452,7 @@ describe('setVersionName', function() {
14311452
amplitude.init(apiKey, null, { forceHttps: true });
14321453
amplitude.logEvent('Event Type 1');
14331454
assert.lengthOf(server.requests, 1);
1434-
assert.equal(server.requests[0].url, 'https://api.amplitude.com/');
1455+
assert.equal(server.requests[0].url, 'https://api.amplitude.com');
14351456
assert.equal(server.requests[0].method, 'POST');
14361457
assert.equal(server.requests[0].async, true);
14371458
});

test/amplitude.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -1087,7 +1087,7 @@ describe('setVersionName', function() {
10871087
amplitude.options.forceHttps = false;
10881088
amplitude.logEvent('Event Type 1');
10891089
assert.lengthOf(server.requests, 1);
1090-
assert.equal(server.requests[0].url, 'http://api.amplitude.com/');
1090+
assert.equal(server.requests[0].url, 'http://api.amplitude.com');
10911091
assert.equal(server.requests[0].method, 'POST');
10921092
assert.equal(server.requests[0].async, true);
10931093
});
@@ -1096,7 +1096,7 @@ describe('setVersionName', function() {
10961096
amplitude.options.forceHttps = true;
10971097
amplitude.logEvent('Event Type 1');
10981098
assert.lengthOf(server.requests, 1);
1099-
assert.equal(server.requests[0].url, 'https://api.amplitude.com/');
1099+
assert.equal(server.requests[0].url, 'https://api.amplitude.com');
11001100
assert.equal(server.requests[0].method, 'POST');
11011101
assert.equal(server.requests[0].async, true);
11021102
});
@@ -1105,7 +1105,7 @@ describe('setVersionName', function() {
11051105
amplitude.init(apiKey, null, { forceHttps: true });
11061106
amplitude.logEvent('Event Type 1');
11071107
assert.lengthOf(server.requests, 1);
1108-
assert.equal(server.requests[0].url, 'https://api.amplitude.com/');
1108+
assert.equal(server.requests[0].url, 'https://api.amplitude.com');
11091109
assert.equal(server.requests[0].method, 'POST');
11101110
assert.equal(server.requests[0].async, true);
11111111
});

test/browser/amplitudejs-requirejs.html

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
<script>
55
requirejs.config({
66
paths: {
7-
'amplitude': 'https://cdn.amplitude.com/libs/amplitude-3.8.0-min.gz'
7+
'amplitude': '/amplitude'
88
}
99
});
1010

test/browser/index.html

+8-11
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,15 @@
33
<head>
44
<meta http-equiv="content-type" content="text/html; charset=utf-8"/>
55
<title>Amplitude Tests</title>
6-
<link rel="stylesheet" href="../../node_modules/mocha/mocha.css"/>
76
</head>
87
<body>
9-
<div id="mocha"></div>
10-
<script src="../../node_modules/mocha/mocha.js"></script>
11-
<script>
12-
mocha.ui('bdd');
13-
var assert = chai.assert;
14-
var expect = chai.expect;
15-
</script>
16-
<script src="../../amplitude.js"></script>
17-
<script src="../../build/tests.js"></script>
18-
<script>(window.mochaPhantomJS || mocha).run();</script>
8+
<div style="display: flex; flex-direction: column">
9+
<a href="./mocha-tests.html">mocha</a>
10+
<a href="amplitudejs-requirejs.html">require js</a>
11+
<a href="amplitudejs-segment.html">segment </a>
12+
<a href="amplitudejs.html">amplitude js</a>
13+
<a href="amplitudejs2.html">amplitude js 2</a>
14+
<a href="snippet.html">snippet</a>
15+
</div>
1916
</body>
2017
</html>

test/browser/mocha-tests.html

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head>
4+
<meta http-equiv="content-type" content="text/html; charset=utf-8"/>
5+
<title>Amplitude Tests</title>
6+
<link rel="stylesheet" href="../../node_modules/mocha/mocha.css"/>
7+
</head>
8+
<body>
9+
<div id="mocha"></div>
10+
<script src="../../node_modules/mocha/mocha.js"></script>
11+
<script>
12+
mocha.ui('bdd');
13+
var assert = chai.assert;
14+
var expect = chai.expect;
15+
</script>
16+
<script src="../../amplitude.js"></script>
17+
<script src="../../build/tests.js"></script>
18+
<script>(window.mochaPhantomJS || mocha).run();</script>
19+
</body>
20+
</html>

test/browser/server.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ const app = express();
99
app.use(express.static(__dirname + '/../..'));
1010
app.use(express.static(__dirname));
1111
app.listen(port);
12-
console.log('Listening on port ' + port + '...');
12+
console.log(`Listening on http://localhost:${port}`);
1313

1414
if (!!process.env.USE_SSL) {
1515
const options = {

test/snippet-tests.js

+9
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,15 @@ describe('Snippet', function() {
5454
assert.deepEqual(amplitude._iq['instance2']._q[1], ['logEvent', 'Event']);
5555
});
5656

57+
it('amplitude object should proxy onInit', function() {
58+
const callback = () => { };
59+
amplitude.getInstance('onInit').onInit(callback);
60+
amplitude.getInstance('onInit').init('API_KEY');
61+
amplitude.getInstance('onInit').logEvent('Event', {prop: 1});
62+
assert.lengthOf(amplitude._iq['oninit']._q, 3);
63+
assert.deepEqual(amplitude._iq['oninit']._q[0], ['onInit', callback]);
64+
});
65+
5766
it('amplitude object should proxy resetSessionId', function() {
5867
amplitude.getInstance('reset_session_id_instance').init('API_KEY');
5968
amplitude.getInstance('reset_session_id_instance').resetSessionId();

0 commit comments

Comments
 (0)