Skip to content

Commit c2db52e

Browse files
committed
Merge pull request #31 from amplitude/fix_init_proxy_queue
Run queued functions after script is loaded and set to window
2 parents 74a9bc4 + df036bb commit c2db52e

File tree

10 files changed

+79
-29
lines changed

10 files changed

+79
-29
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 support for user properties operations (set, setOnce, add, unset).
4+
* Fix bug to run queued functions after script element is loaded and set to window.
45

56
## 2.4.1 (September 21, 2015)
67

README.md

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

2021
amplitude.init("YOUR_API_KEY_HERE");
2122
</script>

amplitude-snippet.min.js

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
(function(t,e){var n=t.amplitude||{};var r=e.createElement("script");r.type="text/javascript";
22
r.async=true;r.src="https://d24n15hnbwhuhn.cloudfront.net/libs/amplitude-2.4.1-min.gz.js";
3-
var s=e.getElementsByTagName("script")[0];s.parentNode.insertBefore(r,s);var i=function(){
4-
this._q=[];return this};function a(t){i.prototype[t]=function(){this._q.push([t].concat(Array.prototype.slice.call(arguments,0)));
5-
return this}}var o=["add","set","setOnce","unset"];for(var c=0;c<o.length;c++){a(o[c]);
6-
}n.Identify=i;n._q=[];function u(t){n[t]=function(){n._q.push([t].concat(Array.prototype.slice.call(arguments,0)));
7-
}}var p=["init","logEvent","logRevenue","setUserId","setUserProperties","setOptOut","setVersionName","setDomain","setDeviceId","setGlobalUserProperties","identify"];
8-
for(var l=0;l<p.length;l++){u(p[l])}t.amplitude=n})(window,document);
3+
r.onload=function(){t.amplitude.runQueuedFunctions()};var s=e.getElementsByTagName("script")[0];
4+
s.parentNode.insertBefore(r,s);var i=function(){this._q=[];return this};function o(t){
5+
i.prototype[t]=function(){this._q.push([t].concat(Array.prototype.slice.call(arguments,0)));
6+
return this}}var a=["add","set","setOnce","unset"];for(var u=0;u<a.length;u++){o(a[u]);
7+
}n.Identify=i;n._q=[];function c(t){n[t]=function(){n._q.push([t].concat(Array.prototype.slice.call(arguments,0)));
8+
}}var l=["init","logEvent","logRevenue","setUserId","setUserProperties","setOptOut","setVersionName","setDomain","setDeviceId","setGlobalUserProperties","identify"];
9+
for(var p=0;p<l.length;p++){c(l[p])}t.amplitude=n})(window,document);

amplitude.js

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -96,14 +96,8 @@
9696
var Amplitude = require('./amplitude');
9797

9898
var old = window.amplitude || {};
99-
var q = old._q || [];
10099
var instance = new Amplitude();
101-
102-
// Apply the queued commands
103-
for (var i = 0; i < q.length; i++) {
104-
var fn = instance[q[i][0]];
105-
fn && fn.apply(instance, q[i].slice(1));
106-
}
100+
instance._q = old._q || [];
107101

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

170165
Amplitude.prototype._eventId = 0;
@@ -265,6 +260,16 @@ Amplitude.prototype.init = function(apiKey, opt_userId, opt_config, callback) {
265260
}
266261
};
267262

