Skip to content

Commit 5419678

Browse files
committed
make instance names case-insensitive
1 parent 596f576 commit 5419678

File tree

7 files changed

+45
-14
lines changed

7 files changed

+45
-14
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ If you want to log events to multiple Amplitude apps, you will need to have sepa
7979

8080
You need to assign a name to each Amplitude app / instance, and use that name consistently when fetching that instance to call functions. **IMPORTANT: Once you have chosen a name for that instance you cannot change it.** Every instance's data and settings are tied to its name, and you will need to continue using that instance name for all future versions of your app to maintain data continuity, so chose your instance names wisely. Note these names do not need to be the names of your apps in the Amplitude dashboards, but they need to remain consistent throughout your code. You also need to be sure that each instance is initialized with the correct apiKey.
8181

82-
Instance names must be nonnull and nonempty strings. You can fetch each instance by name by calling `amplitude.getInstance('INSTANCE_NAME')`.
82+
Instance names must be nonnull and nonempty strings. Names are case-insensitive. You can fetch each instance by name by calling `amplitude.getInstance('INSTANCE_NAME')`.
8383

8484
As mentioned before, each new instance created will have its own apiKey, userId, deviceId, and settings. **You will have to reconfigure all the settings for each instance.** This gives you the freedom to have different settings for each instance.
8585

amplitude.js

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -115,15 +115,19 @@ var utils = require('./utils');
115115
var version = require('./version');
116116
var DEFAULT_OPTIONS = require('./options');
117117

118-
var DEFAULT_INSTANCE = '$defaultInstance';
118+
var DEFAULT_INSTANCE = '$default_instance';
119119

120120
var Amplitude = function() {
121121
this.options = object.merge({}, DEFAULT_OPTIONS); // maintain a copy for backwards compatibilty
122122
this._instances = {}; // mapping of instance names to instances
123123
};
124124

