Skip to content

Commit 795ae63

Browse files
committed
cleaning up
1 parent 3ab6809 commit 795ae63

File tree

5 files changed

+76
-53
lines changed

5 files changed

+76
-53
lines changed

amplitude.js

Lines changed: 26 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -94,16 +94,13 @@
9494
/* jshint expr:true */
9595

9696
var Amplitude = require('./amplitude');
97+
9798
var old = window.amplitude || {};
98-
var newInstance = new Amplitude();
99-
newInstance._q = old._q || [];
100-
for (var instance in old._iq) { // migrate each instance's queue
101-
if (old._iq.hasOwnProperty(instance)) {
102-
newInstance.getInstance(instance)._q = old._iq[instance]._q || [];
103-
}
104-
}
99+
var instance = new Amplitude();
100+
instance._q = old._q || [];
105101

106-
module.exports = newInstance;
102+
// export the instance
103+
module.exports = instance;
107104

108105
}, {"./amplitude":2}],
109106
2: [function(require, module, exports) {
@@ -126,19 +123,22 @@ var IDENTIFY_EVENT = '$identify';
126123
var API_VERSION = 2;
127124
var MAX_STRING_LENGTH = 1024;
128125
var LocalStorageKeys = {
129-
SESSION_ID: 'amplitude_sessionId',
130-
LAST_EVENT_TIME: 'amplitude_lastEventTime',
131126
LAST_EVENT_ID: 'amplitude_lastEventId',
127+
LAST_EVENT_TIME: 'amplitude_lastEventTime',
132128
LAST_IDENTIFY_ID: 'amplitude_lastIdentifyId',
133129
LAST_SEQUENCE_NUMBER: 'amplitude_lastSequenceNumber',
134130
REFERRER: 'amplitude_referrer',
131+
SESSION_ID: 'amplitude_sessionId',
135132

136133
// Used in cookie as well
137134
DEVICE_ID: 'amplitude_deviceId',
138-
USER_ID: 'amplitude_userId',
139-
OPT_OUT: 'amplitude_optOut'
135+
OPT_OUT: 'amplitude_optOut',
136+
USER_ID: 'amplitude_userId'
140137
};
141138

139+
/*
140+
* Amplitude API
141+
*/
142142
var Amplitude = function() {
143143
this._unsentEvents = [];
144144
this._unsentIdentifys = [];
@@ -159,16 +159,6 @@ Amplitude.prototype._updateScheduled = false;
159159

160160
Amplitude.prototype.Identify = Identify;
161161

162-
Amplitude.prototype.runQueuedFunctions = function () {
163-
for (var i = 0; i < this._q.length; i++) {
164-
var fn = this[this._q[i][0]];
165-
if (fn && type(fn) === 'function') {
166-
fn.apply(this, this._q[i].slice(1));
167-
}
168-
}
169-
this._q = []; // clear function queue after running
170-
};
171-
172162
/**
173163
* Initializes Amplitude.
174164
* apiKey The API Key for your app
@@ -181,7 +171,6 @@ Amplitude.prototype.runQueuedFunctions = function () {
181171
Amplitude.prototype.init = function(apiKey, opt_userId, opt_config, callback) {
182172
try {
183173
this.options.apiKey = apiKey;
184-
185174
if (opt_config) {
186175
if (opt_config.saveEvents !== undefined) {
187176
this.options.saveEvents = !!opt_config.saveEvents;
@@ -257,8 +246,18 @@ Amplitude.prototype.init = function(apiKey, opt_userId, opt_config, callback) {
257246
}
258247

259248
if (callback && type(callback) === 'function') {
260-
callback(this);
249+
callback();
250+
}
251+
};
252+
253+
Amplitude.prototype.runQueuedFunctions = function () {
254+
for (var i = 0; i < this._q.length; i++) {
255+
var fn = this[this._q[i][0]];
256+
if (fn && type(fn) === 'function') {
257+
fn.apply(this, this._q[i].slice(1));
258+
}
261259
}
260+
this._q = []; // clear function queue after running
262261
};
263262

264263
Amplitude.prototype._apiKeySet = function(methodName) {
@@ -919,6 +918,9 @@ Amplitude.prototype._mergeEventsAndIdentifys = function(numEvents) {
919918
};
920919
};
921920

921+
/**
922+
* @deprecated
923+
*/
922924
Amplitude.prototype.setGlobalUserProperties = Amplitude.prototype.setUserProperties;
923925

924926
Amplitude.prototype.__VERSION__ = version;

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: 21 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -17,19 +17,22 @@ var IDENTIFY_EVENT = '$identify';
1717
var API_VERSION = 2;
1818
var MAX_STRING_LENGTH = 1024;
1919
var LocalStorageKeys = {
20-
SESSION_ID: 'amplitude_sessionId',
21-
LAST_EVENT_TIME: 'amplitude_lastEventTime',
2220
LAST_EVENT_ID: 'amplitude_lastEventId',
21+
LAST_EVENT_TIME: 'amplitude_lastEventTime',
2322
LAST_IDENTIFY_ID: 'amplitude_lastIdentifyId',
2423
LAST_SEQUENCE_NUMBER: 'amplitude_lastSequenceNumber',
2524
REFERRER: 'amplitude_referrer',
25+
SESSION_ID: 'amplitude_sessionId',
2626

2727
// Used in cookie as well
2828
DEVICE_ID: 'amplitude_deviceId',
29-
USER_ID: 'amplitude_userId',
30-
OPT_OUT: 'amplitude_optOut'
29+
OPT_OUT: 'amplitude_optOut',
30+
USER_ID: 'amplitude_userId'
3131
};
3232

33+
/*
34+
* Amplitude API
35+
*/
3336
var Amplitude = function() {
3437
this._unsentEvents = [];
3538
this._unsentIdentifys = [];
@@ -50,16 +53,6 @@ Amplitude.prototype._updateScheduled = false;
5053

5154
Amplitude.prototype.Identify = Identify;
5255

53-
Amplitude.prototype.runQueuedFunctions = function () {
54-
for (var i = 0; i < this._q.length; i++) {
55-
var fn = this[this._q[i][0]];
56-
if (fn && type(fn) === 'function') {
57-
fn.apply(this, this._q[i].slice(1));
58-
}
59-
}
60-
this._q = []; // clear function queue after running
61-
};
62-
6356
/**
6457
* Initializes Amplitude.
6558
* apiKey The API Key for your app
@@ -72,7 +65,6 @@ Amplitude.prototype.runQueuedFunctions = function () {
7265
Amplitude.prototype.init = function(apiKey, opt_userId, opt_config, callback) {
7366
try {
7467
this.options.apiKey = apiKey;
75-
7668
if (opt_config) {
7769
if (opt_config.saveEvents !== undefined) {
7870
this.options.saveEvents = !!opt_config.saveEvents;
@@ -148,8 +140,18 @@ Amplitude.prototype.init = function(apiKey, opt_userId, opt_config, callback) {
148140
}
149141

150142
if (callback && type(callback) === 'function') {
151-
callback(this);
143+
callback();
144+
}
145+
};
146+
147+
Amplitude.prototype.runQueuedFunctions = function () {
148+
for (var i = 0; i < this._q.length; i++) {
149+
var fn = this[this._q[i][0]];
150+
if (fn && type(fn) === 'function') {
151+
fn.apply(this, this._q[i].slice(1));
152+
}
152153
}
154+
this._q = []; // clear function queue after running
153155
};
154156

155157
Amplitude.prototype._apiKeySet = function(methodName) {
@@ -810,6 +812,9 @@ Amplitude.prototype._mergeEventsAndIdentifys = function(numEvents) {
810812
};
811813
};
812814

815+
/**
816+
* @deprecated
817+
*/
813818
Amplitude.prototype.setGlobalUserProperties = Amplitude.prototype.setUserProperties;
814819

815820
Amplitude.prototype.__VERSION__ = version;

src/index.js

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,10 @@
11
/* jshint expr:true */
22

33
var Amplitude = require('./amplitude');
4+
45
var old = window.amplitude || {};
5-
var newInstance = new Amplitude();
6-
newInstance._q = old._q || [];
7-
for (var instance in old._iq) { // migrate each instance's queue
8-
if (old._iq.hasOwnProperty(instance)) {
9-
newInstance.getInstance(instance)._q = old._iq[instance]._q || [];
10-
}
11-
}
6+
var instance = new Amplitude();
7+
instance._q = old._q || [];
128

13-
module.exports = newInstance;
9+
// export the instance
10+
module.exports = instance;

test/amplitude.js

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,6 @@ describe('Amplitude', function() {
3333
localStorage.clear();
3434
sessionStorage.clear();
3535
cookie.remove(amplitude.options.cookieName);
36-
cookie.remove(amplitude.options.cookieName + keySuffix);
37-
cookie.remove(amplitude.options.cookieName + '_app1');
38-
cookie.remove(amplitude.options.cookieName + '_app2');
3936
cookie.reset();
4037
}
4138

@@ -745,6 +742,28 @@ describe('Amplitude', function() {
745742
assert.deepEqual(events[0].event_properties, {index: 4});
746743
});
747744

745+
it('should save events', function() {
746+
amplitude.init(apiKey, null, {saveEvents: true});
747+
amplitude.logEvent('Event', {index: 1});
748+
amplitude.logEvent('Event', {index: 2});
749+
amplitude.logEvent('Event', {index: 3});
750+
751+
var amplitude2 = new Amplitude();
752+
amplitude2.init(apiKey);
753+
assert.deepEqual(amplitude2._unsentEvents, amplitude._unsentEvents);
754+
});
755+
756+
it('should not save events', function() {
757+
amplitude.init(apiKey, null, {saveEvents: false});
758+
amplitude.logEvent('Event', {index: 1});
759+
amplitude.logEvent('Event', {index: 2});
760+
amplitude.logEvent('Event', {index: 3});
761+
762+
var amplitude2 = new Amplitude();
763+
amplitude2.init(apiKey);
764+
assert.deepEqual(amplitude2._unsentEvents, []);
765+
});
766+
748767
it('should limit events sent', function() {
749768
amplitude.init(apiKey, null, {uploadBatchSize: 10});
750769

0 commit comments

Comments
 (0)