Skip to content

Commit de59453

Browse files
committed
updated Readme and refactored some code
1 parent bfa71f7 commit de59453

File tree

5 files changed

+71
-58
lines changed

5 files changed

+71
-58
lines changed

README.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -63,24 +63,24 @@ The SDK supports the operations set, setOnce, unset, and add on individual user
6363
1. `set`: this sets the value of a user property.
6464

6565
```javascript
66-
var identify = new amplitude.Identify().set('city', 'Boston, MA').set('country', 'United States');
66+
var identify = new amplitude.Identify().set('gender', 'female').set('age', 20);
6767
amplitude.identify(identify);
6868
```
6969

70-
2. `setOnce`: this sets the value of a user property only once. Subsequent `setOnce` operations on that user property will be ignored. In the following example, the city will be set once to `Boston, MA`, and the following setOnce to `San Francisco, CA` will be ignored:
70+
2. `setOnce`: this sets the value of a user property only once. Subsequent `setOnce` operations on that user property will be ignored. In the following example, `sign_up_date` will be set once to `08/24/2015`, and the following setOnce to `09/14/2015` will be ignored:
7171

7272
```javascript
73-
var identify = new amplitude.Identify().setOnce('city', 'Boston, MA');
73+
var identify = new amplitude.Identify().setOnce('sign_up_date', '08/24/2015');
7474
amplitude.identify(identify);
7575
76-
var identify = new amplitude.Identify().setOnce('city', 'San Francisco, CA');
76+
var identify = new amplitude.Identify().setOnce('sign_up_date', '09/14/2015');
7777
amplitude.identify(identify);
7878
```
7979

8080
3. `unset`: this will unset and remove a user property.
8181

8282
```javascript
83-
var identify = new amplitude.Identify().unset('city').unset('country');
83+
var identify = new amplitude.Identify().unset('gender').unset('age');
8484
amplitude.identify(identify);
8585
```
8686

amplitude.js

Lines changed: 23 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -229,22 +229,8 @@ Amplitude.prototype.init = function(apiKey, opt_userId, opt_config) {
229229
//opt_userId !== undefined && opt_userId !== null && log('initialized with userId=' + opt_userId);
230230

231231
if (this.options.saveEvents) {
232-
var savedUnsentEventsString = localStorage.getItem(this.options.unsentKey);
233-
if (savedUnsentEventsString) {
234-
try {
235-
this._unsentEvents = JSON.parse(savedUnsentEventsString);
236-
} catch (e) {
237-
//log(e);
238-
}
239-
}
240-
var savedUnsentIdentifysString = localStorage.getItem(this.options.unsentIdentifyKey);
241-
if (savedUnsentIdentifysString) {
242-
try {
243-
this._unsentIdentifys = JSON.parse(savedUnsentIdentifysString);
244-
} catch (e) {
245-
//log(e);
246-
}
247-
}
232+
this._loadSavedUnsentEvents(this.options.unsentKey, '_unsentEvents');
233+
this._loadSavedUnsentEvents(this.options.unsentIdentifyKey, '_unsentIdentifys');
248234
}
249235

250236
this._sendEventsIfReady();
@@ -270,6 +256,17 @@ Amplitude.prototype.init = function(apiKey, opt_userId, opt_config) {
270256
}
271257
};
272258

