Skip to content

Commit 51b530d

Browse files
committed
added instructions for requirejs and a test page
1 parent c2ef2ef commit 51b530d

File tree

7 files changed

+2174
-12
lines changed

7 files changed

+2174
-12
lines changed

README.md

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ Amplitude-Javascript
2525
</script>
2626
```
2727

28+
Note: if you are using [RequireJS](http://requirejs.org/), follow these [alternate instructions](https://github.com/amplitude/Amplitude-Javascript#loading-with-requirejs) for Step 2.
29+
2830
3. Replace `YOUR_API_KEY_HERE` with the API Key given to you.
2931
4. To track an event anywhere on the page, call:
3032

@@ -344,11 +346,27 @@ var trackClickLinkA = function() {
344346
};
345347
```
346348

349+
In the case that `optOut` is true, then no event will be logged, but the callback will be called. In the case that `batchEvents` is true, if the batch requirements `eventUploadThreshold` and `eventUploadPeriodMillis` are not met when `logEvent` is called, then no request is sent, but the callback is still called. In these cases, the callback will be called with an input status of 0 and response 'No request sent'.
350+
347351
### Init Callbacks ###
348-
You can also pass a callback function to init, which will get called after the SDK finishes its asynchronous loading. *Note: no values are passed to the init callback function*:
352+
You can also pass a callback function to init, which will get called after the SDK finishes its asynchronous loading. *Note: the Amplitude instance is passed to the callback function as an argument*:
349353

350354
```javascript
351-
amplitude.getInstance().init('YOUR_API_KEY_HERE', 'USER_ID_HERE', null, callback_function);
355+
amplitude.getInstance().init('YOUR_API_KEY_HERE', 'USER_ID_HERE', null, function(instance) {
356+
console.log(instance.options.deviceId); // access the instance's deviceId after initialization
357+
});
352358
```
353359

