-
Notifications
You must be signed in to change notification settings - Fork 133
/
amplitudejs.html
136 lines (131 loc) · 6.94 KB
/
amplitudejs.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
<html>
<!--<script src="src/amplitude-snippet.js" type="text/javascript"></script>-->
<script>
(function(window, document) {
var amplitude = window.amplitude || {'_q':[],'_iq':{}};
var as = document.createElement('script');
as.type = 'text/javascript';
as.async = true;
as.src = '/amplitude.js';
as.onload = function() {if(window.amplitude.runQueuedFunctions) {window.amplitude.runQueuedFunctions();} else {console.log('[Amplitude] Error: could not load SDK');}};
var s = document.getElementsByTagName('script')[0];
s.parentNode.insertBefore(as, s);
function proxy(obj, fn) {
obj.prototype[fn] = function() {
this._q.push([fn].concat(Array.prototype.slice.call(arguments, 0))); return this;
};
}
var Identify = function() {this._q = []; return this;};
var identifyFuncs = ['add', 'append', 'clearAll', 'prepend', 'set', 'setOnce', 'unset'];
for (var i = 0; i < identifyFuncs.length; i++) {proxy(Identify,identifyFuncs[i]);}
amplitude.Identify = Identify;
var Revenue = function() {this._q = []; return this;};
var revenueFuncs = ['setProductId', 'setQuantity', 'setPrice', 'setRevenueType', 'setEventProperties'];
for (var j = 0; j < revenueFuncs.length; j++) {proxy(Revenue, revenueFuncs[j]);}
amplitude.Revenue = Revenue;
var funcs = ['init', 'logEvent', 'logRevenue', 'setUserId', 'setUserProperties',
'setOptOut', 'setVersionName', 'setDomain', 'setDeviceId',
'setGlobalUserProperties', 'identify', 'clearUserProperties',
'setGroup', 'logRevenueV2', 'regenerateDeviceId',
'logEventWithTimestamp', 'logEventWithGroups'];
function setUpProxy(instance) {
function proxyMain(fn) {
instance[fn] = function() {
instance._q.push([fn].concat(Array.prototype.slice.call(arguments, 0)));
};
}
for (var k = 0; k < funcs.length; k++) {proxyMain(funcs[k]);}
}
setUpProxy(amplitude);
amplitude.getInstance = function(instance) {
instance = ((!instance || instance.length===0) ? '$default_instance' : instance).toLowerCase();
if (!amplitude._iq.hasOwnProperty(instance)) {
amplitude._iq[instance] = {'_q':[]}; setUpProxy(amplitude._iq[instance]);
}
return amplitude._iq[instance];
};
window.amplitude = amplitude;
})(window, document);
var setUserId = function() {
var userId = prompt('Input userId', 'user01');
amplitude.setUserId(userId);
};
var setEventUploadThreshold = function() {
var eventUploadThreshold = parseInt(prompt('Input eventUploadThreshold', 5));
amplitude.options.eventUploadThreshold = eventUploadThreshold;
};
var logEvent = function() {
var event = prompt('Input event type', 'clicked');
amplitude.logEvent(event);
};
var setCity = function() {
var city = prompt('Input city', 'San Francisco, CA');
amplitude.setUserProperties({city: city});
};
var addToPhotoCount = function() {
var photoCount = parseInt(prompt('Input amount to increment photo count by', '2'), 10);
amplitude.identify(new amplitude.Identify().add('photoCount', photoCount));
};
var clickOnLinkA = function() {
amplitude.logEvent('Clicked on link A', null, function() { window.location='https://www.google.com'; });
};
var setPhotoCount = function() {
var photoCount = parseInt(prompt('Input photo count to set', '2'), 10);
amplitude.identify(new amplitude.Identify().set('photoCount', photoCount), function() {alert('setPhotoCount!');});
};
var setOncePhotoCount = function() {
var photoCount = parseInt(prompt('Input photo count to setOnce', '2'), 10);
amplitude.identify(new amplitude.Identify().setOnce('photoCount', photoCount));
};
var logRevenue = function() {
amplitude.logRevenue(9.99, 1, 'productA');
}
var logRevenueV2 = function() {
var revenue = new amplitude.Revenue().setProductId('other.productId').setQuantity(15).setPrice(145.99);
revenue.setEventProperties({'city': 'Boston'});
amplitude.logRevenueV2(revenue);
}
</script>
<script>
amplitude.init('a2dbce0e18dfe5f8e74493843ff5c053', null, {includeReferrer: true, includeUtm: true, includeGclid: true}, 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'));
amplitude.logEvent('pageLoad');
amplitude.getInstance('app2').logEvent('log event before init');
amplitude.getInstance('app2').init('1d2fe1e104eb3f07a24e94d359f70fd5', '[email protected]', {includeReferrer: true}, function(instance) {
alert(instance.options.deviceId);
});
amplitude.getInstance('app2').identify(new amplitude.Identify().set('key', 'value'));
amplitude.getInstance('app2').logEvent('app 2 page load');
amplitude.getInstance('app3').init('a2dbce0e18dfe5f8e74493843ff5c053', null, {batchEvents: true, eventUploadThreshold: 2});
amplitude.getInstance('app3').logEvent('app3 page Load');
amplitude.getInstance('app4').init('1d2fe1e104eb3f07a24e94d359f70fd5', '[email protected]');
amplitude.getInstance('app4').logEvent('app 4 page load');
</script>
<body>
<h3>Amplitude JS Test</h3>
<ul>
<li><a href="javascript:setUserId();">Set user ID</a></li>
<li><a href="javascript:amplitude.setOptOut(!amplitude.options.optOut);">Toggle opt out</a></li>
<li><a href="javascript:logEvent();">Log event</a></li>
<li><a href="javascript:amplitude.logEvent('clicked button', {color: 'red;', shape: 'triangle', sides: 3});">Log
event with event properties</a></li>
<li><a href="javascript:amplitude.setUserProperties({age: 30, city: 'San Francisco, CA'});">Set user properties</a></li>
<li><a href="javascript:amplitude.options.batchEvents = !amplitude.options.batchEvents;">Toggle batch events</a></li>
<li><a href="javascript:setEventUploadThreshold();">Set event upload threshold</a></li>
<li><a href="javascript:clickOnLinkA();">Click on link A</a></li>
<br><br>Testing Identify calls<br>
<li><a href="javascript:addToPhotoCount();">Add to photo count</a></li>
<li><a href="javascript:amplitude.identify(new amplitude.Identify().unset('photoCount'));">Unset photo count</a></li>
<li><a href="javascript:setPhotoCount();">Set photo count</a></li>
<li><a href="javascript:setOncePhotoCount();">Set photo count once</a></li>
<li><a href="javascript:setCity();">Set city via setUserProperties</a></li>
<li><a href="javascript:amplitude.clearUserProperties();">Clear all user properties</a></li>
<li><a href="javascript:logRevenueV2();">LogRevenueV2</a></li>
<br><br>
<li><a href="/test/browser/amplitudejs2.html">Go to second page</a></li>
</body>
</html>