259+
Amplitude.prototype._loadSavedUnsentEvents = function(unsentKey, queue) {
260+
var savedUnsentEventsString = localStorage.getItem(unsentKey);
261+
if (savedUnsentEventsString) {
262+
try {
263+
this[queue] = JSON.parse(savedUnsentEventsString);
264+
} catch (e) {
265+
//log(e);
266+
}
267+
}
268+
};
269+
273270
Amplitude.prototype.isNewSession = function() {
274271
return this._newSession;
275272
};
@@ -558,17 +555,10 @@ Amplitude.prototype._logEvent = function(eventType, eventProperties, apiProperti
558555

559556
if (eventType === IDENTIFY_EVENT) {
560557
this._unsentIdentifys.push(event);
561-
if (this._unsentIdentifys.length > this.options.savedMaxCount) {
562-
this._unsentIdentifys.splice(0, this._unsentIdentifys.length - this.options.savedMaxCount);
563-
}
558+
this._limitEventsQueued(this._unsentIdentifys);
564559
} else {
565560
this._unsentEvents.push(event);
566-
567-
// Remove old events from the beginning of the array if too many
568-
// have accumulated. Don't want to kill memory. Default is 1000 events.
569-
if (this._unsentEvents.length > this.options.savedMaxCount) {
570-
this._unsentEvents.splice(0, this._unsentEvents.length - this.options.savedMaxCount);
571-
}
561+
this._limitEventsQueued(this._unsentEvents);
572562
}
573563

574564
if (this.options.saveEvents) {
@@ -585,6 +575,14 @@ Amplitude.prototype._logEvent = function(eventType, eventProperties, apiProperti
585575
}
586576
};
587577

578+
// Remove old events from the beginning of the array if too many
579+
// have accumulated. Don't want to kill memory. Default is 1000 events.
580+
Amplitude.prototype._limitEventsQueued = function(queue) {
581+
if (queue.length > this.options.savedMaxCount) {
582+
queue.splice(0, queue.length - this.options.savedMaxCount);
583+
}
584+
};
585+
588586
Amplitude.prototype.logEvent = function(eventType, eventProperties, callback) {
589587
return this._logEvent(eventType, eventProperties, null, null, callback);
590588
};

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: 23 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -117,22 +117,8 @@ Amplitude.prototype.init = function(apiKey, opt_userId, opt_config) {
117117
//opt_userId !== undefined && opt_userId !== null && log('initialized with userId=' + opt_userId);
118118

119119
if (this.options.saveEvents) {
120-
var savedUnsentEventsString = localStorage.getItem(this.options.unsentKey);
121-
if (savedUnsentEventsString) {
122-
try {
123-
this._unsentEvents = JSON.parse(savedUnsentEventsString);
124-
} catch (e) {
125-
//log(e);
126-
}
127-
}
128-
var savedUnsentIdentifysString = localStorage.getItem(this.options.unsentIdentifyKey);
129-
if (savedUnsentIdentifysString) {
130-
try {
131-
this._unsentIdentifys = JSON.parse(savedUnsentIdentifysString);
132-
} catch (e) {
133-
//log(e);
134-
}
135-
}
120+
this._loadSavedUnsentEvents(this.options.unsentKey, '_unsentEvents');
121+
this._loadSavedUnsentEvents(this.options.unsentIdentifyKey, '_unsentIdentifys');
136122
}
137123

138124
this._sendEventsIfReady();
@@ -158,6 +144,17 @@ Amplitude.prototype.init = function(apiKey, opt_userId, opt_config) {
158144
}
159145
};
160146

147+
Amplitude.prototype._loadSavedUnsentEvents = function(unsentKey, queue) {
148+
var savedUnsentEventsString = localStorage.getItem(unsentKey);
149+
if (savedUnsentEventsString) {
150+
try {
151+
this[queue] = JSON.parse(savedUnsentEventsString);
152+
} catch (e) {
153+
//log(e);
154+
}
155+
}
156+
};
157+
161158
Amplitude.prototype.isNewSession = function() {
162159
return this._newSession;
163160
};
@@ -446,17 +443,10 @@ Amplitude.prototype._logEvent = function(eventType, eventProperties, apiProperti
446443

447444
if (eventType === IDENTIFY_EVENT) {
448445
this._unsentIdentifys.push(event);
449-
if (this._unsentIdentifys.length > this.options.savedMaxCount) {
450-
this._unsentIdentifys.splice(0, this._unsentIdentifys.length - this.options.savedMaxCount);
451-
}
446+
this._limitEventsQueued(this._unsentIdentifys);
452447
} else {
453448
this._unsentEvents.push(event);
454-
455-
// Remove old events from the beginning of the array if too many
456-
// have accumulated. Don't want to kill memory. Default is 1000 events.
457-
if (this._unsentEvents.length > this.options.savedMaxCount) {
458-
this._unsentEvents.splice(0, this._unsentEvents.length - this.options.savedMaxCount);
459-
}
449+
this._limitEventsQueued(this._unsentEvents);
460450
}
461451

462452
if (this.options.saveEvents) {
@@ -473,6 +463,14 @@ Amplitude.prototype._logEvent = function(eventType, eventProperties, apiProperti
473463
}
474464
};
475465

466+
// Remove old events from the beginning of the array if too many
467+
// have accumulated. Don't want to kill memory. Default is 1000 events.
468+
Amplitude.prototype._limitEventsQueued = function(queue) {
469+
if (queue.length > this.options.savedMaxCount) {
470+
queue.splice(0, queue.length - this.options.savedMaxCount);
471+
}
472+
};
473+
476474
Amplitude.prototype.logEvent = function(eventType, eventProperties, callback) {
477475
return this._logEvent(eventType, eventProperties, null, null, callback);
478476
};

test/amplitude.js

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -954,6 +954,23 @@ describe('Amplitude', function() {
954954
amplitude2.init(apiKey);
955955
assert.strictEqual(amplitude2.options.optOut, true);
956956
});
957+
958+
it('should limit identify events queued', function() {
959+
amplitude.init(apiKey, null, {savedMaxCount: 10});
960+
961+
amplitude._sending = true;
962+
for (var i = 0; i < 15; i++) {
963+
amplitude.identify(new Identify().add('test', i));
964+
}
965+
amplitude._sending = false;
966+
967+
amplitude.identify(new Identify().add('test', 100));
968+
assert.lengthOf(server.requests, 1);
969+
var events = JSON.parse(querystring.parse(server.requests[0].requestBody).e);
970+
assert.lengthOf(events, 10);
971+
assert.deepEqual(events[0].user_properties, {$add: {'test': 6}});
972+
assert.deepEqual(events[9].user_properties, {$add: {'test': 100}});
973+
});
957974
});
958975

959976
describe('gatherUtm', function() {
@@ -1256,7 +1273,7 @@ describe('Amplitude', function() {
12561273
assert.lengthOf(eventProperties[1][1], 4);
12571274
});
12581275

1259-
it('should handle arrays nested inside dictionaryes', function() {
1276+
it('should handle arrays nested inside dictionaries', function() {
12601277
var test = [longString, 'test'];
12611278
var eventProperties = amplitude._truncate({'name': test});
12621279
assert.lengthOf(Object.keys(eventProperties), 1);

0 commit comments

Comments
 (0)