Skip to content

Commit 8897161

Browse files
committed
proxy identify object until script loads
1 parent 6a15744 commit 8897161

File tree

12 files changed

+182
-25
lines changed

12 files changed

+182
-25
lines changed

README.md

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,15 @@ Amplitude-Javascript
88
2. On every page that uses analytics, paste the following Javascript code between the `<head>` and `</head>` tags:
99

1010
<script type="text/javascript">
11-
(function(e,t){var r=e.amplitude||{};var n=t.createElement("script");n.type="text/javascript";
12-
n.async=true;n.src="https://d24n15hnbwhuhn.cloudfront.net/libs/amplitude-2.4.1-min.gz.js";
13-
var s=t.getElementsByTagName("script")[0];s.parentNode.insertBefore(n,s);r._q=[];function a(e){
14-
r[e]=function(){r._q.push([e].concat(Array.prototype.slice.call(arguments,0)))}}var i=["init","logEvent","logRevenue","setUserId","setUserProperties","setOptOut","setVersionName","setDomain","setDeviceId","setGlobalUserProperties"];
15-
for(var o=0;o<i.length;o++){a(i[o])}e.amplitude=r})(window,document);
11+
(function(t,e){var n=t.amplitude||{};var i=e.createElement("script");i.type="text/javascript";
12+
i.async=true;i.src="https://d24n15hnbwhuhn.cloudfront.net/libs/amplitude-2.4.1-min.gz.js";
13+
var s=e.getElementsByTagName("script")[0];s.parentNode.insertBefore(i,s);n.Identify=function(){
14+
this.p={a:{},s:{},u:{},so:{}}};n.Identify.prototype.add=function(t,e){this.p.a[t]=e;
15+
return this};n.Identify.prototype.set=function(t,e){this.p.s[t]=e;return this};n.Identify.prototype.setOnce=function(t,e){
16+
this.p.so[t]=e;return this};n.Identify.prototype.unset=function(t){this.p.u[t]="-";
17+
return this};n._q=[];function r(t){n[t]=function(){n._q.push([t].concat(Array.prototype.slice.call(arguments,0)));
18+
}}var o=["init","logEvent","logRevenue","setUserId","setUserProperties","setOptOut","setVersionName","setDomain","setDeviceId","setGlobalUserProperties","identify"];
19+
for(var p=0;p<o.length;p++){r(o[p])}t.amplitude=n})(window,document);
1620

1721
amplitude.init("YOUR_API_KEY_HERE");
1822
</script>

amplitude-segment-snippet.min.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1-
(function(e,t){var n=e.amplitude||{};n._q=[];function o(e){n[e]=function(){n._q.push([e].concat(Array.prototype.slice.call(arguments,0)));
2-
}}var r=["init","logEvent","logRevenue","setUserId","setUserProperties","setOptOut","setVersionName","setDomain","setDeviceId","setGlobalUserProperties"];
3-
for(var s=0;s<r.length;s++){o(r[s])}e.amplitude=n})(window,document);
1+
(function(t,e){var n=t.amplitude||{};n.Identify.prototype.add=function(t,e){this.p.a[t]=e;
2+
return this};n.Identify.prototype.setOnce=function(t,e){this.p.so[t]=e;return this;
3+
};n.Identify.prototype.unset=function(t){this.p.u[t]="-";return this};n._q=[];function i(t){
4+
n[t]=function(){n._q.push([t].concat(Array.prototype.slice.call(arguments,0)))}}var o=["init","logEvent","logRevenue","setUserId","setUserProperties","setOptOut","setVersionName","setDomain","setDeviceId","setGlobalUserProperties","identify"];
5+
for(var r=0;r<o.length;r++){i(o[r])}t.amplitude=n})(window,document);

amplitude-snippet.min.js

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

amplitude.js

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -464,8 +464,13 @@ Amplitude.prototype.setUserProperties = function(userProperties) {
464464
};
465465

