Skip to content
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
## Unreleased

* Add support for passing callback function to init.
* Fix bug to check that Window localStorage is available for use.

## 2.4.0 (September 4, 2015)
Expand Down
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -134,4 +134,8 @@ The status and response from the server are passed to the callback function, whi
};
```

You can also pass a callback function to init, which will get called after the SDK finishes its asynchronous loading. Note: no values are passed to the init callback function:

amplitude.init("YOUR_API_KEY_HERE", "USER_ID_HERE", null, callback_function);

In the case that `optOut` is true, then no event will be logged, but the callback will be called. In the case that `batchEvents` is true, if the batch requirements `eventUploadThreshold` and `eventUploadPeriodMillis` are not met when `logEvent` is called, then no request is sent, but the callback is still called. In these cases, the callback will be called with an input status of 0 and response 'No request sent'.
6 changes: 5 additions & 1 deletion amplitude.js
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ Amplitude.prototype._newSession = false;
* - includeUtm (boolean) Whether to send utm parameters with events. Defaults to false.
* - includeReferrer (boolean) Whether to send referrer info with events. Defaults to false.
*/
Amplitude.prototype.init = function(apiKey, opt_userId, opt_config) {
Amplitude.prototype.init = function(apiKey, opt_userId, opt_config, callback) {
try {
this.options.apiKey = apiKey;
if (opt_config) {
Expand Down Expand Up @@ -251,6 +251,10 @@ Amplitude.prototype.init = function(apiKey, opt_userId, opt_config) {
} catch (e) {
log(e);
}

if (callback && typeof(callback) === 'function') {
callback();
}
};

Amplitude.prototype.isNewSession = function() {
Expand Down
4 changes: 2 additions & 2 deletions amplitude.min.js

Large diffs are not rendered by default.

6 changes: 5 additions & 1 deletion src/amplitude.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ Amplitude.prototype._newSession = false;
* - includeUtm (boolean) Whether to send utm parameters with events. Defaults to false.
* - includeReferrer (boolean) Whether to send referrer info with events. Defaults to false.
*/
Amplitude.prototype.init = function(apiKey, opt_userId, opt_config) {
Amplitude.prototype.init = function(apiKey, opt_userId, opt_config, callback) {
try {
this.options.apiKey = apiKey;
if (opt_config) {
Expand Down Expand Up @@ -139,6 +139,10 @@ Amplitude.prototype.init = function(apiKey, opt_userId, opt_config) {
} catch (e) {
log(e);
}

if (callback && typeof(callback) === 'function') {
callback();
}
};

Amplitude.prototype.isNewSession = function() {
Expand Down
13 changes: 13 additions & 0 deletions test/amplitude.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,19 @@ describe('Amplitude', function() {
amplitude.init(apiKey, userId, {language: 'en-GB'});
assert.propertyVal(amplitude.options, 'language', 'en-GB');
});

it ('should not run callback if invalid callback', function() {
amplitude.init(apiKey, userId, null, 'invalid callback');
});

it ('should run valid callbacks', function() {
var counter = 0;
var callback = function() {
counter++;
};
amplitude.init(apiKey, userId, null, callback);
assert.equal(counter, 1);
});
});

describe('setUserProperties', function() {
Expand Down