Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
## Unreleased

* Add support for user properties operations (set, setOnce, add, unset).
* Fix bug to run queued functions after script element is loaded and set to window.

## 2.4.1 (September 21, 2015)

Expand Down
13 changes: 7 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,13 @@ Amplitude-Javascript
<script type="text/javascript">
(function(t,e){var n=t.amplitude||{};var r=e.createElement("script");r.type="text/javascript";
r.async=true;r.src="https://d24n15hnbwhuhn.cloudfront.net/libs/amplitude-2.4.1-min.gz.js";
var s=e.getElementsByTagName("script")[0];s.parentNode.insertBefore(r,s);var i=function(){
this._q=[];return this};function a(t){i.prototype[t]=function(){this._q.push([t].concat(Array.prototype.slice.call(arguments,0)));
return this}}var o=["add","set","setOnce","unset"];for(var c=0;c<o.length;c++){a(o[c]);
}n.Identify=i;n._q=[];function u(t){n[t]=function(){n._q.push([t].concat(Array.prototype.slice.call(arguments,0)));
}}var p=["init","logEvent","logRevenue","setUserId","setUserProperties","setOptOut","setVersionName","setDomain","setDeviceId","setGlobalUserProperties","identify"];
for(var l=0;l<p.length;l++){u(p[l])}t.amplitude=n})(window,document);
r.onload=function(){t.amplitude.runQueuedFunctions()};var s=e.getElementsByTagName("script")[0];
s.parentNode.insertBefore(r,s);var i=function(){this._q=[];return this};function o(t){
i.prototype[t]=function(){this._q.push([t].concat(Array.prototype.slice.call(arguments,0)));
return this}}var a=["add","set","setOnce","unset"];for(var u=0;u<a.length;u++){o(a[u]);
}n.Identify=i;n._q=[];function c(t){n[t]=function(){n._q.push([t].concat(Array.prototype.slice.call(arguments,0)));
}}var l=["init","logEvent","logRevenue","setUserId","setUserProperties","setOptOut","setVersionName","setDomain","setDeviceId","setGlobalUserProperties","identify"];
for(var p=0;p<l.length;p++){c(l[p])}t.amplitude=n})(window,document);

amplitude.init("YOUR_API_KEY_HERE");
</script>
Expand Down
13 changes: 7 additions & 6 deletions amplitude-snippet.min.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
(function(t,e){var n=t.amplitude||{};var r=e.createElement("script");r.type="text/javascript";
r.async=true;r.src="https://d24n15hnbwhuhn.cloudfront.net/libs/amplitude-2.4.1-min.gz.js";
var s=e.getElementsByTagName("script")[0];s.parentNode.insertBefore(r,s);var i=function(){
this._q=[];return this};function a(t){i.prototype[t]=function(){this._q.push([t].concat(Array.prototype.slice.call(arguments,0)));
return this}}var o=["add","set","setOnce","unset"];for(var c=0;c<o.length;c++){a(o[c]);
}n.Identify=i;n._q=[];function u(t){n[t]=function(){n._q.push([t].concat(Array.prototype.slice.call(arguments,0)));
}}var p=["init","logEvent","logRevenue","setUserId","setUserProperties","setOptOut","setVersionName","setDomain","setDeviceId","setGlobalUserProperties","identify"];
for(var l=0;l<p.length;l++){u(p[l])}t.amplitude=n})(window,document);
r.onload=function(){t.amplitude.runQueuedFunctions()};var s=e.getElementsByTagName("script")[0];
s.parentNode.insertBefore(r,s);var i=function(){this._q=[];return this};function o(t){
i.prototype[t]=function(){this._q.push([t].concat(Array.prototype.slice.call(arguments,0)));
return this}}var a=["add","set","setOnce","unset"];for(var u=0;u<a.length;u++){o(a[u]);
}n.Identify=i;n._q=[];function c(t){n[t]=function(){n._q.push([t].concat(Array.prototype.slice.call(arguments,0)));
}}var l=["init","logEvent","logRevenue","setUserId","setUserProperties","setOptOut","setVersionName","setDomain","setDeviceId","setGlobalUserProperties","identify"];
for(var p=0;p<l.length;p++){c(l[p])}t.amplitude=n})(window,document);
19 changes: 12 additions & 7 deletions amplitude.js
Original file line number Diff line number Diff line change
Expand Up @@ -96,14 +96,8 @@
var Amplitude = require('./amplitude');

var old = window.amplitude || {};
var q = old._q || [];
var instance = new Amplitude();

// Apply the queued commands
for (var i = 0; i < q.length; i++) {
var fn = instance[q[i][0]];
fn && fn.apply(instance, q[i].slice(1));
}
instance._q = old._q || [];

// export the instance
module.exports = instance;
Expand Down Expand Up @@ -165,6 +159,7 @@ var Amplitude = function() {
this._unsentIdentifys = [];
this._ua = new UAParser(navigator.userAgent).getResult();
this.options = object.merge({}, DEFAULT_OPTIONS);
this._q = []; // queue for proxied functions before script load
};

