Skip to content

Commit c4a0a0c

Browse files
committed
validate user properties
1 parent 7c987e7 commit c4a0a0c

File tree

11 files changed

+139
-74
lines changed

11 files changed

+139
-74
lines changed

CHANGELOG.md

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

33
* Add tracking of each user's initial_utm parameters (which is captured as a set once operation). Utm parameters are now sent only once per user session.
4+
* Add documentation for SDK functions. You can take a look [here](https://rawgit.com/amplitude/Amplitude-Javascript/defensive_cleanup/documentation/Amplitude.html). A link has also been added to the Readme.
45

56
### 2.10.0 (March 30, 2016)
67

amplitude.js

Lines changed: 26 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -147,20 +147,6 @@ var Amplitude = function Amplitude() {
147147

148148
Amplitude.prototype.Identify = Identify;
149149

150-
/**
151-
* Run functions queued up by proxy loading snippet
152-
* @private
153-
*/
154-
Amplitude.prototype.runQueuedFunctions = function () {
155-
for (var i = 0; i < this._q.length; i++) {
156-
var fn = this[this._q[i][0]];
157-
if (type(fn) === 'function') {
158-
fn.apply(this, this._q[i].slice(1));
159-
}
160-
}
161-
this._q = []; // clear function queue after running
162-
};
163-
164150
/**
165151
* Initializes the Amplitude Javascript SDK with your apiKey and any optional configurations.
166152
* This is required before any other methods can be called.
@@ -173,9 +159,13 @@ Amplitude.prototype.runQueuedFunctions = function () {
173159
* @example amplitude.init('API_KEY', 'USER_ID', {includeReferrer: true, includeUtm: true}, function() { alert('init complete'); });
174160
*/
175161
Amplitude.prototype.init = function init(apiKey, opt_userId, opt_config, opt_callback) {
176-
try {
177-
this.options.apiKey = (type(apiKey) === 'string' && !utils.isEmptyString(apiKey) && apiKey) || null;
162+
if (type(apiKey) !== 'string' || utils.isEmptyString(apiKey)) {
163+
utils.log('Invalid apiKey. Please re-initialize with a valid apiKey');
164+
return;
165+
}
178166

167+
try {
168+
this.options.apiKey = apiKey;
179169
_parseConfig(this.options, opt_config);
180170
this.cookieStorage.options({
181171
expirationDays: this.options.cookieExpiration,
@@ -211,6 +201,12 @@ Amplitude.prototype.init = function init(apiKey, opt_userId, opt_config, opt_cal
211201
this._unsentEvents[i].event_properties = utils.validateProperties(eventProperties);
212202
}
213203

204+
// validate user properties for unsent identifys
205+
for (var j = 0; j < this._unsentIdentifys.length; j++) {
206+
var userProperties = this._unsentIdentifys[j].user_properties;
207+
this._unsentIdentifys[j].user_properties = utils.validateProperties(userProperties);
208+
}
209+
214210
this._sendEventsIfReady(); // try sending unsent events
215211
}
216212

@@ -263,6 +259,20 @@ var _parseConfig = function _parseConfig(options, config) {
263259
}
264260
};
265261

262+
/**
263+
* Run functions queued up by proxy loading snippet
264+
* @private
265+
*/
266+
Amplitude.prototype.runQueuedFunctions = function () {
267+
for (var i = 0; i < this._q.length; i++) {
268+
var fn = this[this._q[i][0]];
269+
if (type(fn) === 'function') {
270+
fn.apply(this, this._q[i].slice(1));
271+
}
272+
}
273+
this._q = []; // clear function queue after running
274+
};
275+
266276
/**
267277
* Check that the apiKey is set before calling a function. Logs a warning message if not set.
268278
* @private
@@ -2189,7 +2199,6 @@ var sessionStorageEnabled = function sessionStorageEnabled() {
21892199
};
21902200

21912201

2192-
21932202
// truncate string values in event and user properties so that request size does not get too large
21942203
var truncate = function truncate(value) {
21952204
if (type(value) === 'array') {

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.

documentation/Amplitude.html

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,7 @@ <h4 class="name" id="__VERSION__"><span class="type-signature"></span>__VERSION_
184184

185185
<dt class="tag-source">Source:</dt>
186186
<dd class="tag-source"><ul class="dummy"><li>
187-
<a href="amplitude.js.html">amplitude.js</a>, <a href="amplitude.js.html#line1010">line 1010</a>
187+
<a href="amplitude.js.html">amplitude.js</a>, <a href="amplitude.js.html#line1020">line 1020</a>
188188
</li></ul></dd>
189189

190190

@@ -267,7 +267,7 @@ <h4 class="name" id="clearUserProperties"><span class="type-signature"></span>cl
267267

268268
<dt class="tag-source">Source:</dt>
269269
<dd class="tag-source"><ul class="dummy"><li>
270-
<a href="amplitude.js.html">amplitude.js</a>, <a href="amplitude.js.html#line622">line 622</a>
270+
<a href="amplitude.js.html">amplitude.js</a>, <a href="amplitude.js.html#line632">line 632</a>
271271
</li></ul></dd>
272272

273273

@@ -354,7 +354,7 @@ <h4 class="name" id="getSessionId"><span class="type-signature"></span>getSessio
354354

355355
<dt class="tag-source">Source:</dt>
356356
<dd class="tag-source"><ul class="dummy"><li>
357-
<a href="amplitude.js.html">amplitude.js</a>, <a href="amplitude.js.html#line208">line 208</a>
357+
<a href="amplitude.js.html">amplitude.js</a>, <a href="amplitude.js.html#line218">line 218</a>
358358
</li></ul></dd>
359359

360360

@@ -533,7 +533,7 @@ <h5>Parameters:</h5>
533533

534534
<dt class="tag-source">Source:</dt>
535535
<dd class="tag-source"><ul class="dummy"><li>
536-
<a href="amplitude.js.html">amplitude.js</a>, <a href="amplitude.js.html#line642">line 642</a>
536+
<a href="amplitude.js.html">amplitude.js</a>, <a href="amplitude.js.html#line652">line 652</a>
537537
</li></ul></dd>
538538

539539

@@ -741,7 +741,7 @@ <h5>Parameters:</h5>
741741

742742
<dt class="tag-source">Source:</dt>
743743
<dd class="tag-source"><ul class="dummy"><li>
744-
<a href="amplitude.js.html">amplitude.js</a>, <a href="amplitude.js.html#line69">line 69</a>
744+
<a href="amplitude.js.html">amplitude.js</a>, <a href="amplitude.js.html#line55">line 55</a>
745745
</li></ul></dd>
746746

747747

@@ -828,7 +828,7 @@ <h4 class="name" id="isNewSession"><span class="type-signature"></span>isNewSess
828828

829829
<dt class="tag-source">Source:</dt>
830830
<dd class="tag-source"><ul class="dummy"><li>
831-
<a href="amplitude.js.html">amplitude.js</a>, <a href="amplitude.js.html#line199">line 199</a>
831+
<a href="amplitude.js.html">amplitude.js</a>, <a href="amplitude.js.html#line209">line 209</a>
832832
</li></ul></dd>
833833

834834

@@ -1028,7 +1028,7 @@ <h5>Parameters:</h5>
10281028

10291029
<dt class="tag-source">Source:</dt>
10301030
<dd class="tag-source"><ul class="dummy"><li>
1031-
<a href="amplitude.js.html">amplitude.js</a>, <a href="amplitude.js.html#line790">line 790</a>
1031+
<a href="amplitude.js.html">amplitude.js</a>, <a href="amplitude.js.html#line800">line 800</a>
10321032
</li></ul></dd>
10331033

10341034

@@ -1210,7 +1210,7 @@ <h5>Parameters:</h5>
12101210

12111211
<dt class="tag-source">Source:</dt>
12121212
<dd class="tag-source"><ul class="dummy"><li>
1213-
<a href="amplitude.js.html">amplitude.js</a>, <a href="amplitude.js.html#line817">line 817</a>
1213+
<a href="amplitude.js.html">amplitude.js</a>, <a href="amplitude.js.html#line827">line 827</a>
12141214
</li></ul></dd>
12151215

12161216

@@ -1298,7 +1298,7 @@ <h4 class="name" id="saveEvents"><span class="type-signature"></span>saveEvents<
12981298

12991299
<dt class="tag-source">Source:</dt>
13001300
<dd class="tag-source"><ul class="dummy"><li>
1301-
<a href="amplitude.js.html">amplitude.js</a>, <a href="amplitude.js.html#line496">line 496</a>
1301+
<a href="amplitude.js.html">amplitude.js</a>, <a href="amplitude.js.html#line506">line 506</a>
13021302
</li></ul></dd>
13031303

13041304

@@ -1431,7 +1431,7 @@ <h5>Parameters:</h5>
14311431

14321432
<dt class="tag-source">Source:</dt>
14331433
<dd class="tag-source"><ul class="dummy"><li>
1434-
<a href="amplitude.js.html">amplitude.js</a>, <a href="amplitude.js.html#line867">line 867</a>
1434+
<a href="amplitude.js.html">amplitude.js</a>, <a href="amplitude.js.html#line877">line 877</a>
14351435
</li></ul></dd>
14361436

14371437

@@ -1564,7 +1564,7 @@ <h5>Parameters:</h5>
15641564

15651565
<dt class="tag-source">Source:</dt>
15661566
<dd class="tag-source"><ul class="dummy"><li>
1567-
<a href="amplitude.js.html">amplitude.js</a>, <a href="amplitude.js.html#line579">line 579</a>
1567+
<a href="amplitude.js.html">amplitude.js</a>, <a href="amplitude.js.html#line589">line 589</a>
15681568
</li></ul></dd>
15691569

15701570

@@ -1700,7 +1700,7 @@ <h5>Parameters:</h5>
17001700

17011701
<dt class="tag-source">Source:</dt>
17021702
<dd class="tag-source"><ul class="dummy"><li>
1703-
<a href="amplitude.js.html">amplitude.js</a>, <a href="amplitude.js.html#line516">line 516</a>
1703+
<a href="amplitude.js.html">amplitude.js</a>, <a href="amplitude.js.html#line526">line 526</a>
17041704
</li></ul></dd>
17051705

17061706

@@ -1789,7 +1789,7 @@ <h4 class="name" id="setGlobalUserProperties"><span class="type-signature"></spa
17891789

17901790
<dt class="tag-source">Source:</dt>
17911791
<dd class="tag-source"><ul class="dummy"><li>
1792-
<a href="amplitude.js.html">amplitude.js</a>, <a href="amplitude.js.html#line1000">line 1000</a>
1792+
<a href="amplitude.js.html">amplitude.js</a>, <a href="amplitude.js.html#line1010">line 1010</a>
17931793
</li></ul></dd>
17941794

17951795

@@ -1920,7 +1920,7 @@ <h5>Parameters:</h5>
19201920

19211921
<dt class="tag-source">Source:</dt>
19221922
<dd class="tag-source"><ul class="dummy"><li>
1923-
<a href="amplitude.js.html">amplitude.js</a>, <a href="amplitude.js.html#line558">line 558</a>
1923+
<a href="amplitude.js.html">amplitude.js</a>, <a href="amplitude.js.html#line568">line 568</a>
19241924
</li></ul></dd>
19251925

19261926

@@ -2051,7 +2051,7 @@ <h5>Parameters:</h5>
20512051

20522052
<dt class="tag-source">Source:</dt>
20532053
<dd class="tag-source"><ul class="dummy"><li>
2054-
<a href="amplitude.js.html">amplitude.js</a>, <a href="amplitude.js.html#line539">line 539</a>
2054+
<a href="amplitude.js.html">amplitude.js</a>, <a href="amplitude.js.html#line549">line 549</a>
20552055
</li></ul></dd>
20562056

20572057

@@ -2211,7 +2211,7 @@ <h5>Parameters:</h5>
22112211

22122212
<dt class="tag-source">Source:</dt>
22132213
<dd class="tag-source"><ul class="dummy"><li>
2214-
<a href="amplitude.js.html">amplitude.js</a>, <a href="amplitude.js.html#line602">line 602</a>
2214+
<a href="amplitude.js.html">amplitude.js</a>, <a href="amplitude.js.html#line612">line 612</a>
22152215
</li></ul></dd>
22162216

22172217

@@ -2347,7 +2347,7 @@ <h5>Parameters:</h5>
23472347

23482348
<dt class="tag-source">Source:</dt>
23492349
<dd class="tag-source"><ul class="dummy"><li>
2350-
<a href="amplitude.js.html">amplitude.js</a>, <a href="amplitude.js.html#line682">line 682</a>
2350+
<a href="amplitude.js.html">amplitude.js</a>, <a href="amplitude.js.html#line692">line 692</a>
23512351
</li></ul></dd>
23522352

23532353

@@ -2400,7 +2400,7 @@ <h2><a href="index.html">Home</a></h2><h3>Classes</h3><ul><li><a href="Amplitude
24002400
<br class="clear">
24012401

24022402
<footer>
2403-
Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 3.4.0</a> on Fri Apr 01 2016 17:48:30 GMT-0700 (PDT)
2403+
Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 3.4.0</a> on Fri Apr 01 2016 18:25:31 GMT-0700 (PDT)
24042404
</footer>
24052405

24062406
<script> prettyPrint(); </script>

documentation/Identify.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1295,7 +1295,7 @@ <h2><a href="index.html">Home</a></h2><h3>Classes</h3><ul><li><a href="Amplitude
12951295
<br class="clear">
12961296

12971297
<footer>
1298-
Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 3.4.0</a> on Fri Apr 01 2016 17:48:30 GMT-0700 (PDT)
1298+
Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 3.4.0</a> on Fri Apr 01 2016 18:25:31 GMT-0700 (PDT)
12991299
</footer>
13001300

13011301
<script> prettyPrint(); </script>

documentation/amplitude.js.html

Lines changed: 27 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -69,20 +69,6 @@ <h1 class="page-title">Source: amplitude.js</h1>
6969

7070
Amplitude.prototype.Identify = Identify;
7171

72-
/**
73-
* Run functions queued up by proxy loading snippet
74-
* @private
75-
*/
76-
Amplitude.prototype.runQueuedFunctions = function () {
77-
for (var i = 0; i &lt; this._q.length; i++) {
78-
var fn = this[this._q[i][0]];
79-
if (type(fn) === 'function') {
80-
fn.apply(this, this._q[i].slice(1));
81-
}
82-
}
83-
this._q = []; // clear function queue after running
84-
};
85-
8672
/**
8773
* Initializes the Amplitude Javascript SDK with your apiKey and any optional configurations.
8874
* This is required before any other methods can be called.
@@ -95,9 +81,13 @@ <h1 class="page-title">Source: amplitude.js</h1>
9581
* @example amplitude.init('API_KEY', 'USER_ID', {includeReferrer: true, includeUtm: true}, function() { alert('init complete'); });
9682
*/
9783
Amplitude.prototype.init = function init(apiKey, opt_userId, opt_config, opt_callback) {
98-
try {
99-
this.options.apiKey = (type(apiKey) === 'string' &amp;&amp; !utils.isEmptyString(apiKey) &amp;&amp; apiKey) || null;
84+
if (type(apiKey) !== 'string' || utils.isEmptyString(apiKey)) {
85+
utils.log('Invalid apiKey. Please re-initialize with a valid apiKey');
86+
return;
87+
}
10088

89+
try {
90+
this.options.apiKey = apiKey;
10191
_parseConfig(this.options, opt_config);
10292
this.cookieStorage.options({
10393
expirationDays: this.options.cookieExpiration,
@@ -133,6 +123,12 @@ <h1 class="page-title">Source: amplitude.js</h1>
133123
this._unsentEvents[i].event_properties = utils.validateProperties(eventProperties);
134124
}
135125

126+
// validate user properties for unsent identifys
127+
for (var j = 0; j &lt; this._unsentIdentifys.length; j++) {
128+
var userProperties = this._unsentIdentifys[j].user_properties;
129+
this._unsentIdentifys[j].user_properties = utils.validateProperties(userProperties);
130+
}
131+
136132
this._sendEventsIfReady(); // try sending unsent events
137133
}
138134

@@ -185,6 +181,20 @@ <h1 class="page-title">Source: amplitude.js</h1>
185181
}
186182
};
187183

184+
/**
185+
* Run functions queued up by proxy loading snippet
186+
* @private
187+
*/
188+
Amplitude.prototype.runQueuedFunctions = function () {
189+
for (var i = 0; i &lt; this._q.length; i++) {
190+
var fn = this[this._q[i][0]];
191+
if (type(fn) === 'function') {
192+
fn.apply(this, this._q[i].slice(1));
193+
}
194+
}
195+
this._q = []; // clear function queue after running
196+
};
197+
188198
/**
189199
* Check that the apiKey is set before calling a function. Logs a warning message if not set.
190200
* @private
@@ -1054,7 +1064,7 @@ <h2><a href="index.html">Home</a></h2><h3>Classes</h3><ul><li><a href="Amplitude
10541064
<br class="clear">
10551065

10561066
<footer>
1057-
Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 3.4.0</a> on Fri Apr 01 2016 17:48:30 GMT-0700 (PDT)
1067+
Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 3.4.0</a> on Fri Apr 01 2016 18:25:31 GMT-0700 (PDT)
10581068
</footer>
10591069

10601070
<script> prettyPrint(); </script>

documentation/identify.js.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -226,7 +226,7 @@ <h2><a href="index.html">Home</a></h2><h3>Classes</h3><ul><li><a href="Amplitude
226226
<br class="clear">
227227

228228
<footer>
229-
Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 3.4.0</a> on Fri Apr 01 2016 17:48:30 GMT-0700 (PDT)
229+
Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 3.4.0</a> on Fri Apr 01 2016 18:25:31 GMT-0700 (PDT)
230230
</footer>
231231

232232
<script> prettyPrint(); </script>

documentation/index.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ <h2><a href="index.html">Home</a></h2><h3>Classes</h3><ul><li><a href="Amplitude
5656
<br class="clear">
5757

5858
<footer>
59-
Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 3.4.0</a> on Fri Apr 01 2016 17:48:30 GMT-0700 (PDT)
59+
Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 3.4.0</a> on Fri Apr 01 2016 18:25:31 GMT-0700 (PDT)
6060
</footer>
6161

6262
<script> prettyPrint(); </script>

0 commit comments

Comments
 (0)