Skip to content

Commit 7d07b01

Browse files
committed
add documentation for requirejs support
1 parent f07bc7b commit 7d07b01

File tree

4 files changed

+137
-16
lines changed

4 files changed

+137
-16
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
* Fix bug where saveReferrer throws exception if sessionStorage is disabled.
44
* Log messages with a try/catch to support IE 8.
55
* Validate event properties during logEvent and initialization before sending request.
6+
* Add instructions for proper integration with RequireJS.
67

78
## 2.9.0 (January 15, 2016)
89

README.md

Lines changed: 56 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -7,21 +7,23 @@ Amplitude-Javascript
77
1. If you haven't already, go to http://amplitude.com and register for an account. You will receive an API Key.
88
2. On every page that uses analytics, paste the following Javascript code between the `<head>` and `</head>` tags:
99

10-
```
11-
<script type="text/javascript">
12-
(function(e,t){var n=e.amplitude||{};var r=t.createElement("script");r.type="text/javascript";
13-
r.async=true;r.src="https://d24n15hnbwhuhn.cloudfront.net/libs/amplitude-2.9.0-min.gz.js";
14-
r.onload=function(){e.amplitude.runQueuedFunctions()};var s=t.getElementsByTagName("script")[0];
15-
s.parentNode.insertBefore(r,s);var i=function(){this._q=[];return this};function a(e){
16-
i.prototype[e]=function(){this._q.push([e].concat(Array.prototype.slice.call(arguments,0)));
17-
return this}}var o=["add","append","clearAll","set","setOnce","unset"];for(var c=0;c<o.length;c++){
18-
a(o[c])}n.Identify=i;n._q=[];function u(e){n[e]=function(){n._q.push([e].concat(Array.prototype.slice.call(arguments,0)));
19-
}}var l=["init","logEvent","logRevenue","setUserId","setUserProperties","setOptOut","setVersionName","setDomain","setDeviceId","setGlobalUserProperties","identify","clearUserProperties"];
20-
for(var p=0;p<l.length;p++){u(l[p])}e.amplitude=n})(window,document);
21-
22-
amplitude.init("YOUR_API_KEY_HERE");
23-
</script>
24-
```
10+
```html
11+
<script type="text/javascript">
12+
(function(e,t){var n=e.amplitude||{};var r=t.createElement("script");r.type="text/javascript";
13+
r.async=true;r.src="https://d24n15hnbwhuhn.cloudfront.net/libs/amplitude-2.9.0-min.gz.js";
14+
r.onload=function(){e.amplitude.runQueuedFunctions()};var s=t.getElementsByTagName("script")[0];
15+
s.parentNode.insertBefore(r,s);var i=function(){this._q=[];return this};function a(e){
16+
i.prototype[e]=function(){this._q.push([e].concat(Array.prototype.slice.call(arguments,0)));
17+
return this}}var o=["add","append","clearAll","set","setOnce","unset"];for(var c=0;c<o.length;c++){
18+
a(o[c])}n.Identify=i;n._q=[];function u(e){n[e]=function(){n._q.push([e].concat(Array.prototype.slice.call(arguments,0)));
19+
}}var l=["init","logEvent","logRevenue","setUserId","setUserProperties","setOptOut","setVersionName","setDomain","setDeviceId","setGlobalUserProperties","identify","clearUserProperties"];
20+
for(var p=0;p<l.length;p++){u(l[p])}e.amplitude=n})(window,document);
21+
22+
amplitude.init("YOUR_API_KEY_HERE");
23+
</script>
24+
```
25+
26+
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.
2527

2628
3. Replace `YOUR_API_KEY_HERE` with the API Key given to you.
2729
4. To track an event anywhere on the page, call:
@@ -261,6 +263,8 @@ var trackClickLinkA = function() {
261263
};
262264
```
263265

266+
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'.
267+
264268
### Init Callbacks ###
265269
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*:
266270

@@ -269,3 +273,40 @@ amplitude.init('YOUR_API_KEY_HERE', 'USER_ID_HERE', null, callback_function);
269273
```
270274

271275
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'.
276+
277+
### Loading with RequireJS ###
278+
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:
279+
280+
```html
281+
<script src='scripts/require.js'></script> <!-- loading RequireJS -->
282+
<script>
283+
require(['https://d24n15hnbwhuhn.cloudfront.net/libs/amplitude-2.9.0-min.gz.js'], function(amplitude) {
284+
amplitude.init('YOUR_API_KEY_HERE'); // replace YOUR_API_KEY_HERE with your Amplitude api key.
285+
window.amplitude = amplitude; // You can bind the amplitude object to window if you want to use it directly.
286+
amplitude.logEvent('Clicked Link A');
287+
});
288+
</script>
289+
```
290+
291+
You can also define the path in your RequireJS configuration like so:
292+
```html
293+
<script src='scripts/require.js'></script> <!-- loading RequireJS -->
294+
<script>
295+
requirejs.config({
296+
paths: {
297+
'amplitude': 'https://d24n15hnbwhuhn.cloudfront.net/libs/amplitude-2.9.0-min.gz'
298+
}
299+
});
300+
301+
require(['amplitude'], function(amplitude) {
302+
amplitude.init('YOUR_API_KEY_HERE'); // replace YOUR_API_KEY_HERE with your Amplitude api key.
303+
window.amplitude = amplitude; // You can bind the amplitude object to window if you want to use it directly.
304+
amplitude.logEvent('Clicked Link A');
305+
});
306+
</script>
307+
<script>
308+
require(['amplitude'], function(amplitude) {
309+
amplitude.logEvent('Page loaded');
310+
});
311+
</script>
312+
```

