Skip to content

Commit ac5dd43

Browse files
committed
Merge pull request #27 from amplitude/init_callback
Added callback support in init
2 parents a7fd07b + 2c2d0a1 commit ac5dd43

File tree

6 files changed

+30
-4
lines changed

6 files changed

+30
-4
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
## Unreleased
22

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

56
## 2.4.0 (September 4, 2015)

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,4 +134,8 @@ The status and response from the server are passed to the callback function, whi
134134
};
135135
```
136136

137+
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:
138+
139+
amplitude.init("YOUR_API_KEY_HERE", "USER_ID_HERE", null, callback_function);
140+
137141
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'.

amplitude.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,7 @@ Amplitude.prototype._newSession = false;
175175
* - includeUtm (boolean) Whether to send utm parameters with events. Defaults to false.
176176
* - includeReferrer (boolean) Whether to send referrer info with events. Defaults to false.
177177
*/
178-
Amplitude.prototype.init = function(apiKey, opt_userId, opt_config) {
178+
Amplitude.prototype.init = function(apiKey, opt_userId, opt_config, callback) {
179179
try {
180180
this.options.apiKey = apiKey;
181181
if (opt_config) {
@@ -251,6 +251,10 @@ Amplitude.prototype.init = function(apiKey, opt_userId, opt_config) {
251251
} catch (e) {
252252
log(e);
253253
}
254+
255+
if (callback && typeof(callback) === 'function') {
256+
callback();
257+
}
254258
};
255259

256260
Amplitude.prototype.isNewSession = function() {

amplitude.min.js

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/amplitude.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ Amplitude.prototype._newSession = false;
6363
* - includeUtm (boolean) Whether to send utm parameters with events. Defaults to false.
6464
* - includeReferrer (boolean) Whether to send referrer info with events. Defaults to false.
6565
*/
66-
Amplitude.prototype.init = function(apiKey, opt_userId, opt_config) {
66+
Amplitude.prototype.init = function(apiKey, opt_userId, opt_config, callback) {
6767
try {
6868
this.options.apiKey = apiKey;
6969
if (opt_config) {
@@ -139,6 +139,10 @@ Amplitude.prototype.init = function(apiKey, opt_userId, opt_config) {
139139
} catch (e) {
140140
log(e);
141141
}
142+
143+
if (callback && typeof(callback) === 'function') {
144+
callback();
145+
}
142146
};
143147

144148
Amplitude.prototype.isNewSession = function() {

test/amplitude.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,19 @@ describe('Amplitude', function() {
6060
amplitude.init(apiKey, userId, {language: 'en-GB'});
6161
assert.propertyVal(amplitude.options, 'language', 'en-GB');
6262
});
63+
64+
it ('should not run callback if invalid callback', function() {
65+
amplitude.init(apiKey, userId, null, 'invalid callback');
66+
});
67+
68+
it ('should run valid callbacks', function() {
69+
var counter = 0;
70+
var callback = function() {
71+
counter++;
72+
};
73+
amplitude.init(apiKey, userId, null, callback);
74+
assert.equal(counter, 1);
75+
});
6376
});
6477

6578
describe('setUserProperties', function() {

0 commit comments

Comments
 (0)