Skip to content

Commit e6bb6fb

Browse files
committed
merging from master
2 parents 59bb483 + ac5dd43 commit e6bb6fb

File tree

7 files changed

+63
-6
lines changed

7 files changed

+63
-6
lines changed

CHANGELOG.md

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

33
* Add support for user properties operations (set, setOnce, add, unset).
4+
* Add support for passing callback function to init.
5+
* Fix bug to check that Window localStorage is available for use.
46

57
## 2.4.0 (September 4, 2015)
68

README.md

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

179+
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:
180+
181+
amplitude.init("YOUR_API_KEY_HERE", "USER_ID_HERE", null, callback_function);
182+
179183
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: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,7 @@ Amplitude.prototype.Identify = Identify;
184184
* - includeUtm (boolean) Whether to send utm parameters with events. Defaults to false.
185185
* - includeReferrer (boolean) Whether to send referrer info with events. Defaults to false.
186186
*/
187-
Amplitude.prototype.init = function(apiKey, opt_userId, opt_config) {
187+
Amplitude.prototype.init = function(apiKey, opt_userId, opt_config, callback) {
188188
try {
189189
this.options.apiKey = apiKey;
190190
if (opt_config) {
@@ -255,6 +255,10 @@ Amplitude.prototype.init = function(apiKey, opt_userId, opt_config) {
255255
} catch (e) {
256256
log(e);
257257
}
258+
259+
if (callback && typeof(callback) === 'function') {
260+
callback();
261+
}
258262
};
259263

260264
Amplitude.prototype._loadSavedUnsentEvents = function(unsentKey, queue) {
@@ -1690,7 +1694,22 @@ module.exports = {
16901694
*/
16911695
var localStorage; // jshint ignore:line
16921696

1693-
if (window.localStorage) {
1697+
// test that Window.localStorage is available and works
1698+
function windowLocalStorageAvailable() {
1699+
var uid = new Date();
1700+
var result;
1701+
try {
1702+
window.localStorage.setItem(uid, uid);
1703+
result = window.localStorage.getItem(uid) === String(uid);
1704+
window.localStorage.removeItem(uid);
1705+
return result;
1706+
} catch (e) {
1707+
// localStorage not available
1708+
}
1709+
return false;
1710+
}
1711+
1712+
if (windowLocalStorageAvailable()) {
16941713
localStorage = window.localStorage;
16951714
} else if (window.globalStorage) {
16961715
// Firefox 2-3 use globalStorage

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
@@ -72,7 +72,7 @@ Amplitude.prototype.Identify = Identify;
7272
* - includeUtm (boolean) Whether to send utm parameters with events. Defaults to false.
7373
* - includeReferrer (boolean) Whether to send referrer info with events. Defaults to false.
7474
*/
75-
Amplitude.prototype.init = function(apiKey, opt_userId, opt_config) {
75+
Amplitude.prototype.init = function(apiKey, opt_userId, opt_config, callback) {
7676
try {
7777
this.options.apiKey = apiKey;
7878
if (opt_config) {
@@ -143,6 +143,10 @@ Amplitude.prototype.init = function(apiKey, opt_userId, opt_config) {
143143
} catch (e) {
144144
log(e);
145145
}
146+
147+
if (callback && typeof(callback) === 'function') {
148+
callback();
149+
}
146150
};
147151

148152
Amplitude.prototype._loadSavedUnsentEvents = function(unsentKey, queue) {

src/localstorage.js

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,22 @@
55
*/
66
var localStorage; // jshint ignore:line
77

8-
if (window.localStorage) {
8+
// test that Window.localStorage is available and works
9+
function windowLocalStorageAvailable() {
10+
var uid = new Date();
11+
var result;
12+
try {
13+
window.localStorage.setItem(uid, uid);
14+
result = window.localStorage.getItem(uid) === String(uid);
15+
window.localStorage.removeItem(uid);
16+
return result;
17+
} catch (e) {
18+
// localStorage not available
19+
}
20+
return false;
21+
}
22+
23+
if (windowLocalStorageAvailable()) {
924
localStorage = window.localStorage;
1025
} else if (window.globalStorage) {
1126
// Firefox 2-3 use globalStorage

test/amplitude.js

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

6679
describe('setUserProperties', function() {

0 commit comments

Comments
 (0)