scripts/version.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ var cwd = process.cwd();
1010

1111
function replaceVersion(filepath) {
1212
var filename = path.join(cwd, filepath);
13-
fs.writeFileSync(filename, fs.readFileSync(filename, 'utf-8').replace(previous, version));
13+
fs.writeFileSync(filename, fs.readFileSync(filename, 'utf-8').split(previous).join(version));
1414
console.log('Updated ', filepath);
1515
}
1616

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
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+
requirejs.config({
6+
paths: {
7+
'amplitude': 'https://d24n15hnbwhuhn.cloudfront.net/libs/amplitude-2.9.1-min.gz'
8+
}
9+
});
10+
11+
require(['amplitude'], function(amplitude) {
12+
amplitude.init('a2dbce0e18dfe5f8e74493843ff5c053', null, {includeReferrer: true}, function() {
13+
alert(amplitude.options.deviceId);
14+
});
15+
16+
window.amplitude = amplitude;
17+
});
18+
19+
var setUserId = function() {
20+
var userId = prompt('Input userId', 'user01');
21+
amplitude.setUserId(userId);
22+
};
23+
var setEventUploadThreshold = function() {
24+
var eventUploadThreshold = parseInt(prompt('Input eventUploadThreshold', 5));
25+
amplitude.options.eventUploadThreshold = eventUploadThreshold;
26+
};
27+
var logEvent = function() {
28+
var event = prompt('Input event type', 'clicked');
29+
amplitude.logEvent(event);
30+
};
31+
var setCity = function() {
32+
var city = prompt('Input city', 'San Francisco, CA');
33+
amplitude.setUserProperties({city: city});
34+
};
35+
var addToPhotoCount = function() {
36+
var photoCount = parseInt(prompt('Input amount to increment photo count by', '2'), 10);
37+
amplitude.identify(new amplitude.Identify().add('photoCount', photoCount));
38+
};
39+
var clickOnLinkA = function() {
40+
amplitude.logEvent('Clicked on link A', null, function() { window.location='https://www.google.com'; });
41+
};
42+
var setPhotoCount = function() {
43+
var photoCount = parseInt(prompt('Input photo count to set', '2'), 10);
44+
amplitude.identify(new amplitude.Identify().set('photoCount', photoCount));
45+
};
46+
var setOncePhotoCount = function() {
47+
var photoCount = parseInt(prompt('Input photo count to setOnce', '2'), 10);
48+
amplitude.identify(new amplitude.Identify().setOnce('photoCount', photoCount));
49+
};
50+
</script>
51+
<script>
52+
require(['amplitude'], function(amplitude) {
53+
amplitude.logEvent('Page loaded');
54+
});
55+
</script>
56+
57+
<body>
58+
<h3>Amplitude JS Test with RequireJS</h3>
59+
<ul>
60+
<li><a href="javascript:setUserId();">Set user ID</a></li>
61+
<li><a href="javascript:amplitude.setOptOut(!amplitude.options.optOut);">Toggle opt out</a></li>
62+
<li><a href="javascript:logEvent();">Log event</a></li>
63+
<li><a href="javascript:amplitude.logEvent('clicked button', {color: 'red;', shape: 'triangle', sides: 3});">Log
64+
event with event properties</a></li>
65+
<li><a href="javascript:amplitude.setUserProperties({age: 30, city: 'San Francisco, CA'});">Set user properties</a></li>
66+
<li><a href="javascript:amplitude.options.batchEvents = !amplitude.options.batchEvents;">Toggle batch events</a></li>
67+
<li><a href="javascript:setEventUploadThreshold();">Set event upload threshold</a></li>
68+
<li><a href="javascript:clickOnLinkA();">Click on link A</a></li>
69+
<br><br>Testing Identify calls<br>
70+
<li><a href="javascript:addToPhotoCount();">Add to photo count</a></li>
71+
<li><a href="javascript:amplitude.identify(new amplitude.Identify().unset('photoCount'));">Unset photo count</a></li>
72+
<li><a href="javascript:setPhotoCount();">Set photo count</a></li>
73+
<li><a href="javascript:setOncePhotoCount();">Set photo count once</a></li>
74+
<li><a href="javascript:setCity();">Set city via setUserProperties</a></li>
75+
<li><a href="javascript:amplitude.clearUserProperties();">Clear all user properties</a></li>
76+
<br><br>
77+
<li><a href="/test/browser/amplitudejs2.html">Go to second page</a></li>
78+
</body>
79+
</html>

0 commit comments

Comments
 (0)