263+
Amplitude.prototype.runQueuedFunctions = function () {
264+
for (var i = 0; i < this._q.length; i++) {
265+
var fn = this[this._q[i][0]];
266+
if (fn && type(fn) === 'function') {
267+
fn.apply(this, this._q[i].slice(1));
268+
}
269+
}
270+
this._q = []; // clear function queue after running
271+
};
272+
268273
Amplitude.prototype._loadSavedUnsentEvents = function(unsentKey, queue) {
269274
var savedUnsentEventsString = localStorage.getItem(unsentKey);
270275
if (savedUnsentEventsString) {

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
as.type = 'text/javascript';
55
as.async = true;
66
as.src = 'https://d24n15hnbwhuhn.cloudfront.net/libs/amplitude-2.4.1-min.gz.js';
7+
as.onload = function() { window.amplitude.runQueuedFunctions(); };
78
var s = document.getElementsByTagName('script')[0];
89
s.parentNode.insertBefore(as, s);
910
var Identify = function() { this._q = []; return this; };

src/amplitude.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ var Amplitude = function() {
5353
this._unsentIdentifys = [];
5454
this._ua = new UAParser(navigator.userAgent).getResult();
5555
this.options = object.merge({}, DEFAULT_OPTIONS);
56+
this._q = []; // queue for proxied functions before script load
5657
};
5758

5859
Amplitude.prototype._eventId = 0;
@@ -153,6 +154,16 @@ Amplitude.prototype.init = function(apiKey, opt_userId, opt_config, callback) {
153154
}
154155
};
155156

157+
Amplitude.prototype.runQueuedFunctions = function () {
158+
for (var i = 0; i < this._q.length; i++) {
159+
var fn = this[this._q[i][0]];
160+
if (fn && type(fn) === 'function') {
161+
fn.apply(this, this._q[i].slice(1));
162+
}
163+
}
164+
this._q = []; // clear function queue after running
165+
};
166+
156167
Amplitude.prototype._loadSavedUnsentEvents = function(unsentKey, queue) {
157168
var savedUnsentEventsString = localStorage.getItem(unsentKey);
158169
if (savedUnsentEventsString) {

src/index.js

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,8 @@
33
var Amplitude = require('./amplitude');
44

55
var old = window.amplitude || {};
6-
var q = old._q || [];
76
var instance = new Amplitude();
8-
9-
// Apply the queued commands
10-
for (var i = 0; i < q.length; i++) {
11-
var fn = instance[q[i][0]];
12-
fn && fn.apply(instance, q[i].slice(1));
13-
}
7+
instance._q = old._q || [];
148

159
// export the instance
1610
module.exports = instance;

test/amplitude.js

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,39 @@ describe('Amplitude', function() {
7676
});
7777
});
7878

79+
describe('runQueuedFunctions', function() {
80+
beforeEach(function() {
81+
amplitude.init(apiKey);
82+
});
83+
84+
afterEach(function() {
85+
reset();
86+
});
87+
88+
it('should run queued functions', function() {
89+
assert.equal(amplitude._unsentCount(), 0);
90+
assert.lengthOf(server.requests, 0);
91+
var userId = 'testUserId'
92+
var eventType = 'test_event'
93+
var functions = [
94+
['setUserId', userId],
95+
['logEvent', eventType]
96+
];
97+
amplitude._q = functions;
98+
assert.lengthOf(amplitude._q, 2);
99+
amplitude.runQueuedFunctions();
100+
101+
assert.equal(amplitude.options.userId, userId);
102+
assert.equal(amplitude._unsentCount(), 1);
103+
assert.lengthOf(server.requests, 1);
104+
var events = JSON.parse(querystring.parse(server.requests[0].requestBody).e);
105+
assert.lengthOf(events, 1);
106+
assert.equal(events[0].event_type, eventType);
107+
108+
assert.lengthOf(amplitude._q, 0);
109+
});
110+
});
111+
79112
describe('setUserProperties', function() {
80113
beforeEach(function() {
81114
amplitude.init(apiKey);

test/browser/amplitudejs.html

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
as.type = 'text/javascript';
88
as.async = true;
99
as.src = '/amplitude.js';
10+
as.onload = function() { window.amplitude.runQueuedFunctions(); };
1011
var s = document.getElementsByTagName('script')[0];
1112
s.parentNode.insertBefore(as, s);
1213
var Identify = function() { this._q = []; return this; };
@@ -69,7 +70,9 @@
6970
}
7071
</script>
7172
<script>
72-
amplitude.init('a2dbce0e18dfe5f8e74493843ff5c053');
73+
amplitude.init('a2dbce0e18dfe5f8e74493843ff5c053', null, null, function() {
74+
alert(amplitude.options.deviceId);
75+
});
7376
amplitude.setVersionName('Web');
7477
amplitude.identify(new amplitude.Identify().add('photoCount', 1));
7578
amplitude.identify(new amplitude.Identify().add('photoCount', 1).set('gender', 'male').unset('karma'));

0 commit comments

Comments
 (0)