Skip to content

Commit

Permalink
Cherry pick onInit function from 5.0.0
Browse files Browse the repository at this point in the history
  • Loading branch information
blazzy committed Mar 12, 2019
1 parent 40ff69f commit b6ca44f
Show file tree
Hide file tree
Showing 10 changed files with 61 additions and 8 deletions.
18 changes: 17 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,20 @@
### 4.6.0 (February 25, 2018)
### 5.0.0 (Unreleased)

* Add `onInit` method that accepts a callback that will be invoked after init
* Allow for api endpoints that do not end with a trailing slash
* Sync with upstream ua-parser for user agent parsing
* Upgrade rollup/babel dependencies

## Breaking Changes
* Drop JSON polyfill. This will break IE 7 and older. You can install your own JSON polyfill before loading amplitude.
* Stop committing generated files to the master branch in the git repository (do not install amplitude from git).
* Drop custom user agent parsing for symbian and blackberry

### 4.7.0 (March 12, 2019)

* Cherry-picked from 5.0.0: Add `onInit` method that accepts a callback that will be invoked after init

### 4.6.0 (February 25, 2019)

* Add support for unsetting utm params when a new session is created
* Update dependencies to pass yarn audit
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ Please see our [installation guide](https://amplitude.zendesk.com/hc/en-us/artic
[![npm version](https://badge.fury.io/js/amplitude-js.svg)](https://badge.fury.io/js/amplitude-js)
[![Bower version](https://badge.fury.io/bo/amplitude-js.svg)](https://badge.fury.io/bo/amplitude-js)

[4.6.0 - Released on February 25, 2019](https://github.com/amplitude/Amplitude-JavaScript/releases/latest)
[4.7.0 - Released on March 12, 2019](https://github.com/amplitude/Amplitude-JavaScript/releases/latest)


# JavaScript SDK Reference #
Expand Down
2 changes: 1 addition & 1 deletion amplitude-snippet.min.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
(function(e,t){var n=e.amplitude||{_q:[],_iq:{}};var r=t.createElement("script")
;r.type="text/javascript";r.async=true
;r.src="https://cdn.amplitude.com/libs/amplitude-4.6.0-min.gz.js"
;r.src="https://cdn.amplitude.com/libs/amplitude-4.7.0-min.gz.js"
;r.onload=function(){if(e.amplitude.runQueuedFunctions){
e.amplitude.runQueuedFunctions()}else{
console.log("[Amplitude] Error: could not load SDK")}}
Expand Down
15 changes: 14 additions & 1 deletion amplitude.js
Original file line number Diff line number Diff line change
Expand Up @@ -3801,7 +3801,7 @@ var uuid = function uuid(a) {
);
};

var version = '4.6.0';
var version = '4.7.0';

var getLanguage = function getLanguage() {
return navigator && (navigator.languages && navigator.languages[0] || navigator.language || navigator.userLanguage) || undefined;
Expand Down Expand Up @@ -3871,6 +3871,7 @@ var AmplitudeClient = function AmplitudeClient(instanceName) {
this._q = []; // queue for proxied functions before script load
this._sending = false;
this._updateScheduled = false;
this._onInit = [];

// event meta data
this._eventId = 0;
Expand Down Expand Up @@ -3983,6 +3984,10 @@ AmplitudeClient.prototype.init = function init(apiKey, opt_userId, opt_config, o
opt_callback(this);
}
}

for (var _i = 0; _i < this._onInit.length; _i++) {
this._onInit[_i]();
}
};

/**
Expand Down Expand Up @@ -4118,6 +4123,14 @@ AmplitudeClient.prototype.isNewSession = function isNewSession() {
return this._newSession;
};

/**
* Store callbacks to call after init
* @private
*/
AmplitudeClient.prototype.onInit = function (callback) {
this._onInit.push(callback);
};

/**
* Returns the id of the current session.
* @public
Expand Down
2 changes: 1 addition & 1 deletion amplitude.min.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "amplitude-js",
"author": "Amplitude <[email protected]>",
"version": "4.6.0",
"version": "4.7.0",
"license": "MIT",
"description": "Javascript library for Amplitude Analytics",
"keywords": [
Expand Down
13 changes: 13 additions & 0 deletions src/amplitude-client.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ var AmplitudeClient = function AmplitudeClient(instanceName) {
this._q = []; // queue for proxied functions before script load
this._sending = false;
this._updateScheduled = false;
this._onInit = [];

// event meta data
this._eventId = 0;
Expand Down Expand Up @@ -149,6 +150,10 @@ AmplitudeClient.prototype.init = function init(apiKey, opt_userId, opt_config, o
opt_callback(this);
}
}

for (let i = 0; i < this._onInit.length; i++) {
this._onInit[i]();
}
};

/**
Expand Down Expand Up @@ -285,6 +290,14 @@ AmplitudeClient.prototype.isNewSession = function isNewSession() {
return this._newSession;
};

/**
* Store callbacks to call after init
* @private
*/
AmplitudeClient.prototype.onInit = function (callback) {
this._onInit.push(callback);
};

/**
* Returns the id of the current session.
* @public
Expand Down
2 changes: 1 addition & 1 deletion src/amplitude-snippet.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
var as = document.createElement('script');
as.type = 'text/javascript';
as.async = true;
as.src = 'https://cdn.amplitude.com/libs/amplitude-4.6.0-min.gz.js';
as.src = 'https://cdn.amplitude.com/libs/amplitude-4.7.0-min.gz.js';
as.onload = function() {if(window.amplitude.runQueuedFunctions) {window.amplitude.runQueuedFunctions();} else {console.log('[Amplitude] Error: could not load SDK');}};
var s = document.getElementsByTagName('script')[0];
s.parentNode.insertBefore(as, s);
Expand Down
2 changes: 1 addition & 1 deletion src/version.js
Original file line number Diff line number Diff line change
@@ -1 +1 @@
export default '4.6.0';
export default '4.7.0';
11 changes: 11 additions & 0 deletions test/amplitude-client.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,17 @@ describe('AmplitudeClient', function() {
assert.equal(new AmplitudeClient('$DEFAULT_INSTANCE')._instanceName, '$default_instance');
});

it('should invoke onInit callbacks', function() {
let onInitCalled = false;
let onInit2Called = false;
amplitude.onInit(() => { onInitCalled = true; });
amplitude.onInit(() => { onInit2Called = true; });

amplitude.init(apiKey);
assert.ok(onInitCalled);
assert.ok(onInit2Called);
});

it('fails on invalid apiKeys', function() {
amplitude.init(null);
assert.equal(amplitude.options.apiKey, undefined);
Expand Down

0 comments on commit b6ca44f

Please sign in to comment.