125125
Amplitude.prototype.getInstance = function(instance) {
126-
instance = utils.isEmptyString(instance) ? DEFAULT_INSTANCE : instance;
126+
if (utils.isEmptyString(instance)) {
127+
instance = DEFAULT_INSTANCE;
128+
}
129+
instance = instance.toLowerCase();
130+
127131
var client = this._instances[instance];
128132
if (client === undefined) {
129133
client = new AmplitudeClient(instance);
@@ -265,7 +269,7 @@ var log = function(s) {
265269
console.log('[Amplitude] ' + s);
266270
};
267271

268-
var DEFAULT_INSTANCE = '$defaultInstance';
272+
var DEFAULT_INSTANCE = '$default_instance';
269273
var IDENTIFY_EVENT = '$identify';
270274
var API_VERSION = 2;
271275
var MAX_STRING_LENGTH = 1024;
@@ -287,7 +291,10 @@ var LocalStorageKeys = {
287291
* AmplitudeClient API
288292
*/
289293
var AmplitudeClient = function(instanceName) {
290-
this._instanceName = utils.isEmptyString(instanceName) ? DEFAULT_INSTANCE : instanceName;
294+
if (utils.isEmptyString(instanceName)) {
295+
instanceName = DEFAULT_INSTANCE;
296+
}
297+
this._instanceName = instanceName.toLowerCase();
291298
this._storageSuffix = this._instanceName === DEFAULT_INSTANCE ? '' : '_' + this._instanceName;
292299
this._unsentEvents = [];
293300
this._unsentIdentifys = [];

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-client.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ var log = function(s) {
1717
console.log('[Amplitude] ' + s);
1818
};
1919

20-
var DEFAULT_INSTANCE = '$defaultInstance';
20+
var DEFAULT_INSTANCE = '$default_instance';
2121
var IDENTIFY_EVENT = '$identify';
2222
var API_VERSION = 2;
2323
var MAX_STRING_LENGTH = 1024;
@@ -39,7 +39,10 @@ var LocalStorageKeys = {
3939
* AmplitudeClient API
4040
*/
4141
var AmplitudeClient = function(instanceName) {
42-
this._instanceName = utils.isEmptyString(instanceName) ? DEFAULT_INSTANCE : instanceName;
42+
if (utils.isEmptyString(instanceName)) {
43+
instanceName = DEFAULT_INSTANCE;
44+
}
45+
this._instanceName = instanceName.toLowerCase();
4346
this._storageSuffix = this._instanceName === DEFAULT_INSTANCE ? '' : '_' + this._instanceName;
4447
this._unsentEvents = [];
4548
this._unsentIdentifys = [];

src/amplitude.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,19 @@ var utils = require('./utils');
66
var version = require('./version');
77
var DEFAULT_OPTIONS = require('./options');
88

9-
var DEFAULT_INSTANCE = '$defaultInstance';
9+
var DEFAULT_INSTANCE = '$default_instance';
1010

1111
var Amplitude = function() {
1212
this.options = object.merge({}, DEFAULT_OPTIONS); // maintain a copy for backwards compatibilty
1313
this._instances = {}; // mapping of instance names to instances
1414
};
1515

1616
Amplitude.prototype.getInstance = function(instance) {
17-
instance = utils.isEmptyString(instance) ? DEFAULT_INSTANCE : instance;
17+
if (utils.isEmptyString(instance)) {
18+
instance = DEFAULT_INSTANCE;
19+
}
20+
instance = instance.toLowerCase();
21+
1822
var client = this._instances[instance];
1923
if (client === undefined) {
2024
client = new AmplitudeClient(instance);

test/amplitude-client.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,11 @@ describe('AmplitudeClient', function() {
4545
reset();
4646
});
4747

48+
it('should make instanceName case-sensitive', function() {
49+
assert.equal(new AmplitudeClient('APP3')._instanceName, 'app3');
50+
assert.equal(new AmplitudeClient('$DEFAULT_INSTANCE')._instanceName, '$default_instance');
51+
});
52+
4853
it('should accept userId', function() {
4954
amplitude.init(apiKey, userId);
5055
assert.equal(amplitude.options.userId, userId);
@@ -335,7 +340,7 @@ describe('AmplitudeClient', function() {
335340
localStorage.setItem('amplitude_unsent', existingEvent);
336341
localStorage.setItem('amplitude_unsent_identify', existingIdentify);
337342

338-
var amplitude2 = new AmplitudeClient('$defaultInstance');
343+
var amplitude2 = new AmplitudeClient('$default_Instance');
339344
amplitude2.init(apiKey, null, {batchEvents: true});
340345

341346
// check event loaded into memory

test/amplitude.js

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,9 +51,12 @@ describe('Amplitude', function() {
5151
amplitude.init(apiKey);
5252
assert.equal(amplitude.getInstance().options.apiKey, apiKey);
5353
assert.equal(amplitude.options, amplitude.getInstance().options);
54-
assert.equal(amplitude.getInstance('$defaultInstance').options.apiKey, apiKey);
55-
assert.equal(amplitude.getInstance(), amplitude.getInstance('$defaultInstance'));
54+
assert.equal(amplitude.getInstance('$default_instance').options.apiKey, apiKey);
55+
assert.equal(amplitude.getInstance(), amplitude.getInstance('$default_instance'));
5656

57+
// test for case insensitivity
58+
assert.equal(amplitude.getInstance(), amplitude.getInstance('$DEFAULT_INSTANCE'));
59+
assert.equal(amplitude.getInstance(), amplitude.getInstance('$DEFAULT_instance'));
5760
assert.equal(amplitude.options.deviceId, amplitude.getInstance().options.deviceId);
5861
});
5962

@@ -66,6 +69,15 @@ describe('Amplitude', function() {
6669
assert.notEqual(app1, app2);
6770
assert.equal(app1.options.apiKey, 1);
6871
assert.equal(app2.options.apiKey, 2);
72+
73+
assert.equal(app1, amplitude.getInstance('app1'));
74+
assert.equal(app1, amplitude.getInstance('APP1'));
75+
assert.equal(app1, amplitude.getInstance('aPp1'));
76+
assert.equal(app2, amplitude.getInstance('app2'));
77+
assert.equal(app2, amplitude.getInstance('APP2'));
78+
assert.equal(app2, amplitude.getInstance('aPp2'));
79+
80+
assert.equal(amplitude.getInstance('APP3')._instanceName, 'app3');
6981
});
7082

7183
it('should return same instance for same key', function() {

0 commit comments

Comments
 (0)