354-
In the case that `optOut` is true, then no event will be logged, but the callback will be called. In the case that `batchEvents` is true, if the batch requirements `eventUploadThreshold` and `eventUploadPeriodMillis` are not met when `logEvent` is called, then no request is sent, but the callback is still called. In these cases, the callback will be called with an input status of 0 and response 'No request sent'.
360+
### Loading with RequireJS ###
361+
If you are using [RequireJS](http://requirejs.org/) to load your Javascript files, you can also use it to load the Amplitude Javascript SDK script directly instead of using our loading snippet. On every page that uses analytics, paste the following Javascript code between the `<head>` and `</head>` tags:
362+
363+
```html
364+
<script src='scripts/require.js'></script> <!-- loading RequireJS -->
365+
<script>
366+
require(['https://d24n15hnbwhuhn.cloudfront.net/libs/amplitude-2.9.0-min.gz.js'], function(amplitude) {
367+
amplitude.init('YOUR_API_KEY_HERE');
368+
window.amplitude = amplitude; // You can bind the amplitude object to window if you want to use it directly.
369+
amplitude.logEvent('Clicked Link A');
370+
});
371+
</script>
372+
```

amplitude.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -158,11 +158,11 @@ Amplitude.prototype.runQueuedFunctions = function () {
158158
* Maintain mapping of old functions to new instance methods
159159
*/
160160
Amplitude.prototype.init = function(apiKey, opt_userId, opt_config, callback) {
161-
this.getInstance().init(apiKey, opt_userId, opt_config, function() {
161+
this.getInstance().init(apiKey, opt_userId, opt_config, function(instance) {
162162
// make options such as deviceId available for callback functions
163-
this.options = this.getInstance().options;
163+
this.options = instance.options;
164164
if (callback && type(callback) === 'function') {
165-
callback();
165+
callback(instance);
166166
}
167167
}.bind(this));
168168
};
@@ -396,7 +396,7 @@ AmplitudeClient.prototype.init = function(apiKey, opt_userId, opt_config, callba
396396
}
397397

398398
if (callback && type(callback) === 'function') {
399-
callback();
399+
callback(this);
400400
}
401401
};
402402

amplitude.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/amplitude-client.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ AmplitudeClient.prototype.init = function(apiKey, opt_userId, opt_config, callba
147147
}
148148

149149
if (callback && type(callback) === 'function') {
150-
callback();
150+
callback(this);
151151
}
152152
};
153153

src/amplitude.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,11 +49,11 @@ Amplitude.prototype.runQueuedFunctions = function () {
4949
* Maintain mapping of old functions to new instance methods
5050
*/
5151
Amplitude.prototype.init = function(apiKey, opt_userId, opt_config, callback) {
52-
this.getInstance().init(apiKey, opt_userId, opt_config, function() {
52+
this.getInstance().init(apiKey, opt_userId, opt_config, function(instance) {
5353
// make options such as deviceId available for callback functions
54-
this.options = this.getInstance().options;
54+
this.options = instance.options;
5555
if (callback && type(callback) === 'function') {
56-
callback();
56+
callback(instance);
5757
}
5858
}.bind(this));
5959
};
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
<html>
2+
<!--<script src="src/amplitude-snippet.js" type="text/javascript"></script>-->
3+
<script src='require.js' type='text/javascript'></script>
4+
<script>
5+
require(['../../amplitude.js'], function(amplitude) {
6+
amplitude.init('a2dbce0e18dfe5f8e74493843ff5c053', null, {includeReferrer: true}, function() {
7+
alert(amplitude.options.deviceId);
8+
});
9+
10+
window.amplitude = amplitude;
11+
});
12+
13+
var setUserId = function() {
14+
var userId = prompt('Input userId', 'user01');
15+
amplitude.setUserId(userId);
16+
};
17+
var setEventUploadThreshold = function() {
18+
var eventUploadThreshold = parseInt(prompt('Input eventUploadThreshold', 5));
19+
amplitude.options.eventUploadThreshold = eventUploadThreshold;
20+
};
21+
var logEvent = function() {
22+
var event = prompt('Input event type', 'clicked');
23+
amplitude.logEvent(event);
24+
};
25+
var setCity = function() {
26+
var city = prompt('Input city', 'San Francisco, CA');
27+
amplitude.setUserProperties({city: city});
28+
};
29+
var addToPhotoCount = function() {
30+
var photoCount = parseInt(prompt('Input amount to increment photo count by', '2'), 10);
31+
amplitude.identify(new amplitude.Identify().add('photoCount', photoCount));
32+
};
33+
var clickOnLinkA = function() {
34+
amplitude.logEvent('Clicked on link A', null, function() { window.location='https://www.google.com'; });
35+
};
36+
var setPhotoCount = function() {
37+
var photoCount = parseInt(prompt('Input photo count to set', '2'), 10);
38+
amplitude.identify(new amplitude.Identify().set('photoCount', photoCount));
39+
};
40+
var setOncePhotoCount = function() {
41+
var photoCount = parseInt(prompt('Input photo count to setOnce', '2'), 10);
42+
amplitude.identify(new amplitude.Identify().setOnce('photoCount', photoCount));
43+
};
44+
</script>
45+
46+
<body>
47+
<h3>Amplitude JS Test with RequireJS</h3>
48+
<ul>
49+
<li><a href="javascript:setUserId();">Set user ID</a></li>
50+
<li><a href="javascript:amplitude.setOptOut(!amplitude.options.optOut);">Toggle opt out</a></li>
51+
<li><a href="javascript:logEvent();">Log event</a></li>
52+
<li><a href="javascript:amplitude.logEvent('clicked button', {color: 'red;', shape: 'triangle', sides: 3});">Log
53+
event with event properties</a></li>
54+
<li><a href="javascript:amplitude.setUserProperties({age: 30, city: 'San Francisco, CA'});">Set user properties</a></li>
55+
<li><a href="javascript:amplitude.options.batchEvents = !amplitude.options.batchEvents;">Toggle batch events</a></li>
56+
<li><a href="javascript:setEventUploadThreshold();">Set event upload threshold</a></li>
57+
<li><a href="javascript:clickOnLinkA();">Click on link A</a></li>
58+
<br><br>Testing Identify calls<br>
59+
<li><a href="javascript:addToPhotoCount();">Add to photo count</a></li>
60+
<li><a href="javascript:amplitude.identify(new amplitude.Identify().unset('photoCount'));">Unset photo count</a></li>
61+
<li><a href="javascript:setPhotoCount();">Set photo count</a></li>
62+
<li><a href="javascript:setOncePhotoCount();">Set photo count once</a></li>
63+
<li><a href="javascript:setCity();">Set city via setUserProperties</a></li>
64+
<li><a href="javascript:amplitude.clearUserProperties();">Clear all user properties</a></li>
65+
<br><br>
66+
<li><a href="/test/browser/amplitudejs2.html">Go to second page</a></li>
67+
</body>
68+
</html>

0 commit comments

Comments
 (0)