Skip to content

Commit 6a8bb10

Browse files
committed
generalize identify prototype from func dict
1 parent 9c8c599 commit 6a8bb10

File tree

5 files changed

+45
-34
lines changed

5 files changed

+45
-34
lines changed

README.md

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +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(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);
11+
(function(e,t){var n=e.amplitude||{};var s=t.createElement("script");s.type="text/javascript";
12+
s.async=true;s.src="https://d24n15hnbwhuhn.cloudfront.net/libs/amplitude-2.4.1-min.gz.js";
13+
var i=t.getElementsByTagName("script")[0];i.parentNode.insertBefore(s,i);var r={add:["a",false],
14+
set:["s",false],setOnce:["so",false],unset:["u",true]};n.Identify=function(){this.p={};
15+
Object.keys(r).forEach(function(e){this.p[r[e][0]]={}}.bind(this))};function a(e,t,s){
16+
n.Identify.prototype[e]=function(e,n){this.p[t][e]=s?"-":n;return this}}Object.keys(r).forEach(function(e){
17+
a(e,r[e][0],r[e][1])});n._q=[];function o(e){n[e]=function(){n._q.push([e].concat(Array.prototype.slice.call(arguments,0)));
18+
}}var c=["init","logEvent","logRevenue","setUserId","setUserProperties","setOptOut","setVersionName","setDomain","setDeviceId","setGlobalUserProperties","identify"];
19+
for(var u=0;u<c.length;u++){o(c[u])}e.amplitude=n})(window,document);
2020

2121
amplitude.init("YOUR_API_KEY_HERE");
2222
</script>

amplitude-segment-snippet.min.js

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
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);
1+
(function(t,e){var n=t.amplitude||{};n.Identify=function(){this.p={};Object.keys(identifyFuncs).forEach(function(t){
2+
this.p[identifyFuncs[t][0]]={}}.bind(this))};function i(t,e,i){n.Identify.prototype[t]=function(t,n){
3+
this.p[e][t]=i?"-":n;return this}}Object.keys(identifyFuncs).forEach(function(t){
4+
i(t,identifyFuncs[t][0],identifyFuncs[t][1])});n._q=[];function s(t){n[t]=function(){
5+
n._q.push([t].concat(Array.prototype.slice.call(arguments,0)))}}var o=["init","logEvent","logRevenue","setUserId","setUserProperties","setOptOut","setVersionName","setDomain","setDeviceId","setGlobalUserProperties","identify"];
6+
for(var c=0;c<o.length;c++){s(o[c])}t.amplitude=n})(window,document);

amplitude-snippet.min.js

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

src/amplitude-snippet.js

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,16 @@
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;};
9+
var identifyFuncs = {'add':['a', false], 'set':['s', false], 'setOnce':['so', false], 'unset':['u', true]};
10+
amplitude.Identify = function(){ this.p = {}; Object.keys(identifyFuncs).forEach(function (key) {
11+
this.p[identifyFuncs[key][0]] = {};
12+
}.bind(this));};
13+
function proxyIdentifyFunc(fn, dict, overrideValue) {
14+
amplitude.Identify.prototype[fn] = function(k,v) { this.p[dict][k]=overrideValue?'-':v; return this; };
15+
}
16+
Object.keys(identifyFuncs).forEach(function (key) {
17+
proxyIdentifyFunc(key, identifyFuncs[key][0], identifyFuncs[key][1]);
18+
});
1419
amplitude._q = [];
1520
function proxy(fn) {
1621
amplitude[fn] = function() {

test/browser/amplitudejs.html

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,16 @@
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;};
12+
var identifyFuncs = {'add':['a', false], 'set':['s', false], 'setOnce':['so', false], 'unset':['u', true]};
13+
amplitude.Identify = function(){ this.p = {}; Object.keys(identifyFuncs).forEach(function (key) {
14+
this.p[identifyFuncs[key][0]] = {};
15+
}.bind(this));};
16+
function proxyIdentifyFunc(fn, dict, overrideValue) {
17+
amplitude.Identify.prototype[fn] = function(k,v) { this.p[dict][k]=overrideValue?'-':v; return this; };
18+
}
19+
Object.keys(identifyFuncs).forEach(function (key) {
20+
proxyIdentifyFunc(key, identifyFuncs[key][0], identifyFuncs[key][1]);
21+
});
1722
amplitude._q = [];
1823
function proxy(fn) {
1924
amplitude[fn] = function() {
@@ -48,7 +53,7 @@
4853
<script>
4954
amplitude.init('a2dbce0e18dfe5f8e74493843ff5c053');
5055
amplitude.setVersionName('Web');
51-
amplitude.identify(new amplitude.Identify().add('photoCount', 1).set('gender', 'male'));
56+
amplitude.identify(new amplitude.Identify().add('photoCount', 1).set('gender', 'male').unset('karma'));
5257
amplitude.logEvent('pageLoad');
5358
</script>
5459
<body>

0 commit comments

Comments
 (0)