466466
Amplitude.prototype.identify = function(identify) {
467-
if (identify instanceof Identify) {
467+
if (identify instanceof Identify && Object.keys(identify.userPropertiesOperations).length > 0) {
468468
this._logEvent(IDENTIFY_EVENT, null, null, identify.userPropertiesOperations);
469+
} else if (type(identify) === 'object' && 'p' in identify) {
470+
var identifyObject = new Identify().fromProxyObject(identify);
471+
if (Object.keys(identifyObject.userPropertiesOperations).length > 0) {
472+
this._logEvent(IDENTIFY_EVENT, null, null, identifyObject.userPropertiesOperations);
473+
}
469474
}
470475
};
471476

@@ -2350,6 +2355,8 @@ module.exports = function(val){
23502355
if (val !== val) return 'nan';
23512356
if (val && val.nodeType === 1) return 'element';
23522357

2358+
if (typeof Buffer != 'undefined' && Buffer.isBuffer(val)) return 'buffer';
2359+
23532360
val = val.valueOf
23542361
? val.valueOf()
23552362
: Object.prototype.valueOf.apply(val)
@@ -3303,6 +3310,24 @@ var Identify = function() {
33033310
this.properties = []; // keep track of keys that have been added
33043311
};
33053312

3313+
Identify.prototype.fromProxyObject = function(proxyObject) {
3314+
this._addOperationsFromProxyObjectDictionary(AMP_OP_SET_ONCE, proxyObject, 'so');
3315+
this._addOperationsFromProxyObjectDictionary(AMP_OP_UNSET, proxyObject, 'u');
3316+
this._addOperationsFromProxyObjectDictionary(AMP_OP_SET, proxyObject, 's');
3317+
this._addOperationsFromProxyObjectDictionary(AMP_OP_ADD, proxyObject, 'a');
3318+
return this;
3319+
};
3320+
3321+
Identify.prototype._addOperationsFromProxyObjectDictionary = function(operation, proxyObject, dictionary) {
3322+
if (dictionary in proxyObject.p) {
3323+
for (var key in proxyObject.p[dictionary]) {
3324+
if (proxyObject.p[dictionary].hasOwnProperty(key)) {
3325+
this._addOperation(operation, key, proxyObject.p[dictionary][key]);
3326+
}
3327+
}
3328+
}
3329+
};
3330+
33063331
Identify.prototype.add = function(property, value) {
33073332
if (type(value) === 'number' || type(value) === 'string') {
33083333
this._addOperation(AMP_OP_ADD, property, value);

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: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,11 @@
66
as.src = 'https://d24n15hnbwhuhn.cloudfront.net/libs/amplitude-2.4.1-min.gz.js';
77
var s = document.getElementsByTagName('script')[0];
88
s.parentNode.insertBefore(as, s);
9+
amplitude.Identify = function(){this.p={'a':{},'s':{},'u':{},'so':{}};};
10+
amplitude.Identify.prototype.add = function(k,v){this.p.a[k]=v;return this;};
11+
amplitude.Identify.prototype.set = function(k,v){this.p.s[k]=v;return this;};
12+
amplitude.Identify.prototype.setOnce = function(k,v){this.p.so[k]=v;return this;};
13+
amplitude.Identify.prototype.unset = function(k){this.p.u[k]='-';return this;};
914
amplitude._q = [];
1015
function proxy(fn) {
1116
amplitude[fn] = function() {
@@ -14,7 +19,7 @@
1419
}
1520
var funcs = ["init", "logEvent", "logRevenue", "setUserId", "setUserProperties",
1621
"setOptOut", "setVersionName", "setDomain", "setDeviceId",
17-
"setGlobalUserProperties"];
22+
"setGlobalUserProperties", "identify"];
1823
for (var i = 0; i < funcs.length; i++) {
1924
proxy(funcs[i]);
2025
}

src/amplitude.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -352,8 +352,13 @@ Amplitude.prototype.setUserProperties = function(userProperties) {
352352
};
353353

354354
Amplitude.prototype.identify = function(identify) {
355-
if (identify instanceof Identify) {
355+
if (identify instanceof Identify && Object.keys(identify.userPropertiesOperations).length > 0) {
356356
this._logEvent(IDENTIFY_EVENT, null, null, identify.userPropertiesOperations);
357+
} else if (type(identify) === 'object' && 'p' in identify) {
358+
var identifyObject = new Identify().fromProxyObject(identify);
359+
if (Object.keys(identifyObject.userPropertiesOperations).length > 0) {
360+
this._logEvent(IDENTIFY_EVENT, null, null, identifyObject.userPropertiesOperations);
361+
}
357362
}
358363
};
359364

src/identify.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,24 @@ var Identify = function() {
2121
this.properties = []; // keep track of keys that have been added
2222
};
2323

24+
Identify.prototype.fromProxyObject = function(proxyObject) {
25+
this._addOperationsFromProxyObjectDictionary(AMP_OP_SET_ONCE, proxyObject, 'so');
26+
this._addOperationsFromProxyObjectDictionary(AMP_OP_UNSET, proxyObject, 'u');
27+
this._addOperationsFromProxyObjectDictionary(AMP_OP_SET, proxyObject, 's');
28+
this._addOperationsFromProxyObjectDictionary(AMP_OP_ADD, proxyObject, 'a');
29+
return this;
30+
};
31+
32+
Identify.prototype._addOperationsFromProxyObjectDictionary = function(operation, proxyObject, dictionary) {
33+
if (dictionary in proxyObject.p) {
34+
for (var key in proxyObject.p[dictionary]) {
35+
if (proxyObject.p[dictionary].hasOwnProperty(key)) {
36+
this._addOperation(operation, key, proxyObject.p[dictionary][key]);
37+
}
38+
}
39+
}
40+
};
41+
2442
Identify.prototype.add = function(property, value) {
2543
if (type(value) === 'number' || type(value) === 'string') {
2644
this._addOperation(AMP_OP_ADD, property, value);

test/amplitude.js

Lines changed: 43 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -153,19 +153,19 @@ describe('Amplitude', function() {
153153

154154
it('should ignore inputs that are not identify objects', function() {
155155
amplitude.identify('This is a test');
156-
assert.lengthOf(amplitude._unsentEvents, 0);
156+
assert.lengthOf(amplitude._unsentIdentifys, 0);
157157
assert.lengthOf(server.requests, 0);
158158

159159
amplitude.identify(150);
160-
assert.lengthOf(amplitude._unsentEvents, 0);
160+
assert.lengthOf(amplitude._unsentIdentifys, 0);
161161
assert.lengthOf(server.requests, 0);
162162

163163
amplitude.identify(['test']);
164-
assert.lengthOf(amplitude._unsentEvents, 0);
164+
assert.lengthOf(amplitude._unsentIdentifys, 0);
165165
assert.lengthOf(server.requests, 0);
166166

167167
amplitude.identify({'user_prop': true});
168-
assert.lengthOf(amplitude._unsentEvents, 0);
168+
assert.lengthOf(amplitude._unsentIdentifys, 0);
169169
assert.lengthOf(server.requests, 0);
170170
});
171171

@@ -197,6 +197,45 @@ describe('Amplitude', function() {
197197
});
198198
});
199199

200+
it('should ignore empty identify objects', function() {
201+
amplitude.identify(new Identify());
202+
assert.lengthOf(amplitude._unsentIdentifys, 0);
203+
assert.lengthOf(server.requests, 0);
204+
});
205+
206+
it('should ignore empty proxy identify objects', function() {
207+
amplitude.identify({'p': {}});
208+
assert.lengthOf(amplitude._unsentIdentifys, 0);
209+
assert.lengthOf(server.requests, 0);
210+
211+
amplitude.identify({});
212+
assert.lengthOf(amplitude._unsentIdentifys, 0);
213+
assert.lengthOf(server.requests, 0);
214+
});
215+
216+
it('should generate an event from a proxy identify object', function() {
217+
var proxyObject = {'p':{
218+
'a':{'key1': 'value1'},
219+
'u':{'key1': 'value2'},
220+
's':{'key2': 'value3', 'key4': 'value5'},
221+
'so':{'key2': 'value4'}
222+
}};
223+
amplitude.identify(proxyObject);
224+
225+
assert.lengthOf(amplitude._unsentEvents, 0);
226+
assert.lengthOf(amplitude._unsentIdentifys, 1);
227+
assert.equal(amplitude._unsentCount(), 1);
228+
assert.lengthOf(server.requests, 1);
229+
var events = JSON.parse(querystring.parse(server.requests[0].requestBody).e);
230+
assert.lengthOf(events, 1);
231+
assert.equal(events[0].event_type, '$identify');
232+
assert.deepEqual(events[0].event_properties, {});
233+
assert.deepEqual(events[0].user_properties, {
234+
'$setOnce': {'key2': 'value4'},
235+
'$unset': {'key1': 'value2'},
236+
'$set': {'key4': 'value5'}
237+
});
238+
});
200239
});
201240

202241
describe('logEvent', function() {

test/browser/amplitudejs.html

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,19 @@
99
as.src = '/amplitude.js';
1010
var s = document.getElementsByTagName('script')[0];
1111
s.parentNode.insertBefore(as, s);
12+
amplitude.Identify = function(){this.p={'a':{},'s':{},'us':{},'so':{}};};
13+
amplitude.Identify.prototype.add = function(k,v){this.p.a[k]=v;return this;};
14+
amplitude.Identify.prototype.set = function(k,v){this.p.s[k]=v;return this;};
15+
amplitude.Identify.prototype.setOnce = function(k,v){this.p.so[k]=v;return this;};
16+
amplitude.Identify.prototype.unset = function(k){this.p.us[k]='-';return this;};
1217
amplitude._q = [];
1318
function proxy(fn) {
1419
amplitude[fn] = function() {
1520
amplitude._q.push([fn].concat(Array.prototype.slice.call(arguments, 0)));
1621
};
1722
}
1823

19-
var funcs = ["init", "logEvent", "logRevenue", "setUserId", "setUserProperties", "setOptOut", "setVersionName", "setDomain", "setGlobalUserProperties"];
24+
var funcs = ["init", "logEvent", "logRevenue", "setUserId", "setUserProperties", "setOptOut", "setVersionName", "setDomain", "setGlobalUserProperties", "identify"];
2025
for (var i = 0; i < funcs.length; i++) {
2126
proxy(funcs[i]);
2227
}
@@ -43,7 +48,8 @@
4348
<script>
4449
amplitude.init('a2dbce0e18dfe5f8e74493843ff5c053');
4550
amplitude.setVersionName('Web');
46-
//amplitude.logEvent('pageLoad');
51+
amplitude.identify(new amplitude.Identify().add('photoCount', 1).set('gender', 'male'));
52+
amplitude.logEvent('pageLoad');
4753
</script>
4854
<body>
4955
<h3>Amplitude JS Test</h3>

0 commit comments

Comments
 (0)