Amplitude.prototype._eventId = 0;
Expand Down Expand Up @@ -265,6 +260,16 @@ Amplitude.prototype.init = function(apiKey, opt_userId, opt_config, callback) {
}
};

Amplitude.prototype.runQueuedFunctions = function () {
for (var i = 0; i < this._q.length; i++) {
var fn = this[this._q[i][0]];
if (fn && type(fn) === 'function') {
fn.apply(this, this._q[i].slice(1));
}
}
this._q = []; // clear function queue after running
};

Amplitude.prototype._loadSavedUnsentEvents = function(unsentKey, queue) {
var savedUnsentEventsString = localStorage.getItem(unsentKey);
if (savedUnsentEventsString) {
Expand Down
4 changes: 2 additions & 2 deletions amplitude.min.js

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions src/amplitude-snippet.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
as.type = 'text/javascript';
as.async = true;
as.src = 'https://d24n15hnbwhuhn.cloudfront.net/libs/amplitude-2.4.1-min.gz.js';
as.onload = function() { window.amplitude.runQueuedFunctions(); };
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what's the browser compatibility of this function?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Chrome and Firefox definitely support it. It's part of the HTML 4.01 standard, and seeing as how IE9 has HTML5, it should be okay: http://www.w3schools.com/tags/ev_onload.asp

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok confirmed IE9 support for onload via my own virtualbox running IE9

var s = document.getElementsByTagName('script')[0];
s.parentNode.insertBefore(as, s);
var Identify = function() { this._q = []; return this; };
Expand Down
11 changes: 11 additions & 0 deletions src/amplitude.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ var Amplitude = function() {
this._unsentIdentifys = [];
this._ua = new UAParser(navigator.userAgent).getResult();
this.options = object.merge({}, DEFAULT_OPTIONS);
this._q = []; // queue for proxied functions before script load
};

Amplitude.prototype._eventId = 0;
Expand Down Expand Up @@ -153,6 +154,16 @@ Amplitude.prototype.init = function(apiKey, opt_userId, opt_config, callback) {
}
};

Amplitude.prototype.runQueuedFunctions = function () {
for (var i = 0; i < this._q.length; i++) {
var fn = this[this._q[i][0]];
if (fn && type(fn) === 'function') {
fn.apply(this, this._q[i].slice(1));
}
}
this._q = []; // clear function queue after running
};

Amplitude.prototype._loadSavedUnsentEvents = function(unsentKey, queue) {
var savedUnsentEventsString = localStorage.getItem(unsentKey);
if (savedUnsentEventsString) {
Expand Down
8 changes: 1 addition & 7 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,8 @@
var Amplitude = require('./amplitude');

var old = window.amplitude || {};
var q = old._q || [];
var instance = new Amplitude();

// Apply the queued commands
for (var i = 0; i < q.length; i++) {
var fn = instance[q[i][0]];
fn && fn.apply(instance, q[i].slice(1));
}
instance._q = old._q || [];

// export the instance
module.exports = instance;
33 changes: 33 additions & 0 deletions test/amplitude.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,39 @@ describe('Amplitude', function() {
});
});

describe('runQueuedFunctions', function() {
beforeEach(function() {
amplitude.init(apiKey);
});

afterEach(function() {
reset();
});

it('should run queued functions', function() {
assert.equal(amplitude._unsentCount(), 0);
assert.lengthOf(server.requests, 0);
var userId = 'testUserId'
var eventType = 'test_event'
var functions = [
['setUserId', userId],
['logEvent', eventType]
];
amplitude._q = functions;
assert.lengthOf(amplitude._q, 2);
amplitude.runQueuedFunctions();

assert.equal(amplitude.options.userId, userId);
assert.equal(amplitude._unsentCount(), 1);
assert.lengthOf(server.requests, 1);
var events = JSON.parse(querystring.parse(server.requests[0].requestBody).e);
assert.lengthOf(events, 1);
assert.equal(events[0].event_type, eventType);

assert.lengthOf(amplitude._q, 0);
});
});

describe('setUserProperties', function() {
beforeEach(function() {
amplitude.init(apiKey);
Expand Down
5 changes: 4 additions & 1 deletion test/browser/amplitudejs.html
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
as.type = 'text/javascript';
as.async = true;
as.src = '/amplitude.js';
as.onload = function() { window.amplitude.runQueuedFunctions(); };
var s = document.getElementsByTagName('script')[0];
s.parentNode.insertBefore(as, s);
var Identify = function() { this._q = []; return this; };
Expand Down Expand Up @@ -69,7 +70,9 @@
}
</script>
<script>
amplitude.init('a2dbce0e18dfe5f8e74493843ff5c053');
amplitude.init('a2dbce0e18dfe5f8e74493843ff5c053', null, null, function() {
alert(amplitude.options.deviceId);
});
amplitude.setVersionName('Web');
amplitude.identify(new amplitude.Identify().add('photoCount', 1));
amplitude.identify(new amplitude.Identify().add('photoCount', 1).set('gender', 'male').unset('karma'));
Expand Down