Skip to content

Commit f672745

Browse files
committed
Merge pull request #48 from amplitude/fix_requirejs_support
Add instructions for proper integration with RequireJS
2 parents c8c6e14 + 4e4322b commit f672745

File tree

10 files changed

+2213
-14
lines changed

10 files changed

+2213
-14
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,5 @@ node_modules
22
components
33
npm-debug.log
44
build
5-
dist
5+
dist
6+
.DS_Store

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
* Fix bug where saveReferrer throws exception if sessionStorage is disabled.
55
* Log messages with a try/catch to support IE 8.
66
* Validate event properties during logEvent and initialization before sending request.
7+
* Add instructions for proper integration with RequireJS.
8+
* Init callback now passes the Amplitude instance as an argument to the callback function.
79

810
## 2.9.0 (January 15, 2016)
911

README.md

Lines changed: 44 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,50 @@ 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'); // replace YOUR_API_KEY_HERE with your Amplitude api key.
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+
```
373+
374+
You can also define the path in your RequireJS configuration like so:
375+
```html
376+
<script src='scripts/require.js'></script> <!-- loading RequireJS -->
377+
<script>
378+
requirejs.config({
379+
paths: {
380+
'amplitude': 'https://d24n15hnbwhuhn.cloudfront.net/libs/amplitude-2.9.0-min.gz'
381+
}
382+
});
383+
384+
require(['amplitude'], function(amplitude) {
385+
amplitude.init('YOUR_API_KEY_HERE'); // replace YOUR_API_KEY_HERE with your Amplitude api key.
386+
window.amplitude = amplitude; // You can bind the amplitude object to window if you want to use it directly.
387+
amplitude.logEvent('Clicked Link A');
388+
});
389+
</script>
390+
<script>
391+
require(['amplitude'], function(amplitude) {
392+
amplitude.logEvent('Page loaded');
393+
});
394+
</script>
395+
```

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
};
@@ -395,7 +395,7 @@ AmplitudeClient.prototype.init = function(apiKey, opt_userId, opt_config, callba
395395
}
396396

397397
if (callback && type(callback) === 'function') {
398-
callback();
398+
callback(this);
399399
}
400400
};
401401

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.

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

src/amplitude-client.js

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

148148
if (callback && type(callback) === 'function') {
149-
callback();
149+
callback(this);
150150
}
151151
};
152152

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: 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.0-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)